def test_overlaid_report(data_img_3d): pytest.importorskip('matplotlib') masker = NiftiMasker(target_affine=np.eye(3) * 8) html = masker.generate_report() assert "Please `fit` the object" in str(html) masker.fit(data_img_3d) html = masker.generate_report() assert '<div class="overlay">' in str(html)
def test_4d_reports(mask): # Dummy 4D data data = np.zeros((10, 10, 10, 3), dtype=int) data[..., 0] = 1 data[..., 1] = 2 data[..., 2] = 3 data_img_4d = Nifti1Image(data, np.eye(4)) # test .fit method masker = NiftiMasker(mask_strategy='epi') masker.fit(data_img_4d) assert masker._report_content['warning_message'] is None html = masker.generate_report() _check_html(html) # test .fit_transform method masker = NiftiMasker(mask_img=mask, standardize=True) masker.fit_transform(data_img_4d) assert masker._report_content['warning_message'] is None html = masker.generate_report() _check_html(html)
########################################################################### # Visualize the mask using the plot_roi method from nilearn.plotting import plot_roi from nilearn.image.image import mean_img # calculate mean image for the background mean_func_img = mean_img(func_filename) plot_roi(mask_img, mean_func_img, display_mode='y', cut_coords=4, title="Mask") ########################################################################### # Visualize the mask using the 'generate_report' method # This report can be displayed in a Jupyter Notebook, # opened in-browser using the .open_in_browser() method, # or saved to a file using the .save_as_html(output_filepath) method. report = nifti_masker.generate_report() report ########################################################################### # Preprocess data with the NiftiMasker nifti_masker.fit(func_filename) fmri_masked = nifti_masker.transform(func_filename) # fmri_masked is now a 2D matrix, (n_voxels x n_time_points) ########################################################################### # Run an algorithm from sklearn.decomposition import FastICA n_components = 10 ica = FastICA(n_components=n_components, random_state=42) components_masked = ica.fit_transform(fmri_masked.T).T
miyawaki_filename = miyawaki_dataset.func[0] miyawaki_mean_img = image.mean_img(miyawaki_filename) plot_epi(miyawaki_mean_img, title='Mean EPI image') ############################################################################### # A NiftiMasker with the default strategy masker = NiftiMasker() masker.fit(miyawaki_filename) # Plot the generated mask using the mask_img_ attribute plot_roi(masker.mask_img_, miyawaki_mean_img, title="Mask from already masked data") ############################################################################### # Plot the generated mask using the .generate_report method report = masker.generate_report() report ############################################################################### # Computing a mask from raw EPI data ############################################################################### # # From raw EPI data, there is no uniform background, and a different # strategy is necessary # Load movie watching based brain development fmri dataset dataset = datasets.fetch_development_fmri(n_subjects=1) epi_filename = dataset.func[0] # Restrict to 100 frames to speed up computation from nilearn.image import index_img