Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:freesurfer/freesurfer into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Greve committed Jan 13, 2022
2 parents e63c6f9 + 939e3e1 commit d3f77c3
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion python/freesurfer/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __str__(self):
lines.append(str(idx).ljust(col1) + elt.name.ljust(col2) + colorstr)
return '\n'.join(lines)

def add(self, index, name, color):
def add(self, index, name, color=None):
self[index] = LookupTable.Element(name, color)

def search(self, name, exact=False):
Expand Down Expand Up @@ -72,6 +72,36 @@ def write(self, filename):
colorstr = ' '.join([str(c).ljust(3) for c in color])
file.write(str(idx).ljust(col1) + elt.name.ljust(col2) + colorstr + '\n')

def extract(self, labels):
"""
Extract a new LookupTable from a list of label indices.
"""
lut = LookupTable()
for label in labels:
elt = self.get(label)
if elt is None:
raise ValueError(f'Index {label} does not exist in the LookupTable.')
lut.add(label, elt.name, elt.color)
return lut

def copy_colors(self, source_lut):
"""
Copies colors of matching label indices from a source LookupTable.
"""
for label in self.keys():
elt = source_lut.get(label)
if elt is not None and elt.color is not None:
self[label].color = elt.color

def copy_names(self, source_lut):
"""
Copies names of matching label indices from a source LookupTable.
"""
for label in self.keys():
elt = source_lut.get(label)
if elt is not None:
self[label].name = elt.name


class RecodingLookupTable(dict):
"""
Expand Down

0 comments on commit d3f77c3

Please sign in to comment.