Skip to content

Commit 5345631

Browse files
committed
RF: make exposed ArraySequence.data return a copy
1 parent 1642ba0 commit 5345631

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

nibabel/streamlines/array_sequence.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import numbers
3+
import warnings
34
from operator import mul
45
from functools import reduce
56

@@ -147,7 +148,22 @@ def total_nb_rows(self):
147148
@property
148149
def data(self):
149150
""" Elements in this array sequence. """
150-
return self._data
151+
warnings.warn("The 'ArraySequence.data' property has been deprecated"
152+
" in favor of 'ArraySequence.get_data()'.",
153+
DeprecationWarning,
154+
stacklevel=2)
155+
return self.get_data()
156+
157+
def get_data(self):
158+
""" Returns a copy of the elements in this array sequence.
159+
160+
Notes
161+
-----
162+
To modify the data on this array sequence, one can use
163+
in-place mathematical operators (e.g., `seq += ...`) or the use
164+
assignment operator (i.e, `seq[...] = value`).
165+
"""
166+
return self.copy()._data
151167

152168
def _check_shape(self, arrseq):
153169
""" Check whether this array sequence is compatible with another. """

nibabel/streamlines/tests/test_array_sequence.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@ def test_save_and_load_arraysequence(self):
441441
# Make sure we can add new elements to it.
442442
loaded_seq.append(SEQ_DATA['data'][0])
443443

444+
def test_get_data(self):
445+
seq_view = SEQ_DATA['seq'][::2]
446+
check_arr_seq_view(seq_view, SEQ_DATA['seq'])
447+
448+
# We make sure the array sequence data does not
449+
# contain more elements than it is supposed to.
450+
data = seq_view.get_data()
451+
assert len(data) < len(seq_view._data)
452+
444453

445454
def test_concatenate():
446455
seq = SEQ_DATA['seq'].copy() # In case there is in-place modification.

0 commit comments

Comments
 (0)