Skip to content

Commit

Permalink
Dev docs nilearn/nilearn@66520a9 : [MAINT] reduce occurence of resamp…
Browse files Browse the repository at this point in the history
…ling related warnings (#4531)

* reduce occurence of force_resample warnings

* reduce cast warnings in tests

* apply to resample_to_img

* fix

* missed one
  • Loading branch information
actions-user committed Sep 11, 2024
1 parent 2722286 commit 7cf6a93
Show file tree
Hide file tree
Showing 762 changed files with 4,484 additions and 4,994 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
gray_matter_map_filenames[0],
interpolation="nearest",
copy_header=True,
force_resample=True,
)

# %%
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
# to the :term:`MNI` template image.
from nilearn.image import resample_to_img

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

# %%
# Let's check the shape and affine have been correctly updated.
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,22 @@

grid = np.mgrid[0:192, 0:128]
circle = (
np.sum((grid - np.array([32, 32])[:, np.newaxis, np.newaxis]) ** 2, axis=0)
np.sum(
(grid - np.array([32.0, 32.0])[:, np.newaxis, np.newaxis]) ** 2, axis=0
)
< 256
)
diamond = (
np.sum(
np.abs(grid - np.array([128, 80])[:, np.newaxis, np.newaxis]), axis=0
np.abs(grid - np.array([128.0, 80.0])[:, np.newaxis, np.newaxis]),
axis=0,
)
< 16
)
rectangle = (
np.max(
np.abs(grid - np.array([64, 96])[:, np.newaxis, np.newaxis]), axis=0
np.abs(grid - np.array([64.0, 96.0])[:, np.newaxis, np.newaxis]),
axis=0,
)
< 16
)
Expand All @@ -90,25 +94,29 @@
from nibabel import Nifti1Image

img = Nifti1Image(
image[:, :, np.newaxis].astype("int32"), affine=source_affine
image[:, :, np.newaxis].astype("float32"), affine=source_affine
)

# %%
# Now resample the image
from nilearn.image import resample_img

img_in_mm_space = resample_img(
img, target_affine=np.eye(4), target_shape=(512, 512, 1), copy_header=True
img,
target_affine=np.eye(4),
target_shape=(512, 512, 1),
copy_header=True,
force_resample=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, copy_header=True
img, target_affine=target_affine_3x3, copy_header=True, force_resample=True
)
img_4d_affine = resample_img(
img, target_affine=target_affine_4x4, copy_header=True
img, target_affine=target_affine_4x4, copy_header=True, force_resample=True
)
target_affine_mm_space_offset_changed = np.eye(4)
target_affine_mm_space_offset_changed[:3, 3] = img_3d_affine.affine[:3, 3]
Expand All @@ -118,12 +126,14 @@
target_affine=target_affine_mm_space_offset_changed,
target_shape=(np.array(img_3d_affine.shape) * 2).astype(int),
copy_header=True,
force_resample=True,
)

img_4d_affine_in_mm_space = resample_img(
img_4d_affine,
target_affine=np.eye(4),
target_shape=(np.array(img_4d_affine.shape) * 2).astype(int),
force_resample=True,
copy_header=True,
)

