Blame 0001-Account-for-nibabel-5.0.0-removal-of-.py3k-shim-use-.patch

428a329
From a8faab52ca5e59069ac871f63a8604d2ac26c433 Mon Sep 17 00:00:00 2001
428a329
From: "Benjamin A. Beasley" <code@musicinmybrain.net>
428a329
Date: Tue, 7 Feb 2023 17:49:20 -0500
428a329
Subject: [PATCH] Account for nibabel 5.0.0 removal of .py3k shim - use
428a329
 numpy.compat.py3k
428a329
428a329
Rebased from:
428a329
428a329
https://salsa.debian.org/med-team/nipy/-/blob/12a4fbea8c99c1e5dc07ee81bc3da1a450617050/debian/patches/nibabel5.0.0.patch
428a329
---
428a329
 .../statistics/formula/tests/test_formula.py         |  2 +-
428a329
 nipy/core/image/image_spaces.py                      |  4 ++--
428a329
 nipy/core/image/tests/test_image_spaces.py           |  4 ++--
428a329
 nipy/io/tests/test_image_io.py                       |  2 +-
428a329
 nipy/labs/spatial_models/parcel_io.py                |  4 ++--
428a329
 .../spatial_models/tests/test_discrete_domain.py     |  2 +-
428a329
 nipy/labs/spatial_models/tests/test_mroi.py          |  4 ++--
428a329
 nipy/labs/spatial_models/tests/test_parcel_io.py     | 12 ++++++------
428a329
 nipy/utils/compat3.py                                |  2 +-
428a329
 nipy/utils/tests/test_compat3.py                     |  2 +-
428a329
 tools/run_log_examples.py                            |  2 +-
428a329
 11 files changed, 20 insertions(+), 20 deletions(-)
428a329
428a329
diff --git a/nipy/algorithms/statistics/formula/tests/test_formula.py b/nipy/algorithms/statistics/formula/tests/test_formula.py
428a329
index b7e88d6e..1323a881 100644
428a329
--- a/nipy/algorithms/statistics/formula/tests/test_formula.py
428a329
+++ b/nipy/algorithms/statistics/formula/tests/test_formula.py
428a329
@@ -18,7 +18,7 @@ from .. import formulae as F
428a329
 from ..formulae import terms, Term
428a329
 from nipy.utils import VisibleDeprecationWarning
428a329
 
428a329
-from nibabel.py3k import asbytes
428a329
+from numpy.compat.py3k import asbytes
428a329
 from nibabel.testing import assert_dt_equal
428a329
 
