Skip to content

Commit

Permalink
First pass at fixes for numpy versiuon 2.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
gb119 committed Oct 9, 2024
1 parent de0bdbb commit d07e897
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 33 deletions.
5 changes: 4 additions & 1 deletion Stoner/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Stoner/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 32 additions & 15 deletions Stoner/Image/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion Stoner/Image/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np

from ..tools.classes import Options
from ..compat import np_version

dtype_range = {
np.bool_: (False, True),
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Stoner/Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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)


Expand Down
5 changes: 3 additions & 2 deletions Stoner/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Stoner/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from dateutil import parser
import numpy as np
from numpy import NaN
from numpy import nan
import asteval

try:
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Stoner/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Stoner/formats/data/instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Stoner/formats/instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d07e897

Please sign in to comment.