Skip to content

Commit fd46c4c

Browse files
authored
Merge pull request #136 from jakirkham/ret_ndarray_encode_decode
Return `ndarray`s from `encode` and (where possible) `decode`
2 parents ecb03c6 + 2012553 commit fd46c4c

File tree

12 files changed

+573
-366
lines changed

12 files changed

+573
-366
lines changed

docs/release.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Release notes
4141
code readability and maintainability. By :user:`John Kirkham <jakirkham>` and
4242
:user:`Alistair Miles <alimanfoo>`; :issue:`119`, :issue:`121`, :issue:`128`.
4343

44+
* Return values from encode() and decode() methods are now returned as numpy
45+
arrays for consistency across codecs. By :user:`John Kirkham <jakirkham>`,
46+
:issue:`136`.
47+
4448
* Improvements to handling of errors in the :class:`numcodecs.blosc.Blosc` and
4549
:class:`numcodecs.lz4.LZ4` codecs when the maximum allowed size of an input
4650
buffer is exceeded. By :user:`Jerome Kelleher <jeromekelleher>`, :issue:`80`,

numcodecs/blosc.c

Lines changed: 184 additions & 116 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

numcodecs/blosc.pyx

Lines changed: 5 additions & 3 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,11 +488,13 @@ 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)
495-
return decompress(buf, out)
496+
out = decompress(buf, out)
497+
return ensure_ndarray(out)
496498

497499
def __repr__(self):
498500
r = '%s(cname=%r, clevel=%r, shuffle=%s, blocksize=%s)' % \

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/lz4.c

Lines changed: 137 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

numcodecs/lz4.pyx

Lines changed: 3 additions & 3 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,11 +219,11 @@ 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)
226-
return decompress(buf, out)
226+
return ensure_ndarray(decompress(buf, out))
227227

228228
def __repr__(self):
229229
r = '%s(acceleration=%r)' % \

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.c

Lines changed: 67 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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):

0 commit comments

Comments
 (0)