Skip to content

Commit 6f19665

Browse files
authored
Merge pull request #228 from CSSFrancis/numpy2
Deprecation: Fix ptp --> np.ptp for numpy 2.0.0
2 parents decf198 + 53155ec commit 6f19665

9 files changed

+36
-22
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Removed
2424

2525
Fixed
2626
-----
27+
- Numpy 2.0.0 compatibility in (#228).
2728

2829
2024-06-06 - version 0.6.0
2930
==========================

diffsims/constants.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import numpy as np
2+
3+
# TODO: Remove this section when minimum numpy version is 1.25.0
4+
if np.__version__ >= "1.25.0":
5+
from numpy.exceptions import VisibleDeprecationWarning
6+
else:
7+
VisibleDeprecationWarning = np.VisibleDeprecationWarning

diffsims/generators/diffraction_generator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ def calculate_ed_data(
527527

528528
# Add z-coordinate
529529
z_range = max(
530-
z_range, coordinates[:, -1].ptp()
530+
z_range, np.ptp(coordinates[:, -1])
531531
) # enforce minimal resolution in reciprocal space
532532
x = [
533533
self.detector[0],

diffsims/simulations/simulation2d.py

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def current_size(self):
253253
return self.rotations.size
254254

255255
def deepcopy(self):
256+
256257
return copy.deepcopy(self)
257258

258259
def _get_transformed_coordinates(

diffsims/tests/crystallography/test_get_hkl.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
get_hkl,
2828
ReciprocalLatticeVector,
2929
)
30+
from diffsims.constants import VisibleDeprecationWarning
3031

3132

3233
class TestGetHKL:
3334
@pytest.mark.parametrize("highest_hkl, n", [([1, 2, 3], 105), ([1, 1, 1], 27)])
3435
def test_get_hkl(self, highest_hkl, n):
3536
highest_hkl = np.array(highest_hkl)
36-
with pytest.warns(np.VisibleDeprecationWarning):
37+
with pytest.warns(VisibleDeprecationWarning):
3738
hkl = get_hkl(highest_hkl)
3839
assert np.allclose(hkl.max(axis=0), highest_hkl)
3940
assert np.allclose(hkl.min(axis=0), -highest_hkl)
@@ -52,7 +53,7 @@ def test_get_hkl(self, highest_hkl, n):
5253
"d, hkl", [(0.5, [6, 6, 21]), (1, [3, 3, 11]), (1.5, [2, 2, 7])]
5354
)
5455
def test_get_highest_hkl(self, silicon_carbide_phase, d, hkl):
55-
with pytest.warns(np.VisibleDeprecationWarning):
56+
with pytest.warns(VisibleDeprecationWarning):
5657
hkl_out = get_highest_hkl(
5758
silicon_carbide_phase.structure.lattice, min_dspacing=d
5859
)
@@ -64,7 +65,7 @@ def test_get_highest_hkl(self, silicon_carbide_phase, d, hkl):
6465

6566
def test_get_equivalent_hkl(self):
6667
phase_225 = Phase(space_group=225)
67-
with pytest.warns(np.VisibleDeprecationWarning):
68+
with pytest.warns(VisibleDeprecationWarning):
6869
hkl1 = get_equivalent_hkl(
6970
[1, 1, 1], operations=phase_225.point_group, unique=True
7071
)
@@ -86,12 +87,12 @@ def test_get_equivalent_hkl(self):
8687
g1 = ReciprocalLatticeVector(phase_225, hkl=[1, 1, 1])
8788
assert np.allclose(g1.symmetrise().hkl, hkl1.data)
8889

89-
with pytest.warns(np.VisibleDeprecationWarning):
90+
with pytest.warns(VisibleDeprecationWarning):
9091
hkl2 = get_equivalent_hkl([1, 1, 1], operations=phase_225.point_group)
9192
assert hkl2.shape[0] == g1.symmetrise().size * 6 == 48
9293

9394
phase_186 = Phase(space_group=186)
94-
with pytest.warns(np.VisibleDeprecationWarning):
95+
with pytest.warns(VisibleDeprecationWarning):
9596
hkl3, mult3 = get_equivalent_hkl(
9697
[2, 2, 0],
9798
operations=phase_186.point_group,
@@ -102,7 +103,7 @@ def test_get_equivalent_hkl(self):
102103
assert mult3 == g3.symmetrise().size == 12
103104
assert np.allclose(hkl3.data[:2], [[2, 2, 0], [-2.7321, 0.7321, 0]], atol=1e-4)
104105

105-
with pytest.warns(np.VisibleDeprecationWarning):
106+
with pytest.warns(VisibleDeprecationWarning):
106107
hkl4, mult4 = get_equivalent_hkl(
107108
Vector3d([[2, 2, 0], [4, 0, 0]]),
108109
phase_186.point_group,

diffsims/tests/utils/test_deprecation.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import warnings
22

3-
import numpy as np
43
import pytest
54

65
from diffsims.utils._deprecated import deprecated, deprecated_argument
76

7+
from diffsims.constants import VisibleDeprecationWarning
8+
89

910
class TestDeprecationWarning:
1011
def test_deprecation_since(self):
@@ -18,7 +19,7 @@ def foo(n):
1819
"""Some docstring."""
1920
return n + 1
2021

21-
with pytest.warns(np.VisibleDeprecationWarning) as record:
22+
with pytest.warns(VisibleDeprecationWarning) as record:
2223
assert foo(4) == 5
2324
desired_msg = (
2425
"Function `foo()` is deprecated and will be removed in version 0.8. Use "
@@ -41,7 +42,7 @@ def foo2(n):
4142
"""
4243
return n + 2
4344

44-
with pytest.warns(np.VisibleDeprecationWarning) as record2:
45+
with pytest.warns(VisibleDeprecationWarning) as record2:
4546
assert foo2(4) == 6
4647
desired_msg2 = "Function `foo2()` is deprecated."
4748
assert str(record2[0].message) == desired_msg2
@@ -58,7 +59,7 @@ def test_deprecation_no_old_doc(self):
5859
def foo(n):
5960
return n + 1
6061

61-
with pytest.warns(np.VisibleDeprecationWarning) as record:
62+
with pytest.warns(VisibleDeprecationWarning) as record:
6263
assert foo(4) == 5
6364
desired_msg = (
6465
"Function `foo()` is deprecated and will be removed in version 0.8. Use "
@@ -79,7 +80,7 @@ def test_deprecation_not_function(self):
7980
def foo(n):
8081
return n + 1
8182

82-
with pytest.warns(np.VisibleDeprecationWarning) as record:
83+
with pytest.warns(VisibleDeprecationWarning) as record:
8384
assert foo(4) == 5
8485
desired_msg = (
8586
"Function `foo()` is deprecated and will be removed in version 0.8. Use "
@@ -118,7 +119,7 @@ def bar_arg_alt(self, **kwargs):
118119
assert my_foo.bar_arg(b=1) == {"b": 1}
119120

120121
# Warns
121-
with pytest.warns(np.VisibleDeprecationWarning) as record2:
122+
with pytest.warns(VisibleDeprecationWarning) as record2:
122123
assert my_foo.bar_arg(a=2) == {"a": 2}
123124
assert str(record2[0].message) == (
124125
r"Argument `a` is deprecated and will be removed in version 1.4. "
@@ -127,7 +128,7 @@ def bar_arg_alt(self, **kwargs):
127128
)
128129

129130
# Warns with alternative
130-
with pytest.warns(np.VisibleDeprecationWarning) as record3:
131+
with pytest.warns(VisibleDeprecationWarning) as record3:
131132
assert my_foo.bar_arg_alt(a=3) == {"b": 3}
132133
assert str(record3[0].message) == (
133134
r"Argument `a` is deprecated and will be removed in version 1.4. "

diffsims/tests/utils/test_fourier_transform.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ def test_freq(shape, dX, rX, dY, rY):
160160
if dX[i] is not None:
161161
assert abs(x[i].item(1) - x[i].item(0)) <= dX[i] + 1e-8
162162
if rY[i] is not None:
163-
assert y[i].ptp() >= rY[i] - 1e-8
163+
assert np.ptp(y[i]) >= rY[i] - 1e-8
164164
if rX[i] is not None:
165-
assert x[i].ptp() >= rX[i] - 1e-8
165+
assert np.ptp(x[i]) >= rX[i] - 1e-8
166166
if dY[i] is not None:
167167
assert abs(y[i].item(1) - y[i].item(0)) <= dY[i] + 1e-8
168168

diffsims/utils/_deprecated.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from typing import Callable, Optional, Union
3434
import warnings
3535

36-
import numpy as np
36+
from diffsims.constants import VisibleDeprecationWarning
3737

3838

3939
class deprecated:
@@ -90,12 +90,14 @@ def __call__(self, func: Callable):
9090
@functools.wraps(func)
9191
def wrapped(*args, **kwargs):
9292
warnings.simplefilter(
93-
action="always", category=np.VisibleDeprecationWarning, append=True
93+
action="always",
94+
category=VisibleDeprecationWarning,
95+
append=True,
9496
)
9597
func_code = func.__code__
9698
warnings.warn_explicit(
9799
message=msg,
98-
category=np.VisibleDeprecationWarning,
100+
category=VisibleDeprecationWarning,
99101
filename=func_code.co_filename,
100102
lineno=func_code.co_firstlineno + 1,
101103
)
@@ -143,12 +145,12 @@ def wrapped(*args, **kwargs):
143145
kwargs[self.alternative] = kwargs.pop(self.name)
144146
msg += f"See the documentation of `{func.__name__}()` for more details."
145147
warnings.simplefilter(
146-
action="always", category=np.VisibleDeprecationWarning
148+
action="always", category=VisibleDeprecationWarning
147149
)
148150
func_code = func.__code__
149151
warnings.warn_explicit(
150152
message=msg,
151-
category=np.VisibleDeprecationWarning,
153+
category=VisibleDeprecationWarning,
152154
filename=func_code.co_filename,
153155
lineno=func_code.co_firstlineno + 1,
154156
)

diffsims/utils/discretise_utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
exp,
4545
prod,
4646
)
47+
import numpy as np
4748
from psutil import virtual_memory
4849
import numba
4950

@@ -321,7 +322,7 @@ def rebin(x, loc, r, k, mem):
321322
else:
322323
r = r.copy()
323324
xmin = array([X.item(0) if X.size > 1 else -1e5 for X in x], dtype=x[0].dtype)
324-
nbins = [int(ceil(x[i].ptp() / r[i])) + 1 for i in range(3)]
325+
nbins = [int(ceil(np.ptp(x[i]) / r[i])) + 1 for i in range(3)]
325326
if prod(nbins) * 32 * 10 > mem:
326327
raise MemoryError
327328
Len = zeros(nbins, dtype="i4")

0 commit comments

Comments
 (0)