11# src/pycemrg/data/labels.py
22
33import yaml
4+ import itertools
45from pathlib import Path
56from typing import Union , List , Set
67
8+
79class LabelManager :
810 """
911 Manages anatomical label definitions from a YAML configuration file.
1012
1113 This class reads a label manifest and provides utilities to translate
1214 between human-readable names, groups, and their integer values.
1315 """
16+
1417 def __init__ (self , config_path : Union [str , Path ]):
1518 """
1619 Initializes the LabelManager by loading the configuration file.
@@ -20,22 +23,24 @@ def __init__(self, config_path: Union[str, Path]):
2023 """
2124 config_file = Path (config_path )
2225 if not config_file .exists ():
23- raise FileNotFoundError (f"Label configuration file not found at: { config_file } " )
26+ raise FileNotFoundError (
27+ f"Label configuration file not found at: { config_file } "
28+ )
2429
25- with open (config_file , 'r' ) as f :
30+ with open (config_file , "r" ) as f :
2631 config = yaml .safe_load (f )
2732
28- self ._labels = config .get (' labels' , {})
29- self ._groups = config .get (' groups' , {})
30-
33+ self ._labels = config .get (" labels" , {})
34+ self ._groups = config .get (" groups" , {})
35+
3136 self ._value_to_name = {v : k for k , v in self ._labels .items ()}
3237
3338 def get_value (self , name : str ) -> int :
3439 """Translates a single label name (e.g., 'LV_myo') to its integer value."""
3540 if name not in self ._labels :
3641 raise KeyError (f"Label name '{ name } ' not found." )
3742 return self ._labels [name ]
38-
43+
3944 def get_name (self , value : int ) -> str :
4045 """Translates an integer value (e.g., 1) to its human-readable name."""
4146 if value not in self ._value_to_name :
@@ -62,9 +67,15 @@ def get_values_from_names(self, names: List[str]) -> List[int]:
6267 f"Label or group name '{ name } ' not found. "
6368 f"Available keys are: { available_keys } "
6469 )
65-
70+
6671 return sorted (list (final_values ))
67-
68- def get_tags_string (self , names : List [str ], separator : str = ',' ) -> str :
69- list_of_values = self .get_values_from_names (names )
70- return separator .join (list_of_values )
72+
73+ def get_tags_string (self , names : List [str ], separator : str = "," ) -> str :
74+ if any (isinstance (i , list ) for i in names ):
75+ flat_names = list (itertools .chain .from_iterable (names ))
76+ else :
77+ flat_names = names
78+
79+ list_of_values = self .get_values_from_names (flat_names )
80+ return separator .join (list_of_values )
81+
0 commit comments