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

Copy input image header to the output by default #4397

Merged
merged 37 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
47028b9
copy header by default in crop_img
man-shu Apr 24, 2024
906a74b
mean_img copy header by default
man-shu Apr 24, 2024
2fcb3fa
threshold_img copy header by default
man-shu Apr 24, 2024
da04af3
copy header by default in binarize_img
man-shu Apr 24, 2024
82b13f3
copy header by default in resample_img and reorder_img
man-shu Apr 24, 2024
510ae96
Merge branch 'main' into fix/4393
man-shu Apr 24, 2024
a3a17ae
test crop_img header
man-shu Apr 24, 2024
7c9e364
test for equality of header fields
man-shu Apr 24, 2024
6993030
remove unecessary print statement
man-shu Apr 25, 2024
ad1277b
move testing function out of conftest
man-shu May 2, 2024
340b168
remove from conftest
man-shu May 2, 2024
59911d5
Merge branch 'main' into fix/4393
man-shu May 2, 2024
71c13f8
fix utility import
man-shu May 2, 2024
b350f58
fix import in resampling
man-shu May 2, 2024
9b4350f
add to changelog
man-shu May 2, 2024
7dc9be1
Merge branch 'main' into fix/4393
man-shu May 15, 2024
88fbc46
remove extra space
man-shu May 15, 2024
ed0b253
Add future warning about copying header by default
man-shu May 29, 2024
b0f7573
changelog entry
man-shu May 30, 2024
afccca1
Merge branch 'warning_copy_header' into fix/4393
man-shu May 30, 2024
59c34c0
add parameter to control header copying
man-shu May 30, 2024
d2f0044
update changelog
man-shu May 30, 2024
20a58f3
use a helper function to throw the warning
man-shu May 31, 2024
d7a487f
set copy header to True in tests
man-shu May 31, 2024
bdb9173
forgot updating
man-shu May 31, 2024
c6cb66c
CI blackify _utils.helpers.py
man-shu May 31, 2024
e0b24c4
forgot changing docstrings and defaults
man-shu May 31, 2024
743aecb
fix failing test
man-shu May 31, 2024
b7ae2e3
test for warning when copy_header=False
man-shu May 31, 2024
a44c21b
Merge branch 'main' into fix/4393
man-shu May 31, 2024
2869ad5
switch to copy_header=True in codebase (tests, examples)
man-shu Jun 3, 2024
ac31d33
Merge branch 'main' into fix/4393
man-shu Jun 3, 2024
9068cbd
roll back blackifying surface module
man-shu Jun 3, 2024
5fe7360
missed a updating in a few tests
man-shu Jun 3, 2024
9965c49
missed a couple of instances
man-shu Jun 3, 2024
6b9665e
update binarize_img api ref example
man-shu Jun 3, 2024
9132c6e
Merge branch 'main' into fix/4393
man-shu Jun 4, 2024
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: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ repos:
| examples/01_plotting/plot_surface_projection_strategies.py
| examples/04_glm_first_level/plot_localizer_surface_analysis.py
| examples/07_advanced/plot_surface_bids_analysis.py
| nilearn/_utils/helpers.py
| nilearn/plotting/html_surface.py
| nilearn/plotting/js_plotting_utils.py
| nilearn/plotting/surf_plotting.py
Expand Down
2 changes: 2 additions & 0 deletions doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ Changes
- :bdg-dark:`Code` Throw error in :func:`nilearn.glm.first_level.first_level_from_bids` if unknown ``kwargs`` are passed (:gh:`4414` by `Michelle Wang`_).

- :bdg-primary:`Doc` Refactor design matrix and contrast formula for the two-sample T-test example in :ref:`sphx_glr_auto_examples_05_glm_second_level_plot_second_level_two_sample_test.py` (:gh:`4407` by `Yichun Huang`_).

- :bdg-success:`API` Allow users to control copying header to the output in :func:`nilearn.image` functions and add future warning to copy headers by default in release 0.13.0 onwards (:gh:`4397` by `Himanshu Aggarwal`_).
3 changes: 2 additions & 1 deletion doc/visual_testing/reporter_visual_inspection_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def report_flm_fiac():
data = datasets.func.fetch_fiac_first_level()
fmri_img = [data["func1"], data["func2"]]

