nipype icon indicating copy to clipboard operation
nipype copied to clipboard

Matlab (called by interfaces.spm) throws an exception not captured by niype.

Open themeo opened this issue 2 years ago • 0 comments

Summary

I can't get Matlab/SPM to work properly. Matlab's run script throws a runtime when calling spm.Info.name().

Actual behavior

Calling spm.Info.name() throws an error. mlab.run().runtime.stderr (in nipype.interfaces/spm/base.py) returns:

/opt/matlab/R2021b/bin/matlab: eval: line 1701: unexpected EOF while looking for matching `'' /opt/matlab/R2021b/bin/matlab: eval: line 1702: syntax error: unexpected end of file

with runtime.errorcode = 2

runtime.output is:

Starting matlab/R2021b Executing /opt/matlab/R2021b/bin/matlab -singleCompThread -nodesktop -nosplash -singleCompThread -r fprintf(1,'Executing code at %s:\n',datestr(now));ver,try,addpath('/home/common/matlab/spm12_r7487_20181114');,if isempty(which('spm')),,throw(MException('SPMCheck:NotFound','SPM not in matlab path'));,end;,spm_path = spm('dir');,[name, version] = spm('ver');,fprintf(1, 'NIPYPE path:%s|name:%s|release:%s', spm_path, name, version);,exit;, ,catch ME,fprintf(2,'MATLAB code threw an exception:\n');fprintf(2,'%s\n',ME.message);if length(ME.stack) ~= 0, fprintf(2,'File:%s\nName:%s\nLine:%d\n',ME.stack.file,ME.stack.name,ME.stack.line);, end;end;;exit MATLAB is selecting SOFTWARE OPENGL rendering.

Pype does not capture this error, and execution follows until:

        out = sd._strip_header(out.runtime.stdout)
        out_dict = {}
        for part in out.split("|"):
            key, val = part.split(":")
            out_dict[key] = val

(in nipype.interfaces/spm/base.py) which raises an exception because runtime.output cannot be split by ":" into 2 parts.

Expected behavior

Interestingly, calling

spm.SPMCommand.set_mlab_paths(paths='/home/common/matlab/spm12_r7487_20181114/',
                              matlab_cmd='/opt/matlab/R2021b/bin/matlab')

also executes spm.Info.name(), but here execution follows without an error.

runtime.output:

MATLAB is selecting SOFTWARE OPENGL rendering.

                                                                                  < M A T L A B (R) >
                                                                        Copyright 1984-2021 The MathWorks, Inc.
                                                                       R2021b (9.11.0.1769968) 64-bit (glnxa64)
                                                                                  September 17, 2021

[...] MATLAB Version: 9.11.0.1769968 (R2021b) MATLAB License Number: 38957 Operating System: Linux 4.19.94-300.el7.x86_64 #1 SMP Thu Jan 9 16:15:13 UTC 2020 x86_64 Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode

MATLAB Version 9.11 (R2021b) [...] Statistical Parametric Mapping Version 7771 (SPM12) [...] NIPYPE path:/home/common/matlab/spm12_r7487_20181114|name:SPM12|release:7771

The last line of the output is then properly processed in spm.Info.name() .

I replicated the same behavior in Python3.8, and Matlab 2018b and 2016b. I also tried using nipype installed via pip instead of conda.

How to replicate the behavior

This is probably a system-specific error (although nipype should capture Matlab's error code), but I'm out of ideas on what might be causing the problem.

I checked that in both calls to mlab.run() (i.e., initiated by spm.SPMCommand.set_mlab_paths and by spm.Info.name()) all fields in mlab.inputs are identical.

It seems that nipype knows the correct paths to Matlab and SPM in both calls.

Script/Workflow details

import nipype.interfaces.spm as spm
spm.SPMCommand.set_mlab_paths(paths='/home/common/matlab/spm12_r7487_20181114/',
                              matlab_cmd='/opt/matlab/R2021b/bin/matlab') #works OK
print(spm.Info.name(paths='/home/common/matlab/spm12_r7487_20181114/')) # raises the error

Platform details:

{'commit_hash': '%h',
 'commit_source': 'archive substitution',
 'networkx_version': '2.7.1',
 'nibabel_version': '3.2.2',
 'nipype_version': '1.7.0',
 'numpy_version': '1.22.3',
 'pkg_path': '/home/language/jaksze/.conda/envs/mri/lib/python3.9/site-packages/nipype',
 'scipy_version': '1.8.0',
 'sys_executable': '/home/language/jaksze/.conda/envs/mri/bin/python3.9',
 'sys_platform': 'linux',
 'sys_version': '3.9.12 | packaged by conda-forge | (main, Mar 24 2022, '
                '23:25:59) \n'
                '[GCC 10.3.0]',
 'traits_version': '6.3.2'}

Execution environment

Choose one

  • Container [Tag: ???]
  • My python environment inside container [Base Tag: ???]
  • My python environment outside container

themeo avatar Mar 29 '22 13:03 themeo