# Using python to automate pseudoLMGenerator and ALPACA processes

**URL:** <https://discourse.slicer.org/t/using-python-to-automate-pseudolmgenerator-and-alpaca-processes/22851>\
**Category:** Support\
**Tags:** python, slicermorph\
**Created:** [April 6, 2022, 3:41pm UTC](https://discourse.slicer.org/t/using-python-to-automate-pseudolmgenerator-and-alpaca-processes/22851 "2022-04-06T15:41:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik.Meilak](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@Erik.Meilak](https://discourse.slicer.org/u/Erik.Meilak)\
**Post date:** [April 6, 2022, 3:41pm UTC](https://discourse.slicer.org/t/using-python-to-automate-pseudolmgenerator-and-alpaca-processes/22851/1 "2022-04-06T15:41:49Z")

</div>

Hello, I am trying to automate some processes which use the modules pseudoLMGenerator and ALPACA but cannot find any of the functions which are available in the GUI.

For example if I try and set the base mesh in the pseudoLMGenerator I expected to find something along the lines of:  
slicer.modules.pseudolmgenerator.logic().setBaseMesh() but not such function or anything similar is available after inputting slicer.modules.pseudolmgenerator.logic().

I have similar issues when trying to automate the ALPACA module also. My question is, is it possible to automate all GUI functions through python scripting and if so how do I find the documentation describing the correct syntax for scripting for these particular modules?

Thanks in advance for any help. Happy to provide further details/screenshots if needed.

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [April 6, 2022, 6:08pm UTC](https://discourse.slicer.org/t/using-python-to-automate-pseudolmgenerator-and-alpaca-processes/22851/2 "2022-04-06T18:08:44Z")

</div>

It is possible to automate ALPACA and the pseudoLMGenerator. You will need to read through the module’s code to see how to do this, but in general:

- parameters such as input files/folders set in the GUI will be defined in the `setup()` function of the widget class
- the widget class contains functions that handle GUI interactions, such as button presses
- the logic class contains functions to run the algorithms. The input parameters can be passed directly, without needing to set GUI parameters.

For the PseudoLMGenerator module, there are multiple steps run by separate button presses. Looking at the button callbacks, you will find the logic functions called and input/output arguments.

ALPACA can be run in batch mode using a single call to the logic function [here](https://github.com/SlicerMorph/SlicerMorph/blob/dbd661146ef45b6f8d2f7778f9a4e8b65c28ff9b/ALPACA/ALPACA.py#L854).

I’m happy to help here with any specific questions. You also might find these [tutorials](https://www.slicer.org/wiki/Documentation/Nightly/Training#PerkLab.27s_Slicer_bootcamp_training_materials) helpful.

---

<div class="post-metadata">

**Author:** ![Erik.Meilak](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@Erik.Meilak](https://discourse.slicer.org/u/Erik.Meilak)\
**Post date:** [April 11, 2022, 9:52am UTC](https://discourse.slicer.org/t/using-python-to-automate-pseudolmgenerator-and-alpaca-processes/22851/3 "2022-04-11T09:52:06Z")

</div>

> [@Erik.Meilak](#):
>
> slicer.modules.pseudolmgenerator.

Hi Sara, thank you for replying so quickly. I am trying to use the module code ( [SlicerMorph/PseudoLMGenerator/PseudoLMGenerator.py at dbd661146ef45b6f8d2f7778f9a4e8b65c28ff9b · SlicerMorph/SlicerMorph · GitHub](https://github.com/SlicerMorph/SlicerMorph/blob/dbd661146ef45b6f8d2f7778f9a4e8b65c28ff9b/PseudoLMGenerator/PseudoLMGenerator.py#L105) )  
to understand how to programmatically use the pseudoLMGenerator but I’m afraid I’m struggling to get it working.

To define the base mesh I try the following command:  
slicer.modules.PseudoLMGeneratorWidget.modelSelector(modelNode)

Where modelNode is a loaded model. However I am met with the following error:  
Traceback (most recent call last):  
File “”, line 1, in   
TypeError: ‘qMRMLNodeComboBox’ object is not callable

Would you be able to give example code on defining the base model for the pseudoLMGenerator followed by executing the “get subsample number” command? Many thanks for your help!

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [April 15, 2022, 5:15pm UTC](https://discourse.slicer.org/t/using-python-to-automate-pseudolmgenerator-and-alpaca-processes/22851/4 "2022-04-15T17:15:36Z")

</div>

You won’t need to set the widget elements, like the `modelSelector` to automate this module. The steps you will need are in the button callback functions.

I can help you with a code snippet, but first can you give me an idea of what you are trying to do? In a typical workflow, pseudo-landmarks are placed only once on an atlas model and transferred to other specimens using `ALPACA` or `projectSemiLM`. Both of these modules preserve the relationship between the pseudo-landmarks, which running the `pseudoLMGenerator` multiple times can not do.

---

<div class="post-metadata">

**Author:** ![Erik.Meilak](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@Erik.Meilak](https://discourse.slicer.org/u/Erik.Meilak)\
**Post date:** [April 20, 2022, 8:39am UTC](https://discourse.slicer.org/t/using-python-to-automate-pseudolmgenerator-and-alpaca-processes/22851/5 "2022-04-20T08:39:53Z")

</div>

I am trying to generate pseudo landmarks for a mesh which are then used for input as “Source Landmarks” in the ALPACA registration process. I have managed to do the entire process manually using the GUI but it is automating the process that is eluding me.  
You mention button callback functions, where would I find documentation/examples of these functions?

---

<div class="post-metadata">

**Author:** ![Erik.Meilak](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@Erik.Meilak](https://discourse.slicer.org/u/Erik.Meilak)\
**Post date:** [April 20, 2022, 10:46am UTC](https://discourse.slicer.org/t/using-python-to-automate-pseudolmgenerator-and-alpaca-processes/22851/6 "2022-04-20T10:46:57Z")

</div>

To put it another way, would you be able to give example code that is able to execute all the actions demonstrated in the following slicermorph youtube tutorials using the python interface?

PseudoLMGenerator and MarkupEditor tutorial: [PseudoLMGenerator and MarkupEditor tutorial - YouTube](https://www.youtube.com/watch?v=yvI65oyxYpA)  
Demo of ALPACA module in SlicerMorph tutorial: [Demo of ALPACA module in SlicerMorph - YouTube](https://www.youtube.com/watch?v=ZRikzsUBeAE)
