# Reference count with Python

**URL:** https://discourse.itk.org/t/reference-count-with-python/1483
**Category:** Beginner Questions
**Tags:** python
**Created:** [December 17, 2018, 2:09pm UTC](https://discourse.itk.org/t/reference-count-with-python/1483 "2018-12-17T14:09:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![finetjul](https://discourse.itk.org/user_avatar/discourse.itk.org/finetjul/32/538_2.png) [@finetjul](https://discourse.itk.org/u/finetjul)
#### Post date: [December 17, 2018, 2:09pm UTC](https://discourse.itk.org/t/reference-count-with-python/1483/1 "2018-12-17T14:09:34Z")

</div>

Hi all,

In Python, I have the following code:

```
  def generateSource():
    reader = itk.ImageReader[ImageType].New()
    ...
    itkToVtkFilter = itk.ImageToVTKImageFilter[ImageType].New()
    itkToVtkFilter.SetInput(reader.GetOutput())
    return itkToVtkFilter.GetImporter()
 
  def main():
    source = generateSource()
    source.Update()

```

Problem is, `reader` and `itkToVtkFilter` are destroyed after `generateSource()` is called. How can you increase reference count in ITK/Python ? What would be the best approach ?

Thanks,  
Julien

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [December 17, 2018, 2:20pm UTC](https://discourse.itk.org/t/reference-count-with-python/1483/2 "2018-12-17T14:20:28Z")

</div>

One approach (I am not sure if it is the best) that I have used in `itk.GetArrayFromImage()`, is to associate the object that should not be destroyed to the object that is returned, as a “private member” (i.e. a member whose name starts with ‘\_’): See [https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Bridge/NumPy/wrapping/PyBuffer.i.in#L152](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Bridge/NumPy/wrapping/PyBuffer.i.in#L152)

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [December 18, 2018, 11:26pm UTC](https://discourse.itk.org/t/reference-count-with-python/1483/3 "2018-12-18T23:26:31Z")

</div>

Actually @finetjul, there is a way simplier solution than the one I suggested above. Thanks @matt.mccormick for pointing this out. You need to update (call `.Update()`) your filter before returning the output of your filter.

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.itk.org/user_avatar/discourse.itk.org/finetjul/32/538_2.png) [@finetjul](https://discourse.itk.org/u/finetjul)
#### Post date: [December 19, 2018, 8:48am UTC](https://discourse.itk.org/t/reference-count-with-python/1483/4 "2018-12-19T08:48:41Z")

</div>

Thanks @fbudin for the 2 solutions. There are problems with both however.

Your first solution (private member) works but the pipeline is never destroyed:  
itkToVtkFilter.SetInput(reader.GetOutput())  
source = itkToVtkFilter.GetImporter()  
source.\_itkToVtkFilter = itkToVtkFilter  
source.\_dicomReader = dicomReader  
return source

For the Update() solution, it does not work. Moreover, I do not want to call `.Update()` on the filter before returning because it would update the whole extent of the reader (which is actually an ImageSeriesReader with lots of 2D slices). I would prefer if the `.Update()` is called after specifying the update extent but that happens further down the code (it is the VTK mapper that specifies the update extent).

Thanks,  
Julien.

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [December 19, 2018, 2:46pm UTC](https://discourse.itk.org/t/reference-count-with-python/1483/5 "2018-12-19T14:46:34Z")

</div>

I see. Then what you want to look at is this:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Wrapping/Generators/Python/itkExtras.py#L806>

You can find some examples on how to use it in the tests, for example here:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Wrapping/Generators/Python/Tests/templated_pipeline.py>

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [December 20, 2018, 2:41pm UTC](https://discourse.itk.org/t/reference-count-with-python/1483/6 "2018-12-20T14:41:48Z")

</div>

The ITK DataObject base class contains a weak pointer to the it’s source filter. So if only a pointer to then end of the pipeline is held the all preceding filters will be deleted. An additional pointer to all root filters in the pipeline needs to be held to keep the whole pipeline around/

I would suggest creating the VTK command object class which holds a pointer the the ITK root filters. Then adding that command class as an observer to the VTK filter to say the “delete” event. This should maintain the reference needed to the root node and automatically be deleted when the VTK filter at the end of the pipeline is deleted. This is an approach that I recall doing in C++, but the translation to python is not clear to me.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [December 21, 2018, 3:51pm UTC](https://discourse.itk.org/t/reference-count-with-python/1483/7 "2018-12-21T15:51:06Z")

</div>

The [pipeline](https://github.com/InsightSoftwareConsortium/ITK/blob/4132cb4a5cc948d32cdbd9c5ec9989a7880fef17/Wrapping/Generators/Python/itkExtras.py#L806-L941) and [autopipeline](https://github.com/InsightSoftwareConsortium/ITK/blob/4132cb4a5cc948d32cdbd9c5ec9989a7880fef17/Wrapping/Generators/Python/itkExtras.py#L944-L955) that @fbudin referenced for generating Python pipelines are quite awesome and worth a look! 👀

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [December 21, 2018, 9:12pm UTC](https://discourse.itk.org/t/reference-count-with-python/1483/8 "2018-12-21T21:12:12Z")

</div>

Here is a short example how to use the Python pipeline:

> def my\_func():
> 
> ```
> my_pipeline=itk.pipeline()
> MedianFilter=itk.MeanImageFilter.IUC2IUC2.New()
> my_pipeline.connect(MedianFilter)
> my_pipeline.expose("Radius")
> thresholdFilter=itk.ThresholdImageFilter.IUC2.New()
> my_pipeline.connect(thresholdFilter)
> my_pipeline.expose("Lower")
> return my_pipeline
> 
> ```
> 
> my\_pipeline = my\_func()  
> my\_pipeline.SetInput(im)  
> my\_pipeline.SetRadius(5)  
> my\_pipeline.SetLower(100)  
> my\_pipeline.Update()  
> arr=itk.GetArrayFromImage(my\_pipeline.GetOutput())

You could also derive the itk.pipeline class and create your pipeline inside the constructor ` __init__ ` of the new pipeline class like it is done in [one ITK test](https://github.com/fbudin69500/ITK/blob/master/Wrapping/Generators/Python/Tests/templated_pipeline.py).
