Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC] display all masker reports in the doc #4295

Merged
merged 11 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/codespell_ignore_words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ mapp
nin
serie
fwe
fo
coo
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ build
dist/
nilearn/_version.py

# notebooks
.ipynb_checkpoints

# virtual environment
env/
venv/
Expand Down
2 changes: 1 addition & 1 deletion doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Enhancements
Changes
-------

- :bdg-primary:`Doc` Render several examples of GLM reports as part of the documentation (:gh:`4267` by `Rémi Gau`_).
- :bdg-primary:`Doc` Render examples of GLM and masker reports as part of the documentation (:gh:`4267` and :gh:`4295` by `Rémi Gau`_).
- :bdg-dark:`Code` Improve colorbar size and labels in mosaic display (:gh:`4284` by `Rémi Gau`_).
- :bdg-primary:`Doc` Render the description of the templates, atlases and datasets of the :mod:`nilearn.datasets` as part of the documentation (:gh:`4232` by `Rémi Gau`_).
- :bdg-dark:`Code` Change the colormap to ``gray`` for the background image in the :class:`nilearn.maskers.NiftiSpheresMasker` (:gh:`4269` by `Rémi Gau`_).
Expand Down
42 changes: 41 additions & 1 deletion doc/modules/generated_reports/masker_reports_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,50 @@
Examples masker reports
=======================

Nifti masker
------------

.. raw:: html
:file: nifti_masker.html
Comment on lines +13 to +14
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mention for each where they were taken from


.. raw:: html
:file: multi_nifti_masker.html

.. raw:: html
:file: multi_nifti_masker_fitted.html


Nifti labels masker
-------------------

.. raw:: html
:file: nifti_labels_masker_fitted.html

.. raw:: html
:file: nifti_labels_masker_atlas.html

.. raw:: html
:file: multi_nifti_labels_masker_atlas.html

.. raw:: html
:file: multi_nifti_labels_masker_fitted.html

Nifti maps masker
-----------------

.. raw:: html
:file: nifti_maps_masker.html

.. raw:: html
:file: multi_nifti_maps_masker_atlas.html

.. raw:: html
:file: multi_nifti_maps_masker_fitted.html

Nifti sphere masker
-------------------

Adapted from :ref:`sphx_glr_auto_examples_04_glm_first_level_plot_adhd_dmn.py`

.. raw:: html
:file: flm_adhd_dmn_sphere_masker.html
:file: nifti_sphere_masker.html
198 changes: 180 additions & 18 deletions doc/visual_testing/reporter_visual_inspection_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@
from nilearn.glm.second_level import SecondLevelModel
from nilearn.image import mean_img, resample_to_img
from nilearn.interfaces.fsl import get_design_from_fslmat
from nilearn.maskers import NiftiSpheresMasker
from nilearn.maskers import (
MultiNiftiLabelsMasker,
MultiNiftiMapsMasker,
MultiNiftiMasker,
NiftiLabelsMasker,
NiftiMapsMasker,
NiftiMasker,
NiftiSpheresMasker,
)
from nilearn.reporting import make_glm_report

REPORTS_DIR = Path(__file__).parent.parent / "modules" / "generated_reports"
Expand All @@ -35,9 +43,8 @@ def report_flm_adhd_dmn():
t_r = 2.0
slice_time_ref = 0.0
n_scans = 176
pcc_coords = (0, -53, 26)

adhd_dataset = datasets.fetch_adhd(n_subjects=1)
pcc_coords = (0, -53, 26)

seed_masker = NiftiSpheresMasker(
[pcc_coords],
Expand All @@ -48,13 +55,13 @@ def report_flm_adhd_dmn():
high_pass=0.01,
t_r=t_r,
memory="nilearn_cache",
memory_level=1,
verbose=0,
)

adhd_dataset = datasets.fetch_adhd(n_subjects=1)
seed_time_series = seed_masker.fit_transform(adhd_dataset.func[0])
report = seed_masker.generate_report()
report.save_as_html(REPORTS_DIR / "flm_adhd_dmn_sphere_masker.html")

masker_report = seed_masker.generate_report()
masker_report.save_as_html(REPORTS_DIR / "nifti_sphere_masker.html")

frametimes = np.linspace(0, (n_scans - 1) * t_r, n_scans)

Expand All @@ -72,7 +79,7 @@ def report_flm_adhd_dmn():
run_imgs=adhd_dataset.func[0], design_matrices=design_matrix
)

report = make_glm_report(
glm_report = make_glm_report(
first_level_model,
contrasts=contrasts,
title="ADHD DMN Report",
Expand All @@ -83,9 +90,9 @@ def report_flm_adhd_dmn():
plot_type="glass",
report_dims=(1200, "a"),
)
glm_report.save_as_html(REPORTS_DIR / "flm_adhd_dmn.html")

report.save_as_html(REPORTS_DIR / "flm_adhd_dmn.html")
report.get_iframe()
return masker_report, glm_report


# %%
Expand Down Expand Up @@ -181,7 +188,7 @@ def report_flm_bids_features():
)

report.save_as_html(REPORTS_DIR / "flm_bids_features.html")
report.get_iframe()
return report


# %%
Expand Down Expand Up @@ -216,9 +223,8 @@ def report_flm_fiac():
bg_img=mean_img_,
height_control="fdr",
)

report.save_as_html(REPORTS_DIR / "flm_fiac.html")
report.get_iframe()
return report


