diff --git a/nibabel/streamlines/array_sequence.py b/nibabel/streamlines/array_sequence.py index 333f717321..5e6df6bf26 100644 --- a/nibabel/streamlines/array_sequence.py +++ b/nibabel/streamlines/array_sequence.py @@ -23,6 +23,16 @@ def is_ndarray_of_int_or_bool(obj): np.issubdtype(obj.dtype, np.bool_))) +def _safe_resize(a, shape): + """ Resize an ndarray safely, using minimal memory """ + try: + a.resize(shape) + except ValueError: + a = a.copy() + a.resize(shape, refcheck=False) + return a + + class _BuildCache(object): def __init__(self, arr_seq, common_shape, dtype): self.offsets = list(arr_seq._offsets) @@ -196,7 +206,7 @@ def _resize_data_to(self, n_rows, build_cache): if self._data.size == 0: self._data = np.empty(new_shape, dtype=build_cache.dtype) else: - self._data.resize(new_shape) + self._data = _safe_resize(self._data, new_shape) def shrink_data(self): self._data.resize((self._get_next_offset(),) + self.common_shape, diff --git a/nibabel/streamlines/tests/test_array_sequence.py b/nibabel/streamlines/tests/test_array_sequence.py index 45f50075f8..33421f45c7 100644 --- a/nibabel/streamlines/tests/test_array_sequence.py +++ b/nibabel/streamlines/tests/test_array_sequence.py @@ -219,6 +219,10 @@ def test_arraysequence_extend(self): seq = SEQ_DATA['seq'].copy() # Copy because of in-place modification. assert_raises(ValueError, seq.extend, data) + # Extend after extracting some slice + working_slice = seq[:2] + seq.extend(ArraySequence(new_data)) + def test_arraysequence_getitem(self): # Get one item for i, e in enumerate(SEQ_DATA['seq']):