diff --git a/numcodecs/astype.py b/numcodecs/astype.py index 0d3a3160..f222860f 100644 --- a/numcodecs/astype.py +++ b/numcodecs/astype.py @@ -50,7 +50,8 @@ def __init__(self, encode_dtype, decode_dtype): def encode(self, buf): # view input data as 1D array - arr = ndarray_from_buffer(buf, self.decode_dtype) + arr = ndarray_from_buffer(buf) + arr = arr.view(self.decode_dtype).reshape(-1, order='A') # convert and copy enc = arr.astype(self.encode_dtype) @@ -60,7 +61,8 @@ def encode(self, buf): def decode(self, buf, out=None): # view encoded data as 1D array - enc = ndarray_from_buffer(buf, self.encode_dtype) + enc = ndarray_from_buffer(buf) + enc = enc.view(self.encode_dtype).reshape(-1, order='A') # convert and copy dec = enc.astype(self.decode_dtype) diff --git a/numcodecs/bz2.py b/numcodecs/bz2.py index a74ba3d4..15500082 100644 --- a/numcodecs/bz2.py +++ b/numcodecs/bz2.py @@ -1,14 +1,10 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function, division import bz2 as _bz2 -import array - - -import numpy as np from numcodecs.abc import Codec -from numcodecs.compat import buffer_copy, handle_datetime +from numcodecs.compat import buffer_copy, to_buffer class BZ2(Codec): @@ -28,18 +24,7 @@ def __init__(self, level=1): def encode(self, buf): - # deal with lack of buffer support for datetime64 and timedelta64 - buf = handle_datetime(buf) - - if isinstance(buf, np.ndarray): - - # cannot compress object array - if buf.dtype == object: - raise ValueError('cannot encode object array') - - # if numpy array, can only handle C contiguous directly - if not buf.flags.c_contiguous: - buf = buf.tobytes(order='A') + buf = to_buffer(buf) # do compression return _bz2.compress(buf, self.level) @@ -47,10 +32,7 @@ def encode(self, buf): # noinspection PyMethodMayBeStatic def decode(self, buf, out=None): - # BZ2 cannot handle ndarray directly at all, coerce everything to - # memoryview - if not isinstance(buf, array.array): - buf = memoryview(buf) + buf = memoryview(to_buffer(buf)) # do decompression dec = _bz2.decompress(buf) diff --git a/numcodecs/categorize.py b/numcodecs/categorize.py index 49a899e7..adfd28cd 100644 --- a/numcodecs/categorize.py +++ b/numcodecs/categorize.py @@ -53,7 +53,8 @@ def __init__(self, labels, dtype, astype='u1'): def encode(self, buf): # view input as ndarray - arr = ndarray_from_buffer(buf, self.dtype) + arr = ndarray_from_buffer(buf) + arr = arr.view(self.dtype).reshape(-1, order='A') # setup output array enc = np.zeros_like(arr, dtype=self.astype) @@ -67,7 +68,8 @@ def encode(self, buf): def decode(self, buf, out=None): # view encoded data as ndarray - enc = ndarray_from_buffer(buf, self.astype) + enc = ndarray_from_buffer(buf) + enc = enc.view(self.astype).reshape(-1, order='A') # setup output if isinstance(out, np.ndarray): diff --git a/numcodecs/checksum32.py b/numcodecs/checksum32.py index bd044621..574446c6 100644 --- a/numcodecs/checksum32.py +++ b/numcodecs/checksum32.py @@ -17,7 +17,8 @@ class Checksum32(Codec): def encode(self, buf): if isinstance(buf, np.ndarray) and buf.dtype == object: raise ValueError('cannot encode object array') - arr = ndarray_from_buffer(buf, dtype='u1') + arr = ndarray_from_buffer(buf) + arr = arr.view('u1').reshape(-1, order='A') checksum = self.checksum(arr) & 0xffffffff enc = np.empty(arr.nbytes + 4, dtype='u1') enc[:4].view('