# How to define and save output of a cli module in Python script?

**URL:** <https://discourse.slicer.org/t/how-to-define-and-save-output-of-a-cli-module-in-python-script/12289>\
**Category:** Support\
**Created:** [June 30, 2020, 9:22am UTC](https://discourse.slicer.org/t/how-to-define-and-save-output-of-a-cli-module-in-python-script/12289 "2020-06-30T09:22:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ponyo2311](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ponyo2311/32/7078_2.png) [@Ponyo2311](https://discourse.slicer.org/u/Ponyo2311)\
**Post date:** [June 30, 2020, 9:22am UTC](https://discourse.slicer.org/t/how-to-define-and-save-output-of-a-cli-module-in-python-script/12289/1 "2020-06-30T09:22:38Z")

</div>

Hi,  
I’m trying to run **N4ITK Bias correction** (a cli module) from Jupyter Notebook and am struggling with defining the output parameter.  
The code I wrote reads my inputs and creates a node, but I don’t know whether it ran (executed the bias correction) and how to save the output. I am working with NIFTII images.  
**My code:**

input\_volume=slicer.util.loadVolume(path\_to\_T1)  
slicernb.ViewDisplay()#it worked  
mask\_volume=slicer.util.loadVolume(path\_to\_mask)  
slicernb.ViewDisplay()#it worked  
output\_volume=(slicer.mrmlScene.AddNewNodeByClass (“vtkMRMLCommandLineModuleNode”,“Name\_for\_corrected volume.nii”))

slicer.mrmlScene.AddNode(output\_volume)

parameters={}  
parameters[‘initialMeshResolution’]=(1,1,1)  
parameters[‘splineDistance’]=0.00  
parameters[‘bfFWHM’]=0.00  
parameters[‘numberOfIterations’]=(200,200,200)  
parameters[‘convergenceThreshold’]=0.0001  
parameters[‘bsplineOrder’]=3  
parameters[‘shrinkFactor’]=4  
parameters[‘weightImageName’]=‘None’  
parameters[‘wienerFilterNoise’]=0.00  
parameters[‘nHistogramBins’]=0  
parameters[‘inputImageName’]=input\_volume.GetID()  
parameters[‘maskImageName’]=mask\_volume.GetID()  
parameters[‘outputImageName’]=output\_volume.GetID()  
parameters[‘outputBiasFieldName’]=‘None’

slicer.cli.run(slicer.modules.n4itkbiasfieldcorrection,node=None,parameters=parameters)

**How could I make it save a corrected T1 volume?**

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 30, 2020, 6:57pm UTC](https://discourse.slicer.org/t/how-to-define-and-save-output-of-a-cli-module-in-python-script/12289/2 "2020-06-30T18:57:21Z")

</div>

You probably want `slicer.cli.runSync`

See [https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python\_scripting#Running\_a\_CLI\_from\_Python](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Running_a_CLI_from_Python)

---

<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:** [June 30, 2020, 8:36pm UTC](https://discourse.slicer.org/t/how-to-define-and-save-output-of-a-cli-module-in-python-script/12289/3 "2020-06-30T20:36:07Z")

</div>

This issues comes up so often that I think we should change the default behavior of run to be `runSync`. It would be a good opportunity to do it now (for Slicer5). What do you think? @pieper @cpinter @jcfr

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 1, 2020, 12:36am UTC](https://discourse.slicer.org/t/how-to-define-and-save-output-of-a-cli-module-in-python-script/12289/4 "2020-07-01T00:36:42Z")

</div>

Yes, that makes sense - people expect a python api to by synchronous by default. Rather than a `runAsync` how about .a method called `start` to be more explicit what it does.

---

<div class="post-metadata">

**Author:** ![Ponyo2311](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ponyo2311/32/7078_2.png) [@Ponyo2311](https://discourse.slicer.org/u/Ponyo2311)\
**Post date:** [July 1, 2020, 9:04am UTC](https://discourse.slicer.org/t/how-to-define-and-save-output-of-a-cli-module-in-python-script/12289/5 "2020-07-01T09:04:52Z")

</div>

Thank you for the reply.

I tried to run the code replacing _slicer.cli.run_ by _slicer.cli.runSync_ and now the Jupyter cell is just running for very long, still with no outputs whatsoever.

It would be useful to know what is happening so i tried add these commands (after restarting the kernel):

_newNode=slicer.cli.runSync(slicer.modules.n4itkbiasfieldcorrection,  
node=None,parameters=parameters)  
newNode.GetOutputText()  
newNode.GetErrorText()_

but with no messages displayed - it seems that the execution is somehow stuck at slicer.cli.runSync() for some reason.

Is there something wrong with my code?

---

<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:** [July 17, 2020, 7:27pm UTC](https://discourse.slicer.org/t/how-to-define-and-save-output-of-a-cli-module-in-python-script/12289/6 "2020-07-17T19:27:11Z")

</div>

Can you provide the complete script (using one of the Slicer sample data sets as input)?