Expand Down
Binary file modified dev/_downloads/2ce0f24d145edf0cb7091fe8dae3bd07/plot_carpet.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified dev/_downloads/44cb3a7d606fae23fb2b04d989cc6947/plot_hrf.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified dev/_downloads/4f6c009516d6956f180d8a05a141b383/plot_oasis.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"outputs": [],
"source": [
"grid = np.mgrid[0:192, 0:128]\ncircle = (\n np.sum((grid - np.array([32, 32])[:, np.newaxis, np.newaxis]) ** 2, axis=0)\n < 256\n)\ndiamond = (\n np.sum(\n np.abs(grid - np.array([128, 80])[:, np.newaxis, np.newaxis]), axis=0\n )\n < 16\n)\nrectangle = (\n np.max(\n np.abs(grid - np.array([64, 96])[:, np.newaxis, np.newaxis]), axis=0\n )\n < 16\n)\n\nimage = np.zeros_like(circle)\nimage[16:160, 16:120] = 1.0\nimage = image + 2 * circle + 3 * rectangle + 4 * diamond + 1\n\nvmax = image.max()\n\nsource_affine = np.eye(4)\n# Use canonical vectors for affine\n# Give the affine an offset\nsource_affine[:2, 3] = np.array([96, 64])\n\n# Rotate it slightly\nangle = np.pi / 180 * 15\nrotation_matrix = np.array(\n [[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]\n)\nsource_affine[:2, :2] = rotation_matrix * 2.0 # 2.0mm voxel size\n\n# We need to turn this data into a nibabel image\nfrom nibabel import Nifti1Image\n\nimg = Nifti1Image(\n image[:, :, np.newaxis].astype(\"int32\"), affine=source_affine\n)"
"grid = np.mgrid[0:192, 0:128]\ncircle = (\n np.sum(\n (grid - np.array([32.0, 32.0])[:, np.newaxis, np.newaxis]) ** 2, axis=0\n )\n < 256\n)\ndiamond = (\n np.sum(\n np.abs(grid - np.array([128.0, 80.0])[:, np.newaxis, np.newaxis]),\n axis=0,\n )\n < 16\n)\nrectangle = (\n np.max(\n np.abs(grid - np.array([64.0, 96.0])[:, np.newaxis, np.newaxis]),\n axis=0,\n )\n < 16\n)\n\nimage = np.zeros_like(circle)\nimage[16:160, 16:120] = 1.0\nimage = image + 2 * circle + 3 * rectangle + 4 * diamond + 1\n\nvmax = image.max()\n\nsource_affine = np.eye(4)\n# Use canonical vectors for affine\n# Give the affine an offset\nsource_affine[:2, 3] = np.array([96, 64])\n\n# Rotate it slightly\nangle = np.pi / 180 * 15\nrotation_matrix = np.array(\n [[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]\n)\nsource_affine[:2, :2] = rotation_matrix * 2.0 # 2.0mm voxel size\n\n# We need to turn this data into a nibabel image\nfrom nibabel import Nifti1Image\n\nimg = Nifti1Image(\n image[:, :, np.newaxis].astype(\"float32\"), affine=source_affine\n)"
]
},
{
Expand All @@ -51,7 +51,7 @@
},
"outputs": [],
"source": [
"from nilearn.image import resample_img\n\nimg_in_mm_space = resample_img(\n img, target_affine=np.eye(4), target_shape=(512, 512, 1), copy_header=True\n)\n\ntarget_affine_3x3 = np.eye(3) * 2\ntarget_affine_4x4 = np.eye(4) * 2\ntarget_affine_4x4[3, 3] = 1.0\nimg_3d_affine = resample_img(\n img, target_affine=target_affine_3x3, copy_header=True\n)\nimg_4d_affine = resample_img(\n img, target_affine=target_affine_4x4, copy_header=True\n)\ntarget_affine_mm_space_offset_changed = np.eye(4)\ntarget_affine_mm_space_offset_changed[:3, 3] = img_3d_affine.affine[:3, 3]\n\nimg_3d_affine_in_mm_space = resample_img(\n img_3d_affine,\n target_affine=target_affine_mm_space_offset_changed,\n target_shape=(np.array(img_3d_affine.shape) * 2).astype(int),\n copy_header=True,\n)\n\nimg_4d_affine_in_mm_space = resample_img(\n img_4d_affine,\n target_affine=np.eye(4),\n target_shape=(np.array(img_4d_affine.shape) * 2).astype(int),\n copy_header=True,\n)"
"from nilearn.image import resample_img\n\nimg_in_mm_space = resample_img(\n img,\n target_affine=np.eye(4),\n target_shape=(512, 512, 1),\n copy_header=True,\n force_resample=True,\n)\n\ntarget_affine_3x3 = np.eye(3) * 2\ntarget_affine_4x4 = np.eye(4) * 2\ntarget_affine_4x4[3, 3] = 1.0\nimg_3d_affine = resample_img(\n img, target_affine=target_affine_3x3, copy_header=True, force_resample=True\n)\nimg_4d_affine = resample_img(\n img, target_affine=target_affine_4x4, copy_header=True, force_resample=True\n)\ntarget_affine_mm_space_offset_changed = np.eye(4)\ntarget_affine_mm_space_offset_changed[:3, 3] = img_3d_affine.affine[:3, 3]\n\nimg_3d_affine_in_mm_space = resample_img(\n img_3d_affine,\n target_affine=target_affine_mm_space_offset_changed,\n target_shape=(np.array(img_3d_affine.shape) * 2).astype(int),\n copy_header=True,\n force_resample=True,\n)\n\nimg_4d_affine_in_mm_space = resample_img(\n img_4d_affine,\n target_affine=np.eye(4),\n target_shape=(np.array(img_4d_affine.shape) * 2).astype(int),\n force_resample=True,\n copy_header=True,\n)"
]
},
{
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@
},
"outputs": [],
"source": [
"from nilearn.image import resample_to_img\n\nresampled_icbm_mask = resample_to_img(\n icbm_mask, data_mask, interpolation=\"nearest\", copy_header=True\n)"
"from nilearn.image import resample_to_img\n\nresampled_icbm_mask = resample_to_img(\n icbm_mask,\n data_mask,\n interpolation=\"nearest\",\n copy_header=True,\n force_resample=True,\n)"
]
},
{
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
]
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], copy_header=True)
fmri_img[1] = resample_img(
fmri_img[1], affine, shape[:3], copy_header=True, force_resample=True
)

# %%
# Let's create mean image for display purposes.
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,11 @@ 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", copy_header=True
icbm_mask,
data_mask,
interpolation="nearest",
copy_header=True,
force_resample=True,
)

