Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy 2.0 compatability #1185

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
- constants in chopped data will inherit the units of the original data

## Changed
- numpy 2.0 compatible
- refactor of artists.quick1D and artists.quick2D
- 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.
- artists now gets turbo colormap straight from matplotlib
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from . import collection as wt_collection
from . import data as wt_data
from . import _group as wt_group
from numpy.lib.npyio import DataSource


# --- define -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -45,7 +46,7 @@ def open(filepath, edit_local=False):
Root-level object in file.
"""
filepath = os.fspath(filepath)
ds = np.DataSource(None)
ds = DataSource(None)
if edit_local is False:
tf = tempfile.mkstemp(prefix="", suffix=".wt5")
with _open(tf[1], "w+b") as tff:
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/collection/_cary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from .. import exceptions as wt_exceptions
from ._collection import Collection
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -64,7 +65,7 @@ def from_Cary(filepath, name=None, parent=None, verbose=True):
name = "cary"
# import array
lines = []
ds = np.DataSource(None)
ds = DataSource(None)
with ds.open(filestr, "rt", encoding="iso-8859-1") as f:
header = f.readline()
columns = f.readline()
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_aramis.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from ._data import Data
from .. import exceptions as wt_exceptions
from numpy.lib.npyio import DataSource


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

if not ".ngc" in filepath.suffixes:
wt_exceptions.WrongFileTypeWarning.warn(filepath, ".ngc")
ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rb")
header = f.readline()
if header != b"NGSNextGen\x01\x00\x00\x00\x01\x00\x00\x00\n":
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_brunold.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ._data import Data
from .. import exceptions as wt_exceptions
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -61,7 +62,7 @@ def from_BrunoldrRaman(filepath, name=None, parent=None, verbose=True) -> Data:
else:
data = parent.create_data(**kwargs)
# array
ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rt")
arr = np.genfromtxt(f, delimiter="\t").T
f.close()
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from ._data import Data
from .. import kit as wt_kit
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -66,7 +67,7 @@ def from_COLORS(
else:
filestrs = [os.fspath(filepaths)]
filepaths = [pathlib.Path(filepaths)]
ds = np.DataSource(None)
ds = DataSource(None)
# define format of dat file -------------------------------------------------------------------
if cols:
pass
Expand Down
9 changes: 5 additions & 4 deletions WrightTools/data/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,27 +852,28 @@ def moment(self, axis, channel=0, moment=1, *, resultant=None):
np.sum(np.diff(x, axis=axis_index), axis=axis_index, keepdims=True)
)

trapezoid = np.trapezoid if (int(np.__version__.split(".")[0]) > 1) else np.trapz
for moment in moments:
about = 0
norm = 1
if moment > 0:
norm = np.trapz(y, x, axis=axis_index)
norm = trapezoid(y, x, axis=axis_index)
norm = np.array(norm)
norm.shape = new_shape
if moment > 1:
about = np.trapz(x * y, x, axis=axis_index)
about = trapezoid(x * y, x, axis=axis_index)
about = np.array(about)
about.shape = new_shape
about /= norm
if moment > 2:
sigma = np.trapz((x - about) ** 2 * y, x, axis=axis_index)
sigma = trapezoid((x - about) ** 2 * y, x, axis=axis_index)
sigma = np.array(sigma)
sigma.shape = new_shape
sigma /= norm
sigma **= 0.5
norm *= sigma**moment

values = np.trapz((x - about) ** moment * y, x, axis=axis_index)
values = trapezoid((x - about) ** moment * y, x, axis=axis_index)
values = np.array(values)
values.shape = new_shape
values /= norm
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_jasco.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ._data import Data
from .. import exceptions as wt_exceptions
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -59,7 +60,7 @@ def from_JASCO(filepath, name=None, parent=None, verbose=True) -> Data:
else:
data = parent.create_data(**kwargs)
# array
ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rt")
arr = np.genfromtxt(f, skip_header=18).T
f.close()
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_kent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from ._data import Data
from .. import kit as wt_kit
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -86,7 +87,7 @@ def from_KENT(
filestrs = [os.fspath(f) for f in filepaths]
filepaths = [pathlib.Path(f) for f in filepaths]
# import full array ---------------------------------------------------------------------------
ds = np.DataSource(None)
ds = DataSource(None)
arr = []
for f in filestrs:
ff = ds.open(f, "rt")
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_labram.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ._data import Data
from .. import exceptions as wt_exceptions
from ..kit import _timestamp as timestamp
from numpy.lib.npyio import DataSource


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

ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rt", encoding="ISO-8859-1")

# header
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_ocean_optics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ._data import Data
from .. import exceptions as wt_exceptions
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -61,7 +62,7 @@ def from_ocean_optics(filepath, name=None, *, parent=None, verbose=True) -> Data
# array
skip_header = 14
skip_footer = 1
ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rt")
arr = np.genfromtxt(f, skip_header=skip_header, skip_footer=skip_footer, delimiter="\t").T
f.close()
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_pycmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ._data import Data
from .. import kit as wt_kit
from .. import units as wt_units
from numpy.lib.npyio import DataSource


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

# header
ds = np.DataSource(None)
ds = DataSource(None)
file_ = ds.open(filestr, "rt")
headers = tidy_headers.read(file_)
file_.seek(0)
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_shimadzu.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ._data import Data
from .. import exceptions as wt_exceptions
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -59,7 +60,7 @@ def from_shimadzu(filepath, name=None, parent=None, verbose=True) -> Data:
else:
data = parent.create_data(**kwargs)
# array
ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rt")
arr = np.genfromtxt(f, skip_header=2, delimiter=",").T
f.close()
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_solis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ._data import Data
from .. import exceptions as wt_exceptions
from ..kit import _timestamp as timestamp
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -68,7 +69,7 @@ def from_Solis(filepath, name=None, parent=None, verbose=True) -> Data:
if not name:
name = filepath.name.split(".")[0]
# create data
ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rt")
axis0 = []
arr = []
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_spcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ._data import Data
from .. import exceptions as wt_exceptions
from ..kit import _timestamp as timestamp
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -65,7 +66,7 @@ def from_spcm(filepath, name=None, *, delimiter=",", parent=None, verbose=True)
# create headers dictionary
headers = collections.OrderedDict()
header_lines = 0
ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rt")
while True:
line = f.readline().strip()
Expand Down
3 changes: 2 additions & 1 deletion WrightTools/data/_tensor27.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ._data import Data
from .. import exceptions as wt_exceptions
from numpy.lib.npyio import DataSource


# --- define --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -70,7 +71,7 @@ def from_Tensor27(filepath, name=None, parent=None, verbose=True) -> Data:
else:
data = parent.create_data(**kwargs)
# array
ds = np.DataSource(None)
ds = DataSource(None)
f = ds.open(filestr, "rt")
arr = np.genfromtxt(f, skip_header=0).T
f.close()
Expand Down
6 changes: 3 additions & 3 deletions docs/write_from_function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ We will walk through by way of example, using :meth:`~WrightTools.data.from_JASC
else:
data = parent.create_data(**kwargs)
# array
ds = np.DataSource(None)
ds = np.lib.npyio.DataSource(None)
f = ds.open(filestr, "rt")
arr = np.genfromtxt(f, skip_header=18).T
f.close()
Expand Down Expand Up @@ -249,7 +249,7 @@ For one-dimensional data formats, this is particularly easy:
.. code-block:: python

# array
ds = np.DataSource(None)
ds = np.lib.npyio.DataSource(None)
f = ds.open(filestr, "rt")
arr = np.genfromtxt(f, skip_header=18).T
f.close()
Expand All @@ -260,7 +260,7 @@ For one-dimensional data formats, this is particularly easy:
:class:`numpy.DataSource` is a class which provides transparent decompression and remote file retrieval.
:func:`numpy.genfromtxt` will handle this itself, however it will leave the downloaded files in the
working directory, and opening explicitly allows you to use the file more directly as well.
Using ``np.DataSource(None)`` causes it to use temporary files which are removed automatically.
Using ``numpy.lib.npyio.DataSource(None)`` causes it to use temporary files which are removed automatically.
Opening in ``"rt"`` mode ensures that you are reading as text.

Parsing multidimensional datasets (and in particular formats which allow arbitrary dimensionality)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
kameyer226 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def read(fname):
with open(os.path.join(here, "WrightTools", "VERSION")) as version_file:
version = version_file.read().strip()

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

setup(
name="WrightTools",
Expand All @@ -45,7 +45,7 @@ def read(fname):
"python-dateutil",
"scipy",
"click",
"tidy_headers>=1.0.0",
"tidy_headers>=1.0.4",
"rich",
],
extras_require={
Expand Down
8 changes: 5 additions & 3 deletions tests/kit/lineshapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import WrightTools as wt

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

# --- test ----------------------------------------------------------------------------------------

Expand All @@ -16,7 +18,7 @@ def test_gaussian():
x0 = 0
FWHM = 1
y = wt.kit.gaussian(x, x0, FWHM, norm="area")
assert np.isclose(np.trapz(y, x), 1, rtol=1e-3, atol=1e-3)
assert np.isclose(trapezoid(y, x), 1, rtol=1e-3, atol=1e-3)
y = wt.kit.gaussian(x, x0, FWHM, norm="height")
assert np.isclose(y.max(), 1, rtol=1e-3, atol=1e-3)

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

Expand All @@ -36,7 +38,7 @@ def test_lorentzian_real():
x0 = 0
G = 0.1
y = wt.kit.lorentzian_real(x, x0, G, norm="area")
assert np.isclose(np.trapz(y, x), 1, rtol=1e-3, atol=1e-3)
assert np.isclose(trapezoid(y, x), 1, rtol=1e-3, atol=1e-3)
y = wt.kit.lorentzian_real(x, x0, G, norm="height")
assert np.isclose(y.max(), 1, rtol=1e-3, atol=1e-3)

Expand Down
Loading