File size: 3,341 Bytes
b368c46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Core-level labels for spectroscopy targets.

Processed graph caches store the model-facing ``core_level`` id explicitly.
Raw ASE databases can either provide per-row core labels through a configured
metadata key or receive one constant core label during preprocessing.
"""

CORE_LEVEL_TO_ID = {
    "K": 0,
    "L1": 1,
    "L2": 2,
    "L3": 3,
}

def canonical_core_level_label(value):
    """Normalize common raw core-level strings to stable labels."""
    if value is None:
        raise ValueError("missing core level")

    label = str(value).strip().upper()
    if not label:
        raise ValueError("empty core level")

    label = label.replace("\u2212", "-").replace("_", " ")
    label = label.replace("-", " ")
    parts = label.split()
    if parts and parts[-1] == "EDGE":
        parts = parts[:-1]
    label = "".join(parts)

    roman_l = {
        "LI": "L1",
        "LII": "L2",
        "LIII": "L3",
        "LIV": "L4",
        "LV": "L5",
    }
    return roman_l.get(label, label)


def normalize_core_level_mapping(mapping):
    """Return a canonical ``{label: id}`` mapping or ``None`` when disabled."""
    if mapping is None:
        return None

    normalized = {}
    used_ids = {}
    for raw_label, raw_id in dict(mapping).items():
        label = canonical_core_level_label(raw_label)
        level_id = int(raw_id)
        if level_id < 0:
            raise ValueError(f"core level id for {label!r} must be non-negative")
        if label in normalized and normalized[label] != level_id:
            raise ValueError(f"conflicting ids for core level {label!r}")
        if level_id in used_ids and used_ids[level_id] != label:
            raise ValueError(
                f"core level id {level_id} is assigned to both "
                f"{used_ids[level_id]!r} and {label!r}"
            )
        normalized[label] = level_id
        used_ids[level_id] = label

    if not normalized:
        return None
    return normalized


def core_level_num_classes(mapping):
    mapping = normalize_core_level_mapping(mapping)
    if mapping is None:
        return 0
    return max(mapping.values()) + 1


def raw_core_level_from_row(row, key="core_level"):
    """Read a raw core-level label from the configured ASE row metadata key."""
    value = row.get(key, None)
    if value is None:
        raise ValueError(f"row is missing required {key!r} core-level metadata")
    return value


def core_level_id_from_row(row, mapping, key="core_level"):
    """Map a raw row core-level label to its configured integer id."""
    mapping = normalize_core_level_mapping(mapping)
    if mapping is None:
        raise ValueError("core_level_to_id mapping is required")

    raw_label = raw_core_level_from_row(row, key=key)
    return core_level_id_from_label(raw_label, mapping)


def core_level_id_from_label(raw_label, mapping):
    """Map a raw core-level label to its configured integer id."""
    mapping = normalize_core_level_mapping(mapping)
    if mapping is None:
        raise ValueError("core_level_to_id mapping is required")

    label = canonical_core_level_label(raw_label)
    if label not in mapping:
        raise ValueError(
            f"core level {label!r} from raw value {raw_label!r} is not present "
            f"in core_level_to_id={mapping}"
        )
    return int(mapping[label])