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)
def _gen_report(): """ Generate an empty HTMLReport for testing """ data = np.zeros((9, 9, 9)) data[3:-3, 3:-3, 3:-3] = 10 data_img_3d = Nifti1Image(data, np.eye(4)) # turn off reporting mask = NiftiMasker() mask.fit(data_img_3d) report = mask.generate_report() return report
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
########################################################################### # 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
the 4D data and compress rearrange them into 2D. For example, if we have a group of voxels we think are in the Amygdala, the masker will extract them from the time series of 3D images (4D series) and arrange them to be a data matrix with the voxel values as rows and the columns as time points, for example.''' mask_filename = haxby_data.mask_vt[0] plotting.plot_roi(mask_filename, bg_img=haxby_data.anat[0], cmap='Paired') '''What we have done here is plotted the voxels that we have applied the mask to agains the backdrop of the actual brain volume''' from nilearn.input_data import NiftiMasker masker = NiftiMasker(mask_img=mask_filename, standardize=True) fmri_masked = masker.fit_transform(fmri_filename) masker.generate_report() print(fmri_masked) print(fmri_masked.shape) '''as you can see, the object fmri_masked is 1452x464, which is the number of time points by the number of voxels.''' '''Now why don't we do a little bit of visualization. What does the time series for some random voxels look like?''' plt.plot(fmri_masked[200:400, 45:52]) plt.title('Time Series for Some Random Masked Voxels') plt.xlabel('Time') plt.ylabel('BOLD signal') plt.tight_layout() '''Pretty cool, huh? Now we're doing science!''' '''Now let's circle back around to that behavioral data. Now that we have arranged the brain data in a 2D, this will make it play nicer with the behavioral