def test_auto_mask(): # This mostly a smoke test data = np.zeros((9, 9, 9)) data[2:-2, 2:-2, 2:-2] = 10 img = Nifti1Image(data, np.eye(4)) masker = MultiNiftiMasker(mask_args=dict(opening=0)) # Check that if we have not fit the masker we get a intelligible # error pytest.raises(ValueError, masker.transform, [[ img, ]]) # Check error return due to bad data format pytest.raises(ValueError, masker.fit, img) # Smoke test the fit masker.fit([[img]]) # Test mask intersection data2 = np.zeros((9, 9, 9)) data2[1:-3, 1:-3, 1:-3] = 10 img2 = Nifti1Image(data2, np.eye(4)) masker.fit([[img, img2]]) assert_array_equal(get_data(masker.mask_img_), np.logical_or(data, data2)) # Smoke test the transform masker.transform([[ img, ]]) # It should also work with a 3D image masker.transform(img) # check exception when transform() called without prior fit() masker2 = MultiNiftiMasker(mask_img=img) with pytest.raises(ValueError, match='has not been fitted. '): masker2.transform(img2)
def test_dtype(): data = np.zeros((9, 9, 9), dtype=np.float64) data[2:-2, 2:-2, 2:-2] = 10 img = Nifti1Image(data, np.eye(4)) masker = MultiNiftiMasker(dtype='auto') masker.fit([[img]]) masked_img = masker.transform([[img]]) assert (masked_img[0].dtype == np.float32)
def test_compute_multi_gray_matter_mask(strategy): imgs = _get_random_imgs((9, 9, 5), 2) masker = MultiNiftiMasker(mask_strategy=strategy, mask_args={'opening': 1}) masker.fit(imgs) # Check that the order of the images does not change the output masker2 = MultiNiftiMasker(mask_strategy=strategy, mask_args={'opening': 1}) masker2.fit(imgs[::-1]) mask_ref = np.zeros((9, 9, 5), dtype='int8') np.testing.assert_array_equal(get_data(masker.mask_img_), mask_ref) np.testing.assert_array_equal(get_data(masker2.mask_img_), mask_ref)
def test_mask_strategy_errors(): # Error with unknown mask_strategy imgs = _get_random_imgs((9, 9, 5), 2) mask = MultiNiftiMasker(mask_strategy='foo') with pytest.raises(ValueError, match="Unknown value of mask_strategy 'foo'"): mask.fit(imgs) # Warning with deprecated 'template' strategy mask = MultiNiftiMasker(mask_strategy='template') with pytest.warns(UserWarning, match="Masking strategy 'template' is deprecated."): mask.fit(imgs)
def test_joblib_cache(): from joblib import hash # Dummy mask mask = np.zeros((40, 40, 40)) mask[20, 20, 20] = 1 mask_img = Nifti1Image(mask, np.eye(4)) with write_tmp_imgs(mask_img, create_files=True) as filename: masker = MultiNiftiMasker(mask_img=filename) masker.fit() mask_hash = hash(masker.mask_img_) get_data(masker.mask_img_) assert mask_hash == hash(masker.mask_img_) # enables to delete "filename" on windows del masker
def test_check_embedded_nifti_masker(): owner = OwningClass() masker = _check_embedded_nifti_masker(owner) assert type(masker) is MultiNiftiMasker for mask, multi_subject in ((MultiNiftiMasker(), True), (NiftiMasker(), False)): owner = OwningClass(mask=mask) masker = _check_embedded_nifti_masker(owner, multi_subject=multi_subject) assert type(masker) == type(mask) for param_key in masker.get_params(): if param_key not in [ 'memory', 'memory_level', 'n_jobs', 'verbose' ]: assert (getattr(masker, param_key) == getattr(mask, param_key)) else: assert (getattr(masker, param_key) == getattr(owner, param_key)) # Check use of mask as mask_img shape = (6, 8, 10, 5) affine = np.eye(4) mask = nibabel.Nifti1Image(np.ones(shape[:3], dtype=np.int8), affine) owner = OwningClass(mask=mask) masker = _check_embedded_nifti_masker(owner) assert masker.mask_img is mask # Check attribute forwarding data = np.zeros((9, 9, 9)) data[2:-2, 2:-2, 2:-2] = 10 imgs = nibabel.Nifti1Image(data, np.eye(4)) mask = MultiNiftiMasker() mask.fit([[imgs]]) owner = OwningClass(mask=mask) masker = _check_embedded_nifti_masker(owner) assert masker.mask_img is mask.mask_img_ # Check conflict warning mask = NiftiMasker(mask_strategy='epi') owner = OwningClass(mask=mask) with pytest.warns(UserWarning): _check_embedded_nifti_masker(owner)
def test_3d_images(): # Test that the MultiNiftiMasker works with 3D images mask_img = Nifti1Image(np.ones((2, 2, 2), dtype=np.int8), affine=np.diag((4, 4, 4, 1))) epi_img1 = Nifti1Image(np.ones((2, 2, 2)), affine=np.diag((4, 4, 4, 1))) epi_img2 = Nifti1Image(np.ones((2, 2, 2)), affine=np.diag((2, 2, 2, 1))) masker = MultiNiftiMasker(mask_img=mask_img) epis = masker.fit_transform([epi_img1, epi_img2]) # This is mostly a smoke test assert len(epis) == 2 # verify that 4D mask arguments are refused mask_img_4d = Nifti1Image(np.ones((2, 2, 2, 2), dtype=np.int8), affine=np.diag((4, 4, 4, 1))) masker2 = MultiNiftiMasker(mask_img=mask_img_4d) with pytest.raises(DimensionError, match="Input data has incompatible dimensionality: " "Expected dimension is 3D and you provided " "a 4D image."): masker2.fit()
def test_nan(): data = np.ones((9, 9, 9)) data[0] = np.nan data[:, 0] = np.nan data[:, :, 0] = np.nan data[-1] = np.nan data[:, -1] = np.nan data[:, :, -1] = np.nan data[3:-3, 3:-3, 3:-3] = 10 img = Nifti1Image(data, np.eye(4)) masker = MultiNiftiMasker(mask_args=dict(opening=0)) masker.fit([img]) mask = get_data(masker.mask_img_) assert mask[1:-1, 1:-1, 1:-1].all() assert not mask[0].any() assert not mask[:, 0].any() assert not mask[:, :, 0].any() assert not mask[-1].any() assert not mask[:, -1].any() assert not mask[:, :, -1].any()
# training data starts after the first 12 files fmri_random_runs_filenames = dataset.func[12:] stimuli_random_runs_filenames = dataset.label[12:] ############################################################################## # We can use :func:`nilearn.maskers.MultiNiftiMasker` to load the fMRI # data, clean and mask it. import numpy as np from nilearn.maskers import MultiNiftiMasker masker = MultiNiftiMasker(mask_img=dataset.mask, detrend=True, standardize=True) masker.fit() fmri_data = masker.transform(fmri_random_runs_filenames) # shape of the binary (i.e. black and wihte values) image in pixels stimulus_shape = (10, 10) # We load the visual stimuli from csv files stimuli = [] for stimulus_run in stimuli_random_runs_filenames: stimuli.append( np.reshape(np.loadtxt(stimulus_run, dtype=int, delimiter=','), (-1, ) + stimulus_shape, order='F')) ############################################################################## # Let's take a look at some of these binary images: