From 234e3c1bb5846d381ece6e4531f32da0b84c8a66 Mon Sep 17 00:00:00 2001 From: StarCells Date: Sat, 14 Nov 2026 17:04:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E8=A1=A8=EF=BC=9A=E5=8D=8A=E9=AB=98?= =?UTF-8?q?=E5=B1=95=E5=AE=BD=E6=8F=92=E5=80=BC=E4=BF=AE=E6=AD=A3=20+=20?= =?UTF-8?q?=E5=8F=AA=E7=BB=9F=E8=AE=A1=E5=AF=BC=E5=87=BA=E5=B3=B0=20+=20Q?= =?UTF-8?q?=5Fsystem=20=E4=B8=8E=20R=5F50%H?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修正半高交点插值(原来用了上一点,左展宽会被压到 0.1 px 量级) - 统计只覆盖被选中导出的峰,pick 掉的连续结构不再进入 Q - 新增 R_50%H = lambda / FWHM_lambda 与 peak_summary.csv 的 wavelength_nm / fwhm_nm / R 列 --- peak_finder.py | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/peak_finder.py b/peak_finder.py index 3bae0be..bd13d96 100644 --- a/peak_finder.py +++ b/peak_finder.py @@ -868,15 +868,18 @@ def ten_percent_widths(spectrum: "SpectrumData", line: dict, position += step current = float(net[position]) if current <= level: - # interpolate between the two bracketing samples + # interpolate between the bracketing samples: x[position] is below + # the level, x[position - step] is above it span = previous - current - offset = (previous - level) / span if span else 0.0 - return float(x[position]) - step * offset * float(x[1] - x[0]) + fraction = (level - current) / span if span else 0.0 + crossing = (float(x[position]) + + fraction * (float(x[position - step]) - float(x[position]))) + return abs(crossing - center) previous = current return float("nan") - left = center - walk(-1) - right = walk(+1) - center + left = walk(-1) + right = walk(+1) 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} @@ -1506,8 +1509,11 @@ def run(args) -> int: if args.output is None: args.output = str(default_output_directory(args.input, indices)) + # Only the peaks being exported enter the summary: with --pick the user has + # already thrown away the continuum bumps and other non-line features. + chosen = [line for line in lines if line["id"] in selected] shapes = [] - for line in lines: + for line in chosen: 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") @@ -1516,7 +1522,7 @@ def run(args) -> int: if np.isfinite(line["fwhm_px"])]) median_width = float(np.median(widths)) if widths.size else float("nan") ratios, used_ids = [], [] - for line, shape in zip(lines, shapes): + for line, shape in zip(chosen, shapes): ratio = shape["ratio"] if not np.isfinite(ratio) or ratio <= 0: continue @@ -1534,11 +1540,22 @@ def run(args) -> int: "q_system": q_system, "n_lines": len(ratios)}} result = export_all(hq, spectrum, lines, calibration, selected, args, meta_out) + def resolving_power(line): + wave = line.get("wavelength_nm", float("nan")) + width = line.get("fwhm_nm", float("nan")) + if not (np.isfinite(wave) and np.isfinite(width)) or width <= 0: + return float("nan") + return float(wave / width) + + powers = [resolving_power(line) for line in chosen] write_csv(result["output"] / "peak_summary.csv", - ["id", "x_px", "left_half_px", "right_half_px", "ratio_right_over_left"], + ["id", "x_px", "left_half_px", "right_half_px", "ratio_right_over_left", + "wavelength_nm", "fwhm_nm", "R_50pctH"], ([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))) + _num(shape["right_px"], 4), _num(shape["ratio"], 4), + _num(line.get("wavelength_nm"), 4), _num(line.get("fwhm_nm"), 4), + _num(power, 5)] + for line, shape, power in zip(chosen, shapes, powers))) tell("") tell(f"{'':2s}{'id':4s}{'nm':>10s}{'x_px':>10s}{'ch':>4s}{'SNR':>9s}{'height':>9s}" @@ -1563,16 +1580,18 @@ def run(args) -> int: if lines: tell("") tell("half-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): + tell(f"{'id':6s}{'x_px':>11s}{'left':>9s}{'right':>9s}{'ratio':>8s}" + f"{'R_50%H':>9s}") + for line, shape, power in zip(chosen, shapes, powers): 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'])}") + f"{_shape(shape['ratio'])}" + f"{('nan' if not np.isfinite(power) else f'{power:9.0f}'):>9s}") if np.isfinite(q_system): tell(f"Q_system = exp(mean|ln A_i|) = {q_system:.4f} (A_i = right/left, " - f"N = {len(ratios)} of {len(lines)} lines)") + f"N = {len(ratios)} of {len(chosen)} exported)") tell(f"exported : {result['output'].expanduser().resolve()}") if calibration is not None: outside = [line["id"] for line in lines if line["extrapolated"]]