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] Advertise nilearn's in built globbing #4334

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Changes from 1 commit
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
43 changes: 38 additions & 5 deletions examples/00_tutorials/plot_nilearn_101.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,49 @@
more_smooth_anat_img = image.smooth_img(smooth_anat_img, fwhm=3)
plotting.plot_img(more_smooth_anat_img)


# %%
# Globbing over multiple 3D volumes
# ---------------------------------
# Nilearn also supports reading multiple volumes at once,
# using glob-style patterns.
# For instance, we can smooth volumes from many subjects
# at once and get a 4D image as output.

# %%
# First let's fetch Haxby dataset for subject 1 and 2
from nilearn import datasets

haxby = datasets.fetch_haxby(subjects=[1, 2])

# %%
# Now we can find the anatomical images from both
# subjects using the `*` wildcard
from pathlib import Path

anats_all_subjects = (
Path(datasets.get_data_dirs()[0]) / "haxby2001" / "subj*" / "anat*"
)

# %%
# Now we can smooth all the anatomical images at once
anats_all_subjects_smooth = image.smooth_img(anats_all_subjects, fwhm=5)

# %%
# This is a 4D image containing one volume per subject
anats_all_subjects_smooth.shape
Remi-Gau marked this conversation as resolved.
Show resolved Hide resolved

# %%
# Saving results to a file
# -------------------------
#
# We can save any in-memory object as follows:
from pathlib import Path

output_dir = Path.cwd() / "results" / "plot_nilearn_101"
output_dir.mkdir(exist_ok=True, parents=True)
print(f"Output will be saved to: {output_dir}")
more_smooth_anat_img.to_filename(output_dir / "more_smooth_anat_img.nii.gz")
anats_all_subjects_smooth.to_filename(
output_dir / "anats_all_subjects_smooth.nii.gz"
)

# %%
# Finally, calling plotting.show() is necessary to display the figure
Expand All @@ -81,8 +113,9 @@
#
# ______
#
# To recap, all the nilearn tools can take data as filenames or in-memory
# objects, and return brain volumes as in-memory objects. These can be
# To recap, all the nilearn tools can take data as filenames or
# glob-style patterns or in-memory objects, and return brain
# volumes as in-memory objects. These can be
# passed on to other nilearn tools, or saved to disk.

# sphinx_gallery_dummy_images=1