Repository: nilearn/nilearn

Issue:
NiftiMasker with target_affine throws exception.
I am using the NifitiMasker in a sklearn pipeline. Everythings works fine until i try to reshape the images:

`nifti_masker = NiftiMasker(target_affine=np.eye(3)*3)`

The error i got was:

`File "/anaconda3/envs/toolbox/lib/python3.6/site-packages/nilearn/masking.py", line 62, in _load_mask_img
    % values)
ValueError: Given mask is not made of 2 values: [-0.0000000e+00  1.4012985e-45  2.8025969e-45 ...  5.1444152e-15
  5.3116801e-15  1.0000000e+00]. Cannot interpret as true or false`

I unsing nilearn version 0.4.2 .


Discussion:
You probably want ro use interpolation='nearest' when resampling to keep only int values.

Thanks for your reply but I don't think thats the problem. The masking expects a mask out of two values so that it can stencil the region that you want to use. But the mask it gets is out of multiple values.  If I would use interpolation='nearest' the multiple values would be integers instead of floats but the problem still remains. 
In my eyes the problem could have two causes:

1. The function gets totaly wrong data or
2.  There is missing a preprocessing step like thresholding that reduce the date to two values.

But at least this is an unexpected behavior.
>The masking expects a mask out of two values so that it can stencil the region that you want to use. >But the mask it gets is out of multiple values.

If mask is extracted by itself with multiple values ? Then we need to look at what is causing this behavior. Can you give us a minimal script with data if you wish to like to know what's happening and propose possible fix/solution for this ?

Just want to make sure that if mask is given explicitly, then mask should have [0, 1].

Thanks
Sure! Here is a py.test that fails: 
```
import numpy as np
from nilearn import datasets
from nilearn.input_data import NiftiMasker
import pytest

def test_reshape():
    mri_images = datasets.fetch_neurovault_motor_task()['images']

    nifti_masker = NiftiMasker()
    nifti_masker.target_affine = np.eye(3) * 5

    nifti_masker.fit(mri_images)
    try:
       nifti_masker.transform(mri_images)
    except ValueError:
        pytest.fail("ValueError accours")
```
Sorry I forgot to track the error. There is indeed a bug in the library: when the mask data are interpolated, the mask should be kept binary. Thx for posting.
Note that a workaround is to process the mask first, making sure it is binary, then mask the data. This is a bit inelegant I agree.
I would do pre (after mask extraction) and post resample sanity checks on the number of unique values in mask image. If they are not same then I would impose ```math_img``` to threshold. 

That this sounds good ? 
Why not thresholding directly then ? The cost is so much smaller than that of resampling.
>Why not thresholding directly then ? 

Without sanity checks ?
Maybe I'm missing something, but if the mask image has to be binary  ---is it really the case ?--- I don't seen any issue with binarizing systematically. 
 
I made a PR. May be we can iterate on that #1805 

Resolving patch:
diff --git a/nilearn/input_data/nifti_masker.py b/nilearn/input_data/nifti_masker.py
--- a/nilearn/input_data/nifti_masker.py
+++ b/nilearn/input_data/nifti_masker.py
@@ -255,7 +255,7 @@ def fit(self, imgs=None, y=None):
             self.mask_img_,
             target_affine=self.target_affine,
             target_shape=self.target_shape,
-            copy=False)
+            copy=False, interpolation='nearest')
         if self.target_affine is not None:
             self.affine_ = self.target_affine
         else:


Test patch:
diff --git a/nilearn/input_data/tests/test_nifti_masker.py b/nilearn/input_data/tests/test_nifti_masker.py
--- a/nilearn/input_data/tests/test_nifti_masker.py
+++ b/nilearn/input_data/tests/test_nifti_masker.py
@@ -14,7 +14,6 @@
 import nibabel
 import numpy as np
 from nibabel import Nifti1Image
-from nose import SkipTest
 from nose.tools import assert_true, assert_false, assert_raises
 from numpy.testing import assert_array_equal, assert_equal
 
@@ -61,6 +60,19 @@ def test_detrend():
     assert_true(np.any(X != 0))
 
 
+def test_resample():
+    # Check that target_affine triggers the right resampling
+    data = np.zeros((9, 9, 9))
+    data[3:-3, 3:-3, 3:-3] = 10
+    img = Nifti1Image(data, np.eye(4))
+    mask = data.astype(np.int)
+    mask_img = Nifti1Image(mask, np.eye(4))
+    masker = NiftiMasker(mask_img=mask_img, target_affine=2 * np.eye(3))
+    # Smoke test the fit
+    X = masker.fit_transform(img)
+    assert_true(np.any(X != 0))
+
+
 def test_with_files():
     # Standard masking
     data = np.zeros((40, 40, 40, 2))