# %%
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"outputs": [],
"source": [
"import warnings\n\nfrom nilearn.image import concat_imgs, mean_img, resample_img\n\n# Avoid getting too many warnings due to resampling\nwith warnings.catch_warnings():\n warnings.simplefilter(\"ignore\")\n fmri_img = [\n concat_imgs(subject_data.func1, auto_resample=True),\n concat_imgs(subject_data.func2, auto_resample=True),\n ]\naffine, shape = fmri_img[0].affine, fmri_img[0].shape\nprint(\"Resampling the second image (this takes time)...\")\nfmri_img[1] = resample_img(fmri_img[1], affine, shape[:3], copy_header=True)"
"import warnings\n\nfrom nilearn.image import concat_imgs, mean_img, resample_img\n\n# Avoid getting too many warnings due to resampling\nwith warnings.catch_warnings():\n warnings.simplefilter(\"ignore\")\n fmri_img = [\n concat_imgs(subject_data.func1, auto_resample=True),\n concat_imgs(subject_data.func2, auto_resample=True),\n ]\naffine, shape = fmri_img[0].affine, fmri_img[0].shape\nprint(\"Resampling the second image (this takes time)...\")\nfmri_img[1] = resample_img(\n fmri_img[1], affine, shape[:3], copy_header=True, force_resample=True\n)"
]
},
{
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified dev/_downloads/a61865436a8c739855dd39d7e9ff3f6e/plot_overlay.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
},
"outputs": [],
"source": [
"from nilearn.image import resample_to_img\n\nmask_img = resample_to_img(\n gm_mask,\n gray_matter_map_filenames[0],\n interpolation=\"nearest\",\n copy_header=True,\n)"
"from nilearn.image import resample_to_img\n\nmask_img = resample_to_img(\n gm_mask,\n gray_matter_map_filenames[0],\n interpolation=\"nearest\",\n copy_header=True,\n force_resample=True,\n)"
]
},
{
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified dev/_downloads/ec9ec7cc0c1ea84207d4b1895aa51c35/plot_atlas.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"outputs": [],
"source": [
"from nilearn.image import resample_to_img\n\nresampled_stat_img = resample_to_img(stat_img, template, copy_header=True)"
"from nilearn.image import resample_to_img\n\nresampled_stat_img = resample_to_img(\n stat_img, template, copy_header=True, force_resample=True\n)"
]
},
{
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified dev/_images/sphx_glr_plot_advanced_decoding_scikit_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_advanced_decoding_scikit_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_beta_series_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_beta_series_002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_beta_series_003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_beta_series_004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_beta_series_005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_beta_series_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_bids_analysis_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_bids_analysis_002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_bids_analysis_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_design_matrix_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_design_matrix_002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_design_matrix_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_first_level_details_024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_anova_svm_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_anova_svm_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_different_estimators_002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_different_estimators_005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_full_analysis_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_full_analysis_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_grid_search_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_grid_search_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_mass_univariate_002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_searchlight_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_haxby_searchlight_thumb.png
Binary file modified dev/_images/sphx_glr_plot_localizer_mass_univariate_methods_001.png
Binary file modified dev/_images/sphx_glr_plot_oasis_vbm_004.png
Binary file modified dev/_images/sphx_glr_plot_second_level_association_test_003.png
Binary file modified dev/_images/sphx_glr_plot_second_level_one_sample_test_003.png
Binary file modified dev/_images/sphx_glr_plot_simulated_data_002.png
Binary file modified dev/_images/sphx_glr_plot_simulated_data_004.png
Binary file modified dev/_images/sphx_glr_plot_simulated_data_006.png
Binary file modified dev/_images/sphx_glr_plot_surface_image_and_maskers_004.png
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ statistical map:
.. code-block:: none
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fac49707e60>
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7f0b7e822600>
Expand Down Expand Up @@ -156,7 +156,7 @@ Visualizing works better with a threshold
.. code-block:: none
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fac48e393d0>
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7f0b7d82eab0>
Expand Down Expand Up @@ -185,6 +185,7 @@ correspondence between rest and task
[get_dataset_dir] Dataset found in /home/runner/work/nilearn/nilearn/nilearn_data/smith_2009
[fetch_single_file] Downloading data from https://www.fmrib.ox.ac.uk/datasets/brainmap+rsns/PNAS_Smith09_rsn10.nii.gz ...
[_chunk_read_] Downloaded 1712128 of 7565016 bytes (22.6%, 3.5s remaining)
[fetch_single_file] ...done. (2 seconds, 0 min)
Expand Down Expand Up @@ -269,7 +270,7 @@ We can then plot it
.. code-block:: none
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fac48d3bbf0>
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7f0b7d6da6f0>
Expand Down Expand Up @@ -480,9 +481,9 @@ image.

.. rst-class:: sphx-glr-timing

**Total running time of the script:** (0 minutes 18.593 seconds)
**Total running time of the script:** (0 minutes 20.366 seconds)

**Estimated memory usage:** 371 MB
**Estimated memory usage:** 373 MB


.. _sphx_glr_download_auto_examples_00_tutorials_plot_3d_and_4d_niimg.py:
Expand Down
Loading

0 comments on commit 7cf6a93

Please sign in to comment.