Run MATLAB script in nipype

Hi!
I’m following this tutorial to learn to wrap MATLAB scripts in nipype.

Here’s my code:

import re
import nibabel as nb
from scipy.io import savemat
from nipype.interfaces.base import (CommandLine, traits, TraitedSpec,
BaseInterface, BaseInterfaceInputSpec, File)

class BrainVolumeMATLABInputSpec(BaseInterfaceInputSpec):
in_file = File(exists=True, mandatory=True)
script_file = File(exists=True, mandatory=True)

class BrainVolumeMATLABOutputSpec(TraitedSpec):
volume = traits.Int(desc=‘brain volume’)

class BrainVolumeMATLAB(BaseInterface):
input_spec = BrainVolumeMATLABInputSpec
output_spec = BrainVolumeMATLABOutputSpec

Specify the interface inputs

in_file = ‘/media/mrsahlgrenska/iNPH/ROI/ROI_BIDS/sub-01_ses-1/ROIinFLAIR/cauddx.nii’
script_file = ‘/home/mrsahlgrenska/Documents/rygg/test/brainvolume.m’

1. save the image in matlab format as tmp_image.mat

tmp_image = ‘tmp_image’
data = nb.load(in_file).get_data()
savemat(tmp_image + ‘.mat’, {b’data’: data}, do_compression=False)

2. load script

with open(script_file) as script_file:
script_content = script_file.read()

3. replace the input_image.mat file with the actual input of this interface

with open(‘newscript.m’, ‘w’) as script_file:
script_file.write(script_content.replace(‘input_image.mat’, ‘tmp_image.mat’))

4. run the matlab script

mlab = CommandLine(‘matlab’, args=‘newscript.m’, terminal_output=‘stream’)
result = mlab.run()

5. extract the volume estimation from the output

expr_tra = re.compile(‘total\ =\s+(?P[0-9]+)’)
volume = int(expr_tra.search(result.runtime.stdout).groupdict()[‘total’])
print(volume)

The error message:

200706-08:56:47,8 nipype.interface INFO:
stdout 2020-07-06T08:56:47.008550:MATLAB is selecting SOFTWARE OPENGL rendering.
200706-08:56:51,0 nipype.interface INFO:
stdout 2020-07-06T08:56:50.997593:
200706-08:56:51,9 nipype.interface INFO:
stdout 2020-07-06T08:56:50.997593: < M A T L A B ® >
200706-08:56:51,9 nipype.interface INFO:
stdout 2020-07-06T08:56:50.997593: Copyright 1984-2020 The MathWorks, Inc.
200706-08:56:51,10 nipype.interface INFO:
stdout 2020-07-06T08:56:50.997593: R2020a Update 3 (9.8.0.1396136) 64-bit (glnxa64)
200706-08:56:51,11 nipype.interface INFO:
stdout 2020-07-06T08:56:50.997593: May 27, 2020
200706-08:56:51,11 nipype.interface INFO:
stdout 2020-07-06T08:56:50.997593:
200706-08:56:51,504 nipype.interface INFO:
stdout 2020-07-06T08:56:51.504070:
200706-08:56:51,536 nipype.interface INFO:
stdout 2020-07-06T08:56:51.536610:To get started, type doc.
200706-08:56:51,537 nipype.interface INFO:
stdout 2020-07-06T08:56:51.537470:For product information, visit www.mathworks.com.
200706-08:56:51,539 nipype.interface INFO:
stdout 2020-07-06T08:56:51.538982:


AttributeError Traceback (most recent call last)
in
41 # 5. extract the volume estimation from the output
42 expr_tra = re.compile(‘total\ =\s+(?P[0-9]+)’)
—> 43 volume = int(expr_tra.search(result.runtime.stdout).groupdict()[‘total’])
44 print(volume)

AttributeError: ‘NoneType’ object has no attribute ‘groupdict’

In the MATLAB script I’ve added the line sprintf("test") just to verify that the script is running. But “test” doesn’t appear anywhere so I’ve come to the conclusion that all I do is open MATLAB without running the script. Any thoughts?

Sincerely, Jesper