Accessing SlicerElastix from within python module

I am trying to use SlicerElastix module from my module as a cli call. However, all these fails

slicer.cli.run(module=slicer.modules.slicerelastix, None,
                                parameters, wait_for_completion=True, update_display=False)
>>> module 'modules' has no attribute 'slicerelastix'

slicer.cli.run(module=slicer.modules.elastix, None,
                                parameters, wait_for_completion=True, update_display=False)
>>> module 'modules' has no attribute 'elastix'

any way to call elastix registration from the module? I have the module installed and running.

After I install SlicerElastix, the second way you tried allows me to access the module:

>>> slicer.modules.elastix
qSlicerScriptedLoadableModule (qSlicerScriptedLoadableModule at: 0x0000026B63E3A8F0)

But then I got a different error when trying to run the module with an existing parameter node:

>>> slicer.cli.run(slicer.modules.elastix, paramNode)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Program Files\Slicer 4.11.0-2019-06-11\bin\Python\slicer\cli.py", line 78, in run
    logic.SetDeleteTemporaryFiles(1 if delete_temporary_files else 0)
AttributeError: 'SlicerBaseLogicPython.vtkSlicerScriptedLoadableMod' object has no attribute 'SetDeleteTemporaryFiles'

SetDeleteTemporaryFiles is a function in vtkSlicerCLIModuleLogic, so it is strange that the ‘logic’ variable in cli.py is a vtkSlicerScriptedLoadableModuleNode instance. Not sure how it can happen.

(Note: I used an old nightly, so it may be different on a newer one)

I have figure out a way to invoke it as

slicer.cli.run(module=slicer.util.getModule("Elastix"), parameters=parameters, 
                                    wait_for_completion=True, update_display=False)

not the usual way but it get invoked, now the parameter set is a bit awkward and the results I got are not the ones from the module itself (The outputTransformNode is not updated at all)

The error I am getting is

>>>'SlicerBaseLogicPython.vtkSlicerScriptedLoadableMod' object has no attribute 'CreateNodeInScene'

You cannot call SlicerElastix as a CLI module because it is not a CLI module but a (Python) loadable module. At the time I’ve added this module, Python CLI option was not available and I would not switch, because loadable module allows creating a more user-friendly GUI.

You can use Sequence registration module as an example of using Elastix from another module.

1 Like

I didn’t even bother checking :slight_smile: Right, if it’s not a CLI then it cannot be called like that.

OK, made it as

        try:
            import Elastix
            elastixLogic = Elastix.ElastixLogic()
            parameterFilenames = elastixLogic.getRegistrationPresets()[0][Elastix.RegistrationPresets_ParameterFilenames]
            useelastix = True
        except Exception as e:
            print(e)
            useelastix = False

and then

                    elastixLogic.registerVolumes(
                                densRnode['node'], densnode['node'],
                                parameterFilenames = parameterFilenames,
                                outputTransformNode = transformNode
                                )

now it is running as the GUI, Thanks so much!!!

3 Likes

Hi Andras,
Is it possible to use elastix as clil module from another module. if not what are the other options.

Thank you

Regards,
Saima Safdar

See an example how Elastix is used by another module here: https://github.com/moselhy/SlicerSequenceRegistration/blob/master/SequenceRegistration/SequenceRegistration.py

1 Like