Skip to content

Commit 3b451fe

Browse files
committed
ENH: Add to annot
1 parent e4bf4aa commit 3b451fe

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

examples/plot_parcellation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
surface = 'inflated'
1818
view = 'frontal'
1919

20-
2120
"""
2221
Bring up the visualization
2322
"""
@@ -46,4 +45,4 @@
4645
annot_path = pjoin(subjects_dir, subject_id, "label", "lh.aparc.annot")
4746
brain.add_annotation(annot_path, hemi='lh', borders=False, alpha=.75)
4847
annot_path = pjoin(subjects_dir, subject_id, "label", "rh.aparc.a2009s.annot")
49-
brain.add_annotation(annot_path, hemi='rh', remove_existing=False)
48+
brain.add_annotation(annot_path, hemi='rh', borders=2, remove_existing=False)

surfer/tests/test_viz.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ def test_annot():
8989
"""
9090
mlab.options.backend = 'test'
9191
annots = ['aparc', 'aparc.a2005s']
92-
borders = [True, False]
92+
borders = [True, False, 2]
9393
alphas = [1, 0.5]
9494
brain = Brain(*std_args)
9595
for a, b, p in zip(annots, borders, alphas):
9696
brain.add_annotation(a, b, p)
97+
assert_raises(ValueError, brain.add_annotation, 'aparc', borders=-1)
9798
brain.close()
9899

99100

surfer/viz.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,10 @@ def add_annotation(self, annot, borders=True, alpha=1, hemi=None,
905905
----------
906906
annot : str
907907
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.
910912
alpha : float in [0, 1]
911913
Alpha level to control opacity
912914
hemi : str | None
@@ -960,13 +962,7 @@ def add_annotation(self, annot, borders=True, alpha=1, hemi=None,
960962
orig_ids=True)
961963

962964
# 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)
970966

971967
# Handle null labels properly
972968
# (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,
10121008
scalar >= thresh)
10131009
borders : bool | int
10141010
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.
10171013
hemi : str | None
10181014
If None, it is assumed to belong to the hemipshere being
10191015
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,
10951091
i += 1
10961092
label_name = name % i
10971093

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"""
10981108
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')
11001110
if borders:
11011111
n_vertices = label.size
11021112
edges = utils.mesh_edges(self.geo[hemi].faces)
@@ -1107,22 +1117,13 @@ def add_label(self, label, color=None, alpha=1, scalar_thresh=None,
11071117
for _ in range(borders):
11081118
keep_idx = np.in1d(self.geo[hemi].faces.ravel(), keep_idx)
11091119
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)]
11131124
show[keep_idx] = 1
11141125
label *= show
11151126

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-
11261127
def remove_labels(self, labels=None, hemi=None):
11271128
"""Remove one or more previously added labels from the image.
11281129

0 commit comments

Comments
 (0)