xane3_mp / src /data /core_levels.py
vitorgrizzi's picture
Release the evaluated XANE(3) Materials Project baseline and inference example
b368c46 verified
Raw History Blame Contribute Delete
3.34 kB
"""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])