Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/transform/utils.py: 79%
28 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-14 02:13 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-14 02:13 +0000
1import numpy
4def argsort(arr, axis=0):
5 """Returns index that sorts arrays
7 :param array arr:
8 :returns tuple(list(int)):
9 """
10 idx = list(numpy.ix_(*[numpy.arange(i) for i in arr.shape]))
11 idx[axis] = arr.argsort(axis)
12 return tuple(idx)
15def isDiagonal(arr):
16 """Checks whether an array is diagonal
18 :param array arr:
19 """
20 return numpy.count_nonzero(arr - numpy.diag(numpy.diagonal(arr))) == 0
23def dtypeRange(dtype):
24 """Data length of dtype
26 :param dtype:
27 :returns 2-tuple: min and max
28 """
29 dtype = numpy.array(0, dtype=dtype).dtype
30 try:
31 dinfo = numpy.finfo(dtype)
32 except ValueError:
33 try:
34 dinfo = numpy.iinfo(dtype)
35 except ValueError:
36 return False, True
37 return dinfo.min, dinfo.max
40def makeFinite(arr, maxnum=None):
41 """Make isFinite in-place
43 :param num or array-like array arr:
44 :param num maxnum:
45 :returns array: same if input was ndarray
46 """
47 arr = numpy.asarray(arr, dtype="<f4")
48 if maxnum is None:
49 dmin, dmax = dtypeRange(arr.dtype)
50 else:
51 maxnum = numpy.asarray(maxnum, arr.dtype).item()
52 dmin, dmax = -abs(maxnum), abs(maxnum)
53 msk = arr == numpy.inf
54 arr[msk] = dmax
55 msk = arr == -numpy.inf
56 arr[msk] = dmin
57 return arr