Skip to content

Commit 3a6f620

Browse files
ericspodKumoLiu
andauthored
Updating to match Numpy 2.0 requirements (#7857)
Fixes #7856. ### Description This introduces changes to meet Numpy 2.0 requirements. MONAI itself is compatible with Numpy 2.0 however some dependencies are not such as older versions of Pytorch. This PR adjusts the MAX_SEED value to be compatible with Numpy 2.0 behaviour changes, uses the `ptp` function, and some other minor tweaks. The versions for dependencies are also fixed to exclude Numpy 2.0. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Eric Kerfoot <[email protected]> Co-authored-by: YunLiu <[email protected]>
1 parent 9f56a3a commit 3a6f620

File tree

11 files changed

+15
-15
lines changed

11 files changed

+15
-15
lines changed

environment-dev.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ channels:
55
- nvidia
66
- conda-forge
77
dependencies:
8-
- numpy>=1.20
8+
- numpy>=1.24,<2.0
99
- pytorch>=1.9
1010
- torchvision
11-
- pytorch-cuda=11.6
11+
- pytorch-cuda>=11.6
1212
- pip
1313
- pip:
1414
- -r requirements-dev.txt

monai/data/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ def compute_shape_offset(
927927
corners = in_affine_ @ corners
928928
all_dist = corners_out[:-1].copy()
929929
corners_out = corners_out[:-1] / corners_out[-1]
930-
out_shape = np.round(corners_out.ptp(axis=1)) if scale_extent else np.round(corners_out.ptp(axis=1) + 1.0)
930+
out_shape = np.round(np.ptp(corners_out, axis=1)) if scale_extent else np.round(np.ptp(corners_out, axis=1) + 1.0)
931931
offset = None
932932
for i in range(corners.shape[1]):
933933
min_corner = np.min(all_dist - all_dist[:, i : i + 1], 1)

monai/transforms/io/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def switch_endianness(data, new="<"):
8686
if new not in ("<", ">"):
8787
raise NotImplementedError(f"Not implemented option new={new}.")
8888
if current_ != new:
89-
data = data.byteswap().newbyteorder(new)
89+
data = data.byteswap().view(data.dtype.newbyteorder(new))
9090
elif isinstance(data, tuple):
9191
data = tuple(switch_endianness(x, new) for x in data)
9292
elif isinstance(data, list):

monai/transforms/spatial/functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def rotate(img, angle, output_shape, mode, padding_mode, align_corners, dtype, l
373373
if output_shape is None:
374374
corners = np.asarray(np.meshgrid(*[(0, dim) for dim in im_shape], indexing="ij")).reshape((len(im_shape), -1))
375375
corners = transform[:-1, :-1] @ corners # type: ignore
376-
output_shape = np.asarray(corners.ptp(axis=1) + 0.5, dtype=int)
376+
output_shape = np.asarray(np.ptp(corners, axis=1) + 0.5, dtype=int)
377377
else:
378378
output_shape = np.asarray(output_shape, dtype=int)
379379
shift = create_translate(input_ndim, ((np.array(im_shape) - 1) / 2).tolist())

monai/transforms/transform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ def set_random_state(self, seed: int | None = None, state: np.random.RandomState
203203
204204
"""
205205
if seed is not None:
206-
_seed = id(seed) if not isinstance(seed, (int, np.integer)) else seed
207-
_seed = _seed % MAX_SEED
206+
_seed = np.int64(id(seed) if not isinstance(seed, (int, np.integer)) else seed)
207+
_seed = _seed % MAX_SEED # need to account for Numpy2.0 which doesn't silently convert to int64
208208
self.R = np.random.RandomState(_seed)
209209
return self
210210

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
torch>=1.9
2-
numpy>=1.20,<=1.26.0
2+
numpy>=1.24,<2.0

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ setup_requires =
4242
ninja
4343
install_requires =
4444
torch>=1.9
45-
numpy>=1.20
45+
numpy>=1.24,<2.0
4646

4747
[options.extras_require]
4848
all =

tests/test_meta_tensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def test_shape(self):
448448

449449
def test_astype(self):
450450
t = MetaTensor([1.0], affine=torch.tensor(1), meta={"fname": "filename"})
451-
for np_types in ("float32", "np.float32", "numpy.float32", np.float32, float, "int", np.compat.long, np.uint16):
451+
for np_types in ("float32", "np.float32", "numpy.float32", np.float32, float, "int", np.uint16):
452452
self.assertIsInstance(t.astype(np_types), np.ndarray)
453453
for pt_types in ("torch.float", torch.float, "torch.float64"):
454454
self.assertIsInstance(t.astype(pt_types), torch.Tensor)

tests/test_nifti_endianness.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_switch(self): # verify data types
8282
after = switch_endianness(before)
8383
np.testing.assert_allclose(after.astype(float), expected_float)
8484

85-
before = np.array(["1.12", "-9.2", "42"], dtype=np.string_)
85+
before = np.array(["1.12", "-9.2", "42"], dtype=np.bytes_)
8686
after = switch_endianness(before)
8787
np.testing.assert_array_equal(before, after)
8888

tests/test_signal_fillempty.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TestSignalFillEmptyNumpy(unittest.TestCase):
3030
def test_correct_parameters_multi_channels(self):
3131
self.assertIsInstance(SignalFillEmpty(replacement=0.0), SignalFillEmpty)
3232
sig = np.load(TEST_SIGNAL)
33-
sig[:, 123] = np.NAN
33+
sig[:, 123] = np.nan
3434
fillempty = SignalFillEmpty(replacement=0.0)
3535
fillemptysignal = fillempty(sig)
3636
self.assertTrue(not np.isnan(fillemptysignal).any())
@@ -42,7 +42,7 @@ class TestSignalFillEmptyTorch(unittest.TestCase):
4242
def test_correct_parameters_multi_channels(self):
4343
self.assertIsInstance(SignalFillEmpty(replacement=0.0), SignalFillEmpty)
4444
sig = convert_to_tensor(np.load(TEST_SIGNAL))
45-
sig[:, 123] = convert_to_tensor(np.NAN)
45+
sig[:, 123] = convert_to_tensor(np.nan)
4646
fillempty = SignalFillEmpty(replacement=0.0)
4747
fillemptysignal = fillempty(sig)
4848
self.assertTrue(not torch.isnan(fillemptysignal).any())

0 commit comments

Comments
 (0)