Skip to content

Commit 57cddff

Browse files
committed
Error at runtime if zfpy isn't present
1 parent 9bdbaf0 commit 57cddff

File tree

2 files changed

+104
-105
lines changed

2 files changed

+104
-105
lines changed

numcodecs/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,9 @@
125125
register_codec(Fletcher32)
126126

127127
# Optional depenedencies
128-
with suppress(ImportError):
129-
from numcodecs.zfpy import ZFPY
128+
from numcodecs.zfpy import ZFPY
130129

131-
register_codec(ZFPY)
130+
register_codec(ZFPY)
132131

133132
with suppress(ImportError):
134133
from numcodecs.msgpacks import MsgPack

numcodecs/zfpy.py

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,113 @@
1-
import warnings
2-
from contextlib import suppress
3-
from importlib.metadata import PackageNotFoundError, version
1+
import importlib.util
2+
from importlib.metadata import version
43
from types import ModuleType
54
from typing import Optional
65

7-
_zfpy: Optional[ModuleType] = None
6+
import numpy as np
87

9-
_zfpy_version: tuple = ()
10-
with suppress(PackageNotFoundError):
11-
_zfpy_version = tuple(map(int, version("zfpy").split(".")))
8+
from .abc import Codec
9+
from .compat import ensure_bytes, ensure_contiguous_ndarray, ndarray_copy
10+
11+
_zfpy: Optional[ModuleType]
12+
_zfpy_spec = importlib.util.find_spec("zfpy")
13+
14+
if _zfpy_spec is None:
15+
_zfpy = None
16+
else:
17+
import zfpy as _zfpy # type: ignore[no-redef]
1218