428a329
 from nose.tools import (assert_true, assert_equal, assert_false,
428a329
diff --git a/nipy/core/image/image_spaces.py b/nipy/core/image/image_spaces.py
428a329
index c349e381..72c5a096 100644
428a329
--- a/nipy/core/image/image_spaces.py
428a329
+++ b/nipy/core/image/image_spaces.py
428a329
@@ -60,7 +60,7 @@ array([[ 2.,  0.,  0.,  0.],
428a329
 It also works with nibabel images, which can only have xyz_affines:
428a329
 
428a329
 >>> import nibabel as nib
428a329
->>> nimg = nib.Nifti1Image(data, affine)
428a329
+>>> nimg = nib.Nifti1Image(data.astype('float'), affine)
428a329
 >>> xyz_affine(nimg)
428a329
 array([[ 2.,  0.,  0.,  0.],
428a329
        [ 0.,  3.,  0.,  0.],
428a329
@@ -195,7 +195,7 @@ def is_xyz_affable(img, name2xyz=None):
428a329
     Nibabel images always have xyz affines
428a329
 
428a329
     >>> import nibabel as nib
428a329
-    >>> nimg = nib.Nifti1Image(arr, np.diag([2,3,4,1]))
428a329
+    >>> nimg = nib.Nifti1Image(arr.astype('float'), np.diag([2,3,4,1]))
428a329
     >>> is_xyz_affable(nimg)
428a329
     True
428a329
     """
428a329
diff --git a/nipy/core/image/tests/test_image_spaces.py b/nipy/core/image/tests/test_image_spaces.py
428a329
index 262b27f7..528bdf7f 100644
428a329
--- a/nipy/core/image/tests/test_image_spaces.py
428a329
+++ b/nipy/core/image/tests/test_image_spaces.py
428a329
@@ -34,7 +34,7 @@ def test_image_xyz_affine():
428a329
     img4_r = img4.reordered_axes([3,2,0,1])
428a329
     assert_false(is_xyz_affable(img4_r))
428a329
     assert_raises(AxesError, xyz_affine, img4_r)
428a329
-    nimg = nib.Nifti1Image(arr, aff)
428a329
+    nimg = nib.Nifti1Image(arr.astype('float'), aff)
428a329
     assert_true(is_xyz_affable(nimg))
428a329
     assert_array_equal(xyz_affine(nimg), aff)
428a329
     # Any dimensions not spatial, AxesError
428a329
@@ -77,7 +77,7 @@ def test_image_as_xyz_image():
428a329
     assert_array_equal(img.get_data(), img_t0_r.get_data())
428a329
     assert_equal(img.coordmap, img_t0_r.coordmap)
428a329
     # Test against nibabel image
428a329
-    nimg = nib.Nifti1Image(arr, np.diag([2,3,4,1]))
428a329
+    nimg = nib.Nifti1Image(arr.astype('float'), np.diag([2,3,4,1]))
428a329
     nimg_r = as_xyz_image(nimg)
428a329
     assert_true(nimg is nimg_r)
428a329
     # It's sometimes impossible to make an xyz affable image
428a329
diff --git a/nipy/io/tests/test_image_io.py b/nipy/io/tests/test_image_io.py
428a329
index 5c5d940f..b15fea54 100644
428a329
--- a/nipy/io/tests/test_image_io.py
428a329
+++ b/nipy/io/tests/test_image_io.py
428a329
@@ -44,7 +44,7 @@ def test_badfile():
428a329
     # nibabel prior 2.1.0 was throwing a ImageFileError for the not-recognized
428a329
     # file type.  >=2.1.0 give a FileNotFoundError.
428a329
     try:
428a329
-        from nibabel.py3k import FileNotFoundError
428a329
+        from numpy.compat.py3k import FileNotFoundError
428a329
     except ImportError:
428a329
         FileNotFoundError = IOError
428a329
     assert_raises((ImageFileError, FileNotFoundError), load_image, filename)
428a329
diff --git a/nipy/labs/spatial_models/parcel_io.py b/nipy/labs/spatial_models/parcel_io.py
428a329
index 89030f8d..934d9faa 100644
428a329
--- a/nipy/labs/spatial_models/parcel_io.py
428a329
+++ b/nipy/labs/spatial_models/parcel_io.py
428a329
@@ -53,7 +53,7 @@ def mask_parcellation(mask_images, nb_parcel, threshold=0, output_image=None):
428a329
     else:
428a329
         # mask_images should be a list
428a329
         mask_data = intersect_masks(mask_images, threshold=0) > 0
428a329
-        mask = Nifti1Image(mask_data.astype('u8'),
428a329
+        mask = Nifti1Image(mask_data.astype('u1'),
428a329
                            get_affine(load(mask_images[0])))
428a329
 
428a329
     domain = grid_domain_from_image(mask)
428a329
@@ -99,7 +99,7 @@ def parcel_input(mask_images, learning_images, ths=.5, fdim=None):
428a329
     else:
428a329
         # mask_images should be a list
428a329
         grp_mask = intersect_masks(mask_images, threshold=ths) > 0
428a329
-        mask = Nifti1Image(grp_mask.astype('u8'),
428a329
+        mask = Nifti1Image(grp_mask.astype('u1'),
428a329
                            get_affine(load(mask_images[0])))
428a329
 
428a329
     # build the domain
428a329
diff --git a/nipy/labs/spatial_models/tests/test_discrete_domain.py b/nipy/labs/spatial_models/tests/test_discrete_domain.py
428a329
index c2c6ece2..d3b269dc 100644
428a329
--- a/nipy/labs/spatial_models/tests/test_discrete_domain.py
428a329
+++ b/nipy/labs/spatial_models/tests/test_discrete_domain.py
428a329
@@ -142,7 +142,7 @@ def test_image_feature():
428a329
     mask = np.random.randn(*shape[:3]) > .5
428a329
     noise = np.random.randn(*shape[:3])
428a329
     affine = np.eye(4)
428a329
-    mim = Nifti1Image(mask.astype('u8'), affine)
428a329
+    mim = Nifti1Image(mask.astype('u1'), affine)
428a329
     nim = Nifti1Image(noise, affine)
428a329
     ddom = grid_domain_from_image(mim)
428a329
     ddom.make_feature_from_image(nim, 'noise')
428a329
diff --git a/nipy/labs/spatial_models/tests/test_mroi.py b/nipy/labs/spatial_models/tests/test_mroi.py
428a329
index 7f8c5420..0ceb7ad8 100644
428a329
--- a/nipy/labs/spatial_models/tests/test_mroi.py
428a329
+++ b/nipy/labs/spatial_models/tests/test_mroi.py
428a329
@@ -205,8 +205,8 @@ def test_example():
428a329
     # Test example runs correctly
428a329
     eg_img = pjoin(dirname(__file__), 'some_blobs.nii')
428a329
     nim = load(eg_img)
428a329
-    mask_image = Nifti1Image((nim.get_data() ** 2 > 0).astype('u8'),
428a329
-                             get_affine(nim))
428a329
+    arr = nim.get_data() ** 2 > 0
428a329
+    mask_image = Nifti1Image(arr.astype('u1'), get_affine(nim))
428a329
     domain = grid_domain_from_image(mask_image)
428a329
     data = nim.get_data()
428a329
     values = data[data != 0]
428a329
diff --git a/nipy/labs/spatial_models/tests/test_parcel_io.py b/nipy/labs/spatial_models/tests/test_parcel_io.py
428a329
index 62d03318..87e8f26b 100644
428a329
--- a/nipy/labs/spatial_models/tests/test_parcel_io.py
428a329
+++ b/nipy/labs/spatial_models/tests/test_parcel_io.py
428a329
@@ -17,7 +17,7 @@ def test_mask_parcel():
428a329
     """
428a329
     n_parcels = 20
428a329
     shape = (10, 10, 10)
428a329
-    mask_image = Nifti1Image(np.ones(shape), np.eye(4))
428a329
+    mask_image = Nifti1Image(np.ones(shape).astype('u1'), np.eye(4))
428a329
     wim = mask_parcellation(mask_image, n_parcels)
428a329
     assert_equal(np.unique(wim.get_data()), np.arange(n_parcels))
428a329
 
428a329
@@ -33,8 +33,8 @@ def test_mask_parcel_multi_subj():
428a329
     with InTemporaryDirectory():
428a329
         for subject in range(n_subjects):
428a329
             path = 'mask%s.nii' % subject
428a329
-            save(Nifti1Image((rng.rand(*shape) > .1).astype('u8'),
428a329
-                             np.eye(4)), path)
428a329
+            arr = rng.rand(*shape) > .1
428a329
+            save(Nifti1Image(arr.astype('u1'), np.eye(4)), path)
428a329
             mask_images.append(path)
428a329
 
428a329
         wim = mask_parcellation(mask_images, n_parcels)
428a329
@@ -47,7 +47,7 @@ def test_parcel_intra_from_3d_image():
428a329
     # Generate an image
428a329
     shape = (10, 10, 10)
428a329
     n_parcel, nn, mu = 10, 6, 1.
428a329
-    mask_image = Nifti1Image(np.ones(shape), np.eye(4))
428a329
+    mask_image = Nifti1Image(np.ones(shape).astype('u1'), np.eye(4))
428a329
     with InTemporaryDirectory() as dir_context:
428a329
         surrogate_3d_dataset(mask=mask_image, out_image_file='image.nii')
428a329
 
428a329
@@ -67,7 +67,7 @@ def test_parcel_intra_from_3d_images_list():
428a329
     shape = (10, 10, 10)
428a329
     n_parcel, nn, mu = 10, 6, 1.
428a329
     method = 'ward'
428a329
-    mask_image = Nifti1Image(np.ones(shape), np.eye(4))
428a329
+    mask_image = Nifti1Image(np.ones(shape).astype('u1'), np.eye(4))
428a329
 
428a329
     with InTemporaryDirectory() as dir_context:
428a329
         data_image = ['image_%d.nii' % i for i in range(5)]
428a329
@@ -88,7 +88,7 @@ def test_parcel_intra_from_4d_image():
428a329
     shape = (10, 10, 10)
428a329
     n_parcel, nn, mu = 10, 6, 1.
428a329
     method = 'ward'
428a329
-    mask_image = Nifti1Image(np.ones(shape), np.eye(4))
428a329
+    mask_image = Nifti1Image(np.ones(shape).astype('u1'), np.eye(4))
428a329
     with InTemporaryDirectory() as dir_context:
428a329
         surrogate_3d_dataset(n_subj=10, mask=mask_image, 
428a329
                              out_image_file='image.nii')    
428a329
diff --git a/nipy/utils/compat3.py b/nipy/utils/compat3.py
428a329
index d5a1c175..b9bb01e9 100644
428a329
--- a/nipy/utils/compat3.py
428a329
+++ b/nipy/utils/compat3.py
428a329
@@ -1,6 +1,6 @@
428a329
 """ Routines for Python 3 compatibility
428a329
 
428a329
-These are in addition to the nibabel.py3k routines.
428a329
+These are in addition to the numpy.compat.py3k routines.
428a329
 """
428a329
 from __future__ import absolute_import
428a329
 
428a329
diff --git a/nipy/utils/tests/test_compat3.py b/nipy/utils/tests/test_compat3.py
428a329
index 59175ee3..4b25b581 100644
428a329
--- a/nipy/utils/tests/test_compat3.py
428a329
+++ b/nipy/utils/tests/test_compat3.py
428a329
@@ -3,7 +3,7 @@
428a329
 from __future__ import with_statement
428a329
 from __future__ import absolute_import
428a329
 
428a329
-from nibabel.py3k import asstr, asbytes
428a329
+from numpy.compat.py3k import asstr, asbytes
428a329
 
428a329
 from ..compat3 import to_str, open4csv
428a329
 
428a329
diff --git a/tools/run_log_examples.py b/tools/run_log_examples.py
428a329
index d3fd0fab..e98c47ce 100755
428a329
--- a/tools/run_log_examples.py
428a329
+++ b/tools/run_log_examples.py
428a329
@@ -25,7 +25,7 @@ from os.path import (abspath, expanduser, join as pjoin, sep as psep, isfile,
428a329
 from subprocess import Popen, PIPE
428a329
 import re
428a329
 
428a329
-from nibabel.py3k import asstr
428a329
+from numpy.compat.py3k import asstr
428a329
 
428a329
 from argparse import ArgumentParser, RawDescriptionHelpFormatter
428a329
 
428a329
-- 
428a329
2.39.1
428a329