Skip to content

Commit a2faeb5

Browse files
committed
Add usage example
1 parent 3e8861c commit a2faeb5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

numcodecs/bitinfo.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ class BitInfo(BitRound):
3030
axes: int or list of int, optional
3131
Axes along which to calculate the bit information. If None, all axes
3232
are used.
33+
34+
Examples
35+
--------
36+
>>> import xarray as xr
37+
>>> ds = xr.tutorial.open_dataset("air_temperature")
38+
>>> # Note these data have already undergone lossy compression,
39+
>>> # which should not be combined with bitinformation in practice
40+
41+
>>> from numcodecs import Blosc, BitInfo
42+
>>> compressor = Blosc(cname="zstd", clevel=3)
43+
>>> filters = [BitInfo(info_level=0.99)]
44+
>>> encoding = {"air": {"compressor": compressor, "filters": filters}}
45+
>>> ds.to_zarr('xbit.zarr', mode="w", encoding=encoding)
3346
"""
3447

3548
codec_id = 'bitinfo'
@@ -204,8 +217,7 @@ def bitpaircount(a, b):
204217

205218

206219
def mutual_information(a, b, base=2):
207-
"""Calculate the mutual information between two arrays.
208-
"""
220+
"""Calculate the mutual information between two arrays."""
209221
assert a.dtype == b.dtype
210222
assert a.dtype.kind == "u"
211223

@@ -265,9 +277,7 @@ def get_keepbits(info_per_bit, inflevel=0.99):
265277

266278
cdf = _cdf_from_info_per_bit(info_per_bit)
267279
bitdim_non_mantissa_bits = NMBITS[len(info_per_bit)]
268-
keepmantissabits = (
269-
(cdf > inflevel).argmax() + 1 - bitdim_non_mantissa_bits
270-
)
280+
keepmantissabits = (cdf > inflevel).argmax() + 1 - bitdim_non_mantissa_bits
271281

272282
return keepmantissabits
273283

0 commit comments

Comments
 (0)