mean_img_ = mean_img(fmri_img[0])
mean_img_ = mean_img(fmri_img[0], copy_header=True)

design_files = [data["design_matrix1"], data["design_matrix2"]]
design_matrices = [pd.DataFrame(np.load(df)["X"]) for df in design_files]
Expand Down Expand Up @@ -249,6 +249,7 @@ def report_slm_oasis():
datasets.fetch_icbm152_brain_gm_mask(),
oasis_dataset.gray_matter_maps[0],
interpolation="nearest",
copy_header=True,
)

design_matrix = _make_design_matrix_slm_oasis(oasis_dataset, n_subjects)
Expand Down
2 changes: 1 addition & 1 deletion examples/00_tutorials/plot_decoding_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from nilearn import plotting
from nilearn.image import mean_img

plotting.view_img(mean_img(fmri_filename), threshold=None)
plotting.view_img(mean_img(fmri_filename, copy_header=True), threshold=None)
man-shu marked this conversation as resolved.
Show resolved Hide resolved

# %%
# Feature extraction: from :term:`fMRI` volumes to a data matrix
Expand Down
2 changes: 1 addition & 1 deletion examples/00_tutorials/plot_single_subject_single_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
from nilearn.image import concat_imgs, mean_img

fmri_img = concat_imgs(subject_data.func)
mean_img = mean_img(fmri_img)
mean_img = mean_img(fmri_img, copy_header=True)

# %%
# Specifying the experimental paradigm
Expand Down
2 changes: 1 addition & 1 deletion examples/01_plotting/plot_demo_more_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@

# Compute voxel-wise mean functional image across time dimension. Now we have
# functional image in 3D assigned in mean_haxby_img
mean_haxby_img = image.mean_img(haxby_func_filename)
mean_haxby_img = image.mean_img(haxby_func_filename, copy_header=True)

# %%
# Showing how to use `add_edges`
Expand Down
2 changes: 1 addition & 1 deletion examples/01_plotting/plot_demo_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@

# Compute the voxel_wise mean of functional images across time.
# Basically reducing the functional image from 4D to 3D
mean_haxby_img = image.mean_img(haxby_func_filename)
mean_haxby_img = image.mean_img(haxby_func_filename, copy_header=True)

# Visualizing mean image (3D)
plotting.plot_epi(mean_haxby_img, title="plot_epi")
Expand Down
2 changes: 1 addition & 1 deletion examples/01_plotting/plot_haxby_masks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from nilearn import image

func_filename = haxby_dataset.func[0]
mean_img = image.mean_img(func_filename)
mean_img = image.mean_img(func_filename, copy_header=True)

z_slice = -14

Expand Down
2 changes: 1 addition & 1 deletion examples/01_plotting/plot_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# Compute the mean EPI: we do the mean along the axis 3, which is time
func_filename = haxby_dataset.func[0]
mean_haxby = mean_img(func_filename)
mean_haxby = mean_img(func_filename, copy_header=True)

from nilearn.plotting import plot_epi, show

Expand Down
2 changes: 1 addition & 1 deletion examples/02_decoding/plot_haxby_different_estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
# Use the average :term:`EPI` as a background
from nilearn.image import mean_img

mean_epi_img = mean_img(func_filename)
mean_epi_img = mean_img(func_filename, copy_header=True)

for classifier_name in sorted(classifiers):
coef_img = classifiers_data[classifier_name]["map"]
Expand Down
2 changes: 1 addition & 1 deletion examples/02_decoding/plot_haxby_frem.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
# Compute the mean EPI to be used for the background of the plotting
from nilearn.image import mean_img

background_img = mean_img(func_filenames)
background_img = mean_img(func_filenames, copy_header=True)

# %%
# Fit FREM
Expand Down
2 changes: 1 addition & 1 deletion examples/02_decoding/plot_haxby_glm_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
from nilearn.image import mean_img
from nilearn.reporting import make_glm_report

