Skip to content

Commit 6dd75bf

Browse files
christianbrodbecklarsoner
authored andcommitted
[MRG] ADD: Brain.remove_foci() (#251)
* ADD: Brain.remove_foci() * STY * STY * TEST
1 parent cdb4ffe commit 6dd75bf

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

surfer/tests/test_viz.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,21 @@ def test_foci():
227227
coords = [[-36, 18, -3],
228228
[-43, 25, 24],
229229
[-48, 26, -2]]
230-
brain.add_foci(coords, map_surface="white", color="gold")
230+
brain.add_foci(coords, map_surface="white", color="gold", name='test1')
231231

232232
subj_dir = utils._get_subjects_dir()
233233
annot_path = pjoin(subj_dir, subject_id, 'label', 'lh.aparc.a2009s.annot')
234234
ids, ctab, names = nib.freesurfer.read_annot(annot_path)
235235
verts = np.arange(0, len(ids))
236236
coords = np.random.permutation(verts[ids == 74])[:10]
237237
scale_factor = 0.7
238-
brain.add_foci(coords, coords_as_verts=True,
239-
scale_factor=scale_factor, color="#A52A2A")
238+
brain.add_foci(coords, coords_as_verts=True, scale_factor=scale_factor,
239+
color="#A52A2A", name='test2')
240+
with pytest.raises(ValueError):
241+
brain.remove_foci(['test4'])
242+
brain.remove_foci('test1')
243+
brain.remove_foci()
244+
assert len(brain.foci_dict) == 0
240245
brain.close()
241246

242247

surfer/viz.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,34 @@ def remove_data(self, hemi=None):
14541454
if all(len(brain.data) == 0 for brain in self.brains):
14551455
self.n_times = self._times = None
14561456

1457+
def remove_foci(self, name=None):
1458+
"""Remove foci added with ``Brain.add_foci()``.
1459+
1460+
Parameters
1461+
----------
1462+
name : str | list of str | None
1463+
Names of the foci to remove (if None, remove all).
1464+
1465+
Notes
1466+
-----
1467+
Only foci added with a unique names can be removed.
1468+
"""
1469+
if name is None:
1470+
keys = tuple(self.foci_dict)
1471+
else:
1472+
if isinstance(name, str):
1473+
keys = (name,)
1474+
else:
1475+
keys = name
1476+
if not all(key in self.foci_dict for key in keys):
1477+
missing = ', '.join(key for key in keys if key not in
1478+
self.foci_dict)
1479+
raise ValueError("foci=%r: no foci named %s" % (name, missing))
1480+
1481+
for key in keys:
1482+
for points in self.foci_dict.pop(key):
1483+
points.remove()
1484+
14571485
def remove_labels(self, labels=None, hemi=None):
14581486
"""Remove one or more previously added labels from the image.
14591487

0 commit comments

Comments
 (0)