Skip to content

Commit c99f970

Browse files
committed
Support __add__. Remove unnecessary syntatic sugar.
1 parent c69a5ba commit c99f970

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

nibabel/streamlines/tests/test_tractogram.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_extend(self):
187187
sdict2 = PerArrayDict(len(DATA['tractogram']),
188188
DATA['data_per_streamline'])
189189

190-
sdict += sdict2
190+
sdict.extend(sdict2)
191191
assert_equal(len(sdict), len(sdict2))
192192
for k, v in DATA['tractogram'].data_per_streamline.items():
193193
assert_arrays_equal(sdict[k][:len(DATA['tractogram'])], v)
@@ -253,7 +253,7 @@ def test_extend(self):
253253
sdict = PerArraySequenceDict(total_nb_rows, DATA['data_per_point'])
254254
sdict2 = PerArraySequenceDict(total_nb_rows, DATA['data_per_point'])
255255

256-
sdict += sdict2
256+
sdict.extend(sdict2)
257257
assert_equal(len(sdict), len(sdict2))
258258
for k, v in DATA['tractogram'].data_per_point.items():
259259
assert_arrays_equal(sdict[k][:len(DATA['tractogram'])], v)
@@ -602,9 +602,15 @@ def test_tractogram_to_world(self):
602602
def test_tractogram_extend(self):
603603
# Load tractogram that contains some metadata.
604604
t = DATA['tractogram'].copy()
605-
new_t = DATA['tractogram'].copy()
606605

607606
# Double the tractogram.
607+
new_t = t + t
608+
assert_equal(len(new_t), 2*len(t))
609+
assert_tractogram_equal(new_t[:len(t)], DATA['tractogram'])
610+
assert_tractogram_equal(new_t[len(t):], DATA['tractogram'])
611+
612+
# Double the tractogram inplace.
613+
new_t = DATA['tractogram'].copy()
608614
new_t += t
609615
assert_equal(len(new_t), 2*len(t))
610616
assert_tractogram_equal(new_t[:len(t)], DATA['tractogram'])

nibabel/streamlines/tractogram.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ def extend(self, other):
138138
for key in self.keys():
139139
self[key] = np.concatenate([self[key], other[key]])
140140

141-
def __iadd__(self, other):
142-
self.extend(other)
143-
return self
144-
145141

146142
class PerArraySequenceDict(PerArrayDict):
147143
""" Dictionary for which key access can do slicing on the values.
@@ -192,10 +188,6 @@ def extend(self, other):
192188
for key in self.keys():
193189
self[key].extend(other[key])
194190

195-
def __iadd__(self, other):
196-
self.extend(other)
197-
return self
198-
199191

200192
class LazyDict(collections.MutableMapping):
201193
""" Dictionary of generator functions.
@@ -495,13 +487,18 @@ def extend(self, other):
495487
must match those contained in the other tractogram.
496488
"""
497489
self.streamlines.extend(other.streamlines)
498-
self.data_per_streamline += other.data_per_streamline
499-
self.data_per_point += other.data_per_point
490+
self.data_per_streamline.extend(other.data_per_streamline)
491+
self.data_per_point.extend(other.data_per_point)
500492

501493
def __iadd__(self, other):
502494
self.extend(other)
503495
return self
504496

497+
def __add__(self, other):
498+
tractogram = self.copy()
499+
tractogram += other
500+
return tractogram
501+
505502

506503
class LazyTractogram(Tractogram):
507504
""" Lazy container for streamlines and their data information.

0 commit comments

Comments
 (0)