Skip to content

Setting mask image fails SPM Level1Design if input 'spm_mat_dir' is not default #2880

@jAchtzehn

Description

@jAchtzehn

Summary

Actually there are two issues caused by the same problem:

  1. issue: When using a mask image for SPM level1design and also changing the input "spm_mat_dir" the Level1Design node fails.

  2. issue: Even without changing the spm_mat_dir input, a problem arises when running different analyses in parallel: because each analysis is loading/writing the same SPM.mat file (in the default MATLAB folder) a file I/O error is happening at random (after ~50 runs).

Actual behavior

During level1design with an explicit mask as an input, the script is trying to load a (temporary?!) SPM.mat file in the folder (i) specified by "spm_mat_dir" or (ii) if that is left undefined in the default MATLAB folder (defined by startup.m or the last folder when MATLAB was last closed). However this SPM.mat file is always loaded at the default MATLAB folder, regardless of the "spm_mat_dir" input.

  1. issue: This of course returns an error that the file is not found.
Unable to read file 'SPM'. No such file or directory.
  1. issue: If the analyses is run many times (as I have to do it for trial-wise beta estimations) there seem to be problems because multiple Level1Design nodes running in parallel are saving/loading the same .mat file resulting in this error:
Unable to read MAT-file /Users/jachtzehn/matlab/SPM.mat. File might be corrupt.

"/Users/jachtzehn/matlab/" is my default MATLAB startup folder.

Expected behavior

The SPM.mat file should be saved and loaded in the folder specified by "spm_mat_dir" input or at least in a node specific folder.

How to replicate the behavior

Run a level1design on any functional data included an explicit mask (defined by "mask_image" input) + change the "spm_mat_dir" folder to anything other than default.

Script/Workflow details

When "mask_image" input if defined, model.py of the spm interface adds a couple of lines to the .m file that load a SPM.mat file, modify its contents and save it again. The respective code in model.py are: line 167 - 179:

if isdefined(self.inputs.mask_image):
            # SPM doesn't handle explicit masking properly, especially
            # when you want to use the entire mask image
            postscript = "load SPM;\n"
            postscript += ("SPM.xM.VM = spm_vol('%s');\n" % simplify_list(
                self.inputs.mask_image))
            postscript += "SPM.xM.I = 0;\n"
            postscript += "SPM.xM.T = [];\n"
            postscript += ("SPM.xM.TH = ones(size(SPM.xM.TH))*(%s);\n" %
                           self.inputs.mask_threshold)
            postscript += ("SPM.xM.xs = struct('Masking', "
                           "'explicit masking only');\n")
            postscript += "save SPM SPM;\n"

As you can see, this only adds "load SPM" to the script, so during runtime the script loads the SPM.mat file from the default Matlab folder, not the level1design node folder.

Platform details:

'commit_hash': '%h',
 'commit_source': 'archive substitution',
 'networkx_version': '2.0',
 'nibabel_version': '2.2.1',
 'nipype_version': '1.1.7',
 'numpy_version': '1.13.3',
 'pkg_path': '/Users/jachtzehn/miniconda2/envs/py27_nipype/lib/python2.7/site-packages/nipype',
 'scipy_version': '1.0.0',
 'sys_executable': '/Users/jachtzehn/miniconda2/envs/py27_nipype/bin/python',
 'sys_platform': 'darwin',
 'sys_version': '2.7.14 | packaged by conda-forge | (default, Mar 30 2018, 18:21:11) \n[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]',
 'traits_version': '4.6.0'}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions