Skip to content

Commit e35f327

Browse files
committed
RF: Update ArrayProxy.reshape to take -1 entries
1 parent c6950f5 commit e35f327

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

nibabel/arrayproxy.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,23 @@ def reshape(self, shape):
194194
``np.reshape(array_proxy, shape)``
195195
'''
196196
size = np.prod(self._shape)
197-
if np.prod(shape) != size:
197+
198+
# Calculate new shape if not fully specified
199+
shape_arr = np.asarray(shape)
200+
unknowns = shape_arr == -1
201+
if len(unknowns) > 1:
202+
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
206+
207+
if shape_arr.prod() != size:
198208
raise ValueError("cannot reshape array of size {:d} into shape "
199209
"{!s}".format(size, shape))
200-
return ArrayProxy(file_like=self.file_like,
201-
spec=(shape, self._dtype, self._offset,
202-
self._slope, self._inter),
203-
mmap=self._mmap)
210+
return self.__class__(file_like=self.file_like,
211+
spec=(shape, self._dtype, self._offset,
212+
self._slope, self._inter),
213+
mmap=self._mmap)
204214

205215

206216
def is_proxy(obj):

0 commit comments

Comments
 (0)