Skip to content

Commit 56465df

Browse files
authored
Merge pull request #1185 from wright-group/numpy-2.0
numpy 2.0 compatability
2 parents 7a9c584 + 48e9121 commit 56465df

19 files changed

+44
-26
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
1919
- constants in chopped data will inherit the units of the original data
2020

2121
## Changed
22+
- numpy 2.0 compatible
2223
- refactor of artists.quick1D and artists.quick2D
2324
- quick2D and quick1D will not force `autosave=True` if the number of figures is large. Instead, interactive plotting will be truncated if the number of figures is large.
2425
- artists now gets turbo colormap straight from matplotlib

WrightTools/_open.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from . import collection as wt_collection
1414
from . import data as wt_data
1515
from . import _group as wt_group
16+
from numpy.lib.npyio import DataSource
1617

1718

1819
# --- define -------------------------------------------------------------------------------------
@@ -45,7 +46,7 @@ def open(filepath, edit_local=False):
4546
Root-level object in file.
4647
"""
4748
filepath = os.fspath(filepath)
48-
ds = np.DataSource(None)
49+
ds = DataSource(None)
4950
if edit_local is False:
5051
tf = tempfile.mkstemp(prefix="", suffix=".wt5")
5152
with _open(tf[1], "w+b") as tff:

WrightTools/collection/_cary.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from .. import exceptions as wt_exceptions
1313
from ._collection import Collection
14+
from numpy.lib.npyio import DataSource
1415

1516

1617
# --- define --------------------------------------------------------------------------------------
@@ -64,7 +65,7 @@ def from_Cary(filepath, name=None, parent=None, verbose=True):
6465
name = "cary"
6566
# import array
6667
lines = []
67-
ds = np.DataSource(None)
68+
ds = DataSource(None)
6869
with ds.open(filestr, "rt", encoding="iso-8859-1") as f:
6970
header = f.readline()
7071
columns = f.readline()

WrightTools/data/_aramis.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from ._data import Data
1414
from .. import exceptions as wt_exceptions
15+
from numpy.lib.npyio import DataSource
1516

1617

1718
# --- define --------------------------------------------------------------------------------------
@@ -51,7 +52,7 @@ def from_Aramis(filepath, name=None, parent=None, verbose=True) -> Data:
5152

5253
if not ".ngc" in filepath.suffixes:
5354
wt_exceptions.WrongFileTypeWarning.warn(filepath, ".ngc")
54-
ds = np.DataSource(None)
55+
ds = DataSource(None)
5556
f = ds.open(filestr, "rb")
5657
header = f.readline()
5758
if header != b"NGSNextGen\x01\x00\x00\x00\x01\x00\x00\x00\n":

WrightTools/data/_brunold.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from ._data import Data
1212
from .. import exceptions as wt_exceptions
13+
from numpy.lib.npyio import DataSource
1314

1415

1516
# --- define --------------------------------------------------------------------------------------
@@ -61,7 +62,7 @@ def from_BrunoldrRaman(filepath, name=None, parent=None, verbose=True) -> Data:
6162
else:
6263
data = parent.create_data(**kwargs)
6364
# array
64-
ds = np.DataSource(None)
65+
ds = DataSource(None)
6566
f = ds.open(filestr, "rt")
6667
arr = np.genfromtxt(f, delimiter="\t").T
6768
f.close()

WrightTools/data/_colors.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from ._data import Data
1515
from .. import kit as wt_kit
16+
from numpy.lib.npyio import DataSource
1617

1718

1819
# --- define --------------------------------------------------------------------------------------
@@ -66,7 +67,7 @@ def from_COLORS(
6667
else:
6768
filestrs = [os.fspath(filepaths)]
6869
filepaths = [pathlib.Path(filepaths)]
69-
ds = np.DataSource(None)
70+
ds = DataSource(None)
7071
# define format of dat file -------------------------------------------------------------------
7172
if cols:
7273
pass

WrightTools/data/_data.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -852,27 +852,28 @@ def moment(self, axis, channel=0, moment=1, *, resultant=None):
852852
np.sum(np.diff(x, axis=axis_index), axis=axis_index, keepdims=True)
853853
)
854854

855+
trapezoid = np.trapezoid if (int(np.__version__.split(".")[0]) > 1) else np.trapz
855856
for moment in moments:
856857
about = 0
857858
norm = 1
858859
if moment > 0:
859-
norm = np.trapz(y, x, axis=axis_index)
860+
norm = trapezoid(y, x, axis=axis_index)
860861
norm = np.array(norm)
861862
norm.shape = new_shape
862863
if moment > 1:
863-
about = np.trapz(x * y, x, axis=axis_index)
864+
about = trapezoid(x * y, x, axis=axis_index)
864865
about = np.array(about)
865866
about.shape = new_shape
866867
about /= norm
867868
if moment > 2:
868-
sigma = np.trapz((x - about) ** 2 * y, x, axis=axis_index)
869+
sigma = trapezoid((x - about) ** 2 * y, x, axis=axis_index)
869870
sigma = np.array(sigma)
870871
sigma.shape = new_shape
871872
sigma /= norm
872873
sigma **= 0.5
873874
norm *= sigma**moment
874875

875-
values = np.trapz((x - about) ** moment * y, x, axis=axis_index)
876+
values = trapezoid((x - about) ** moment * y, x, axis=axis_index)
876877
values = np.array(values)
877878
values.shape = new_shape
878879
values /= norm

WrightTools/data/_jasco.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from ._data import Data
1212
from .. import exceptions as wt_exceptions
13+
from numpy.lib.npyio import DataSource
1314

1415

1516
# --- define --------------------------------------------------------------------------------------
@@ -59,7 +60,7 @@ def from_JASCO(filepath, name=None, parent=None, verbose=True) -> Data:
5960
else:
6061
data = parent.create_data(**kwargs)
6162
# array
62-
ds = np.DataSource(None)
63+
ds = DataSource(None)
6364
f = ds.open(filestr, "rt")
6465
arr = np.genfromtxt(f, skip_header=18).T
6566
f.close()

WrightTools/data/_kent.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from ._data import Data
1616
from .. import kit as wt_kit
17+
from numpy.lib.npyio import DataSource
1718

1819

1920
# --- define --------------------------------------------------------------------------------------
@@ -86,7 +87,7 @@ def from_KENT(
8687
filestrs = [os.fspath(f) for f in filepaths]
8788
filepaths = [pathlib.Path(f) for f in filepaths]
8889
# import full array ---------------------------------------------------------------------------
89-
ds = np.DataSource(None)
90+
ds = DataSource(None)
9091
arr = []
9192
for f in filestrs:
9293
ff = ds.open(f, "rt")

WrightTools/data/_labram.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ._data import Data
1212
from .. import exceptions as wt_exceptions
1313
from ..kit import _timestamp as timestamp
14+
from numpy.lib.npyio import DataSource
1415

1516

1617
# --- define --------------------------------------------------------------------------------------
@@ -62,7 +63,7 @@ def from_LabRAM(filepath, name=None, parent=None, verbose=True) -> Data:
6263
else:
6364
data = parent.create_data(**kwargs)
6465

65-
ds = np.DataSource(None)
66+
ds = DataSource(None)
6667
f = ds.open(filestr, "rt", encoding="ISO-8859-1")
6768

6869
# header

WrightTools/data/_ocean_optics.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from ._data import Data
1212
from .. import exceptions as wt_exceptions
13+
from numpy.lib.npyio import DataSource
1314

1415

1516
# --- define --------------------------------------------------------------------------------------
@@ -61,7 +62,7 @@ def from_ocean_optics(filepath, name=None, *, parent=None, verbose=True) -> Data
6162
# array
6263
skip_header = 14
6364
skip_footer = 1
64-
ds = np.DataSource(None)
65+
ds = DataSource(None)
6566
f = ds.open(filestr, "rt")
6667
arr = np.genfromtxt(f, skip_header=skip_header, skip_footer=skip_footer, delimiter="\t").T
6768
f.close()

WrightTools/data/_pycmds.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ._data import Data
1616
from .. import kit as wt_kit
1717
from .. import units as wt_units
18+
from numpy.lib.npyio import DataSource
1819

1920

2021
# --- define --------------------------------------------------------------------------------------
@@ -51,7 +52,7 @@ def from_PyCMDS(filepath, name=None, parent=None, verbose=True, *, collapse=True
5152
filestr = os.fspath(filepath)
5253

5354
# header
54-
ds = np.DataSource(None)
55+
ds = DataSource(None)
5556
file_ = ds.open(filestr, "rt")
5657
headers = tidy_headers.read(file_)
5758
file_.seek(0)

WrightTools/data/_shimadzu.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from ._data import Data
1212
from .. import exceptions as wt_exceptions
13+
from numpy.lib.npyio import DataSource
1314

1415

1516
# --- define --------------------------------------------------------------------------------------
@@ -59,7 +60,7 @@ def from_shimadzu(filepath, name=None, parent=None, verbose=True) -> Data:
5960
else:
6061
data = parent.create_data(**kwargs)
6162
# array
62-
ds = np.DataSource(None)
63+
ds = DataSource(None)
6364
f = ds.open(filestr, "rt")
6465
arr = np.genfromtxt(f, skip_header=2, delimiter=",").T
6566
f.close()

WrightTools/data/_solis.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ._data import Data
1414
from .. import exceptions as wt_exceptions
1515
from ..kit import _timestamp as timestamp
16+
from numpy.lib.npyio import DataSource
1617

1718

1819
# --- define --------------------------------------------------------------------------------------
@@ -68,7 +69,7 @@ def from_Solis(filepath, name=None, parent=None, verbose=True) -> Data:
6869
if not name:
6970
name = filepath.name.split(".")[0]
7071
# create data
71-
ds = np.DataSource(None)
72+
ds = DataSource(None)
7273
f = ds.open(filestr, "rt")
7374
axis0 = []
7475
arr = []

WrightTools/data/_spcm.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ._data import Data
1414
from .. import exceptions as wt_exceptions
1515
from ..kit import _timestamp as timestamp
16+
from numpy.lib.npyio import DataSource
1617

1718

1819
# --- define --------------------------------------------------------------------------------------
@@ -65,7 +66,7 @@ def from_spcm(filepath, name=None, *, delimiter=",", parent=None, verbose=True)
6566
# create headers dictionary
6667
headers = collections.OrderedDict()
6768
header_lines = 0
68-
ds = np.DataSource(None)
69+
ds = DataSource(None)
6970
f = ds.open(filestr, "rt")
7071
while True:
7172
line = f.readline().strip()

WrightTools/data/_tensor27.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from ._data import Data
1212
from .. import exceptions as wt_exceptions
13+
from numpy.lib.npyio import DataSource
1314

1415

1516
# --- define --------------------------------------------------------------------------------------
@@ -70,7 +71,7 @@ def from_Tensor27(filepath, name=None, parent=None, verbose=True) -> Data:
7071
else:
7172
data = parent.create_data(**kwargs)
7273
# array
73-
ds = np.DataSource(None)
74+
ds = DataSource(None)
7475
f = ds.open(filestr, "rt")
7576
arr = np.genfromtxt(f, skip_header=0).T
7677
f.close()

docs/write_from_function.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ We will walk through by way of example, using :meth:`~WrightTools.data.from_JASC
7272
else:
7373
data = parent.create_data(**kwargs)
7474
# array
75-
ds = np.DataSource(None)
75+
ds = np.lib.npyio.DataSource(None)
7676
f = ds.open(filestr, "rt")
7777
arr = np.genfromtxt(f, skip_header=18).T
7878
f.close()
@@ -249,7 +249,7 @@ For one-dimensional data formats, this is particularly easy:
249249
.. code-block:: python
250250
251251
# array
252-
ds = np.DataSource(None)
252+
ds = np.lib.npyio.DataSource(None)
253253
f = ds.open(filestr, "rt")
254254
arr = np.genfromtxt(f, skip_header=18).T
255255
f.close()
@@ -260,7 +260,7 @@ For one-dimensional data formats, this is particularly easy:
260260
:class:`numpy.DataSource` is a class which provides transparent decompression and remote file retrieval.
261261
:func:`numpy.genfromtxt` will handle this itself, however it will leave the downloaded files in the
262262
working directory, and opening explicitly allows you to use the file more directly as well.
263-
Using ``np.DataSource(None)`` causes it to use temporary files which are removed automatically.
263+
Using ``numpy.lib.npyio.DataSource(None)`` causes it to use temporary files which are removed automatically.
264264
Opening in ``"rt"`` mode ensures that you are reading as text.
265265

266266
Parsing multidimensional datasets (and in particular formats which allow arbitrary dimensionality)

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def read(fname):
2828
with open(os.path.join(here, "WrightTools", "VERSION")) as version_file:
2929
version = version_file.read().strip()
3030

31-
docs_require = ["sphinx", "sphinx-gallery==0.8.2", "sphinx-rtd-theme"]
31+
docs_require = ["sphinx<8.0", "sphinx-gallery==0.8.2", "sphinx-rtd-theme"]
3232

3333
setup(
3434
name="WrightTools",
@@ -45,7 +45,7 @@ def read(fname):
4545
"python-dateutil",
4646
"scipy",
4747
"click",
48-
"tidy_headers>=1.0.0",
48+
"tidy_headers>=1.0.4",
4949
"rich",
5050
],
5151
extras_require={

tests/kit/lineshapes.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import WrightTools as wt
99

10+
# numpy 2.0 compatibility
11+
trapezoid = np.trapezoid if int(np.__version__.split(".")[0]) > 1 else np.trapz
1012

1113
# --- test ----------------------------------------------------------------------------------------
1214

@@ -16,7 +18,7 @@ def test_gaussian():
1618
x0 = 0
1719
FWHM = 1
1820
y = wt.kit.gaussian(x, x0, FWHM, norm="area")
19-
assert np.isclose(np.trapz(y, x), 1, rtol=1e-3, atol=1e-3)
21+
assert np.isclose(trapezoid(y, x), 1, rtol=1e-3, atol=1e-3)
2022
y = wt.kit.gaussian(x, x0, FWHM, norm="height")
2123
assert np.isclose(y.max(), 1, rtol=1e-3, atol=1e-3)
2224

@@ -26,7 +28,7 @@ def test_lorentzian_complex():
2628
x0 = 0
2729
G = 0.1
2830
y = wt.kit.lorentzian_complex(x, x0, G, norm="area_int")
29-
assert np.isclose(np.trapz(np.abs(y) ** 2, x), 1, rtol=1e-3, atol=1e-3)
31+
assert np.isclose(trapezoid(np.abs(y) ** 2, x), 1, rtol=1e-3, atol=1e-3)
3032
y = wt.kit.lorentzian_complex(x, x0, G, norm="height_imag")
3133
assert np.isclose(y.imag.max(), 1, rtol=1e-3, atol=1e-3)
3234

@@ -36,7 +38,7 @@ def test_lorentzian_real():
3638
x0 = 0
3739
G = 0.1
3840
y = wt.kit.lorentzian_real(x, x0, G, norm="area")
39-
assert np.isclose(np.trapz(y, x), 1, rtol=1e-3, atol=1e-3)
41+
assert np.isclose(trapezoid(y, x), 1, rtol=1e-3, atol=1e-3)
4042
y = wt.kit.lorentzian_real(x, x0, G, norm="height")
4143
assert np.isclose(y.max(), 1, rtol=1e-3, atol=1e-3)
4244

0 commit comments

Comments
 (0)