@@ -112,6 +112,28 @@ def __setitem__(self, key, value):
112
112
113
113
self .store [key ] = value
114
114
115
+ def extend (self , other ):
116
+ if len (self ) != len (other ):
117
+ msg = ("Size mismatched between the two PerArrayDict objects."
118
+ " This PerArrayDict has {0} elements whereas the other "
119
+ " has {1} elements." ).format (len (self ), len (other ))
120
+ raise ValueError (msg )
121
+
122
+ if sorted (self .keys ()) != sorted (other .keys ()):
123
+ msg = ("Key mismatched between the two PerArrayDict objects."
124
+ " This PerArrayDict contains '{0}' whereas the other "
125
+ " contains '{1}'." ).format (sorted (self .keys ()),
126
+ sorted (other .keys ()))
127
+ raise ValueError (msg )
128
+
129
+ self .n_rows += other .n_rows
130
+ for key in self .keys ():
131
+ self [key ] = np .concatenate ([self [key ], other [key ]])
132
+
133
+ def __iadd__ (self , other ):
134
+ self .extend (other )
135
+ return self
136
+
115
137
116
138
class PerArraySequenceDict (PerArrayDict ):
117
139
""" Dictionary for which key access can do slicing on the values.
@@ -136,6 +158,28 @@ def __setitem__(self, key, value):
136
158
137
159
self .store [key ] = value
138
160
161
+ def extend (self , other ):
162
+ if len (self ) != len (other ):
163
+ msg = ("Size mismatched between the two PerArrayDict objects."
164
+ " This PerArrayDict has {0} elements whereas the other "
165
+ " has {1} elements." ).format (len (self ), len (other ))
166
+ raise ValueError (msg )
167
+
168
+ if sorted (self .keys ()) != sorted (other .keys ()):
169
+ msg = ("Key mismatched between the two PerArrayDict objects."
170
+ " This PerArrayDict contains '{0}' whereas the other "
171
+ " contains '{1}'." ).format (sorted (self .keys ()),
172
+ sorted (other .keys ()))
173
+ raise ValueError (msg )
174
+
175
+ self .n_rows += other .n_rows
176
+ for key in self .keys ():
177
+ self [key ].extend (other [key ])
178
+
179
+ def __iadd__ (self , other ):
180
+ self .extend (other )
181
+ return self
182
+
139
183
140
184
class LazyDict (collections .MutableMapping ):
141
185
""" Dictionary of generator functions.
@@ -418,6 +462,16 @@ def to_world(self, lazy=False):
418
462
419
463
return self .apply_affine (self .affine_to_rasmm , lazy = lazy )
420
464
465
+ def extend (self , other ):
466
+ # TODO: Make sure the other tractogram is compatible.
467
+ self .streamlines .extend (other .streamlines )
468
+ self .data_per_streamline += other .data_per_streamline
469
+ self .data_per_point += other .data_per_point
470
+
471
+ def __iadd__ (self , other ):
472
+ self .extend (other )
473
+ return self
474
+
421
475
422
476
class LazyTractogram (Tractogram ):
423
477
""" Lazy container for streamlines and their data information.
@@ -653,6 +707,9 @@ def _gen_data():
653
707
def __getitem__ (self , idx ):
654
708
raise NotImplementedError ('LazyTractogram does not support indexing.' )
655
709
710
+ def extend (self , other ):
711
+ raise NotImplementedError ('LazyTractogram does not support concatenation.' )
712
+
656
713
def __iter__ (self ):
657
714
count = 0
658
715
for tractogram_item in self .data :
0 commit comments