# %%
Expand Down Expand Up @@ -269,16 +275,173 @@ def report_slm_oasis():
height_control=None,
plot_type="glass",
)

report.save_as_html(REPORTS_DIR / "slm_oasis.html")
report.get_iframe()
return report


# %%
# Adapted from examples/03_connectivity/plot_probabilistic_atlas_extraction.py
def report_nifti_maps_masker():

atlas = datasets.fetch_atlas_msdl()
atlas_filename = atlas["maps"]

data = datasets.fetch_development_fmri(n_subjects=1)

masker = NiftiMapsMasker(
maps_img=atlas_filename,
standardize="zscore_sample",
standardize_confounds="zscore_sample",
memory="nilearn_cache",
cmap="grey",
)
masker.fit(data.func[0])

report = masker.generate_report(displayed_maps=[2, 6, 7, 16, 21])
report.save_as_html(REPORTS_DIR / "nifti_maps_masker.html")
return report


# %%
# Adapted from examples/06_manipulating_images/plot_nifti_labels_simple.py
def report_nifti_labels_masker():

atlas = datasets.fetch_atlas_schaefer_2018()

atlas.labels = np.insert(atlas.labels, 0, "Background")

masker = NiftiLabelsMasker(
atlas.maps,
labels=atlas.labels,
standardize="zscore_sample",
)
masker.fit()
report = masker.generate_report()
report.save_as_html(REPORTS_DIR / "nifti_labels_masker_atlas.html")

data = datasets.fetch_development_fmri(n_subjects=1)
masker.fit(data.func[0])
report = masker.generate_report()
report.save_as_html(REPORTS_DIR / "nifti_labels_masker_fitted.html")
return report


# %%
# Adapted from examples/06_manipulating_images/plot_nifti_simple.py
def report_nifti_masker():

masker = NiftiMasker(
standardize="zscore_sample",
mask_strategy="epi",
memory="nilearn_cache",
memory_level=2,
smoothing_fwhm=8,
cmap="grey",
)

data = datasets.fetch_development_fmri(n_subjects=1)
masker.fit(data.func[0])
report = masker.generate_report()
report.save_as_html(REPORTS_DIR / "nifti_masker.html")
return report


def report_multi_nifti_masker():

data = datasets.fetch_miyawaki2008()

masker = MultiNiftiMasker(
mask_img=data.mask,
detrend=True,
standardize="zscore_sample",
n_jobs=2,
cmap="grey",
)
masker.fit()
empty_report = masker.generate_report()
empty_report.save_as_html(REPORTS_DIR / "multi_nifti_masker.html")

fmri_random_runs_filenames = data.func[12:]
masker.fit(fmri_random_runs_filenames)
report = masker.generate_report()
report.save_as_html(REPORTS_DIR / "multi_nifti_masker_fitted.html")
return empty_report, report


# %%
# Adapted from examples/03_connectivity/plot_atlas_comparison.py
def report_multi_nifti_labels_masker():

yeo = datasets.fetch_atlas_yeo_2011()

data = datasets.fetch_development_fmri(n_subjects=2)

masker = MultiNiftiLabelsMasker(
labels_img=yeo["thick_17"],
standardize="zscore_sample",
standardize_confounds="zscore_sample",
memory="nilearn_cache",
n_jobs=2,
)

masker.fit()
report = masker.generate_report()
report.save_as_html(REPORTS_DIR / "multi_nifti_labels_masker_atlas.html")

_ = masker.fit_transform(data.func, confounds=data.confounds)
report = masker.generate_report()
report.save_as_html(REPORTS_DIR / "multi_nifti_labels_masker_fitted.html")
return report


# %%
# Adapted from examples/03_connectivity/plot_atlas_comparison.py
def report_multi_nifti_maps_masker():

difumo = datasets.fetch_atlas_difumo(
dimension=64, resolution_mm=2, legacy_format=False
)

data = datasets.fetch_development_fmri(n_subjects=2)

masker = MultiNiftiMapsMasker(
maps_img=difumo.maps,
standardize="zscore_sample",
standardize_confounds="zscore_sample",
memory="nilearn_cache",
n_jobs=2,
)

masker.fit()
empty_report = masker.generate_report()
empty_report.save_as_html(
REPORTS_DIR / "multi_nifti_maps_masker_atlas.html"
)

_ = masker.fit_transform(data.func, confounds=data.confounds)
report = masker.generate_report()
report.save_as_html(REPORTS_DIR / "multi_nifti_maps_masker_fitted.html")

return empty_report, report


# %%
if __name__ == "__main__":

print("\nGenerating GLM reports templates\n")
print("\nGenerating masker reports templates\n")
t0 = time.time()

report_nifti_masker()
report_nifti_maps_masker()
report_nifti_labels_masker()
report_multi_nifti_masker()
report_multi_nifti_labels_masker()
report_multi_nifti_maps_masker()

t1 = time.time()
print(f"\nTook: {(t1 - t0)} seconds\n")

print("\nGenerating GLM reports templates\n")
t0 = time.time()

report_flm_adhd_dmn()
Expand All @@ -287,5 +450,4 @@ def report_slm_oasis():
report_slm_oasis()

t1 = time.time()

print(f"\nRun took: {(t1 - t0)} seconds\n")
print(f"\nTook: {(t1 - t0)} seconds\n")