Skip to content

Commit 97fc7c9

Browse files
committed
Ensure encode returns a ndarray
Standardize the output of the `encode` function a bit by ensuring it returns an `ndarray`. This is done without copying as it only takes a view onto the data. Has the advantage of leveraging objects under the hood that might not appear as nice like objects' internal buffers without impacting the user or unnecessarily copying the data to `bytes`. Also makes it a bit easier to work with the output as users can do anything they would normally want to with `ndarray`s.
1 parent bc61f5c commit 97fc7c9

File tree

8 files changed

+23
-18
lines changed

8 files changed

+23
-18
lines changed

numcodecs/blosc.pyx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AS_STRING
1616

1717
from .compat_ext cimport Buffer
1818
from .compat_ext import Buffer
19-
from .compat import PY2, text_type, ensure_contiguous_ndarray
19+
from .compat import PY2, text_type, ensure_ndarray, ensure_contiguous_ndarray
2020
from .abc import Codec
2121

2222

@@ -488,7 +488,8 @@ class Blosc(Codec):
488488

489489
def encode(self, buf):
490490
buf = ensure_contiguous_ndarray(buf, self.max_buffer_size)
491-
return compress(buf, self._cname_bytes, self.clevel, self.shuffle, self.blocksize)
491+
out = compress(buf, self._cname_bytes, self.clevel, self.shuffle, self.blocksize)
492+
return ensure_ndarray(out)
492493

493494
def decode(self, buf, out=None):
494495
buf = ensure_contiguous_ndarray(buf, self.max_buffer_size)

numcodecs/bz2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
from numcodecs.abc import Codec
7-
from numcodecs.compat import ndarray_copy, ensure_contiguous_ndarray
7+
from numcodecs.compat import ndarray_copy, ensure_ndarray, ensure_contiguous_ndarray
88

99

1010
class BZ2(Codec):
@@ -28,7 +28,7 @@ def encode(self, buf):
2828
buf = ensure_contiguous_ndarray(buf)
2929

3030
# do compression
31-
return _bz2.compress(buf, self.level)
31+
return ensure_ndarray(_bz2.compress(buf, self.level))
3232

3333
# noinspection PyMethodMayBeStatic
3434
def decode(self, buf, out=None):

numcodecs/gzip.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
from .abc import Codec
8-
from .compat import ndarray_copy, ensure_contiguous_ndarray, PY2
8+
from .compat import ndarray_copy, ensure_ndarray, ensure_contiguous_ndarray, PY2
99

1010

1111
class GZip(Codec):
@@ -38,9 +38,13 @@ def encode(self, buf):
3838
mode='wb',
3939
compresslevel=self.level) as compressor:
4040
compressor.write(buf)
41-
compressed = compressed.getvalue()
4241

43-
return compressed
42+
try:
43+
compressed = compressed.getbuffer()
44+
except AttributeError: # pragma: py3 no cover
45+
compressed = compressed.getvalue()
46+
47+
return ensure_ndarray(compressed)
4448

4549
# noinspection PyMethodMayBeStatic
4650
def decode(self, buf, out=None):

numcodecs/lz4.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AS_STRING
1313

1414
from .compat_ext cimport Buffer
1515
from .compat_ext import Buffer
16-
from .compat import PY2, ensure_contiguous_ndarray
16+
from .compat import PY2, ensure_ndarray, ensure_contiguous_ndarray
1717
from .abc import Codec
1818

1919

@@ -219,7 +219,7 @@ class LZ4(Codec):
219219

220220
def encode(self, buf):
221221
buf = ensure_contiguous_ndarray(buf, self.max_buffer_size)
222-
return compress(buf, self.acceleration)
222+
return ensure_ndarray(compress(buf, self.acceleration))
223223

224224
def decode(self, buf, out=None):
225225
buf = ensure_contiguous_ndarray(buf, self.max_buffer_size)

numcodecs/lzma.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
if _lzma:
1616

1717
from .abc import Codec
18-
from .compat import ndarray_copy, ensure_contiguous_ndarray
18+
from .compat import ndarray_copy, ensure_ndarray, ensure_contiguous_ndarray
1919

2020
# noinspection PyShadowingBuiltins
2121
class LZMA(Codec):
@@ -51,8 +51,8 @@ def encode(self, buf):
5151
buf = ensure_contiguous_ndarray(buf)
5252

5353
# do compression
54-
return _lzma.compress(buf, format=self.format, check=self.check,
55-
preset=self.preset, filters=self.filters)
54+
return ensure_ndarray(_lzma.compress(buf, format=self.format, check=self.check,
55+
preset=self.preset, filters=self.filters))
5656

5757
def decode(self, buf, out=None):
5858

numcodecs/vlen.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import numpy as np
1313
from .abc import Codec
1414
from .compat_ext cimport Buffer
1515
from .compat_ext import Buffer
16-
from .compat import ensure_contiguous_ndarray
16+
from .compat import ensure_ndarray, ensure_contiguous_ndarray
1717
from cpython cimport (PyBytes_GET_SIZE, PyBytes_AS_STRING, PyBytes_Check,
1818
PyBytes_FromStringAndSize, PyUnicode_AsUTF8String)
1919
from cpython.buffer cimport PyBUF_ANY_CONTIGUOUS
@@ -130,7 +130,7 @@ class VLenUTF8(Codec):
130130
memcpy(data, encv, l)
131131
data += l
132132

133-
return out
133+
return ensure_ndarray(out)
134134

135135
@cython.wraparound(False)
136136
@cython.boundscheck(False)

numcodecs/zlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
from .abc import Codec
7-
from .compat import ndarray_copy, ensure_contiguous_ndarray
7+
from .compat import ndarray_copy, ensure_ndarray, ensure_contiguous_ndarray
88

99

1010
class Zlib(Codec):
@@ -28,7 +28,7 @@ def encode(self, buf):
2828
buf = ensure_contiguous_ndarray(buf)
2929

3030
# do compression
31-
return _zlib.compress(buf, self.level)
31+
return ensure_ndarray(_zlib.compress(buf, self.level))
3232

3333
# noinspection PyMethodMayBeStatic
3434
def decode(self, buf, out=None):

numcodecs/zstd.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AS_STRING
1313

1414
from .compat_ext cimport Buffer
1515
from .compat_ext import Buffer
16-
from .compat import ensure_contiguous_ndarray
16+
from .compat import ensure_ndarray, ensure_contiguous_ndarray
1717
from .abc import Codec
1818

1919

@@ -214,7 +214,7 @@ class Zstd(Codec):
214214

215215
def encode(self, buf):
216216
buf = ensure_contiguous_ndarray(buf)
217-
return compress(buf, self.level)
217+
return ensure_ndarray(compress(buf, self.level))
218218

219219
def decode(self, buf, out=None):
220220
buf = ensure_contiguous_ndarray(buf)

0 commit comments

Comments
 (0)