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 ?
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.
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).
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.
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.