Skip to content

Commit f8b4868

Browse files
author
Jose Alonso Solis-Lemus
committed
label_mapper
1 parent cd761f2 commit f8b4868

1 file changed

Lines changed: 60 additions & 1 deletion

File tree

src/pycemrg/data/labels.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import yaml
44
import itertools
55
from pathlib import Path
6-
from typing import Union, List, Set
6+
from typing import Union, List, Set, Dict
77

88

99
class LabelManager:
@@ -78,3 +78,62 @@ def get_tags_string(self, names: List[str], separator: str = ",") -> str:
7878

7979
list_of_values = self.get_values_from_names(flat_names)
8080
return separator.join(map(str, list_of_values))
81+
82+
83+
class LabelMapper:
84+
"""
85+
Creates a mapping between two different label standards.
86+
87+
This class uses composition, taking two configured LabelManager instances—one
88+
for a "source" standard and one for a "target" standard—and provides
89+
methods to translate between them based on common anatomical names.
90+
"""
91+
92+
def __init__(self, source: LabelManager, target: LabelManager):
93+
"""
94+
Initializes the LabelMapper.
95+
96+
Args:
97+
source: A LabelManager instance configured with the source labels.
98+
target: A LabelManager instance configured with the target labels.
99+
"""
100+
self.source = source
101+
self.target = target
102+
103+
def get_source_to_target_mapping(self) -> Dict[int, int]:
104+
"""
105+
Generates a dictionary mapping source integer tags to target integer tags.
106+
107+
It iterates through the anatomical names defined in the source manager,
108+
finds the corresponding name in the target manager, and creates a
109+
mapping from the source value to the target value.
110+
111+
Returns:
112+
A dictionary of {source_tag: target_tag}.
113+
"""
114+
mapping = {}
115+
# We iterate through the source labels' items (name: value)
116+
for name, source_value in self.source._labels.items():
117+
try:
118+
# Find the same name in the target manager
119+
target_value = self.target.get_value(name)
120+
if source_value != target_value:
121+
mapping[source_value] = target_value
122+
except KeyError:
123+
# This name exists in source but not target; ignore it.
124+
pass
125+
return mapping
126+
127+
def get_source_tags(self, names: List[str]) -> List[int]:
128+
"""
129+
Convenience method to resolve names/groups using the source standard.
130+
Equivalent to calling `source_manager.get_values_from_names()`.
131+
"""
132+
return self.source.get_values_from_names(names)
133+
134+
def get_target_tags(self, names: List[str]) -> List[int]:
135+
"""
136+
Convenience method to resolve names/groups using the target standard.
137+
Equivalent to calling `target_manager.get_values_from_names()`.
138+
"""
139+
return self.target.get_values_from_names(names)

0 commit comments

Comments
 (0)