简表改为 10% 峰高处的左右展宽与比值

- peak_summary.csv: id, x_px, left_10pct_px, right_10pct_px, ratio_right_over_left

- 翼展被相邻谱线截断时记 nan,不硬算
This commit is contained in:
starcells committed 2026-11-14 16:21:52 +08:00
1 parent e6e7de0375
commit f053723791
1 file changed
+68
+68
View File
@@ -836,6 +836,52 @@ def _clean_output(directory: Path) -> None:
stale.unlink()
def ten_percent_widths(spectrum: "SpectrumData", line: dict,
fraction: float = 0.10,
limit_px: float = float("nan")) -> dict:
"""Left and right half-widths where the profile falls to ``fraction`` of the peak.
Measured on the primary channel's net profile, relative to the fitted centre,
with linear interpolation between samples. The ratio right/left is a direct
asymmetry measure: 1.0 is symmetric, >1 means the red wing is broader.
"""
channel = line.get("primary_channel") or next(iter(line["channels"]))
peak = line["channels"][channel]
x = spectrum.x_px[channel]
net = spectrum.net[channel]
center = float(peak["center_px"])
height = float(peak["height_net_adu"])
empty = {"channel": channel, "left_px": float("nan"), "right_px": float("nan"),
"ratio": float("nan"), "level_adu": float("nan")}
if not np.isfinite(center) or not np.isfinite(height) or height <= 0:
return empty
level = fraction * height
index = int(np.argmin(np.abs(x - center)))
def walk(step: int) -> float:
previous = float(net[index])
position = index
while 0 <= position + step < net.size:
if np.isfinite(limit_px) and abs(float(x[position]) - center) > limit_px:
return float("nan") # the wing is cut off by the next line
position += step
current = float(net[position])
if current <= level:
# interpolate between the two bracketing samples
span = previous - current
offset = (previous - level) / span if span else 0.0
return float(x[position]) - step * offset * float(x[1] - x[0])
previous = current
return float("nan")
left = center - walk(-1)
right = walk(+1) - center
ratio = (right / left) if np.isfinite(left) and np.isfinite(right) and left > 0 else float("nan")
return {"channel": channel, "left_px": left, "right_px": right,
"ratio": ratio, "level_adu": level}
def export_all(hq, spectrum: SpectrumData, lines: list[dict], calibration,
selected: set[str], args, meta: dict) -> dict:
import matplotlib
@@ -1463,6 +1509,18 @@ def run(args) -> int:
"recording": str(Path(args.input).expanduser().resolve())}
result = export_all(hq, spectrum, lines, calibration, selected, args, meta_out)
shapes = []
for line in lines:
gaps = [abs(other["sensor_x_px"] - line["sensor_x_px"])
for other in lines if other is not line]
limit = 0.5 * min(gaps) if gaps else float("nan")
shapes.append(ten_percent_widths(spectrum, line, limit_px=limit))
write_csv(result["output"] / "peak_summary.csv",
["id", "x_px", "left_10pct_px", "right_10pct_px", "ratio_right_over_left"],
([line["id"], _num(line["sensor_x_px"], 4), _num(shape["left_px"], 4),
_num(shape["right_px"], 4), _num(shape["ratio"], 4)]
for line, shape in zip(lines, shapes)))
tell("")
tell(f"{'':2s}{'id':4s}{'nm':>10s}{'x_px':>10s}{'ch':>4s}{'SNR':>9s}{'height':>9s}"
f"{'FWHM px':>9s}{'FWHM nm':>9s}{'slit y':>9s} flags")
@@ -1483,6 +1541,16 @@ def run(args) -> int:
total = sum(len(items) for items in analysis.per_channel.values())
tell("")
tell(f"{len(lines)} line(s) from {total} channel peak(s); {len(selected)} selected.")
if lines:
tell("")
tell("10% height widths (asymmetry = right / left):")
tell(f"{'id':6s}{'x_px':>11s}{'left':>9s}{'right':>9s}{'ratio':>8s}")
for line, shape in zip(lines, shapes):
def _shape(value):
return f"{'nan':>8s}" if not np.isfinite(value) else f"{value:8.3f}"
tell(f"{line['id']:6s}{line['sensor_x_px']:11.3f}"
f"{_shape(shape['left_px'])}{_shape(shape['right_px'])}"
f"{_shape(shape['ratio'])}")
tell(f"exported : {result['output'].expanduser().resolve()}")
if calibration is not None:
outside = [line["id"] for line in lines if line["extrapolated"]]