Skip to content

Commit f4196a4

Browse files
authored
Introduce a flat option to ensure_contiguous_ndarray to switch off flatten for ZFPY codec (#307)
* Optional flatten the encode array * Remove skipping Python 3.9 and later for Zfp * Updated docs/release.rst about ZFPY * Removed the trailing whitespace * Add pip install zfpy to start test_zfpy * Add raise valueerror * Shorten the raise error lines * Modified ci-osx clang to 12.0.1 * Revert the change in ci-osx.yaml * Update release.rst * Change flat to flatten * Missed one flat, changed to flatten * Change flat to flatten in zfpy.py
1 parent 3229a74 commit f4196a4

File tree

8 files changed

+48
-9
lines changed

8 files changed

+48
-9
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __getattr__(cls, name):
2424
return Mock()
2525

2626

27-
MOCK_MODULES = ['msgpack', 'zfpy']
27+
MOCK_MODULES = ['msgpack']
2828
sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)
2929

3030

docs/release.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Release notes
55

66
Unreleased
77
----------
8+
* Fix a flatten array error for ZFPY, ZFPY codec is supported on Python 3.9
9+
and 3.10 on Linux and MacOS, the docs about ZFPY is also available.
10+
By :user:`Haiying Xu <halehawk>`, `John Kirkham <jakirkham>`, `Ryan Abernathey <rabernat>` :
11+
issue:`303`.
812

913
.. _release_0.9.1:
1014

numcodecs/compat.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def ensure_ndarray(buf):
5050
return arr
5151

5252

53-
def ensure_contiguous_ndarray(buf, max_buffer_size=None):
53+
def ensure_contiguous_ndarray(buf, max_buffer_size=None, flatten=True):
5454
"""Convenience function to coerce `buf` to a numpy array, if it is not already a
5555
numpy array. Also ensures that the returned value exports fully contiguous memory,
5656
and supports the new-style buffer interface. If the optional max_buffer_size is
@@ -91,8 +91,10 @@ def ensure_contiguous_ndarray(buf, max_buffer_size=None):
9191

9292
# check memory is contiguous, if so flatten
9393
if arr.flags.c_contiguous or arr.flags.f_contiguous:
94-
# can flatten without copy
95-
arr = arr.reshape(-1, order='A')
94+
# check if flatten flag is on or not
95+
if flatten:
96+
# can flatten without copy
97+
arr = arr.reshape(-1, order='A')
9698

9799
else:
98100
raise ValueError('an array with contiguous memory is required')

numcodecs/tests/test_zfpy.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
np.random.normal(loc=1000, scale=1, size=(100, 10)),
3939
np.random.normal(loc=1000, scale=1, size=(10, 10, 10)),
4040
np.random.normal(loc=1000, scale=1, size=(2, 5, 10, 10)),
41-
np.asfortranarray(np.random.normal(loc=1000, scale=1, size=(5, 10, 20))),
4241
np.random.randint(-(2 ** 31), -(2 ** 31) + 20, size=1000, dtype="i4").reshape(
4342
100, 10
4443
),
@@ -84,3 +83,26 @@ def test_err_decode_object_buffer():
8483

8584
def test_err_encode_object_buffer():
8685
check_err_encode_object_buffer(ZFPY())
86+
87+
88+
def test_err_encode_list():
89+
data = ['foo', 'bar', 'baz']
90+
for codec in codecs:
91+
with pytest.raises(TypeError):
92+
codec.encode(data)
93+
94+
95+
def test_err_encode_non_contiguous():
96+
# non-contiguous memory
97+
arr = np.arange(1000, dtype='i4')[::2]
98+
for codec in codecs:
99+
with pytest.raises(ValueError):
100+
codec.encode(arr)
101+
102+
103+
def test_err_encode_fortran_array():
104+
# fortran array
105+
arr = np.asfortranarray(np.random.normal(loc=1000, scale=1, size=(5, 10, 20)))
106+
for codec in codecs:
107+
with pytest.raises(ValueError):
108+
codec.encode(arr)

numcodecs/zfpy.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from .abc import Codec
1111
from .compat import ndarray_copy, ensure_contiguous_ndarray, ensure_bytes
12+
import numpy as np
1213

1314
# noinspection PyShadowingBuiltins
1415
class ZFPY(Codec):
@@ -54,8 +55,16 @@ def __init__(
5455

5556
def encode(self, buf):
5657

57-
# normalise inputs
58-
buf = ensure_contiguous_ndarray(buf)
58+
# not flatten c-order array and raise exception for f-order array
59+
if not isinstance(buf, np.ndarray):
60+
raise TypeError("The zfp codec does not support none numpy arrays."
61+
f" Your buffers were {type(buf)}.")
62+
if buf.flags.c_contiguous:
63+
flatten = False
64+
else:
65+
raise ValueError("The zfp codec does not support F order arrays. "
66+
f"Your arrays flags were {buf.flags}.")
67+
buf = ensure_contiguous_ndarray(buf, flatten=flatten)
5968

6069
# do compression
6170
return _zfpy.compress_numpy(

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ cython
22
numpy
33
msgpack
44
pytest
5+
zfpy

requirements_dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Cython==0.29.21
22
msgpack==1.0.2
33
numpy==1.21.0
4-
zfpy==0.5.5; python_version < '3.9'
4+
zfpy==0.5.5
5+
56

requirements_rtfd.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ numpydoc
66
mock
77
numpy
88
cython
9-
zfpy==0.5.5; python_version < '3.9'
9+
zfpy==0.5.5

0 commit comments

Comments
 (0)