Nilearn: Error using SpaceNetRegressor

Hi all,

Encountering a strange error using the SpaceNetRegressor fit function. My input is a list of images y_train and an array of scalars X_train. Each are the same length. All the images are correct and the same dimensions.

Here’s my (simplified) code:

decoder = SpaceNetRegressor(mask=V_img, penalty=params_snet['penalty'],
                        eps=params_snet['eps'],  # prefer large alphas
                        memory=params_snet['memory'])

istrain = train_set[:,f]
y_train = Xs[glm][istrain][:,0]
X_train = np.array(V_betas)
X_train = list(X_train[istrain])

print(len(X_train))
print(y_train.size)
print(X_train[0].shape)

decoder.fit(X_train, y_train)

The output is:

120
120
(182, 218, 182)
[NiftiMasker.fit] Loading data from None
[NiftiMasker.fit] Resampling mask
/Users/lpzatr/anaconda3/lib/python3.6/site-packages/nilearn/_utils/cache_mixin.py:291: UserWarning: memory_level is currently set to 0 but a Memory object has been provided. Setting memory_level to 1.
  warnings.warn("memory_level is currently set to 0 but "
/Users/lpzatr/anaconda3/lib/python3.6/site-packages/nilearn/decoding/space_net.py:195: RuntimeWarning: divide by zero encountered in log10
  return np.logspace(np.log10(alpha_min), np.log10(alpha_max),
/Users/lpzatr/anaconda3/lib/python3.6/site-packages/numpy/core/function_base.py:117: RuntimeWarning: invalid value encountered in double_scalars
  delta = stop - start
/Users/lpzatr/anaconda3/lib/python3.6/site-packages/nilearn/decoding/proximal_operators.py:20: RuntimeWarning: invalid value encountered in maximum
  shrink[y_nz] = np.maximum(1 - alpha / np.abs(y[y_nz]), 0)
/Users/lpzatr/anaconda3/lib/python3.6/site-packages/nilearn/decoding/space_net.py:274: RuntimeWarning: invalid value encountered in reduce
  if w.ptp() == 0:

...

~/anaconda3/lib/python3.6/site-packages/scipy/stats/mstats_basic.py in spearmanr(x, y, use_ties)
    457     df = n-2
    458     if df < 0:
--> 459         raise ValueError("The input must have at least 3 entries!")
    460 
    461     # Gets the ranks and rank differences

ValueError: The input must have at least 3 entries!

Any idea where to look for issues? No idea why the input is less than 3 entries at this point.

I wonder if the strange behavior (which I might agree calling a bug) comes from the fact that you are giving a list of images. Do you reproduce the problem with a 4D images?

Hi Gael,

Thanks for your quick response. I ran this using a 4D image (assuming the order is x,y,z,subj?) and it seemed to get a bit further, but ultimately gave the same error:

istrain = train_set[:,f]
y_train = Xs[glm][istrain][:,0]
X_train = V_betas[:,:,:,istrain]

X_train_img = nib.Nifti1Image(X_train, V_img.affine, V_img.header)

print(y_train.size)
print(X_train.shape)

decoder.fit(X_train_img, y_train)

Output:

120
(182, 218, 182, 120)
[NiftiMasker.fit] Loading data from None
[NiftiMasker.fit] Resampling mask
/Users/lpzatr/anaconda3/lib/python3.6/site-packages/nilearn/_utils/cache_mixin.py:291: UserWarning: memory_level is currently set to 0 but a Memory object has been provided. Setting memory_level to 1.
  warnings.warn("memory_level is currently set to 0 but "
[NiftiMasker.transform_single_imgs] Loading data from Nifti1Image(
shape=(182, 218, 182, 120),
affine=array([[  -1.,    0.,    0.,   90.],
       [   0.,    1.,    0., -126.],
       [   0.,    0.,    1.,  -72.],
       [   0.,    0.,    0.,    1.]])
)
[NiftiMasker.transform_single_imgs] Extracting region signals
[NiftiMasker.transform_single_imgs] Cleaning extracted signals

...

~/anaconda3/lib/python3.6/site-packages/scipy/stats/mstats_basic.py in spearmanr(x, y, use_ties)
    457     df = n-2
    458     if df < 0:
--> 459         raise ValueError("The input must have at least 3 entries!")
    460 
    461     # Gets the ranks and rank differences

ValueError: The input must have at least 3 entries!

I’m using Jupyter Notebook so it’s tricky to debug, but I’ll get it set up in an IDE and try and dig further.

OK, that’s not the problem.

Looking at the traceback better, the input is three entries in the inner cross-validation loop that is used to set parameters, in particular in the test set there. You are not specifying a cv, are you?

Nope, I haven’t set a cv parameter. According to the docs that means it uses a default of 8 folds…

Just ran it with cv=None (so I guess 3 folds) and it gives the same error.

@andrew.reid Is it easy for you to give X_train_img, y_train uploading somewhere ?

It helps to diagnose the error.

Thanks

Hi!

In the process of doing that, I’ve found the issue. I was using my intercept column (all 1’s) instead of an actual data column as the y input. This cause an np.dot step to return nans and everything failed at that point!

It appears to be running fine now. Thanks for your help anyways… love the nilearn package!

OK, good that you diagnosed the problem. We need to add a test to capture such situation and raise a meaningful error.

Good you please add an issue to the nilearn issue tracker to explain the problem.

Thanks!

I am in the process of preparing a PR for that.