mean_img_ = mean_img(func_filename)
mean_img_ = mean_img(func_filename, copy_header=True)
report = make_glm_report(
glm,
contrasts=conditions,
Expand Down
2 changes: 1 addition & 1 deletion examples/02_decoding/plot_haxby_searchlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
# Use the :term:`fMRI` mean image as a surrogate of anatomical data
from nilearn import image

mean_fmri = image.mean_img(fmri_img)
mean_fmri = image.mean_img(fmri_img, copy_header=True)

from nilearn.plotting import plot_img, plot_stat_map, show

Expand Down
2 changes: 1 addition & 1 deletion examples/02_decoding/plot_miyawaki_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
score_map_img = masker.inverse_transform(cut_score)

thresholded_score_map_img = threshold_img(
score_map_img, threshold=1e-6, copy=False
score_map_img, threshold=1e-6, copy=False, copy_header=True
)


Expand Down
2 changes: 1 addition & 1 deletion examples/03_connectivity/plot_data_driven_parcellations.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@

# Compute mean over time on the functional image to use the mean
# image for compressed representation comparisons
mean_func_img = mean_img(dataset.func[0])
mean_func_img = mean_img(dataset.func[0], copy_header=True)

# Compute common vmin and vmax
vmin = np.min(get_data(mean_func_img))
Expand Down
2 changes: 1 addition & 1 deletion examples/04_glm_first_level/plot_first_level_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def plot_contrast(first_level_model):
from nilearn.image import resample_to_img

resampled_icbm_mask = resample_to_img(
icbm_mask, data_mask, interpolation="nearest"
icbm_mask, data_mask, interpolation="nearest", copy_header=True
)

# %%
Expand Down
2 changes: 1 addition & 1 deletion examples/04_glm_first_level/plot_predictions_residuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
fmri_img = image.concat_imgs(subject_data.func)

# Make an average
mean_img = image.mean_img(fmri_img)
mean_img = image.mean_img(fmri_img, copy_header=True)
mask = masking.compute_epi_mask(mean_img)

# Clean and smooth data
Expand Down
4 changes: 2 additions & 2 deletions examples/04_glm_first_level/plot_spm_multimodal_faces.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
]
affine, shape = fmri_img[0].affine, fmri_img[0].shape
print("Resampling the second image (this takes time)...")
fmri_img[1] = resample_img(fmri_img[1], affine, shape[:3])
fmri_img[1] = resample_img(fmri_img[1], affine, shape[:3], copy_header=True)

# %%
# Let's create mean image for display purposes.
mean_image = mean_img(fmri_img)
mean_image = mean_img(fmri_img, copy_header=True)

# %%
# Make the design matrices.
Expand Down
2 changes: 1 addition & 1 deletion examples/04_glm_first_level/plot_two_runs_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
# Create a mean image for plotting purpose.
from nilearn.image import mean_img

mean_img_ = mean_img(fmri_imgs[0])
mean_img_ = mean_img(fmri_imgs[0], copy_header=True)

# %%
# The design matrices were pre-computed,
Expand Down
1 change: 1 addition & 0 deletions examples/05_glm_second_level/plot_oasis.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
gm_mask,
gray_matter_map_filenames[0],
interpolation="nearest",
copy_header=True,
)

# %%
Expand Down
1 change: 1 addition & 0 deletions examples/05_glm_second_level/plot_thresholding.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
threshold=3.29,
cluster_threshold=10,
two_sided=True,
copy_header=True,
)

# %%
Expand Down
11 changes: 8 additions & 3 deletions examples/06_manipulating_images/plot_affine_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,26 @@
from nilearn.image import resample_img

img_in_mm_space = resample_img(
img, target_affine=np.eye(4), target_shape=(512, 512, 1)
img, target_affine=np.eye(4), target_shape=(512, 512, 1), copy_header=True
)

target_affine_3x3 = np.eye(3) * 2
target_affine_4x4 = np.eye(4) * 2
target_affine_4x4[3, 3] = 1.0
img_3d_affine = resample_img(img, target_affine=target_affine_3x3)
img_4d_affine = resample_img(img, target_affine=target_affine_4x4)
img_3d_affine = resample_img(
img, target_affine=target_affine_3x3, copy_header=True
)
img_4d_affine = resample_img(
img, target_affine=target_affine_4x4, copy_header=True
)
target_affine_mm_space_offset_changed = np.eye(4)
target_affine_mm_space_offset_changed[:3, 3] = img_3d_affine.affine[:3, 3]

