Skip to content

Commit 989e6bb

Browse files
committed
RF: Use _safe_resize helper function
1 parent 84fa165 commit 989e6bb

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

nibabel/streamlines/array_sequence.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def is_ndarray_of_int_or_bool(obj):
2323
np.issubdtype(obj.dtype, np.bool_)))
2424

2525

26+
def _safe_resize(a, shape):
27+
""" Resize an ndarray safely, using minimal memory """
28+
try:
29+
a.resize(shape)
30+
except ValueError:
31+
a = a.copy()
32+
a.resize(shape, refcheck=False)
33+
return a
34+
35+
2636
class _BuildCache(object):
2737
def __init__(self, arr_seq, common_shape, dtype):
2838
self.offsets = list(arr_seq._offsets)
@@ -196,7 +206,7 @@ def _resize_data_to(self, n_rows, build_cache):
196206
if self._data.size == 0:
197207
self._data = np.empty(new_shape, dtype=build_cache.dtype)
198208
else:
199-
self._data.resize(new_shape)
209+
self._data = _safe_resize(self._data, new_shape)
200210

201211
def shrink_data(self):
202212
self._data.resize((self._get_next_offset(),) + self.common_shape,

0 commit comments

Comments
 (0)