Nilearn error using concat_imgs

Hi all,

I’m trying to concatenate two functional (.nii) data files to use in nilearn. Tried FSL, but kept getting errors with fslmerge.

Here’s the script I used in nilearn -------
output=nilearn.image.concat_imgs(basepath,‘cs_imagine_set1.nii.gz’, basepath,‘cs_imagine_set2.nii.gz’,
memory_level=0,
auto_resample=False, verbose=0)

When I run this, I get the following error -------
Traceback (most recent call last):
File “”, line 3, in
File “/Users/nibl/.local/lib/python2.7/site-packages/nilearn/_utils/niimg_conversions.py”, line 418, in concat_niimgs
ndim = ensure_ndim - 1
TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’

Is the ensure_ndim option not optional? If so, do I set it to the expected number of volumes for my concatenated file?

thanks for helping!
jenny

The signature of concat_imgs is:

concat_imgs(niimgs, dtype=<class 'numpy.float32'>, ensure_ndim=None, memory=Memory(cachedir=None), memory_level=0, auto_resample=False, verbose=0)

Thus, your call binds parameters as follows:

concat_imgs(basepath,'cs_imagine_set1.nii.gz', basepath,'cs_imagine_set2.nii.gz',
            memory_level=0, auto_resample=False, verbose=0)

concat_imgs(niimgs=basepath,
            dtype='cs_imagine_set1.nii.gz',
            ensure_ndim=basepath,
            memory='cs_imagine_set2.nii.gz',
            memory_level=0, auto_resample=False, verbose=0)

Unfortunately for you, you didn’t have a clash where you’d already assigned memory_level as a positional argument, rather than a keyword argument, so the reported exception ended up so apparently unrelated to your error. The actual error you had was that the images you wanted to concatenate were not specified as a single iterable variable (such as a list or tuple) in the first position.

What you almost certainly wanted was:

output = concat_imgs([os.path.join(basepath,'cs_imagine_set1.nii.gz'),
                      os.path.join(basepath,'cs_imagine_set2.nii.gz')],
                     memory_level=0, auto_resample=False, verbose=0)