Revert "新增 --stability N:多帧重复测量峰心与 FWHM 的标准差"

This reverts commit 429b823b44.
This commit is contained in:
starcells committed 2026-11-14 16:18:18 +08:00
1 parent 429b823b44
commit e6e7de0375
1 file changed
-74
-74
View File
@@ -836,44 +836,6 @@ def _clean_output(directory: Path) -> None:
stale.unlink()
def summarise_repeats(runs: list[list[dict]], tolerance_px: float = 0.0) -> list[dict]:
"""Mean and standard deviation of each line over repeated frames.
Cross-frame matching uses the nearest centre within `tolerance` (default
1.5 x the reference FWHM), so a line that drifts by more than that is left as
a missing sample instead of being matched to its neighbour.
"""
if not runs:
return []
reference = runs[0]
summary = []
for line in reference:
width = line["fwhm_px"] if np.isfinite(line["fwhm_px"]) else 8.0
limit = tolerance_px if tolerance_px > 0 else max(2.0, 1.5 * width)
centres, widths = [], []
for run in runs:
match = None
for candidate in run:
distance = abs(candidate["sensor_x_px"] - line["sensor_x_px"])
if distance <= limit and (match is None or distance < abs(
match["sensor_x_px"] - line["sensor_x_px"])):
match = candidate
if match is None:
continue
centres.append(match["sensor_x_px"])
if np.isfinite(match["fwhm_px"]):
widths.append(match["fwhm_px"])
entry = {"id": line["id"], "n": len(centres),
"x_mean": float(np.mean(centres)) if centres else float("nan"),
"x_std": float(np.std(centres, ddof=1)) if len(centres) > 1 else float("nan"),
"fwhm_mean": float(np.mean(widths)) if widths else float("nan"),
"fwhm_std": float(np.std(widths, ddof=1)) if len(widths) > 1 else float("nan"),
"wavelength_nm": line.get("wavelength_nm", float("nan"))}
summary.append(entry)
return summary
def export_all(hq, spectrum: SpectrumData, lines: list[dict], calibration,
selected: set[str], args, meta: dict) -> dict:
import matplotlib
@@ -1351,8 +1313,6 @@ def build_parser() -> argparse.ArgumentParser:
detect.add_argument("--fit-window", type=float, default=0.0, metavar="PX",
help="half-width of the fit window in sensor px; 0 = automatic")
detect.add_argument("--shape", choices=SHAPES, default="gauss", help="line-shape model")
detect.add_argument("--stability", type=int, default=1, metavar="N",
help="also analyse N consecutive frames and report centre/FWHM scatter")
export = parser.add_argument_group("selection and export")
export.add_argument("--select", default="all", metavar="TOKENS",
@@ -1378,8 +1338,6 @@ def build_parser() -> argparse.ArgumentParser:
def validate_arguments(args) -> None:
if args.average < 1:
raise ValueError("--average must be >= 1")
if args.stability < 1:
raise ValueError("--stability must be >= 1")
if args.max_peaks < 1:
raise ValueError("--max-peaks must be >= 1")
if args.peak_distance <= 0 or args.baseline_window < 0 or args.fwhm_span <= 0:
@@ -1499,44 +1457,12 @@ def run(args) -> int:
else:
selected = parse_selection(args.select, lines)
summary = None
if getattr(args, "stability", 1) > 1:
base_frame, runs = args.frame, [lines]
try:
for step in range(1, args.stability):
args.frame, args.average = base_frame + step, 1
try:
runs.append(analyse(args, tell=lambda *a, **k: None).lines)
except (ValueError, RuntimeError, OSError):
break
finally:
args.frame = base_frame
if len(runs) > 1:
summary = summarise_repeats(runs)
if args.output is None:
args.output = str(default_output_directory(args.input, indices))
meta_out = {"created": _timestamp(),
"recording": str(Path(args.input).expanduser().resolve())}
result = export_all(hq, spectrum, lines, calibration, selected, args, meta_out)
if summary:
write_csv(result["output"] / "peak_summary.csv",
["id", "n_frames", "x_mean_px", "x_std_px", "fwhm_mean_px",
"fwhm_std_px", "wavelength_nm"],
([row["id"], row["n"], _num(row["x_mean"], 4), _num(row["x_std"], 4),
_num(row["fwhm_mean"], 4), _num(row["fwhm_std"], 4),
_num(row["wavelength_nm"], 7)] for row in summary))
tell("")
tell(f"centre/width scatter over {args.stability} frame(s):")
tell(f"{'id':6s}{'n':>3s}{'x_px':>11s}{'s_x':>8s}{'FWHM':>9s}{'s_FWHM':>8s}")
for row in summary:
def _f(value, width, digits):
return f"{'nan':>{width}s}" if not np.isfinite(value) else f"{value:{width}.{digits}f}"
tell(f"{row['id']:6s}{row['n']:3d}{_f(row['x_mean'], 11, 3)}"
f"{_f(row['x_std'], 9, 3)}{_f(row['fwhm_mean'], 9, 3)}"
f"{_f(row['fwhm_std'], 9, 3)}")
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")