Skip to content

Commit 441118e

Browse files
committed
Changed default behavior to not throw error on non 1-to-1 mappings. Added example to doc str.
1 parent 2fbf7c3 commit 441118e

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

surfa/core/labels.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import numpy as np
3+
import warnings
34
from copy import deepcopy
45
import surfa as sf
56

@@ -290,7 +291,7 @@ def __init__(self, mapping, target=None):
290291
self.mapping = dict(mapping)
291292
self.target = target
292293

293-
def invert(self, target_labels=None, strict=True):
294+
def invert(self, target_labels=None, strict=False):
294295
"""
295296
Invert the label mapping dictionary
296297
@@ -317,6 +318,28 @@ def invert(self, target_labels=None, strict=True):
317318
------
318319
KeyError
319320
If `strict` is set to `True` and inverted label mapping is not 1-to-1
321+
322+
Examples
323+
--------
324+
Load an aseg volume, recode the labels to tissue types using the tissue_type_recoder,
325+
then invert the tissue_type_recoder and remap the labels back.
326+
Note that the tissue_type_recoder is not 1-to-1, so labels classes in the second
327+
remapped volume will be merged.
328+
329+
# load the aseg
330+
>>> aseg = sf.load_volume('aseg.mgz')
331+
332+
# create the tissue_type_recoder obj
333+
>>> lr = sf.freesurfer.tissue_type_recoder()
334+
335+
# remap the aseg labels
336+
>>> aseg_to_tissue = sf.labels.recode(aseg,lr)
337+
338+
# invert the label recoder and assign the standard cLUT as target labels
339+
>>> inv_lr = lr.invert(target_labels=sf.freesurfer.labels())
340+
341+
# re-remap the aseg labels
342+
>>> tissue_to_aseg = sf.labels.recode(aseg,inv_lr)
320343
"""
321344
# invert the mapping dictionary, handling many-to-1 mapping case
322345
inv_mapping = {}
@@ -331,6 +354,8 @@ def invert(self, target_labels=None, strict=True):
331354
# raise key error if many-to-1 and strict
332355
if False in test and strict:
333356
raise KeyError('Cannot strictly invert a many-to-1 LabelRecoder')
357+
elif False in test:
358+
warnings.warn('The label remapping is not 1-to-1, some classes will be merged.')
334359

335360
[x.sort() for x in inv_mapping.values()]
336361
inv_mapping = {k:v[0] for k,v in inv_mapping.items()}

0 commit comments

Comments
 (0)