Skip to content

Commit d9f21fe

Browse files
committed
address @jakirkham comments
1 parent 492c543 commit d9f21fe

19 files changed

+196
-197
lines changed

numcodecs/astype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, encode_dtype, decode_dtype):
5050
def encode(self, buf):
5151

5252
# normalise input
53-
arr = ensure_ndarray(buf, dtype=self.decode_dtype)
53+
arr = ensure_ndarray(buf).view(self.decode_dtype)
5454

5555
# convert and copy
5656
enc = arr.astype(self.encode_dtype)
@@ -60,7 +60,7 @@ def encode(self, buf):
6060
def decode(self, buf, out=None):
6161

6262
# normalise input
63-
enc = ensure_ndarray(buf, dtype=self.encode_dtype)
63+
enc = ensure_ndarray(buf).view(self.encode_dtype)
6464

6565
# convert and copy
6666
dec = enc.astype(self.decode_dtype)

numcodecs/blosc.c

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

numcodecs/categorize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def encode(self, buf):
5656

5757
# normalise input
5858
if self.dtype == object:
59-
arr = np.asanyarray(buf, dtype=object)
59+
arr = np.asarray(buf, dtype=object)
6060
else:
61-
arr = ensure_ndarray(buf, dtype=self.dtype)
61+
arr = ensure_ndarray(buf).view(self.dtype)
6262

6363
# flatten to simplify implementation
6464
arr = arr.reshape(-1, order='A')
@@ -75,7 +75,7 @@ def encode(self, buf):
7575
def decode(self, buf, out=None):
7676

7777
# normalise input
78-
enc = ensure_ndarray(buf, dtype=self.astype)
78+
enc = ensure_ndarray(buf).view(self.astype)
7979

8080
# flatten to simplify implementation
8181
enc = enc.reshape(-1, order='A')

numcodecs/checksum32.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class Checksum32(Codec):
1515
checksum = None
1616

1717
def encode(self, buf):
18-
arr = ensure_contiguous_ndarray(buf, dtype='u1')
18+
arr = ensure_contiguous_ndarray(buf).view('u1')
1919
checksum = self.checksum(arr) & 0xffffffff
2020
enc = np.empty(arr.nbytes + 4, dtype='u1')
2121
enc[:4].view('<u4')[0] = checksum
2222
ndarray_copy(arr, enc[4:])
2323
return enc
2424

2525
def decode(self, buf, out=None):
26-
arr = ensure_contiguous_ndarray(buf, dtype='u1')
26+
arr = ensure_contiguous_ndarray(buf).view('u1')
2727
expect = arr[:4].view('<u4')[0]
2828
checksum = self.checksum(arr[4:]) & 0xffffffff
2929
if expect != checksum:

numcodecs/compat.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ def ensure_text(l, encoding='utf-8'):
3333
return text_type(l, encoding=encoding)
3434

3535

36-
def ensure_ndarray(buf, dtype=None):
36+
def ensure_ndarray(buf):
3737
"""Convenience function to coerce `buf` to a numpy array, if it is not already a
3838
numpy array.
3939
4040
Parameters
4141
----------
4242
buf : array-like or bytes-like
4343
A numpy array or any object exporting a buffer interface.
44-
dtype : dtype, optional
45-
Request that the data be viewed as the given dtype.
4644
4745
Returns
4846
-------
@@ -60,10 +58,11 @@ def ensure_ndarray(buf, dtype=None):
6058
# already a numpy array
6159
arr = buf
6260

63-
elif isinstance(buf, array.array) and buf.typecode == 'u':
64-
# guard condition, do not support array.array with unicode type, this is
65-
# problematic because numpy does not support it on all platforms
66-
raise TypeError('array.array with unicode type is not supported')
61+
elif isinstance(buf, array.array) and buf.typecode in 'cu':
62+
# Guard condition, do not support array.array with unicode type, this is
63+
# problematic because numpy does not support it on all platforms. Also do not
64+
# support char as it was removed in Python 3.
65+
raise TypeError('array.array with char or unicode type is not supported')
6766

6867
else:
6968

@@ -88,14 +87,10 @@ def ensure_ndarray(buf, dtype=None):
8887
# interface, so we have to manually hack it back in after the fact
8988
arr = arr.view(buf.typecode)
9089

91-
if dtype is not None:
92-
# view as requested dtype
93-
arr = arr.view(dtype)
94-
9590
return arr
9691

9792

98-
def ensure_contiguous_ndarray(buf, dtype=None):
93+
def ensure_contiguous_ndarray(buf):
9994
"""Convenience function to coerce `buf` to a numpy array, if it is not already a
10095
numpy array. Also ensures that the returned value exports fully contiguous memory,
10196
and supports the new-style buffer interface.
@@ -104,8 +99,6 @@ def ensure_contiguous_ndarray(buf, dtype=None):
10499
----------
105100
buf : array-like or bytes-like
106101
A numpy array or any object exporting a buffer interface.
107-
dtype : dtype, optional
108-
Request that the data be viewed as the given dtype.
109102
110103
Returns
111104
-------
@@ -120,7 +113,7 @@ def ensure_contiguous_ndarray(buf, dtype=None):
120113
"""
121114

122115
# ensure input is a numpy array
123-
arr = ensure_ndarray(buf, dtype=dtype)
116+
arr = ensure_ndarray(buf)
124117

125118
# check for datetime or timedelta ndarray, the buffer interface doesn't support those
126119
if isinstance(buf, np.ndarray) and buf.dtype.kind in 'Mm':
@@ -167,12 +160,14 @@ def ndarray_copy(src, dst):
167160
src = ensure_ndarray(src)
168161
dst = ensure_ndarray(dst)
169162

163+
# flatten source array
164+
src = src.reshape(-1, order='A')
165+
170166
# ensure same data type
171167
if dst.dtype != object:
172168
src = src.view(dst.dtype)
173169

174170
# reshape source to match destination
175-
src = src.reshape(-1, order='A')
176171
if src.shape != dst.shape:
177172
if dst.flags.f_contiguous:
178173
order = 'F'

numcodecs/compat_ext.c

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

numcodecs/delta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, dtype, astype=None):
5757
def encode(self, buf):
5858

5959
# normalise input
60-
arr = ensure_ndarray(buf, dtype=self.dtype)
60+
arr = ensure_ndarray(buf).view(self.dtype)
6161

6262
# flatten to simplify implementation
6363
arr = arr.reshape(-1, order='A')
@@ -76,7 +76,7 @@ def encode(self, buf):
7676
def decode(self, buf, out=None):
7777

7878
# normalise input
79-
enc = ensure_ndarray(buf, dtype=self.astype)
79+
enc = ensure_ndarray(buf).view(self.astype)
8080

8181
# flatten to simplify implementation
8282
enc = enc.reshape(-1, order='A')

numcodecs/fixedscaleoffset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __init__(self, offset, scale, dtype, astype=None):
8888
def encode(self, buf):
8989

9090
# normalise input
91-
arr = ensure_ndarray(buf, dtype=self.dtype)
91+
arr = ensure_ndarray(buf).view(self.dtype)
9292

9393
# flatten to simplify implementation
9494
arr = arr.reshape(-1, order='A')
@@ -107,7 +107,7 @@ def encode(self, buf):
107107
def decode(self, buf, out=None):
108108

109109
# interpret buffer as numpy array
110-
enc = ensure_ndarray(buf, dtype=self.astype)
110+
enc = ensure_ndarray(buf).view(self.astype)
111111

112112
# flatten to simplify implementation
113113
enc = enc.reshape(-1, order='A')

0 commit comments

Comments
 (0)