Coverage for graphica/core/label_utils.py: 88.9%
12 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-12 16:38 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-12 16:38 +0000
1"""列名から軸ラベルを推測する。"""
2import re
4# 「ラベル (単位)」か「ラベル [単位]」。出力は丸括弧にそろえる
5_LABEL_WITH_UNIT_PATTERN = re.compile(r'^(?P<label>.+?)\s*[\(\[](?P<unit>[^()\[\]]+)[\)\]]\s*$')
8def infer_axis_label_from_column_name(column_name: object) -> str | None:
9 """列名が「ラベル (単位)」の形ならそのラベルを返す('Wavelength[nm]' -> 'Wavelength (nm)')。
11 形が合わなければ None(単位の無い列名にラベルをでっちあげない)。
12 """
13 if not isinstance(column_name, str):
14 return None
15 match = _LABEL_WITH_UNIT_PATTERN.match(column_name.strip())
16 if not match:
17 return None
18 label, unit = match.group('label').strip(), match.group('unit').strip()
19 if not label or not unit: 19 ↛ 20line 19 didn't jump to line 20 because the condition on line 19 was never true
20 return None
21 return f"{label} ({unit})"