def test_components_img(): shape = (6, 8, 10, 5) affine = np.eye(4) rng = np.random.RandomState(0) # Create a "multi-subject" dataset data = [] for i in range(8): this_data = rng.normal(size=shape) # Create fake activation to get non empty mask this_data[2:4, 2:4, 2:4, :] += 10 data.append(nibabel.Nifti1Image(this_data, affine)) mask_img = nibabel.Nifti1Image(np.ones(shape[:3], dtype=np.int8), affine) n_components = 3 multi_pca = MultiPCA(mask=mask_img, n_components=n_components, random_state=0) # fit to the data and test for components images multi_pca.fit(data) components_img = multi_pca.components_img_ assert_true(isinstance(components_img, nibabel.Nifti1Image)) check_shape = data[0].shape[:3] + (n_components, ) assert_equal(components_img.shape, check_shape) assert_equal(len(components_img.shape), 4)
def subject_pca(subject_dir, n_components=512, smoothing_fwhm=6, mask_img='gm_mask.nii'): files = sorted(glob.glob(os.path.join( subject_dir, 'rfMRI_REST?_??/rfMRI_REST?_??.nii.gz'))) confounds = get_confounds(subject_dir, files, mask_img=mask_img) multi_pca = MultiPCA(mask=mask_img, smoothing_fwhm=smoothing_fwhm, t_r=.7, low_pass=.1, n_components=n_components, do_cca=False, n_jobs=N_JOBS, verbose=12) multi_pca.fit(files, confounds=confounds) return multi_pca.components_ * multi_pca.variance_[:, np.newaxis]
def test_with_globbing_patterns_with_single_image(): # With single image data_4d = np.zeros((40, 40, 40, 3)) data_4d[20, 20, 20] = 1 img_4d = nibabel.Nifti1Image(data_4d, affine=np.eye(4)) multi_pca = MultiPCA(n_components=3) with write_tmp_imgs(img_4d, create_files=True, use_wildcards=True) as img: input_image = _tmp_dir() + img multi_pca.fit(input_image) components_img = multi_pca.components_img_ assert_true(isinstance(components_img, nibabel.Nifti1Image)) # n_components = 3 check_shape = img_4d.shape[:3] + (3, ) assert_equal(components_img.shape, check_shape) assert_equal(len(components_img.shape), 4)
def test_with_globbing_patterns_with_single_image(): # With single image data_4d = np.zeros((40, 40, 40, 3)) data_4d[20, 20, 20] = 1 img_4d = nibabel.Nifti1Image(data_4d, affine=np.eye(4)) multi_pca = MultiPCA(n_components=3) with write_tmp_imgs(img_4d, create_files=True, use_wildcards=True) as img: input_image = _tmp_dir() + img multi_pca.fit(input_image) components_img = multi_pca.components_img_ assert_true(isinstance(components_img, nibabel.Nifti1Image)) # n_components = 3 check_shape = img_4d.shape[:3] + (3,) assert_equal(components_img.shape, check_shape) assert_equal(len(components_img.shape), 4)
def __init__(self, method, n_parcels=50, random_state=0, mask=None, smoothing_fwhm=4., standardize=False, detrend=False, low_pass=None, high_pass=None, t_r=None, target_affine=None, target_shape=None, mask_strategy='epi', mask_args=None, memory=Memory(cachedir=None), memory_level=0, n_jobs=1, verbose=1): self.method = method self.n_parcels = n_parcels MultiPCA.__init__(self, n_components=200, random_state=random_state, mask=mask, memory=memory, smoothing_fwhm=smoothing_fwhm, standardize=standardize, detrend=detrend, low_pass=low_pass, high_pass=high_pass, t_r=t_r, target_affine=target_affine, target_shape=target_shape, mask_strategy=mask_strategy, mask_args=mask_args, memory_level=memory_level, n_jobs=n_jobs, verbose=verbose)
def __init__(self, algorithm, n_parcels=50, n_components=100, linkage='ward', init='k-means++', connectivity=None, random_state=0, mask=None, target_affine=None, target_shape=None, low_pass=None, high_pass=None, t_r=None, smoothing_fwhm=None, standardize=False, detrend=False, memory=Memory(cachedir=None), memory_level=0, n_jobs=1, verbose=1, shelve=False): self.algorithm = algorithm self.n_parcels = n_parcels self.linkage = linkage self.init = init self.connectivity = connectivity self.shelve = shelve MultiPCA.__init__(self, n_components=n_components, random_state=random_state, mask=mask, memory=memory, memory_level=memory_level, n_jobs=n_jobs, verbose=verbose)
def fit(self, imgs, y=None, confounds=None): """ Fit the clustering technique to fmri images Parameters ---------- X : List of Niimg-like objects Data from which parcellations will be returned. """ if self.algorithm is None: raise ValueError("Parcellation algorithm must be specified in " "['minibatchkmeans', 'featureagglomeration'].") valid_algorithms = self.VALID_ALGORITHMS if self.algorithm not in valid_algorithms: raise ValueError("Invalid algorithm={0} is provided. Please one " "among them {1}".format(self.algorithm, valid_algorithms)) if not hasattr(imgs, '__iter__'): imgs = [imgs] if isinstance(self.mask, (NiftiMasker, MultiNiftiMasker)): if self.memory is None and self.mask.memory is not None: self.memory = self.mask.memory if self.memory_level is None and \ self.mask.memory_level is not None: self.memory_level = self.mask.memory_level if self.n_jobs is None and self.mask.n_jobs is not None: self.n_jobs = self.mask.n_jobs MultiPCA.fit(self, imgs) if self.verbose: print("[Parcellations] Learning the data") self._fit_method(self.components_) return self
def test_components_img(): shape = (6, 8, 10, 5) affine = np.eye(4) rng = np.random.RandomState(0) # Create a "multi-subject" dataset data = [] for i in range(8): this_data = rng.normal(size=shape) # Create fake activation to get non empty mask this_data[2:4, 2:4, 2:4, :] += 10 data.append(nibabel.Nifti1Image(this_data, affine)) mask_img = nibabel.Nifti1Image(np.ones(shape[:3], dtype=np.int8), affine) n_components = 3 multi_pca = MultiPCA(mask=mask_img, n_components=n_components, random_state=0) # fit to the data and test for components images multi_pca.fit(data) components_img = multi_pca.components_img_ assert_true(isinstance(components_img, nibabel.Nifti1Image)) check_shape = data[0].shape[:3] + (n_components,) assert_equal(components_img.shape, check_shape) assert_equal(len(components_img.shape), 4)
def _raw_fit(self, data): group = self.group sub_num = self.sub_num if group: data = MultiPCA._raw_fit(self, data).T else: data = data.reshape((3, self.n_components, -1)) data = data[sub_num - 1] # plt.plot(data[:, :3]) # plt.tight_layout() # plt.show() self.hvmf_fit(data.T) return self
def test_multi_pca(): # Smoke test the MultiPCA # XXX: this is mostly a smoke test shape = (6, 8, 10, 5) affine = np.eye(4) rng = np.random.RandomState(0) # Create a "multi-subject" dataset data = [] for i in range(8): this_data = rng.normal(size=shape) # Create fake activation to get non empty mask this_data[2:4, 2:4, 2:4, :] += 10 data.append(nibabel.Nifti1Image(this_data, affine)) mask_img = nibabel.Nifti1Image(np.ones(shape[:3], dtype=np.int8), affine) multi_pca = MultiPCA(mask=mask_img, n_components=3) # Test that the components are the same if we put twice the same data components1 = multi_pca.fit(data).components_ components2 = multi_pca.fit(2 * data).components_ np.testing.assert_array_almost_equal(components1, components2) # Smoke test fit with 'confounds' argument confounds = [np.arange(10).reshape(5, 2)] * 8 multi_pca.fit(data, confounds=confounds) # Smoke test that multi_pca also works with single subject data multi_pca.fit(data[0]) # Check that asking for too little components raises a ValueError multi_pca = MultiPCA() nose.tools.assert_raises(ValueError, multi_pca.fit, data[:2]) # Smoke test the use of a masker and without CCA multi_pca = MultiPCA(mask=MultiNiftiMasker(mask_args=dict(opening=0)), do_cca=False, n_components=3) multi_pca.fit(data[:2]) # Smoke test the transform and inverse_transform multi_pca.inverse_transform(multi_pca.transform(data[-2:]))
def test_multi_pca_score(): shape = (6, 8, 10, 5) affine = np.eye(4) rng = np.random.RandomState(0) # Create a "multi-subject" dataset imgs = [] for i in range(8): this_img = rng.normal(size=shape) imgs.append(nibabel.Nifti1Image(this_img, affine)) mask_img = nibabel.Nifti1Image(np.ones(shape[:3], dtype=np.int8), affine) # Assert that score is between zero and one multi_pca = MultiPCA(mask=mask_img, random_state=0, memory_level=0, n_components=3) multi_pca.fit(imgs) s = multi_pca.score(imgs) assert_true(np.all(s <= 1)) assert_true(np.all(0 <= s)) # Assert that score does not fail with single subject data multi_pca = MultiPCA(mask=mask_img, random_state=0, memory_level=0, n_components=3) multi_pca.fit(imgs[0]) s = multi_pca.score(imgs[0]) assert_true(isinstance(s, float)) assert (0. <= s <= 1.) # Assert that score is one for n_components == n_sample # in single subject configuration multi_pca = MultiPCA(mask=mask_img, random_state=0, memory_level=0, n_components=5) multi_pca.fit(imgs[0]) s = multi_pca.score(imgs[0]) assert_almost_equal(s, 1., 1) # Per component score multi_pca = MultiPCA(mask=mask_img, random_state=0, memory_level=0, n_components=5) multi_pca.fit(imgs[0]) masker = NiftiMasker(mask_img).fit() s = multi_pca._raw_score(masker.transform(imgs[0]), per_component=True) assert_equal(s.shape, (5, )) assert_true(np.all(s <= 1)) assert_true(np.all(0 <= s))
def test_multi_pca(): # Smoke test the MultiPCA # XXX: this is mostly a smoke test shape = (6, 8, 10, 5) affine = np.eye(4) rng = np.random.RandomState(0) # Create a "multi-subject" dataset data = [] for i in range(8): this_data = rng.normal(size=shape) # Create fake activation to get non empty mask this_data[2:4, 2:4, 2:4, :] += 10 data.append(nibabel.Nifti1Image(this_data, affine)) mask_img = nibabel.Nifti1Image(np.ones(shape[:3], dtype=np.int8), affine) multi_pca = MultiPCA(mask=mask_img, n_components=3, random_state=0) # fit to the data and test for masker attributes multi_pca.fit(data) assert_true(multi_pca.mask_img_ == mask_img) assert_true(multi_pca.mask_img_ == multi_pca.masker_.mask_img_) # Test that the components are the same if we put twice the same data, and # that fit output is deterministic components1 = multi_pca.components_ components2 = multi_pca.fit(data).components_ components3 = multi_pca.fit(2 * data).components_ np.testing.assert_array_equal(components1, components2) np.testing.assert_array_almost_equal(components1, components3) # Smoke test fit with 'confounds' argument confounds = [np.arange(10).reshape(5, 2)] * 8 multi_pca.fit(data, confounds=confounds) # Smoke test that multi_pca also works with single subject data multi_pca.fit(data[0]) # Check that asking for too little components raises a ValueError multi_pca = MultiPCA() assert_raises(ValueError, multi_pca.fit, data[:2]) # Test fit on data with the use of a masker masker = MultiNiftiMasker() multi_pca = MultiPCA(mask=masker, n_components=3) multi_pca.fit(data) assert_true(multi_pca.mask_img_ == multi_pca.masker_.mask_img_) # Smoke test the use of a masker and without CCA multi_pca = MultiPCA(mask=MultiNiftiMasker(mask_args=dict(opening=0)), do_cca=False, n_components=3) multi_pca.fit(data[:2]) # Smoke test the transform and inverse_transform multi_pca.inverse_transform(multi_pca.transform(data[-2:])) # Smoke test to fit with no img assert_raises(TypeError, multi_pca.fit) multi_pca = MultiPCA(mask=mask_img, n_components=3) assert_raises_regex( ValueError, "Object has no components_ attribute. " "This is probably because fit has not been called", multi_pca.transform, data) # Test if raises an error when empty list of provided. assert_raises_regex( ValueError, 'Need one or more Niimg-like objects as input, ' 'an empty list was given.', multi_pca.fit, []) # Test passing masker arguments to estimator multi_pca = MultiPCA(target_affine=affine, target_shape=shape[:3], n_components=3, mask_strategy='background') multi_pca.fit(data)
def test_multi_pca_score(): shape = (6, 8, 10, 5) affine = np.eye(4) rng = np.random.RandomState(0) # Create a "multi-subject" dataset imgs = [] for i in range(8): this_img = rng.normal(size=shape) imgs.append(nibabel.Nifti1Image(this_img, affine)) mask_img = nibabel.Nifti1Image(np.ones(shape[:3], dtype=np.int8), affine) # Assert that score is between zero and one multi_pca = MultiPCA(mask=mask_img, random_state=0, memory_level=0, n_components=3) multi_pca.fit(imgs) s = multi_pca.score(imgs) assert_true(np.all(s <= 1)) assert_true(np.all(0 <= s)) # Assert that score does not fail with single subject data multi_pca = MultiPCA(mask=mask_img, random_state=0, memory_level=0, n_components=3) multi_pca.fit(imgs[0]) s = multi_pca.score(imgs[0]) assert_true(isinstance(s, float)) assert(0. <= s <= 1.) # Assert that score is one for n_components == n_sample # in single subject configuration multi_pca = MultiPCA(mask=mask_img, random_state=0, memory_level=0, n_components=5) multi_pca.fit(imgs[0]) s = multi_pca.score(imgs[0]) assert_almost_equal(s, 1., 1) # Per component score multi_pca = MultiPCA(mask=mask_img, random_state=0, memory_level=0, n_components=5) multi_pca.fit(imgs[0]) masker = NiftiMasker(mask_img).fit() s = multi_pca._raw_score(masker.transform(imgs[0]), per_component=True) assert_equal(s.shape, (5,)) assert_true(np.all(s <= 1)) assert_true(np.all(0 <= s))
def _raw_fit(self, data): """ Fits the parcellation method on this reduced data. Data are coming from a base decomposition estimator which computes the mask and reduces the dimensionality of images using randomized_svd. Parameters ---------- data : ndarray Shape (n_samples, n_features) Returns ------- labels_ : numpy.ndarray Labels to each cluster in the brain. connectivity_ : numpy.ndarray voxel-to-voxel connectivity matrix computed from a mask. Note that, this attribute is returned only for selected methods such as 'ward', 'complete', 'average'. """ valid_methods = self.VALID_METHODS if self.method is None: raise ValueError("Parcellation method is specified as None. " "Please select one of the method in " "{0}".format(valid_methods)) if self.method is not None and self.method not in valid_methods: raise ValueError("The method you have selected is not implemented " "'{0}'. Valid methods are in {1}".format( self.method, valid_methods)) # we delay importing Ward or AgglomerativeClustering and same # time import plotting module before that. # Because sklearn.cluster imports scipy hierarchy and hierarchy imports # matplotlib. So, we force import matplotlib first using our # plotting to avoid backend display error with matplotlib # happening in Travis try: from nilearn import plotting except: pass components = MultiPCA._raw_fit(self, data) mask_img_ = self.masker_.mask_img_ if self.verbose: print("[{0}] computing {1}".format(self.__class__.__name__, self.method)) if self.method == 'kmeans': from sklearn.cluster import MiniBatchKMeans kmeans = MiniBatchKMeans(n_clusters=self.n_parcels, init='k-means++', random_state=self.random_state, verbose=max(0, self.verbose - 1)) labels = self._cache(_estimator_fit, func_memory_level=1)(components.T, kmeans) else: mask_ = _safe_get_data(mask_img_).astype(np.bool) shape = mask_.shape connectivity = image.grid_to_graph(n_x=shape[0], n_y=shape[1], n_z=shape[2], mask=mask_) # from data.new_agglo import NewAgglomerativeClustering as AgglomerativeClustering from sklearn.cluster import AgglomerativeClustering agglomerative = AgglomerativeClustering(n_clusters=self.n_parcels, connectivity=connectivity, linkage=self.method, memory=self.memory, compute_full_tree=True) labels = self._cache(_estimator_fit, func_memory_level=1)(components.T, agglomerative) self.agglomerative = agglomerative self.connectivity_ = connectivity # Avoid 0 label labels = labels + 1 self.labels_img_ = self.masker_.inverse_transform(labels) return self # Avoid 0 label labels = labels + 1 self.labels_img_ = self.masker_.inverse_transform(labels) return self
def test_multi_pca(): # Smoke test the MultiPCA # XXX: this is mostly a smoke test shape = (6, 8, 10, 5) affine = np.eye(4) rng = np.random.RandomState(0) # Create a "multi-subject" dataset data = [] for i in range(8): this_data = rng.normal(size=shape) # Create fake activation to get non empty mask this_data[2:4, 2:4, 2:4, :] += 10 data.append(nibabel.Nifti1Image(this_data, affine)) mask_img = nibabel.Nifti1Image(np.ones(shape[:3], dtype=np.int8), affine) multi_pca = MultiPCA(mask=mask_img, n_components=3, random_state=0) # fit to the data and test for masker attributes multi_pca.fit(data) assert_true(multi_pca.mask_img_ == mask_img) assert_true(multi_pca.mask_img_ == multi_pca.masker_.mask_img_) # Test that the components are the same if we put twice the same data, and # that fit output is deterministic components1 = multi_pca.components_ components2 = multi_pca.fit(data).components_ components3 = multi_pca.fit(2 * data).components_ np.testing.assert_array_equal(components1, components2) np.testing.assert_array_almost_equal(components1, components3) # Smoke test fit with 'confounds' argument confounds = [np.arange(10).reshape(5, 2)] * 8 multi_pca.fit(data, confounds=confounds) # Smoke test that multi_pca also works with single subject data multi_pca.fit(data[0]) # Check that asking for too little components raises a ValueError multi_pca = MultiPCA() assert_raises(ValueError, multi_pca.fit, data[:2]) # Test fit on data with the use of a masker masker = MultiNiftiMasker() multi_pca = MultiPCA(mask=masker, n_components=3) multi_pca.fit(data) assert_true(multi_pca.mask_img_ == multi_pca.masker_.mask_img_) # Smoke test the use of a masker and without CCA multi_pca = MultiPCA(mask=MultiNiftiMasker(mask_args=dict(opening=0)), do_cca=False, n_components=3) multi_pca.fit(data[:2]) # Smoke test the transform and inverse_transform multi_pca.inverse_transform(multi_pca.transform(data[-2:])) # Smoke test to fit with no img assert_raises(TypeError, multi_pca.fit) multi_pca = MultiPCA(mask=mask_img, n_components=3) assert_raises_regex(ValueError, "Object has no components_ attribute. " "This is probably because fit has not been called", multi_pca.transform, data) # Test if raises an error when empty list of provided. assert_raises_regex(ValueError, 'Need one or more Niimg-like objects as input, ' 'an empty list was given.', multi_pca.fit, []) # Test passing masker arguments to estimator multi_pca = MultiPCA(target_affine=affine, target_shape=shape[:3], n_components=3, mask_strategy='background') multi_pca.fit(data)