# Accessing SlicerElastix from within python module

**URL:** <https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410>\
**Category:** Support\
**Tags:** elastix\
**Created:** [December 6, 2019, 1:39pm UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410 "2019-12-06T13:39:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [December 6, 2019, 1:39pm UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410/1 "2019-12-06T13:39:44Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [December 6, 2019, 2:08pm UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410/2 "2019-12-06T14:08:44Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [December 6, 2019, 2:24pm UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410/3 "2019-12-06T14:24:28Z")

</div>

I have figure out a way to invoke it as

```auto
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

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

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 6, 2019, 2:34pm UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410/4 "2019-12-06T14:34:33Z")

</div>

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](https://github.com/moselhy/SlicerSequenceRegistration/blob/e9ac5c24e30ecee6a0f8dfee472aa54ff11497cf/SequenceRegistration/SequenceRegistration.py#L557-L562) module as an example of using Elastix from another module.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [December 6, 2019, 2:53pm UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410/5 "2019-12-06T14:53:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)\
**Post date:** [December 6, 2019, 2:54pm UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410/6 "2019-12-06T14:54:03Z")

</div>

OK, made it as

```python
        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

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

```

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

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [April 28, 2020, 6:46am UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410/7 "2020-04-28T06:46:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 28, 2020, 3:17pm UTC](https://discourse.slicer.org/t/accessing-slicerelastix-from-within-python-module/9410/8 "2020-04-28T15:17:41Z")

</div>

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