nipype
nipype copied to clipboard
Getting AttributeError when receving Matlab output using nipype
Great work from the team.
Moving forward, I would like to link nipype with Matlab. Specifically, the objective is to submit some argument from Python through nipype to a Matlab script and, submit the Matlab output back to Python.
Working with an extension of the example, in this case, we have a .m
(i.e., name get_print
) file which has been set to be under Matlab search path.
The .m
file is
function opt=get_print(ipt_str)
opt = 1 + 1;
if_possible_return_multiple_output=3 % Suggestion to return multiple output is welcome
end
Then, the Python script is as below
def _my_script(self):
"""This is where you implement your script"""
script = """
%s=get_print(%s)
""" % (self.inputs.name, self.output.val)
return script
When execute the Python script, the Python compiler return an error;
AttributeError: ‘HelloWorld’ object has no attribute ‘output’
May I know how solve this issue when expecting an output from Matlab operation?
The complete Python code is
from nipype.interfaces.base import traits
from nipype.interfaces.base import TraitedSpec
from nipype.interfaces.matlab import MatlabCommand, MatlabInputSpec
class HelloWorldInputSpec(MatlabInputSpec):
name = traits.Str(mandatory=True,
desc='Name of person to say hello to')
class HelloWorldOutputSpec(TraitedSpec):
matlab_output = traits.Str()
class HelloWorld(MatlabCommand):
input_spec = HelloWorldInputSpec
output_spec = HelloWorldOutputSpec
def _my_script(self): # similar to _bipolar_reference
"""This is where you implement your script"""
script = """
%s=get_print(%s)
""" % (self.inputs.name, self.output.val)
return script
def run(self, **inputs):
# Inject your script
self.inputs.script = self._my_script()
results = super(MatlabCommand, self).run(**inputs)
stdout = results.runtime.stdout
# Attach stdout to outputs to access matlab results
results.outputs.matlab_output = stdout
return results
def _list_outputs(self):
outputs = self._outputs().get()
return outputs
hello = HelloWorld()
hello.inputs.name = 'hello_world'
out = hello.run()
output_matlab=out.outputs.matlab_output