Skip to content

Commit f38891b

Browse files
committed
Fix Codec.__init__
1 parent 23d1800 commit f38891b

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

numcodecs/bitinfo.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ class BitInfo(BitRound):
2323
Parameters
2424
----------
2525
26-
inflevel: float
27-
The number of bits of the mantissa to keep. The range allowed
28-
depends on the dtype input data. If keepbits is
29-
equal to the maximum allowed for the data type, this is equivalent
30-
to no transform.
26+
info_level: float
27+
The level of information to preserve in the data. The value should be
28+
between 0. and 1.0. Higher values preserve more information.
3129
3230
axes: int or list of int, optional
3331
Axes along which to calculate the bit information. If None, all axes
@@ -36,13 +34,22 @@ class BitInfo(BitRound):
3634

3735
codec_id = 'bitinfo'
3836

39-
def __init__(self, inflevel: float, axes=None):
40-
if (inflevel < 0) or (inflevel > 1.0):
41-
raise ValueError("Please provide `inflevel` from interval [0.,1.]")
37+
def __init__(self, info_level: float, axes=None):
38+
if (info_level < 0) or (info_level > 1.0):
39+
raise ValueError("Please provide `info_level` from interval [0.,1.]")
4240

43-
self.inflevel = inflevel
41+
elif axes is not None and not isinstance(axes, list):
42+
if int(axes) != axes:
43+
raise ValueError("axis must be an integer or a list of integers.")
44+
axes = [axes]
45+
46+
elif isinstance(axes, list) and not all(int(ax) == ax for ax in axes):
47+
raise ValueError("axis must be an integer or a list of integers.")
48+
49+
self.info_level = info_level
4450
self.axes = axes
4551

52+
4653
def encode(self, buf):
4754
"""Create int array by rounding floating-point data
4855
@@ -68,11 +75,11 @@ def encode(self, buf):
6875

6976
for ax in self.axes:
7077
info_per_bit = bitinformation(a, axis=ax)
71-
keepbits.append(get_keepbits(info_per_bit, self.inflevel))
78+
keepbits.append(get_keepbits(info_per_bit, self.info_level))
7279

7380
keepbits = max(keepbits)
7481

75-
return BitRound._bitround(a, keepbits, dtype)
82+
return BitRound.bitround(buf, keepbits, dtype)
7683

7784

7885
def exponent_bias(dtype):
@@ -117,12 +124,12 @@ def signed_exponent(A):
117124
118125
Parameters
119126
----------
120-
A : :py:class:`numpy.array`
127+
a : array
121128
Array to transform
122129
123130
Returns
124131
-------
125-
B : :py:class:`numpy.array`
132+
array
126133
127134
Example
128135
-------
@@ -162,8 +169,7 @@ def signed_exponent(A):
162169
eabs = np.uint64(eabs)
163170
esign = np.uint64(esign)
164171
esigned = esign | (eabs << sbits)
165-
B = (sf | esigned).view(np.int64)
166-
return B
172+
return (sf | esigned).view(np.int64)
167173

168174

169175
def bitpaircount_u1(a, b):
@@ -260,7 +266,8 @@ def get_keepbits(info_per_bit, inflevel=0.99):
260266

261267
def _cdf_from_info_per_bit(info_per_bit):
262268
"""Convert info_per_bit to cumulative distribution function"""
263-
tol = info_per_bit[-4:].max() * 1.5
264-
info_per_bit[info_per_bit < tol] = 0
269+
# TODO this threshold isn't working yet
270+
#tol = info_per_bit[-4:].max() * 1.5
271+
#info_per_bit[info_per_bit < tol] = 0
265272
cdf = info_per_bit.cumsum()
266273
return cdf / cdf[-1]

numcodecs/bitround.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,21 @@ def encode(self, buf):
5959
if self.keepbits > bits:
6060
raise ValueError("Keepbits too large for given dtype")
6161

62-
return self._bitround(a, self.keepbits, a.dtype)
62+
return self.bitround(a, self.keepbits, a.dtype)
63+
64+
def decode(self, buf, out=None):
65+
"""Remake floats from ints
66+
67+
As with ``encode``, preserves itemsize.
68+
"""
69+
buf = ensure_ndarray_like(buf)
70+
# Cast back from `int` to `float` type (noop if a `float`ing type buffer is provided)
71+
dt = np.dtype(buf.dtype.str.replace("i", "f"))
72+
data = buf.view(dt)
73+
return ndarray_copy(data, out)
6374

6475
@staticmethod
65-
def _bitround(buf, keepbits, dtype):
76+
def bitround(buf, keepbits, dtype):
6677
bits = max_bits[str(dtype)]
6778
a_int_dtype = np.dtype(buf.dtype.str.replace("f", "i"))
6879
all_set = np.array(-1, dtype=a_int_dtype)
@@ -73,14 +84,3 @@ def _bitround(buf, keepbits, dtype):
7384
b += ((b >> maskbits) & 1) + half_quantum1
7485
b &= mask
7586
return b
76-
77-
def decode(self, buf, out=None):
78-
"""Remake floats from ints
79-
80-
As with ``encode``, preserves itemsize.
81-
"""
82-
buf = ensure_ndarray_like(buf)
83-
# Cast back from `int` to `float` type (noop if a `float`ing type buffer is provided)
84-
dt = np.dtype(buf.dtype.str.replace("i", "f"))
85-
data = buf.view(dt)
86-
return ndarray_copy(data, out)

0 commit comments

Comments
 (0)