Skip to content

Commit

Permalink
fixes for numpy2 deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Jul 1, 2024
1 parent 153147a commit e83f449
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 70 deletions.
110 changes: 48 additions & 62 deletions larch/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,68 +51,54 @@
'trunc')

# inherit these from numpy

from_numpy = ('Inf', 'NAN', 'abs', 'absolute', 'add', 'all', 'allclose',
'alltrue', 'amax', 'amin', 'angle', 'any', 'append', 'arange',
'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2',
'arctanh', 'argmax', 'argmin', 'argsort', 'argwhere', 'around',
'array', 'asanyarray', 'asarray', 'atleast_1d', 'atleast_2d',
'atleast_3d', 'average', 'bartlett', 'bincount', 'bitwise_and',
'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blackman',
'broadcast', 'ceil', 'chararray', 'choose', 'clip',
'column_stack', 'common_type', 'complex128', 'compress',
'concatenate', 'conjugate', 'convolve', 'copysign', 'corrcoef',
'correlate', 'cos', 'cosh', 'cov', 'cross', 'cumprod',
'cumproduct', 'cumsum', 'datetime_data', 'deg2rad', 'degrees',
'delete', 'diag', 'diag_indices', 'diag_indices_from',
'diagflat', 'diagonal', 'diff', 'digitize', 'divide', 'dot',
'dsplit', 'dstack', 'dtype', 'e', 'ediff1d', 'empty',
'empty_like', 'equal', 'exp', 'exp2', 'expand_dims', 'expm1',
'extract', 'eye', 'fabs', 'fft', 'fill_diagonal', 'finfo', 'fix',
'flatiter', 'flatnonzero', 'fliplr', 'flipud', 'float64',
'floor', 'floor_divide', 'fmax', 'fmin', 'fmod', 'format_parser',
'frexp', 'frombuffer', 'fromfile', 'fromfunction', 'fromiter',
'frompyfunc', 'fromregex', 'fromstring', 'genfromtxt',
'getbufsize', 'geterr', 'gradient', 'greater', 'greater_equal',
'hamming', 'hanning', 'histogram', 'histogram2d', 'histogramdd',
'hsplit', 'hstack', 'hypot', 'i0', 'identity', 'iinfo', 'imag',
'in1d', 'index_exp', 'indices', 'inexact', 'inf', 'info',
'infty', 'inner', 'insert', 'int32', 'integer', 'interp',
'intersect1d', 'invert', 'iscomplex', 'iscomplexobj', 'isfinite',
'isinf', 'isnan', 'isneginf', 'isposinf', 'isreal', 'isrealobj',
'isscalar', 'issctype', 'iterable', 'kaiser', 'kron', 'ldexp',
'left_shift', 'less', 'less_equal', 'linalg', 'linspace',
'little_endian', 'loadtxt', 'log', 'log10', 'log1p', 'log2',
'logaddexp', 'logaddexp2', 'logical_and', 'logical_not',
'logical_or', 'logical_xor', 'logspace', 'longcomplex',
'longdouble', 'longfloat', 'longlong', 'mask_indices', 'mat',
'matrix', 'max', 'maximum', 'maximum_sctype', 'may_share_memory',
'mean', 'median', 'memmap', 'meshgrid', 'mgrid', 'min',
'minimum', 'mintypecode', 'mod', 'modf', 'msort', 'multiply',
'nan', 'nan_to_num', 'nanargmax', 'nanargmin', 'nanmax',
'nanmin', 'nansum', 'nbytes', 'ndarray', 'ndenumerate', 'ndim',
'ndindex', 'negative', 'nextafter', 'nonzero', 'not_equal',
'number', 'obj2sctype', 'ogrid', 'ones', 'ones_like', 'outer',
'packbits', 'percentile', 'pi', 'piecewise', 'place', 'poly',
'poly1d', 'polyadd', 'polyder', 'polydiv', 'polyint', 'polymul',
'polynomial', 'polysub', 'polyval', 'power', 'prod', 'product',
'ptp', 'put', 'putmask', 'rad2deg', 'radians', 'random', 'ravel',
'real', 'real_if_close', 'reciprocal', 'record', 'remainder',
'repeat', 'reshape', 'resize', 'right_shift', 'rint', 'roll',
'rollaxis', 'roots', 'rot90', 'round', 'round_', 'row_stack',
'savetxt', 'savez', 'searchsorted', 'select', 'setbufsize',
'setdiff1d', 'seterr', 'setxor1d', 'shape', 'short', 'sign',
'signbit', 'signedinteger', 'sin', 'sinc', 'single',
'singlecomplex', 'sinh', 'size', 'sometrue', 'sort',
'sort_complex', 'spacing', 'split', 'sqrt', 'square', 'squeeze',
'std', 'subtract', 'sum', 'swapaxes', 'take', 'tan', 'tanh',
'tensordot', 'tile', 'trace', 'transpose', 'trapz', 'tri',
'tril', 'tril_indices', 'tril_indices_from', 'trim_zeros',
'triu', 'triu_indices', 'triu_indices_from', 'true_divide',
'trunc', 'ubyte', 'uint', 'uint32', 'union1d', 'unique',
'unpackbits', 'unravel_index', 'unsignedinteger', 'unwrap',
'ushort', 'vander', 'var', 'vdot', 'vectorize', 'vsplit',
'vstack', 'where', 'who', 'zeros', 'zeros_like')
from_numpy = ('abs', 'add', 'all', 'amax', 'amin', 'angle', 'any', 'append',
'arange', 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2',
'arctanh', 'argmax', 'argmin', 'argsort', 'argwhere', 'around', 'array',
'asarray', 'atleast_1d', 'atleast_2d', 'atleast_3d', 'average', 'bartlett',
'bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blackman',
'broadcast', 'ceil', 'choose', 'clip', 'column_stack', 'common_type',
'complex128', 'compress', 'concatenate', 'conjugate', 'convolve',
'copysign', 'corrcoef', 'correlate', 'cos', 'cosh', 'cov', 'cross',
'cumprod', 'cumsum', 'datetime_data', 'deg2rad', 'degrees', 'delete',
'diag', 'diag_indices', 'diag_indices_from', 'diagflat', 'diagonal',
'diff', 'digitize', 'divide', 'dot', 'dsplit', 'dstack', 'dtype', 'e',
'ediff1d', 'empty', 'empty_like', 'equal', 'exp', 'exp2', 'expand_dims',
'expm1', 'extract', 'eye', 'fabs', 'fft', 'fill_diagonal', 'finfo', 'fix',
'flatiter', 'flatnonzero', 'fliplr', 'flipud', 'float64', 'floor',
'floor_divide', 'fmax', 'fmin', 'fmod', 'format_parser', 'frexp',
'frombuffer', 'fromfile', 'fromfunction', 'fromiter', 'frompyfunc',
'fromregex', 'fromstring', 'genfromtxt', 'getbufsize', 'geterr',
'gradient', 'greater', 'greater_equal', 'hamming', 'hanning', 'histogram',
'histogram2d', 'histogramdd', 'hsplit', 'hstack', 'hypot', 'i0',
'identity', 'iinfo', 'imag', 'indices', 'inexact', 'inf', 'info', 'inner',
'insert', 'int32', 'integer', 'interp', 'intersect1d', 'invert',
'iscomplex', 'iscomplexobj', 'isfinite', 'isinf', 'isnan', 'isneginf',
'isposinf', 'isreal', 'isrealobj', 'isscalar', 'iterable', 'kaiser',
'kron', 'ldexp', 'left_shift', 'less', 'less_equal', 'linalg', 'linspace',
'little_endian', 'loadtxt', 'log', 'log10', 'log1p', 'log2', 'logaddexp',
'logaddexp2', 'logical_and', 'logical_not', 'logical_or', 'logical_xor',
'logspace', 'longdouble', 'longlong', 'mask_indices', 'matrix', 'maximum',
'may_share_memory', 'mean', 'median', 'memmap', 'meshgrid', 'minimum',
'mintypecode', 'mod', 'modf', 'msort', 'multiply', 'nan', 'nan_to_num',
'nanargmax', 'nanargmin', 'nanmax', 'nanmin', 'nansum', 'ndarray',
'ndenumerate', 'ndim', 'ndindex', 'negative', 'nextafter', 'nonzero',
'not_equal', 'number', 'ones', 'ones_like', 'outer', 'packbits',
'percentile', 'pi', 'piecewise', 'place', 'poly', 'poly1d', 'polyadd',
'polyder', 'polydiv', 'polyint', 'polymul', 'polynomial', 'polysub',
'polyval', 'power', 'prod', 'ptp', 'put', 'putmask', 'rad2deg', 'radians',
'random', 'ravel', 'real', 'real_if_close', 'reciprocal', 'record',
'remainder', 'repeat', 'reshape', 'resize', 'right_shift', 'rint', 'roll',
'rollaxis', 'roots', 'rot90', 'round', 'searchsorted', 'select',
'setbufsize', 'setdiff1d', 'seterr', 'setxor1d', 'shape', 'short', 'sign',
'signbit', 'signedinteger', 'sin', 'sinc', 'single', 'sinh', 'size',
'sort', 'sort_complex', 'spacing', 'split', 'sqrt', 'square', 'squeeze',
'std', 'subtract', 'sum', 'swapaxes', 'take', 'tan', 'tanh', 'tensordot',
'tile', 'trace', 'transpose', 'tri', 'tril', 'tril_indices',
'tril_indices_from', 'trim_zeros', 'triu', 'triu_indices',
'triu_indices_from', 'true_divide', 'trunc', 'ubyte', 'uint', 'uint32',
'union1d', 'unique', 'unravel_index', 'unsignedinteger', 'unwrap',
'ushort', 'vander', 'var', 'vdot', 'vectorize', 'vsplit', 'vstack',
'where', 'zeros', 'zeros_like')

