Commit 3b451fe0 authored by Eric Larson's avatar Eric Larson

ENH: Add to annot

parent e4bf4aab
......@@ -17,7 +17,6 @@ hemi = 'both'
surface = 'inflated'
view = 'frontal'
"""
Bring up the visualization
"""
......@@ -46,4 +45,4 @@ subjects_dir = os.environ["SUBJECTS_DIR"]
annot_path = pjoin(subjects_dir, subject_id, "label", "lh.aparc.annot")
brain.add_annotation(annot_path, hemi='lh', borders=False, alpha=.75)
annot_path = pjoin(subjects_dir, subject_id, "label", "rh.aparc.a2009s.annot")
brain.add_annotation(annot_path, hemi='rh', remove_existing=False)
brain.add_annotation(annot_path, hemi='rh', borders=2, remove_existing=False)
......@@ -89,11 +89,12 @@ def test_annot():
"""
mlab.options.backend = 'test'
annots = ['aparc', 'aparc.a2005s']
borders = [True, False]
borders = [True, False, 2]
alphas = [1, 0.5]
brain = Brain(*std_args)
for a, b, p in zip(annots, borders, alphas):
brain.add_annotation(a, b, p)
assert_raises(ValueError, brain.add_annotation, 'aparc', borders=-1)
brain.close()
......
......@@ -905,8 +905,10 @@ class Brain(object):
----------
annot : str
Either path to annotation file or annotation name
borders : bool
Show only borders of regions
borders : bool | int
Show only label borders. If int, specify the number of steps
(away from the true border) along the cortical mesh to include
as part of the border definition.
alpha : float in [0, 1]
Alpha level to control opacity
hemi : str | None
......@@ -960,13 +962,7 @@ class Brain(object):
orig_ids=True)
# Maybe zero-out the non-border vertices
if borders:
n_vertices = labels.size
edges = utils.mesh_edges(self.geo[hemi].faces)
border_edges = labels[edges.row] != labels[edges.col]
show = np.zeros(n_vertices, dtype=np.int)
show[np.unique(edges.row[border_edges])] = 1
labels *= show
self._to_borders(labels, hemi, borders)
# Handle null labels properly
# (tksurfer doesn't use the alpha channel, so sometimes this
......@@ -1012,8 +1008,8 @@ class Brain(object):
scalar >= thresh)
borders : bool | int
Show only label borders. If int, specify the number of steps
along the cortical mesh to include in the border definition
(higher numbers create thicker borders).
(away from the true border) along the cortical mesh to include
as part of the border definition.
hemi : str | None
If None, it is assumed to belong to the hemipshere being
shown. If two hemispheres are being shown, an error will
......@@ -1095,8 +1091,22 @@ class Brain(object):
i += 1
label_name = name % i
self._to_borders(label, hemi, borders, restrict_idx=ids)
# make a list of all the plotted labels
ll = []
views = self._toggle_render(False)
for brain in self._brain_list:
if brain['hemi'] == hemi:
ll.append(brain['brain'].add_label(label, label_name,
color, alpha))
self.labels_dict[label_name] = ll
self._toggle_render(True, views)
def _to_borders(self, label, hemi, borders, restrict_idx=None):
"""Helper to potentially convert a label/parc to borders"""
if not isinstance(borders, (bool, int)) or borders < 0:
raise TypeError('borders must be a bool or positive integer')
raise ValueError('borders must be a bool or positive integer')
if borders:
n_vertices = label.size
edges = utils.mesh_edges(self.geo[hemi].faces)
......@@ -1107,22 +1117,13 @@ class Brain(object):
for _ in range(borders):
keep_idx = np.in1d(self.geo[hemi].faces.ravel(), keep_idx)
keep_idx.shape = self.geo[hemi].faces.shape
keep_idx = self.geo[hemi].faces[np.any(keep_idx,
axis=1)].ravel()
keep_idx = keep_idx[np.in1d(keep_idx, ids)]
keep_idx = self.geo[hemi].faces[np.any(keep_idx, axis=1)]
keep_idx = np.unique(keep_idx)
if restrict_idx is not None:
keep_idx = keep_idx[np.in1d(keep_idx, restrict_idx)]
show[keep_idx] = 1
label *= show
# make a list of all the plotted labels
ll = []
views = self._toggle_render(False)
for brain in self._brain_list:
if brain['hemi'] == hemi:
ll.append(brain['brain'].add_label(label, label_name,
color, alpha))
self.labels_dict[label_name] = ll
self._toggle_render(True, views)
def remove_labels(self, labels=None, hemi=None):
"""Remove one or more previously added labels from the image.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment