Skip to content

Commit fbac7e4

Browse files
committed
FIX: Correct reshape method
1 parent e35f327 commit fbac7e4

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

nibabel/arrayproxy.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,21 @@ def __getitem__(self, slicer):
188188
return apply_read_scaling(raw_data, self._slope, self._inter)
189189

190190
def reshape(self, shape):
191-
''' Return an ArrayProxy with a new shape, without modifying data
192-
193-
``array_proxy.reshape(shape)`` is equivalent to
194-
``np.reshape(array_proxy, shape)``
195-
'''
191+
''' Return an ArrayProxy with a new shape, without modifying data '''
196192
size = np.prod(self._shape)
197193

198194
# Calculate new shape if not fully specified
199-
shape_arr = np.asarray(shape)
200-
unknowns = shape_arr == -1
201-
if len(unknowns) > 1:
195+
from operator import mul
196+
from functools import reduce
197+
n_unknowns = len([e for e in shape if e == -1])
198+
if n_unknowns > 1:
202199
raise ValueError("can only specify one unknown dimension")
203-
elif len(unknowns) == 1:
204-
uk_val = size // np.prod(shape_arr[~unknowns])
205-
shape_arr[unknowns] = uk_val
200+
elif n_unknowns == 1:
201+
known_size = reduce(mul, shape, -1)
202+
unknown_size = size // known_size
203+
shape = tuple(unknown_size if e == -1 else e for e in shape)
206204

207-
if shape_arr.prod() != size:
205+
if np.prod(shape) != size:
208206
raise ValueError("cannot reshape array of size {:d} into shape "
209207
"{!s}".format(size, shape))
210208
return self.__class__(file_like=self.file_like,

0 commit comments

Comments
 (0)