def test_masker_attributes_with_fit(): # Test base module at sub-class data, mask_img, components, rng = _make_canica_test_data(n_subjects=3) # Passing mask_img canica = CanICA(n_components=3, mask=mask_img, random_state=0) canica.fit(data) assert canica.mask_img_ == mask_img assert canica.mask_img_ == canica.masker_.mask_img_ # Passing masker masker = MultiNiftiMasker(mask_img=mask_img) canica = CanICA(n_components=3, mask=masker, random_state=0) canica.fit(data) assert canica.mask_img_ == canica.masker_.mask_img_ canica = CanICA(mask=mask_img, n_components=3) with pytest.raises(ValueError, match="Object has no components_ attribute. " "This is probably because fit has not been called"): canica.transform(data) # Test if raises an error when empty list of provided. with pytest.raises(ValueError, match='Need one or more Niimg-like objects as input, ' 'an empty list was given.'): canica.fit([]) # Test passing masker arguments to estimator canica = CanICA(n_components=3, target_affine=np.eye(4), target_shape=(6, 8, 10), mask_strategy='background') canica.fit(data)
for ii in range(1, n_timecourses) ] print("Fitting data for subject %d..." % si) subj_canica = CanICA(n_components=n_components, smoothing_fwhm=6., memory="nilearn_cache", memory_level=5, max_iter=max_iter, threshold=3., verbose=10, random_state=0) subj_canica.fit(subj_img_list) print("Projecting subject data into computed components...") subj_loadings = subj_canica.transform(subj_img) subj_component_imgs = subj_canica.masker_.inverse_transform( subj_canica.components_) subj_loadings_imgs = [] for time_series, component_volume in zip(subj_loadings.T, subj_component_imgs.get_data()): tiled_img_data = np.tile(component_volume[..., np.newaxis], len(time_series)) loaded_time_series = tiled_img_data * time_series loading_img = nibabel.Nifti1Image(loaded_time_series, subj_img.get_affine()) subj_loadings_imgs.append(loading_img) loadings_imgs.append(subj_loadings_imgs) all_loadings_images = [ img for subj_images in loadings_imgs for img in subj_loadings_imgs