img_3d_affine_in_mm_space = resample_img(
img_3d_affine,
target_affine=target_affine_mm_space_offset_changed,
target_shape=(np.array(img_3d_affine.shape) * 2).astype(int),
copy_header=True,
)

img_4d_affine_in_mm_space = resample_img(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
# Two types of strategies can be used from this threshold function
# Type 1: strategy used will be based on scoreatpercentile
threshold_percentile_img = threshold_img(
tmap_filename, threshold="97%", copy=False
tmap_filename, threshold="97%", copy=False, copy_header=True
)


# Type 2: threshold strategy used will be based on image intensity
# Here, threshold value should be within the limits i.e. less than max value.
threshold_value_img = threshold_img(tmap_filename, threshold=3.0, copy=False)
threshold_value_img = threshold_img(
tmap_filename, threshold=3.0, copy=False, copy_header=True
)

# %%
# Visualization
Expand Down
4 changes: 2 additions & 2 deletions examples/06_manipulating_images/plot_mask_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
)

miyawaki_filename = miyawaki_dataset.func[0]
miyawaki_mean_img = image.mean_img(miyawaki_filename)
miyawaki_mean_img = image.mean_img(miyawaki_filename, copy_header=True)
plot_epi(miyawaki_mean_img, title="Mean EPI image")
# %%
# A NiftiMasker with the default strategy
Expand Down Expand Up @@ -77,7 +77,7 @@
epi_img = index_img(epi_filename, slice(0, 100))

# To display the background
mean_img = image.mean_img(epi_img)
mean_img = image.mean_img(epi_img, copy_header=True)
plot_epi(mean_img, title="Mean EPI image")

# %%
Expand Down
4 changes: 2 additions & 2 deletions examples/06_manipulating_images/plot_nifti_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from nilearn.plotting import plot_roi

# calculate mean image for the background
mean_func_img = mean_img(func_filename)
mean_func_img = mean_img(func_filename, copy_header=True)

plot_roi(mask_img, mean_func_img, display_mode="y", cut_coords=4, title="Mask")

Expand Down Expand Up @@ -80,7 +80,7 @@
from nilearn.plotting import plot_stat_map, show

# calculate mean image for the background
mean_func_img = mean_img(func_filename)
mean_func_img = mean_img(func_filename, copy_header=True)

plot_stat_map(
index_img(components, 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# to the :term:`MNI` template image.
from nilearn.image import resample_to_img

resampled_stat_img = resample_to_img(stat_img, template)
resampled_stat_img = resample_to_img(stat_img, template, copy_header=True)

# %%
# Let's check the shape and affine have been correctly updated.
Expand Down
2 changes: 1 addition & 1 deletion examples/06_manipulating_images/plot_roi_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@

# First, compute the voxel-wise mean of smooth EPI image
# (first argument) using image processing module `image`
mean_img = image.mean_img(fmri_img)
mean_img = image.mean_img(fmri_img, copy_header=True)
# Second, we visualize the mean image with coordinates positioned manually
plot_epi(mean_img, title="Smoothed mean EPI", cut_coords=cut_coords)

Expand Down
2 changes: 1 addition & 1 deletion examples/06_manipulating_images/plot_smooth_mean_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
first_epi_file = data.func[0]

# First compute the mean image, from the 4D series of image
mean_func = image.mean_img(first_epi_file)
mean_func = image.mean_img(first_epi_file, copy_header=True)

# %%
# Then we smooth, with a varying amount of smoothing, from none to 20mm
Expand Down
2 changes: 1 addition & 1 deletion examples/07_advanced/plot_haxby_mass_univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
from nilearn.image import get_data

# Use the fMRI mean image as a surrogate of anatomical data
mean_fmri_img = image.mean_img(func_filename)
mean_fmri_img = image.mean_img(func_filename, copy_header=True)

threshold = -np.log10(0.1) # 10% corrected

Expand Down
2 changes: 1 addition & 1 deletion examples/07_advanced/plot_ica_resting_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
# Show some interesting components

# Use the mean as a background
mean_img = image.mean_img(func_filename)
mean_img = image.mean_img(func_filename, copy_header=True)

plot_stat_map(image.index_img(component_img, 0), mean_img)

Expand Down