13-
if _zfpy_version:
14-
# Check NumPy version
15-
_numpy_version: tuple = tuple(map(int, version("numpy").split('.')))
19+
20+
def _check_compatible_numpy() -> None:
21+
_numpy_version = tuple(map(int, version("numpy").split('.')))
22+
_zfpy_version = tuple(map(int, version("zfpy").split(".")))
1623
if _numpy_version >= (2, 0, 0) and _zfpy_version <= (1, 0, 1): # pragma: no cover
1724
_zfpy_version = ()
18-
warnings.warn(
25+
raise RuntimeError(
1926
"NumPy version >= 2.0.0 detected. The zfpy library is incompatible with this version of NumPy. "
2027
"Please downgrade to NumPy < 2.0.0 or wait for an update from zfpy.",
21-
UserWarning,
22-
stacklevel=2,
2328
)
24-
else:
25-
with suppress(ImportError):
26-
import zfpy as _zfpy # type: ignore[no-redef]
27-
28-
if _zfpy:
29-
import numpy as np
30-
31-
from .abc import Codec
32-
from .compat import ensure_bytes, ensure_contiguous_ndarray, ndarray_copy
33-
34-
# noinspection PyShadowingBuiltins
35-
class ZFPY(Codec):
36-
"""Codec providing compression using zfpy via the Python standard
37-
library.
38-
39-
Parameters
40-
----------
41-
mode : integer
42-
One of the zfpy mode choice, e.g., ``zfpy.mode_fixed_accuracy``.
43-
tolerance : double, optional
44-
A double-precision number, specifying the compression accuracy needed.
45-
rate : double, optional
46-
A double-precision number, specifying the compression rate needed.
47-
precision : int, optional
48-
A integer number, specifying the compression precision needed.
49-
50-
"""
51-
52-
codec_id = "zfpy"
53-
54-
def __init__(
55-
self,
56-
mode=_zfpy.mode_fixed_accuracy,
57-
tolerance=-1,
58-
rate=-1,
59-
precision=-1,
60-
compression_kwargs=None,
61-
):
62-
self.mode = mode
63-
if mode == _zfpy.mode_fixed_accuracy:
64-
self.compression_kwargs = {"tolerance": tolerance}
65-
elif mode == _zfpy.mode_fixed_rate:
66-
self.compression_kwargs = {"rate": rate}
67-
elif mode == _zfpy.mode_fixed_precision:
68-
self.compression_kwargs = {"precision": precision}
69-
70-
self.tolerance = tolerance
71-
self.rate = rate
72-
self.precision = precision
73-
74-
def encode(self, buf):
75-
# not flatten c-order array and raise exception for f-order array
76-
if not isinstance(buf, np.ndarray):
77-
raise TypeError(
78-
"The zfp codec does not support none numpy arrays."
79-
f" Your buffers were {type(buf)}."
80-
)
81-
if buf.flags.c_contiguous:
82-
flatten = False
83-
else:
84-
raise ValueError(
85-
"The zfp codec does not support F order arrays. "
86-
f"Your arrays flags were {buf.flags}."
87-
)
88-
buf = ensure_contiguous_ndarray(buf, flatten=flatten)
89-
90-
# do compression
91-
return _zfpy.compress_numpy(buf, write_header=True, **self.compression_kwargs)
92-
93-
def decode(self, buf, out=None):
94-
# normalise inputs
95-
buf = ensure_bytes(buf)
96-
if out is not None:
97-
out = ensure_contiguous_ndarray(out)
98-
99-
# do decompression
100-
dec = _zfpy.decompress_numpy(buf)
101-
102-
# handle destination
103-
if out is not None:
104-
return ndarray_copy(dec, out)
105-
else:
106-
return dec
107-
108-
def __repr__(self):
109-
return (
110-
f"{type(self).__name__}(mode={self.mode!r}, "
111-
f"tolerance={self.tolerance}, rate={self.rate}, "
112-
f"precision={self.precision})"
29+
30+
31+
class ZFPY(Codec):
32+
"""Codec providing compression using zfpy via the Python standard
33+
library.
34+
35+
Parameters
36+
----------
37+
mode : integer
38+
One of the zfpy mode choice, e.g., ``zfpy.mode_fixed_accuracy``.
39+
tolerance : double, optional
40+
A double-precision number, specifying the compression accuracy needed.
41+
rate : double, optional
42+
A double-precision number, specifying the compression rate needed.
43+
precision : int, optional
44+
A integer number, specifying the compression precision needed.
45+
46+
"""
47+
48+
codec_id = "zfpy"
49+
50+
def __init__(
51+
self,
52+
mode=_zfpy.mode_fixed_accuracy,
53+
tolerance=-1,
54+
rate=-1,
55+
precision=-1,
56+
compression_kwargs=None,
57+
):
58+
if _zfpy is None:
59+
raise RuntimeError("The ZFPY codec requires the 'zfpy' package to be installed.")
60+
_check_compatible_numpy()
61+
62+
self.mode = mode
63+
if mode == _zfpy.mode_fixed_accuracy:
64+
self.compression_kwargs = {"tolerance": tolerance}
65+
elif mode == _zfpy.mode_fixed_rate:
66+
self.compression_kwargs = {"rate": rate}
67+
elif mode == _zfpy.mode_fixed_precision:
68+
self.compression_kwargs = {"precision": precision}
69+
70+
self.tolerance = tolerance
71+
self.rate = rate
72+
self.precision = precision
73+
74+
def encode(self, buf):
75+
# not flatten c-order array and raise exception for f-order array
76+
if not isinstance(buf, np.ndarray):
77+
raise TypeError(
78+
"The zfp codec does not support none numpy arrays."
79+
f" Your buffers were {type(buf)}."
11380
)
81+
if buf.flags.c_contiguous:
82+
flatten = False
83+
else:
84+
raise ValueError(
85+
"The zfp codec does not support F order arrays. "
86+
f"Your arrays flags were {buf.flags}."
87+
)
88+
buf = ensure_contiguous_ndarray(buf, flatten=flatten)
89+
90+
# do compression
91+
return _zfpy.compress_numpy(buf, write_header=True, **self.compression_kwargs)
92+
93+
def decode(self, buf, out=None):
94+
# normalise inputs
95+
buf = ensure_bytes(buf)
96+
if out is not None:
97+
out = ensure_contiguous_ndarray(out)
98+
99+
# do decompression
100+
dec = _zfpy.decompress_numpy(buf)
101+
102+
# handle destination
103+
if out is not None:
104+
return ndarray_copy(dec, out)
105+
else:
106+
return dec
107+
108+
def __repr__(self):
109+
return (
110+
f"{type(self).__name__}(mode={self.mode!r}, "
111+
f"tolerance={self.tolerance}, rate={self.rate}, "
112+
f"precision={self.precision})"
113+
)

0 commit comments

Comments
 (0)