numpy_renames = {'ln':'log', 'asin':'arcsin', 'acos':'arccos',
'atan':'arctan', 'atan2':'arctan2', 'atanh':'arctanh',
Expand Down
2 changes: 1 addition & 1 deletion larch/math/peaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def peak_indices(y, threshold=0.1, min_dist=1):
ndarray
Array with numeric indexes of the peaks that were detected.
"""
thres = threshold * y.ptp()+ y.min()
thres = threshold * np.ptp(y) + y.min()
min_dist = int(min_dist)

# compute first order difference
Expand Down
2 changes: 1 addition & 1 deletion larch/math/tomography.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def ensure_radians(a):
"""ensure angle data is in radians, not degrees,
converts degrees to radians if a peak-to-peak > 32 or step size > 0.2
"""
if a.ptp() > 32 or np.diff(a).mean() > 0.20:
if np.ptp(a) > 32 or np.diff(a).mean() > 0.20:
a = np.radians(a)
return a

Expand Down
2 changes: 1 addition & 1 deletion larch/wxlib/columnframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ def onOK(self, event=None):
buff.append("{group}.mu = {group}.ydat")
buff.append("sort_xafs({group}, overwrite=True, fix_repeats=True)")
else:
buff.append("{group}.scale = 1./({group}.ydat.ptp()+1.e-15)")
buff.append("{group}.scale = 1./(ptp({group}.ydat)+1.e-15)")

array_desc = dict(xdat=self.workgroup.plot_xlabel,
ydat=self.workgroup.plot_ylabel,
Expand Down
2 changes: 1 addition & 1 deletion larch/wxlib/specfile_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def onOK(self, event=None):
buff.append("{group}.mu = {group}.ydat")
buff.append("sort_xafs({group}, overwrite=True, fix_repeats=True)")
else:
buff.append("{group}.scale = 1./({group}.ydat.ptp()+1.e-16)")
buff.append("{group}.scale = 1./(ptp({group}.ydat.ptp)+1.e-16)")
script = "\n".join(buff)

self.config['array_desc'] = dict(xdat=self.workgroup.plot_xlabel,
Expand Down
2 changes: 1 addition & 1 deletion larch/wxlib/xas_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ def onOK(self, event=None):
buff.append("{group}.mu = {group}.ydat")
buff.append("sort_xafs({group}, overwrite=True, fix_repeats=True)")
else:
buff.append("{group}.scale = 1./({group}.ydat.ptp()+1.e-16)")
buff.append("{group}.scale = 1./(ptp({group}.ydat))+1.e-16)")
script = "\n".join(buff)

self.array_sel["xarr"] = xarr
Expand Down
4 changes: 2 additions & 2 deletions larch/wxlib/xrfdisplay_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,6 @@ def get_mean(self):

def get_cps(self):
valid = np.where(self.times > 0)[0]
if len(valid) < 1 or self.times[valid].ptp() < 0.5:
if len(valid) < 1 or np.ptp(self.times[valid]) < 0.5:
return 0
return self.data[valid].sum() / self.times[valid].ptp()
return self.data[valid].sum() / np.ptp(self.times[valid])
2 changes: 1 addition & 1 deletion larch/xafs/pre_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _finde0(energy, mu_input, estep=None, use_smooth=True):
# find points of high derivative
dmu[np.where(~np.isfinite(dmu))] = -1.0
dm_min = dmu[nmin:-nmin].min()
dm_ptp = max(1.e-10, dmu[nmin:-nmin].ptp())
dm_ptp = max(1.e-10, np.ptp(dmu[nmin:-nmin]))
dmu = (dmu - dm_min)/dm_ptp

dhigh = 0.60 if len(en) > 20 else 0.30
Expand Down

0 comments on commit e83f449

Please sign in to comment.