From d07e89702c74bb9a41a3c55183adb41952d49f26 Mon Sep 17 00:00:00 2001 From: Gavin Burnell Date: Wed, 9 Oct 2024 18:04:40 +0100 Subject: [PATCH] First pass at fixes for numpy versiuon 2.0+ --- Stoner/Analysis.py | 5 +++- Stoner/Core.py | 2 +- Stoner/Image/core.py | 47 ++++++++++++++++++++---------- Stoner/Image/util.py | 3 +- Stoner/Util.py | 4 +-- Stoner/compat.py | 5 ++-- Stoner/core/base.py | 4 +-- Stoner/core/utils.py | 4 +-- Stoner/formats/data/instruments.py | 2 +- Stoner/formats/instruments.py | 2 +- recipe/meta.yaml | 8 ++--- requirements.txt | 2 +- 12 files changed, 55 insertions(+), 33 deletions(-) diff --git a/Stoner/Analysis.py b/Stoner/Analysis.py index c22ea08de..64c8e74f5 100755 --- a/Stoner/Analysis.py +++ b/Stoner/Analysis.py @@ -5,7 +5,10 @@ import numpy as np import numpy.ma as ma -from scipy.integrate import cumtrapz +try: + from scipy.integrate import cumtrapz +except ImportError: + from scipy.integrate import cumulative_trapezoid as cumtrapz from scipy.optimize import curve_fit from .tools import isiterable, isTuple diff --git a/Stoner/Core.py b/Stoner/Core.py index a75ba7392..8caa0395b 100755 --- a/Stoner/Core.py +++ b/Stoner/Core.py @@ -26,7 +26,7 @@ import csv import numpy as np -from numpy import NaN # NOQA pylint: disable=unused-import +from numpy import nan # NOQA pylint: disable=unused-import from numpy import ma from .compat import string_types, int_types, index_types, _pattern_type, path_types diff --git a/Stoner/Image/core.py b/Stoner/Image/core.py index b40bd4f87..024cdadcd 100755 --- a/Stoner/Image/core.py +++ b/Stoner/Image/core.py @@ -27,6 +27,7 @@ transform, ) +from ..compat import np_version from ..core.base import typeHintedDict, metadataObject from ..core.exceptions import StonerLoadError, StonerUnrecognisedFormat from ..Core import DataFile @@ -46,21 +47,37 @@ IMAGE_FILES = [("Tiff File", "*.tif;*.tiff"), ("PNG files", "*.png", "Numpy Files", "*.npy")] -dtype_range = { - np.bool_: (False, True), - np.bool8: (False, True), - np.uint8: (0, 255), - np.uint16: (0, 65535), - np.int8: (-128, 127), - np.int16: (-32768, 32767), - np.int64: (-(2**63), 2**63 - 1), - np.uint64: (0, 2**64 - 1), - np.int32: (-(2**31), 2**31 - 1), - np.uint32: (0, 2**32 - 1), - np.float16: (-1, 1), - np.float32: (-1, 1), - np.float64: (-1, 1), -} +if np_version.major == 1 and np_version.minor < 24: + dtype_range = { + np.bool_: (False, True), + np.bool8: (False, True), + np.uint8: (0, 255), + np.uint16: (0, 65535), + np.int8: (-128, 127), + np.int16: (-32768, 32767), + np.int64: (-(2**63), 2**63 - 1), + np.uint64: (0, 2**64 - 1), + np.int32: (-(2**31), 2**31 - 1), + np.uint32: (0, 2**32 - 1), + np.float16: (-1, 1), + np.float32: (-1, 1), + np.float64: (-1, 1), + } +else: + dtype_range = { + np.bool_: (False, True), + np.uint8: (0, 255), + np.uint16: (0, 65535), + np.int8: (-128, 127), + np.int16: (-32768, 32767), + np.int64: (-(2**63), 2**63 - 1), + np.uint64: (0, 2**64 - 1), + np.int32: (-(2**31), 2**31 - 1), + np.uint32: (0, 2**32 - 1), + np.float16: (-1, 1), + np.float32: (-1, 1), + np.float64: (-1, 1), + } def _add_core_(result, other): diff --git a/Stoner/Image/util.py b/Stoner/Image/util.py index c6857cc79..f0f88d955 100755 --- a/Stoner/Image/util.py +++ b/Stoner/Image/util.py @@ -9,6 +9,7 @@ import numpy as np from ..tools.classes import Options +from ..compat import np_version dtype_range = { np.bool_: (False, True), @@ -24,7 +25,7 @@ np.float64: (-1.0, 1.0), } -if float(np.version.version.split(".")[1]) < 1.24: +if np_version.major==1 and np_version.minon<24: dtype_range[np.bool8] = (False, True) integer_types = (np.uint8, np.uint16, np.int8, np.int16) diff --git a/Stoner/Util.py b/Stoner/Util.py index c9a789bed..6580c8464 100755 --- a/Stoner/Util.py +++ b/Stoner/Util.py @@ -79,7 +79,7 @@ def _h_sat_susceptibility(d, i, Ms_vals, Hsat_vals, h_sat_fraction): # pylint: if hs.size > 1: Hsat_err[1 - i] = sem(hs) else: - Hsat_err[1 - i] = np.NaN + Hsat_err[1 - i] = np.nan return (Hsat, Hsat_err) @@ -95,7 +95,7 @@ def _h_sat_delta_M(d, i, Ms_vals, Hsat_vals, h_sat_fraction): # pylint: disable if hs.size > 1: hs = hs[np.argmin(np.abs(hs))] Hsat[1 - i] = hs # Get the H_sat value - Hsat_err[1 - i] = np.NaN + Hsat_err[1 - i] = np.nan return (Hsat, Hsat_err) diff --git a/Stoner/compat.py b/Stoner/compat.py index c0c8dacc0..6a4b22fb0 100755 --- a/Stoner/compat.py +++ b/Stoner/compat.py @@ -130,8 +130,9 @@ def get_filedialog(what="file", **opts): raise RuntimeError(f"Unable to recognise required file dialog type:{what}") return fileDialog.openDialog(mode=funcs[what], **opts) - -if np_version.minor >= 20: +if np_version.major >= 2: + int_types += (int, np.int8, np.int16, np.int32, np.int64) +elif np_version.minor >= 20: int_types += (int, np.int0, np.int8, np.int16, np.int32, np.int64) else: int_types += (np.int, np.int0, np.int8, np.int16, np.int32, np.int64) diff --git a/Stoner/core/base.py b/Stoner/core/base.py index dee453246..47afb59f3 100755 --- a/Stoner/core/base.py +++ b/Stoner/core/base.py @@ -24,7 +24,7 @@ from dateutil import parser import numpy as np -from numpy import NaN +from numpy import nan import asteval try: @@ -70,7 +70,7 @@ def literal_eval(string: str) -> Any: global _asteval_interp # pylint: disable=W0603 if _asteval_interp is None: _asteval_interp = asteval.Interpreter( - usersyms={"np": np, "re": re, "NaN": NaN, "nan": NaN, "None": None, "datetime": datetime} + usersyms={"np": np, "re": re, "NaN": nan, "nan": nan, "None": None, "datetime": datetime} ) try: return _asteval_interp(string, show_errors=False) diff --git a/Stoner/core/utils.py b/Stoner/core/utils.py index f1c61032c..90f50c32b 100755 --- a/Stoner/core/utils.py +++ b/Stoner/core/utils.py @@ -80,11 +80,11 @@ def add_core(other: Union["DataFile", np.ndarray, List[Numeric], MappingType], n order[k] = newdata.find_col(k) except (KeyError, re.error): mask = newdata.mask - newdata.add_column(np.ones(len(newdata)) * np.NaN, header=k) + newdata.add_column(np.ones(len(newdata)) * np.nan, header=k) newdata.mask[:, :-1] = mask newdata.mask[:, -1] = np.ones(len(newdata), dtype=bool) order[k] = newdata.shape[1] - 1 - row = np.ones(newdata.shape[1]) * np.NaN + row = np.ones(newdata.shape[1]) * np.nan mask = np.ones_like(row, dtype=bool) for k in order: row[order[k]] = other[k] diff --git a/Stoner/formats/data/instruments.py b/Stoner/formats/data/instruments.py index dbd2648f4..d2f091573 100755 --- a/Stoner/formats/data/instruments.py +++ b/Stoner/formats/data/instruments.py @@ -193,7 +193,7 @@ def load_qdfile(new_data, filename=None, *args, **kargs): if data.shape[0] == 0: raise StonerLoadError("No data in file!") if data.shape[1] < len(column_headers): # Trap for buggy QD software not giving ewnough columns of data - data = np.append(data, np.ones((data.shape[0], len(column_headers) - data.shape[1])) * np.NaN, axis=1) + data = np.append(data, np.ones((data.shape[0], len(column_headers) - data.shape[1])) * np.nan, axis=1) elif data.shape[1] > len(column_headers): # too much data data = data[:, : len(column_headers) - data.shape[1]] new_data.data = data diff --git a/Stoner/formats/instruments.py b/Stoner/formats/instruments.py index 4e7295233..2d1f20ea5 100755 --- a/Stoner/formats/instruments.py +++ b/Stoner/formats/instruments.py @@ -223,7 +223,7 @@ def _load(self, filename=None, *args, **kargs): if data.shape[0] == 0: raise Core.StonerLoadError("No data in file!") if data.shape[1] < len(column_headers): # Trap for buggy QD software not giving ewnough columns of data - data = np.append(data, np.ones((data.shape[0], len(column_headers) - data.shape[1])) * np.NaN, axis=1) + data = np.append(data, np.ones((data.shape[0], len(column_headers) - data.shape[1])) * np.nan, axis=1) elif data.shape[1] > len(column_headers): # too much data data = data[:, : len(column_headers) - data.shape[1]] self.data = data diff --git a/recipe/meta.yaml b/recipe/meta.yaml index 340c9ac30..134e4e87f 100755 --- a/recipe/meta.yaml +++ b/recipe/meta.yaml @@ -17,13 +17,13 @@ build: requirements: build: - - python >=3.7 + - python >=3.9 - pytest - pytest-runner run: - - python >=3.7 - - scipy>1.7 + - python >=3.9 + - scipy >1.7 - numpy >=1.18 - matplotlib >=3.0 - scikit-image >=0.17 @@ -33,7 +33,7 @@ requirements: - image-registration >=0.2.1 - lmfit >=0.9.7 - memoization >=0.1.4 - - npTDMS >=0.11 + - npTDMS >=1.9.0 - python-dateutil >=2.7.0 - statsmodels - tabulate >=0.8 diff --git a/requirements.txt b/requirements.txt index f71110681..aa616d17c 100755 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ filemagic>=1.6 image-registration>=0.2.1 lmfit>=0.9.7 memoization>=0.1.4 -npTDMS>=0.11 +npTDMS>=1.9.0 python-dateutil>=2.7.0 statsmodels tabulate>=0.8