Returning a vector from CLI module

Hello Developers.

I am developing a CLI module and calling it from my scripted module using “slicer.cli.run” . I can parse the arguments from my scripted module to CLI module but I failed to get the returning arguments from my CLI module.

I am trying to copy fudicial registration module but unfortunately I wasn’t able to get the returning argument in my scripted module. I am missing simething, it might be very basic, but I have wasted alot of time in it.

My XML is:

<?xml version="1.0" encoding="utf-8"?>
Examples CLI_test 0.0.1 http://www.example.com/Slicer/Modules/CLI_test Slicer FirstName LastName (Institution), FirstName LastName (Institution) This work was partially funded by NIH grant NXNNXXNNNNNN-NNXN IO sigmas sigmas s output
  <label>Sigmas</label>
  <description><![CDATA[A double value (in units of mm) passed to the algorithm]]></description>
     </float-vector>

<double>
  <name>rms</name>
  <longflag>rms</longflag>
  <description><![CDATA[Display RMS Error.]]></description>
  <flag>R</flag>
  <label>RMS Error</label>
  <channel>output</channel>
  <default>0.1</default>
</double>

The part of updating rms value in my CLI .cpp code is:

rms=42.0 ;
std::ofstream rts;
rts.open(returnParameterFile.c_str() );
rts << “rms=” << rms << std::endl;
rts.close();

I dont know how to get this “rms” value in my scripted module (python code). Thank you in advance

Just to be clear, If I run my CLI module as standalone program it does generate an output text file, my question is, how can I get the output if I call my CLI from scripted module with slicer.cli.run command.

Thank you.

Return values are stored in the parameter node and you can retrieve it using GetParameterAsString() method. For example:

parameterNode = slicer.util.getNode('Execution Model Tour')
returnedRmsValue = float(parameterNode.GetParameterAsString('rms'))

Thank you for you reply Andras, I have tried this command, the problem is my ‘rms’ variable doesn’t get updated. its just returning the same initial value.

Ideally, inital rms value should go into my CLI module , it should get updated, and I should get the updated variable.

this is the python code from where I am calling my CLI

module = slicer.modules.cli_test

rms=5.0
a=(np.random.randint(0, 100, size=(30, 10, 2)) ).tolist()

cliParams = {
  'sigmas' : a,
  'rms' : rms,


}

cliNode = slicer.cli.run(module, None, cliParams, wait_for_completion=True)

returnedRmsValue = float(cliNode.GetParameterAsString('rms'))
print(returnedRmsValue)

The returned RMS value should be 49, but it is still giving 5.
Am I missing something?

A parameter can be either input or output, defined by the channel element. For example, <channel>output</channel> means an output parameter.

yes, I have defined the parameter as output. (xml attached in my first post). Even then it is not updating.

It works for me with the ExecutionModelTour test module. See how to correctly write output parameters here:

There are no whitespaces in your code above, maybe that breaks the output file parsing.

Thank you fro your response Andras, I have tried the exact same thing. Even hardcoded the text, it is still returning the same value.

Am I making any mistake in calling a CLI?

cliNode = slicer.cli.run(module, None, cliParams, wait_for_completion=True)

parameters being:

rms=5.0
a=(np.random.randint(0, 100, size=(30, 10, 2)) ).tolist()
sigmaout=23.0
cliParams = {
  'sigmas' : a,
  'rms' : rms,
  'sigmaout' : sigmaout

}

Thank you Andras, it worked. there were some other problems. Thank you for your help

1 Like