Skip to content

Commit 5537931

Browse files
authored
Merge pull request #724 from effigies/test/array_seq_extension
RF: Use safe resizing for ArraySequence extension
2 parents 7927c2d + 989e6bb commit 5537931

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-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,

nibabel/streamlines/tests/test_array_sequence.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ def test_arraysequence_extend(self):
219219
seq = SEQ_DATA['seq'].copy() # Copy because of in-place modification.
220220
assert_raises(ValueError, seq.extend, data)
221221

222+
# Extend after extracting some slice
223+
working_slice = seq[:2]
224+
seq.extend(ArraySequence(new_data))
225+
222226
def test_arraysequence_getitem(self):
223227
# Get one item
224228
for i, e in enumerate(SEQ_DATA['seq']):

0 commit comments

Comments
 (0)