@@ -500,11 +500,20 @@ def __init__(self, subject_id, hemi, surf, title=None,
500
500
self .contour_list = []
501
501
self .morphometry_list = []
502
502
self .annot_list = []
503
- self .data_dict = dict (lh = None , rh = None )
503
+ self ._data_dicts = dict (lh = [] , rh = [] )
504
504
# note that texts gets treated differently
505
505
self .texts_dict = dict ()
506
+ self ._times = None
506
507
self .n_times = None
507
508
509
+ @property
510
+ def data_dict (self ):
511
+ """For backwards compatibility"""
512
+ lh_list = self ._data_dicts ['lh' ]
513
+ rh_list = self ._data_dicts ['rh' ]
514
+ return dict (lh = lh_list [- 1 ] if lh_list else None ,
515
+ rh = rh_list [- 1 ] if rh_list else None )
516
+
508
517
###########################################################################
509
518
# HELPERS
510
519
def _toggle_render (self , state , views = None ):
@@ -1052,20 +1061,38 @@ def add_data(self, array, min=None, max=None, thresh=None,
1052
1061
transparent = False , time = 0 , time_idx = 0 ,
1053
1062
vertices = vertices , smooth_mat = smooth_mat )
1054
1063
1064
+ # clean up existing data
1065
+ if remove_existing :
1066
+ self .remove_data (hemi )
1067
+
1055
1068
# Create time array and add label if 2D
1056
1069
if array .ndim == 2 :
1070
+ # check time array
1057
1071
if time is None :
1058
1072
time = np .arange (array .shape [1 ])
1059
- self ._times = time
1060
- self .n_times = array .shape [1 ]
1061
- if not self .n_times == len (time ):
1062
- raise ValueError ('time is not the same length as '
1063
- 'array.shape[1]' )
1073
+ else :
1074
+ time = np .asarray (time )
1075
+ if time .shape != (array .shape [1 ],):
1076
+ raise ValueError ('time has shape %s, but need shape %s '
1077
+ '(array.shape[1])' %
1078
+ (time .shape , (array .shape [1 ],)))
1079
+
1080
+ if self .n_times is None :
1081
+ self .n_times = len (time )
1082
+ self ._times = time
1083
+ elif len (time ) != self .n_times :
1084
+ raise ValueError ("New n_times is different from previous "
1085
+ "n_times" )
1086
+ elif not np .array_equal (time , self ._times ):
1087
+ raise ValueError ("Not all time values are consistent with "
1088
+ "previously set times." )
1089
+
1064
1090
# initial time
1065
1091
if initial_time is None :
1066
1092
initial_time_index = None
1067
1093
else :
1068
1094
initial_time_index = self .index_for_time (initial_time )
1095
+
1069
1096
# time label
1070
1097
if isinstance (time_label , string_types ):
1071
1098
time_label_fmt = time_label
@@ -1077,8 +1104,6 @@ def time_label(x):
1077
1104
data ["time_idx" ] = 0
1078
1105
y_txt = 0.05 + 0.05 * bool (colorbar )
1079
1106
else :
1080
- self ._times = None
1081
- self .n_times = None
1082
1107
initial_time_index = None
1083
1108
1084
1109
surfs = []
@@ -1103,10 +1128,7 @@ def time_label(x):
1103
1128
data ['colorbars' ] = bars
1104
1129
data ['orig_ctable' ] = ct
1105
1130
1106
- if remove_existing and self .data_dict [hemi ] is not None :
1107
- for surf in self .data_dict [hemi ]['surfaces' ]:
1108
- surf .parent .parent .remove ()
1109
- self .data_dict [hemi ] = data
1131
+ self ._data_dicts [hemi ].append (data )
1110
1132
1111
1133
if initial_time_index is not None :
1112
1134
self .set_data_time_index (initial_time_index )
@@ -1340,6 +1362,26 @@ def _to_borders(self, label, hemi, borders, restrict_idx=None):
1340
1362
show [keep_idx ] = 1
1341
1363
label *= show
1342
1364
1365
+ def remove_data (self , hemi = None ):
1366
+ """Remove data shown with ``Brain.add_data()``.
1367
+
1368
+ Parameters
1369
+ ----------
1370
+ hemi : str | None
1371
+ Hemisphere from which to remove data (default is all shown
1372
+ hemispheres).
1373
+ """
1374
+ hemis = self ._check_hemis (hemi )
1375
+ for hemi in hemis :
1376
+ for data in self ._data_dicts [hemi ]:
1377
+ for surf in data ['surfaces' ]:
1378
+ surf .parent .parent .remove ()
1379
+ self ._data_dicts [hemi ] = []
1380
+
1381
+ # if no data is left, reset time properties
1382
+ if not self ._data_dicts ['lh' ] and not self ._data_dicts ['rh' ]:
1383
+ self .n_times = self ._times = None
1384
+
1343
1385
def remove_labels (self , labels = None , hemi = None ):
1344
1386
"""Remove one or more previously added labels from the image.
1345
1387
@@ -1797,8 +1839,7 @@ def set_data_time_index(self, time_idx, interpolation='quadratic'):
1797
1839
1798
1840
views = self ._toggle_render (False )
1799
1841
for hemi in ['lh' , 'rh' ]:
1800
- data = self .data_dict [hemi ]
1801
- if data is not None :
1842
+ for data in self ._data_dicts [hemi ]:
1802
1843
# interpolation
1803
1844
if isinstance (time_idx , float ):
1804
1845
times = np .arange (self .n_times )
0 commit comments