diff --git a/.gitignore b/.gitignore index 2f69274..ef6e363 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ recordings/ # ---- 运行产物 ----------------------------------------------------------- peak_exports/ wavelength_calibration/ +calibration_exports/ *.npy *.log diff --git a/README.md b/README.md index 9863978..d6a29e4 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,14 @@ Raspberry Pi HQ Camera(IMX477)RAW 光谱工具集:录制、回放、寻峰 | `hq_spectrum.py` | 核心库 + 实时播放器:PC1B 解码、CFA 分离、ROI、降维、预览、交互窗口 | | `hq_record.py` | 把相机 RAW 流录成 `recordings/<时间戳>/{frames.bin,frames.jsonl,meta.json}` | | `hq_average.py` | 多帧平均成一条新录制(降噪) | -| `spectro_core.py` | **共享核心**:模块加载、标定解析、基线/噪声/寻峰/拟合/双峰分解、CSV/JSON 输出 | +| `spectro_core.py` | **共享核心**:模块加载、标定解析、几何(裁剪/反斜切)、基线/噪声/寻峰/拟合/双峰分解、CSV/JSON 输出 | +| `spectro_prep.py` | 预处理:定 ROI、测谱线倾角、反斜切,产出 `geometry.json` + 预览图 | | `peak_finder.py` | 寻峰与峰信息导出:峰形图、峰形数据、像素坐标、RAW 像素块;`--pick` 交互选峰 | -| `wavelength_calibration.py` | 用汞灯谱线标定 像素→波长,产出 `wavelength_calibration/` | +| `wavelength_calibration.py` | 用汞灯谱线标定 像素→波长,产出 `calibration_exports/` | | `wavelength_player.py` | 带已存标定的回放(波长轴、瀑布图) | | `PEAK_FINDER.md` / `WAVELENGTH_PLAYER.md` | 两个工具的详细说明 | | `test_peak_finder.py` / `test_wavelength_player.py` | 单元测试(含真实录制的端到端用例) | -| `wavelength_calibration/` | 汞灯标定的输出目录(**本地生成,不入库**):`calibration.json`、残差、候选、逐像素波长表 | +| `calibration_exports/` | 汞灯标定的输出目录(**本地生成,不入库**):`calibration.json`、残差、逐像素波长表 | ## 依赖 @@ -43,14 +44,21 @@ python3 hq_record.py --output recordings --exposure 66351 --gain 16 --duration 1 ```bash python3 hq_spectrum.py --input recordings/hq_20261101_114417 --fps 15 python3 wavelength_player.py --input recordings/hq_20261101_114417 \ - --calibration wavelength_calibration/calibration.json + --calibration calibration_exports/hq_20261101_114417_f0000/calibration.json +``` + +预处理(裁剪 + 测倾斜 + 反斜切),其它工具用 `--roi ` 复用: + +```bash +python spectro_prep.py --input recordings/hq_20261101_114417 +python peak_finder.py --input recordings/hq_20261101_114417 --roi geometry/hq_20261101_114417/geometry.json ``` 寻峰并导出(`--calibration` 只是**应用**已有的标定,不给就只输出像素坐标): ```bash python peak_finder.py --input recordings/hq_20261101_114417 \ - --calibration wavelength_calibration/calibration.json --select all + --calibration calibration_exports/hq_20261101_114417_f0000/calibration.json --select all python peak_finder.py --input recordings/hq_20261101_114417 --pick ``` @@ -86,7 +94,7 @@ $env:PEAK_FINDER_TEST_CALIBRATION = "wavelength_calibration\calibration.json" ## 仓库里不放什么 `recordings/`(原始录制,单个约 470 MB)、`peak_exports/`(导出结果)、 -`wavelength_calibration/`(标定产物)、`__pycache__/` 都在 `.gitignore` 里。 +`calibration_exports/`(标定产物)、`__pycache__/` 都在 `.gitignore` 里。 标定结果不随源码走:它绑定某一次实验的光路和传感器模式,`calibration.json` 里还记录了 参考录制的本机路径。需要时用 `wavelength_calibration.py` 重新生成,或用 `--calibration` 指到别处。 diff --git a/peak_finder.py b/peak_finder.py index 69a6ff7..ce7ff70 100644 --- a/peak_finder.py +++ b/peak_finder.py @@ -74,7 +74,7 @@ import numpy as np try: # shared core lives beside this script from spectro_core import ( # noqa: F401 (re-exported for compatibility) GAUSS_TO_FWHM, Calibration, _fit_pair, _jsonable, _num, attach_wavelength, - deblend_close_peaks, dedupe_channel_peaks, estimate_baseline, estimate_noise, + deblend_close_peaks, dedupe_channel_peaks, default_output_directory as _shared_output, estimate_baseline, estimate_noise, find_peak_candidates, fit_profile, group_channels, half_max_crossings, is_saturated, load_hq, merge_coincident_lines, model_values, parabola_center, peak_prominence, refine_peak, summarise_line, write_csv, write_json) @@ -1401,19 +1401,10 @@ def run(args) -> int: def default_output_directory(recording, indices) -> Path: - """Default export folder that names the analysed frame(s). - - ``peak_exports/_f0003`` for one frame and - ``peak_exports/_f0003-0010_avg8`` for an averaged stack, so that - exports of different frames never overwrite each other. - """ + """Default export folder: ``peak_exports/_f0003[_avgN]``.""" stem = Path(recording).expanduser().resolve().name - if len(indices) > 1: - return Path("peak_exports") / ( - f"{stem}_f{indices[0]:04d}-{indices[-1]:04d}_avg{len(indices)}") - return Path("peak_exports") / f"{stem}_f{indices[0]:04d}" - + return _shared_output("peak_exports", stem, indices) def _timestamp() -> str: from datetime import datetime diff --git a/spectro_core.py b/spectro_core.py index 45c9946..4b73b07 100644 --- a/spectro_core.py +++ b/spectro_core.py @@ -858,3 +858,18 @@ def attach_wavelength(lines: list[dict], calibration: Optional[Calibration]) -> line.update({"match_species": "", "match_nm": float("nan"), "match_delta_nm": float("nan"), "match_label": ""}) line["x_plot"] = line["wavelength_nm"] if calibration is not None else line["sensor_x_px"] + +def default_output_directory(root: str, stem: str, indices) -> Path: + """Folders that name the analysed frame(s), shared by every tool. + + `/_f0007` for one frame and `/_f0005-0007_avg3` + for an averaged stack, so exports of different frames never overwrite each + other. + """ + + indices = list(indices) + if not indices: + raise ValueError("at least one frame index is required") + if len(indices) > 1: + return Path(root) / f"{stem}_f{indices[0]:04d}-{indices[-1]:04d}_avg{len(indices)}" + return Path(root) / f"{stem}_f{indices[0]:04d}" diff --git a/test_wavelength_calibration.py b/test_wavelength_calibration.py index 3f6d074..5e44d8f 100644 --- a/test_wavelength_calibration.py +++ b/test_wavelength_calibration.py @@ -177,5 +177,27 @@ class OutputTests(unittest.TestCase): wc.run(args) +class NamingTests(unittest.TestCase): + """The calibration folder must follow the same rule as peak_exports/.""" + + def test_peaks_inside_an_export_folder_mirrors_its_name(self): + path = Path(r"C:\\x\\peak_exports\\hq_20261112_200710_f0000\\peaks.json") + out = wc.default_output_directory(peaks=str(path)) + self.assertEqual(out.parent.name, "calibration_exports") + self.assertEqual(out.name, "hq_20261112_200710_f0000") + + def test_single_frame_and_stack_names(self): + out = wc.default_output_directory(recording=r"C:\\rec\\hq_20261112_200710", frames=[7]) + self.assertEqual(out.name, "hq_20261112_200710_f0007") + out = wc.default_output_directory(recording=r"C:\\rec\\hq_20261112_200710", frames=[5, 6, 7]) + self.assertEqual(out.name, "hq_20261112_200710_f0005-0007_avg3") + + def test_moved_peaks_file_falls_back_to_its_recording(self): + out = wc.default_output_directory( + peaks=r"C:\\tmp\\peaks.json", + context={"recording": r"C:\\rec\\hq_20261112_195107", "frames": [0]}) + self.assertEqual(out.name, "hq_20261112_195107_f0000") + + if __name__ == "__main__": unittest.main() diff --git a/wavelength_calibration.py b/wavelength_calibration.py index 7439573..3575583 100644 --- a/wavelength_calibration.py +++ b/wavelength_calibration.py @@ -58,7 +58,8 @@ def build_parser() -> argparse.ArgumentParser: fit_group.add_argument("--assign", metavar="ID:NM,...", help="confirmed peak identities, e.g. P003:546.0735,P004:576.9598") fit_group.add_argument("--order", type=int, choices=(1, 2), default=1) - fit_group.add_argument("--output", default="wavelength_calibration", metavar="DIR") + fit_group.add_argument("--output", default=None, metavar="DIR", + help="output directory (default calibration_exports/_f)") fit_group.add_argument("--quiet", action="store_true", help="suppress the peak table") return p @@ -200,6 +201,24 @@ def fit(confirmed: list[dict], order: int, width: int | None) -> tuple[np.ndarra return coeff, residual +def default_output_directory(peaks: str | None = None, recording: str | None = None, + frames=None, context=None) -> Path: + """Same naming rule as peak_finder, under calibration_exports/.""" + + if peaks: + folder = Path(peaks).expanduser().resolve().parent + if "_f" in folder.name: # peak_exports/_f0007 + return Path("calibration_exports") / folder.name + reference = (context or {}).get("recording") + if reference: # peaks.json moved somewhere else + return core.default_output_directory( + "calibration_exports", Path(reference).name, + (context or {}).get("frames") or frames or [0]) + return Path("calibration_exports") / Path(peaks).stem + stem = Path(recording).expanduser().resolve().name + return core.default_output_directory("calibration_exports", stem, frames or [0]) + + def mean_abs_gap(peaks: list[dict]) -> float: xs = np.sort([peak["x"] for peak in peaks]) return float(np.mean(np.diff(xs))) if len(xs) > 1 else float("nan") @@ -312,6 +331,9 @@ def run(args) -> int: print(f"No --assign given; nothing fitted. Peaks read from {context['source']}.") return 0 + if args.output is None: + args.output = str(default_output_directory(args.peaks, args.input, + context.get("frames"), context)) confirmed = parse_assign(args.assign, peaks, args.order) width = context.get("full_size") width = int(width[0]) if isinstance(width, (list, tuple)) and width else None