From ee92cf21bbfa6cd5af86ccc0a298c59047f905a0 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Thu, 21 Dec 2023 11:29:43 +0000 Subject: [PATCH] Trying to fix up examples for nibabel --- examples/.gitignore | 6 ++++++ examples/compute_fmri_contrast.py | 4 ++-- examples/data/README_mni_basal_ganglia.rst | 2 +- examples/labs/need_data/demo_blob_from_image.py | 2 +- examples/labs/need_data/demo_roi.py | 2 +- examples/labs/need_data/demo_ward_clustering.py | 2 +- examples/labs/need_data/example_roi_and_glm.py | 2 +- examples/labs/need_data/first_level_fiac.py | 4 ++-- examples/labs/need_data/localizer_glm_ar.py | 2 +- examples/labs/need_data/one_sample_t_test.py | 4 ++-- examples/labs/need_data/viz.py | 4 ++-- examples/labs/need_data/viz3d.py | 6 +++--- 12 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 examples/.gitignore diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000000..9104fb957e --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,6 @@ +# Generated data files +ammon_TO_anubis.npy +labs/fmri_data.nii +labs/localizer_paradigm.csv +labs/need_data/results/ +labs/zmap.nii diff --git a/examples/compute_fmri_contrast.py b/examples/compute_fmri_contrast.py index edbc576b73..99a56ba1eb 100755 --- a/examples/compute_fmri_contrast.py +++ b/examples/compute_fmri_contrast.py @@ -77,9 +77,9 @@ # Show Z-map image mean_map = multi_session_model.means[0] plot_map(z_map.get_fdata(), - z_map.get_affine(), + z_map.affine, anat=mean_map.get_fdata(), - anat_affine=mean_map.get_affine(), + anat_affine=mean_map.affine, cmap=cm.cold_hot, threshold=2.5, black_bg=True) diff --git a/examples/data/README_mni_basal_ganglia.rst b/examples/data/README_mni_basal_ganglia.rst index ca9d88d844..661cea6677 100644 --- a/examples/data/README_mni_basal_ganglia.rst +++ b/examples/data/README_mni_basal_ganglia.rst @@ -21,7 +21,7 @@ The script to extract the data was:: 'AtlasGrey.mnc') atlas_img = nib.load(atlas_fname) # Data is in fact uint8, but with trivial float scaling - data = atlas_img.get_data().astype(np.uint8) + data = np.array(atlas_img.dataobj).astype(np.uint8) bg_data = np.zeros_like(data) for code in (14, 16, 39, 53): # LR striatum, LR caudate in_mask = data == code diff --git a/examples/labs/need_data/demo_blob_from_image.py b/examples/labs/need_data/demo_blob_from_image.py index 0c63394415..51ff4181df 100755 --- a/examples/labs/need_data/demo_blob_from_image.py +++ b/examples/labs/need_data/demo_blob_from_image.py @@ -38,7 +38,7 @@ # prepare the data nim = load(input_image) mask_image = Nifti1Image((nim.get_fdata() ** 2 > 0).astype('u8'), - nim.get_affine()) + nim.affine) domain = grid_domain_from_image(mask_image) data = nim.get_fdata() values = data[data != 0] diff --git a/examples/labs/need_data/demo_roi.py b/examples/labs/need_data/demo_roi.py index 4e6bee7012..b1664154c1 100755 --- a/examples/labs/need_data/demo_roi.py +++ b/examples/labs/need_data/demo_roi.py @@ -63,7 +63,7 @@ # prepare the data nim = load(input_image) -affine = nim.get_affine() +affine = nim.affine shape = nim.shape data = nim.get_fdata() values = data[data != 0] diff --git a/examples/labs/need_data/demo_ward_clustering.py b/examples/labs/need_data/demo_ward_clustering.py index 6b34963ca4..8c7044d362 100755 --- a/examples/labs/need_data/demo_ward_clustering.py +++ b/examples/labs/need_data/demo_ward_clustering.py @@ -43,5 +43,5 @@ label_image = path.join(write_dir, 'label.nii') wdata = mask - 1 wdata[mask] = u -save(Nifti1Image(wdata, load(mask_image).get_affine()), label_image) +save(Nifti1Image(wdata, load(mask_image).affine), label_image) print(f"Label image written in {label_image}") diff --git a/examples/labs/need_data/example_roi_and_glm.py b/examples/labs/need_data/example_roi_and_glm.py index ee2b04cef8..db351d2830 100755 --- a/examples/labs/need_data/example_roi_and_glm.py +++ b/examples/labs/need_data/example_roi_and_glm.py @@ -48,7 +48,7 @@ get_second_level_dataset() mask = load(mask_path) -mask_array, affine = mask.get_fdata() > 0, mask.get_affine() +mask_array, affine = mask.get_fdata() > 0, mask.affine # timing n_scans = 128 diff --git a/examples/labs/need_data/first_level_fiac.py b/examples/labs/need_data/first_level_fiac.py index 6f49bb92dc..60b9515ccd 100755 --- a/examples/labs/need_data/first_level_fiac.py +++ b/examples/labs/need_data/first_level_fiac.py @@ -95,8 +95,8 @@ def length_p_vector(con, p): if contrast_id == 'Effects_of_interest': vmax = max(- z_map.get_fdata().min(), z_map.get_fdata().max()) vmin = - vmax - plot_map(z_map.get_fdata(), z_map.get_affine(), - anat=mean_map.get_fdata(), anat_affine=mean_map.get_affine(), + plot_map(z_map.get_fdata(), z_map.affine, + anat=mean_map.get_fdata(), anat_affine=mean_map.affine, cmap=cm.cold_hot, vmin=vmin, vmax=vmax, diff --git a/examples/labs/need_data/localizer_glm_ar.py b/examples/labs/need_data/localizer_glm_ar.py index 3175815ddd..b640f79c65 100755 --- a/examples/labs/need_data/localizer_glm_ar.py +++ b/examples/labs/need_data/localizer_glm_ar.py @@ -142,7 +142,7 @@ vmax = max(- z_map.get_fdata().min(), z_map.get_fdata().max()) if index > 0: plt.clf() - plot_map(z_map.get_fdata(), z_map.get_affine(), + plot_map(z_map.get_fdata(), z_map.affine, cmap=cm.cold_hot, vmin=- vmax, vmax=vmax, diff --git a/examples/labs/need_data/one_sample_t_test.py b/examples/labs/need_data/one_sample_t_test.py index c7c96981ee..ac99beea73 100755 --- a/examples/labs/need_data/one_sample_t_test.py +++ b/examples/labs/need_data/one_sample_t_test.py @@ -54,7 +54,7 @@ # Compute a population-level mask as the intersection of individual masks grp_mask = Nifti1Image(intersect_masks(mask_images).astype(np.int8), - load(mask_images[0]).get_affine()) + load(mask_images[0]).affine) # concatenate the individual images first_level_image = concat_images(betas) @@ -76,7 +76,7 @@ # look at the result vmax = max(- z_map.get_fdata().min(), z_map.get_fdata().max()) vmin = - vmax -plot_map(z_map.get_fdata(), z_map.get_affine(), +plot_map(z_map.get_fdata(), z_map.affine, cmap=cm.cold_hot, vmin=vmin, vmax=vmax, diff --git a/examples/labs/need_data/viz.py b/examples/labs/need_data/viz.py index 468948232f..ef98703e28 100755 --- a/examples/labs/need_data/viz.py +++ b/examples/labs/need_data/viz.py @@ -30,7 +30,7 @@ # First example, with a anatomical template img = load(os.path.join(data_dir, 'spmT_0029.nii.gz')) data = img.get_fdata() -affine = img.get_affine() +affine = img.affine viz.plot_map(data, affine, cut_coords=(-52, 10, 22), threshold=2.0, cmap=viz.cm.cold_hot) @@ -41,7 +41,7 @@ anat_img = load(example_data.get_filename('neurospin', 'sulcal2000', 'nobias_anubis.nii.gz')) anat = anat_img.get_fdata() - anat_affine = anat_img.get_affine() + anat_affine = anat_img.affine except OSError as e: # File does not exist: the data package is not installed print(e) diff --git a/examples/labs/need_data/viz3d.py b/examples/labs/need_data/viz3d.py index b901a58547..e2a417a416 100755 --- a/examples/labs/need_data/viz3d.py +++ b/examples/labs/need_data/viz3d.py @@ -36,7 +36,7 @@ vmin, vmax = brain_map.get_fdata().min(), brain_map.get_fdata().max() # make a simple 2D plot -plot_map(brain_map.get_fdata(), brain_map.get_affine(), +plot_map(brain_map.get_fdata(), brain_map.affine, cmap=cm.cold_hot, vmin=vmin, vmax=vmax, @@ -46,7 +46,7 @@ # More plots using 3D if True: # replace with False to skip this - plot_map(brain_map.get_fdata(), brain_map.get_affine(), + plot_map(brain_map.get_fdata(), brain_map.affine, cmap=cm.cold_hot, vmin=vmin, vmax=vmax, @@ -56,7 +56,7 @@ from nipy.labs import viz3d try: - viz3d.plot_map_3d(brain_map.get_fdata(), brain_map.get_affine(), + viz3d.plot_map_3d(brain_map.get_fdata(), brain_map.affine, cmap=cm.cold_hot, vmin=vmin, vmax=vmax,