@@ -905,8 +905,10 @@ def add_annotation(self, annot, borders=True, alpha=1, hemi=None,
905
905
----------
906
906
annot : str
907
907
Either path to annotation file or annotation name
908
- borders : bool
909
- Show only borders of regions
908
+ borders : bool | int
909
+ Show only label borders. If int, specify the number of steps
910
+ (away from the true border) along the cortical mesh to include
911
+ as part of the border definition.
910
912
alpha : float in [0, 1]
911
913
Alpha level to control opacity
912
914
hemi : str | None
@@ -960,13 +962,7 @@ def add_annotation(self, annot, borders=True, alpha=1, hemi=None,
960
962
orig_ids = True )
961
963
962
964
# Maybe zero-out the non-border vertices
963
- if borders :
964
- n_vertices = labels .size
965
- edges = utils .mesh_edges (self .geo [hemi ].faces )
966
- border_edges = labels [edges .row ] != labels [edges .col ]
967
- show = np .zeros (n_vertices , dtype = np .int )
968
- show [np .unique (edges .row [border_edges ])] = 1
969
- labels *= show
965
+ self ._to_borders (labels , hemi , borders )
970
966
971
967
# Handle null labels properly
972
968
# (tksurfer doesn't use the alpha channel, so sometimes this
@@ -1012,8 +1008,8 @@ def add_label(self, label, color=None, alpha=1, scalar_thresh=None,
1012
1008
scalar >= thresh)
1013
1009
borders : bool | int
1014
1010
Show only label borders. If int, specify the number of steps
1015
- along the cortical mesh to include in the border definition
1016
- (higher numbers create thicker borders) .
1011
+ (away from the true border) along the cortical mesh to include
1012
+ as part of the border definition .
1017
1013
hemi : str | None
1018
1014
If None, it is assumed to belong to the hemipshere being
1019
1015
shown. If two hemispheres are being shown, an error will
@@ -1095,8 +1091,22 @@ def add_label(self, label, color=None, alpha=1, scalar_thresh=None,
1095
1091
i += 1
1096
1092
label_name = name % i
1097
1093
1094
+ self ._to_borders (label , hemi , borders , restrict_idx = ids )
1095
+
1096
+ # make a list of all the plotted labels
1097
+ ll = []
1098
+ views = self ._toggle_render (False )
1099
+ for brain in self ._brain_list :
1100
+ if brain ['hemi' ] == hemi :
1101
+ ll .append (brain ['brain' ].add_label (label , label_name ,
1102
+ color , alpha ))
1103
+ self .labels_dict [label_name ] = ll
1104
+ self ._toggle_render (True , views )
1105
+
1106
+ def _to_borders (self , label , hemi , borders , restrict_idx = None ):
1107
+ """Helper to potentially convert a label/parc to borders"""
1098
1108
if not isinstance (borders , (bool , int )) or borders < 0 :
1099
- raise TypeError ('borders must be a bool or positive integer' )
1109
+ raise ValueError ('borders must be a bool or positive integer' )
1100
1110
if borders :
1101
1111
n_vertices = label .size
1102
1112
edges = utils .mesh_edges (self .geo [hemi ].faces )
@@ -1107,22 +1117,13 @@ def add_label(self, label, color=None, alpha=1, scalar_thresh=None,
1107
1117
for _ in range (borders ):
1108
1118
keep_idx = np .in1d (self .geo [hemi ].faces .ravel (), keep_idx )
1109
1119
keep_idx .shape = self .geo [hemi ].faces .shape
1110
- keep_idx = self .geo [hemi ].faces [np .any (keep_idx ,
1111
- axis = 1 )].ravel ()
1112
- keep_idx = keep_idx [np .in1d (keep_idx , ids )]
1120
+ keep_idx = self .geo [hemi ].faces [np .any (keep_idx , axis = 1 )]
1121
+ keep_idx = np .unique (keep_idx )
1122
+ if restrict_idx is not None :
1123
+ keep_idx = keep_idx [np .in1d (keep_idx , restrict_idx )]
1113
1124
show [keep_idx ] = 1
1114
1125
label *= show
1115
1126
1116
- # make a list of all the plotted labels
1117
- ll = []
1118
- views = self ._toggle_render (False )
1119
- for brain in self ._brain_list :
1120
- if brain ['hemi' ] == hemi :
1121
- ll .append (brain ['brain' ].add_label (label , label_name ,
1122
- color , alpha ))
1123
- self .labels_dict [label_name ] = ll
1124
- self ._toggle_render (True , views )
1125
-
1126
1127
def remove_labels (self , labels = None , hemi = None ):
1127
1128
"""Remove one or more previously added labels from the image.
1128
1129
0 commit comments