diff --git a/.gitignore b/.gitignore index 2f69274..fdcfa9e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ recordings/ # ---- 运行产物 ----------------------------------------------------------- peak_exports/ wavelength_calibration/ +geometry/ *.npy *.log diff --git a/PEAK_FINDER.md b/PEAK_FINDER.md index 122731e..dbc99bb 100644 --- a/PEAK_FINDER.md +++ b/PEAK_FINDER.md @@ -13,6 +13,30 @@ 支持的数据是 `hq_record.py` 录制的 RAW(`BGGR_PISP_COMP1`、2028×1520、uint8 容器)。 逐帧元数据里的黑电平(`SensorBlackLevels`,本数据集为 4096)会被自动使用。 +## 预处理:先定几何(spectro_prep.py) + +裁剪和反斜切只该定一次。`spectro_prep.py` 读一帧,自动找 ROI、测谱线倾角,产出 + +` +geometry//geometry.json ROI、shear(px/row)、倾角、测法与逐线样本 +geometry//geometry_preview.png 全幅 RAW + ROI 框 + 倾斜参考线 +` + +之后把这份几何交给其它工具: + +`ash +python spectro_prep.py --input $rec +python peak_finder.py --input $rec --roi geometry/$($rec name)/geometry.json --select all +` + +- `--roi` 现在接受三种写法:`auto`、`x0,y0,x1,y1`、以及 `geometry.json` 路径。 +- 用几何文件时,降维前会按 `shear_px_per_row` 把每一行水平重采样(线性插值), + 让倾斜的谱线变直;几何参数会原样写进 `peaks.json` 的 `geometry` 字段。 +- `--shear` 可强制取值(`0` 表示不做),`--rows` 可再收窄行范围。 + +倾角用“重采样后谱线最锐”判据(匹配滤波)测:把各行的窗口按候选斜率对齐后平均, +取峰高最大的斜率。测不出足够增益(<5%)就不写 shear,并在输出里说明——倾斜小于 +仪器的分辨能力时,硬做反斜切只会引入插值模糊。 ## 标定:把峰交给 wavelength_calibration.py 标定不再是另起一套寻峰,而是**消费本程序导出的峰**: diff --git a/README.md b/README.md index 9863978..72fb356 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ 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_player.py` | 带已存标定的回放(波长轴、瀑布图) | @@ -46,6 +47,13 @@ python3 wavelength_player.py --input recordings/hq_20261101_114417 \ --calibration wavelength_calibration/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 diff --git a/peak_finder.py b/peak_finder.py index 69a6ff7..673ea0a 100644 --- a/peak_finder.py +++ b/peak_finder.py @@ -75,9 +75,9 @@ 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, - find_peak_candidates, fit_profile, group_channels, half_max_crossings, is_saturated, load_hq, + find_peak_candidates, fit_profile, group_channels, half_max_crossings, is_saturated, load_hq, Geometry, merge_coincident_lines, model_values, parabola_center, peak_prominence, - refine_peak, summarise_line, write_csv, write_json) + refine_peak, shear_planes, summarise_line, write_csv, write_json) except ImportError as exc: # pragma: no cover - placement error raise SystemExit('peak_finder.py needs spectro_core.py in the same directory ' '(or on sys.path)') from exc @@ -418,8 +418,10 @@ def read_frames(hq, source, args, tell): class SpectrumData: """Reduced spectra plus the raw image needed by the exports.""" - def __init__(self, hq, source, image, original, indices, roi, args, black, full_scale): + def __init__(self, hq, source, image, original, indices, roi, args, black, full_scale, + geometry=None): self.hq = hq + self.geometry = geometry self.source = source self.image = image self.original = original @@ -443,6 +445,9 @@ class SpectrumData: def _build(self) -> None: hq = self.hq planes = hq.extract_planes(self.image, self.pattern, self.roi) + if self.geometry is not None and self.geometry.shear: + # de-tilt: align every row on the row_origin before reducing along rows + planes = shear_planes(planes, self.geometry, self.step_px, self.roi.y0) counts_g = planes["G"].shape[1] counts_rb = min(planes["R"].shape[1], planes["B"].shape[1]) positions = hq.channel_x_positions(self.roi, self.pattern, counts_g, counts_rb) @@ -1261,13 +1266,14 @@ class Analysis: """Everything one recording yields: reduced spectrum, grouped lines, calibration.""" def __init__(self, hq, spectrum: "SpectrumData", lines: list[dict], calibration, - indices: list[int], per_channel: dict[str, list[dict]]): + indices: list[int], per_channel: dict[str, list[dict]], geometry=None): self.hq = hq self.spectrum = spectrum self.lines = lines self.calibration = calibration self.indices = indices self.per_channel = per_channel + self.geometry = geometry def analyse(args, tell=print) -> "Analysis": @@ -1298,29 +1304,37 @@ def analyse(args, tell=print) -> "Analysis": if not saved or len(saved) != 4: raise ValueError("--roi calibration needs a calibration JSON with a roi field") roi = hq.parse_roi(",".join(str(value) for value in saved), *source.full_size) + geometry = Geometry(roi, method="calibration ROI") else: - roi = hq.parse_roi(args.roi, *source.full_size) - if roi is None: + def detect_auto(): # hq_spectrum.make_preview() bins integer Bayer sites, so hand it # the original uint16 frame (or a rounded copy of the mean frame). preview_source = original if original is not None else np.clip( np.rint(image), 0.0, 65535.0).astype(np.uint16) - roi = hq.detect_auto_roi(hq.make_preview(preview_source, source.pattern), - *source.full_size) - if roi is None: - raise ValueError("auto ROI failed; specify --roi x0,y0,x1,y1") + return hq.detect_auto_roi(hq.make_preview(preview_source, source.pattern), + *source.full_size) + + # --roi accepts a crop, 'auto', or a geometry.json written by spectro_prep.py + geometry = Geometry.from_spec(args.roi, source.full_size, detect_auto) + roi = hq.parse_roi(",".join(str(value) for value in geometry.roi), *source.full_size) roi = resolve_rows(args.rows, roi, source.full_size[1], hq) meta = dict(source.base_meta) meta.update({key: value for key, value in frame_meta.items() if not str(key).startswith("_")}) black, full_scale = hq.resolve_levels(meta, source.bit_depth, args.black, args.full_scale) - spectrum = SpectrumData(hq, source, image, original, indices, roi, args, black, full_scale) + spectrum = SpectrumData(hq, source, image, original, indices, roi, args, black, full_scale, + geometry=geometry) finally: source.close() tell(f"recording : {Path(args.input).expanduser().resolve()}") tell(f"frames : {indices[0]}-{indices[-1]} (mean of {len(indices)})") tell(f"ROI : {roi.text()} ({roi.width} x {roi.height} px)") + if geometry.shear: + tell(f"geometry : {geometry}") + tell(f" {geometry.method or 'geometry file'}") + else: + tell(f"de-tilt : none (run spectro_prep.py to measure and apply)") tell(f"black level : {black:g} ADU full scale {full_scale:g} ADU") tell("noise (ADU) : " + ", ".join(f"{c}={spectrum.noise[c]:.1f}" for c in hq.CHANNELS)) if calibration is not None: @@ -1336,7 +1350,7 @@ def analyse(args, tell=print) -> "Analysis": line["sample_step_px"] = spectrum.step_px if getattr(args, "match", None): match_lines(lines, load_line_table(args.match), args.match_tolerance) - return Analysis(hq, spectrum, lines, calibration, indices, per_channel) + return Analysis(hq, spectrum, lines, calibration, indices, per_channel, geometry) def run(args) -> int: @@ -1366,7 +1380,8 @@ def run(args) -> int: 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())} + "recording": str(Path(args.input).expanduser().resolve()), + "geometry": analysis.geometry.as_dict() if analysis.geometry else None} result = export_all(hq, spectrum, lines, calibration, selected, args, meta_out) tell("") diff --git a/spectro_core.py b/spectro_core.py index 45c9946..d2eddea 100644 --- a/spectro_core.py +++ b/spectro_core.py @@ -858,3 +858,211 @@ 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"] + + +# --------------------------------------------------------------- geometry +class Geometry: + """Crop + de-tilt + row range, shared by every tool that reads a recording. + + ``shear_px_per_row`` is dx/drow of a spectral line: the centre moves right by + that many sensor pixels per sensor row. ``row_origin_px`` is the sensor row + where the shear is zero. + """ + + VERSION = 1 + + def __init__(self, roi, shear_px_per_row: float = 0.0, row_origin_px: float | None = None, + source: str = "", method: str = "", tilt_deg: float = float("nan"), + notes: str = ""): + if hasattr(roi, "x0"): # hq_spectrum.ROI object + roi = (roi.x0, roi.y0, roi.x1, roi.y1) + self.roi = tuple(int(value) for value in roi) + if len(self.roi) != 4 or self.roi[0] >= self.roi[2] or self.roi[1] >= self.roi[3]: + raise ValueError("geometry roi must be x0,y0,x1,y1 with x0 < x1 and y0 < y1") + self.shear = float(shear_px_per_row) + if not math.isfinite(self.shear): + raise ValueError("shear must be finite") + self.row_origin = float(row_origin_px if row_origin_px is not None + else 0.5 * (self.roi[1] + self.roi[3])) + self.source = source + self.method = method + self.tilt_deg = float(tilt_deg) + self.notes = notes + + def as_dict(self) -> dict: + return { + "format": "spectro-geometry", "version": self.VERSION, + "roi": list(self.roi), "roi_text": f"({self.roi[0]},{self.roi[1]})-({self.roi[2]},{self.roi[3]})", + "shear_px_per_row": self.shear, + "shear_sign_convention": "dx/drow; positive = line centre moves right as the row grows", + "row_origin_px": self.row_origin, + "tilt_deg": self.tilt_deg, + "source": self.source, "method": self.method, "notes": self.notes, + } + + @classmethod + def from_dict(cls, data: dict) -> "Geometry": + if not isinstance(data, dict): + raise ValueError("geometry JSON must contain an object") + if data.get("format") not in (None, "spectro-geometry"): + raise ValueError(f"not a geometry file: {data.get('format')}") + roi = data.get("roi") + if not isinstance(roi, (list, tuple)) or len(roi) != 4: + raise ValueError("geometry needs a roi of four numbers") + return cls(roi, data.get("shear_px_per_row", 0.0), data.get("row_origin_px"), + source=str(data.get("source", "")), method=str(data.get("method", "")), + tilt_deg=float(data.get("tilt_deg", float("nan"))), + notes=str(data.get("notes", ""))) + + @classmethod + def load(cls, path) -> "Geometry": + path = Path(path).expanduser() + try: + data = json.loads(path.read_text(encoding="utf-8-sig")) + except OSError as exc: + raise ValueError(f"cannot read geometry {path}: {exc}") from exc + except json.JSONDecodeError as exc: + raise ValueError(f"geometry {path} is not valid JSON: {exc}") from exc + return cls.from_dict(data) + + @classmethod + def from_spec(cls, spec, full_size, detect_roi) -> "Geometry": + """Build from 'auto', 'x0,y0,x1,y1' or a geometry JSON path.""" + text = str(spec).strip() + if text.lower().endswith(".json"): + return cls.load(text) + if text.lower() == "auto": + roi = detect_roi() + if roi is None: + raise ValueError("auto ROI failed; specify --roi x0,y0,x1,y1 " + "or run spectro_prep.py") + return cls(roi, method="auto ROI") + parts = [piece for piece in text.replace(";", ",").split(",") if piece.strip()] + if len(parts) != 4: + raise ValueError("--roi needs 'auto', x0,y0,x1,y1 or a geometry JSON path") + try: + roi = [int(float(piece)) for piece in parts] + except ValueError as exc: + raise ValueError(f"cannot parse ROI {text!r}") from exc + width, height = full_size + roi = [max(0, roi[0]), max(0, roi[1]), min(int(width), roi[2]), min(int(height), roi[3])] + return cls(roi, method="explicit crop") + + def __str__(self) -> str: + text = (f"({self.roi[0]},{self.roi[1]})-({self.roi[2]},{self.roi[3]}) " + f"{self.roi[2] - self.roi[0]}x{self.roi[3] - self.roi[1]} px") + if self.shear: + text += f", shear {self.shear:+.4f} px/row ({self.tilt_deg:+.2f} deg)" + return text + + +def shear_planes(planes: dict, geometry: Geometry, step_px: float = 2.0, + y0: int | None = None) -> dict: + """Shift every plane row horizontally so a tilted line becomes vertical. + + ``planes[channel][i, j]`` samples sensor row ``y0 + step*i`` and column + ``x0 + step*j``; the row is resampled at ``j + shear*(row - row_origin)/step``. + """ + + if not geometry.shear: + return planes + origin = y0 if y0 is not None else geometry.roi[1] + aligned = {} + for channel, plane in planes.items(): + plane = np.asarray(plane) + count = plane.shape[0] + rows = origin + step_px * np.arange(count) + offsets = geometry.shear * (rows - geometry.row_origin) / step_px + if np.allclose(offsets, 0.0): + aligned[channel] = plane + continue + columns = np.arange(plane.shape[1]) + out = np.empty(plane.shape, dtype=np.float64) + for index, offset in enumerate(offsets): + row = plane[index].astype(np.float64) + out[index] = np.interp(columns + offset, columns, row, + left=row[0], right=row[-1]) + aligned[channel] = out + return aligned + + +def measure_tilt(planes: dict, index: int, half: int, black: float, channel: str = "G", + step_px: float = 2.0, limit_px_per_row: float = 0.12, + bins: int = 8) -> dict: + """dx/drow of one line, found by making the line as sharp as possible. + + For a trial slope every row is resampled horizontally by + `slope*(row - centre)/step`, the rows are averaged and the peak height of + that average is scored; the slope that maximises it is the tilt. Unlike + band cross-correlation this does not depend on how the bands are cut. + """ + + plane = np.asarray(planes[channel]) + n_rows, n_cols = plane.shape + lo, hi = max(0, index - half), min(n_cols, index + half + 1) + crop = plane[:, lo:hi].astype(np.float64) - black + np.maximum(crop, 0.0, out=crop) + usable = n_rows - n_rows % bins + if usable < bins * 8: + raise ValueError("not enough rows to measure tilt") + crop = crop[:usable].reshape(usable // bins, bins, hi - lo).mean(axis=1) + rows = (np.arange(usable, dtype=np.float64).reshape(-1, bins).mean(axis=1)) * step_px + strength = crop.max(axis=1) + keep = strength > 0.15 * strength.max() + crop, rows = crop[keep], rows[keep] + if crop.shape[0] < 8: + raise ValueError("the line is too faint over the slit to measure tilt") + columns = np.arange(hi - lo, dtype=np.float64) + offsets = (rows - rows.mean()) / step_px + first = crop.mean(axis=0) + if np.argmax(first) < 0.15 * first.size or np.argmax(first) > 0.85 * first.size: + raise ValueError("line is at the window edge; pass its real column index") + + def profile_for(slope: float) -> np.ndarray: + total = np.zeros(hi - lo, dtype=np.float64) + for row, offset in zip(crop, offsets * slope): + total += np.interp(columns + offset, columns, row, left=row[0], right=row[-1]) + total /= crop.shape[0] + edge = np.linspace(total[:3].mean(), total[-3:].mean(), total.size) + return total - edge + + def sharpness(slope: float) -> float: + return float(profile_for(slope).max()) + + grid = np.linspace(-limit_px_per_row, limit_px_per_row, 49) + scores = [sharpness(slope) for slope in grid] + best = float(grid[int(np.argmax(scores))]) + low, high = best - (grid[1] - grid[0]), best + (grid[1] - grid[0]) + for _ in range(40): # golden-section refinement + left = low + 0.382 * (high - low) + right = low + 0.618 * (high - low) + if sharpness(left) > sharpness(right): + high = right + else: + low = left + slope = 0.5 * (low + high) + profile = profile_for(slope) + noise = estimate_noise(profile) + span = float(rows.max() - rows.min()) + return { + "slope": float(slope), "angle_deg": float(np.degrees(np.arctan(slope))), + "span_px": span, "pp_shift_px": float(slope * span), + "sharpness": float(profile.max()), + "sharpness_ratio": float(profile.max() / max(profile_for(0.0).max(), 1e-12)), + "noise_adu": float(noise), "rows": [int(rows.min() / step_px * 2), int(rows.max() / step_px * 2)], + "bins": int(crop.shape[0]), + } + + +def _shift_between(reference: np.ndarray, other: np.ndarray) -> tuple[float, float]: + """Sub-sample shift of `other` against `reference`, plus the correlation peak.""" + + correlation = np.correlate(other, reference, mode="full") + index = int(np.argmax(correlation)) + offset = 0.0 + if 0 < index < correlation.size - 1: + left, middle, right = correlation[index - 1], correlation[index], correlation[index + 1] + denominator = left - 2.0 * middle + right + if denominator: + offset = 0.5 * (left - right) / denominator + return index - (reference.size - 1) + offset, float(correlation[index]) diff --git a/spectro_prep.py b/spectro_prep.py new file mode 100644 index 0000000..6527aa5 --- /dev/null +++ b/spectro_prep.py @@ -0,0 +1,218 @@ +#!/usr/bin/env python3 +"""Define the analysis geometry once: crop, de-tilt, row range — and preview it. + +Writes ``geometry.json`` (plus a preview PNG) that peak_finder.py, +wavelength_calibration.py and anything else can consume with ``--roi ``. +The file records what was done and how the tilt was measured, so a later reader +does not have to guess where the numbers came from. +""" +from __future__ import annotations + +import argparse +import sys +from pathlib import Path + +import numpy as np + +try: # shared core lives beside this script + import spectro_core as core +except ImportError as exc: # pragma: no cover - placement error + raise SystemExit('spectro_prep.py needs spectro_core.py in the same directory ' + '(or on sys.path)') from exc + +TILT_LIMIT_DEG = 0.20 # below this a measured tilt is not applied + + +def build_parser() -> argparse.ArgumentParser: + p = argparse.ArgumentParser(description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + p.add_argument("--input", required=True, metavar="REC_DIR", help="hq_record recording") + p.add_argument("--frame", type=int, default=0, help="zero-based frame (default 0)") + p.add_argument("--roi", default="auto", + help="'auto', x0,y0,x1,y1, or an existing geometry JSON to start from") + p.add_argument("--rows", default=None, metavar="Y0,Y1", + help="restrict the analysed sensor rows inside the crop") + p.add_argument("--shear", default="auto", metavar="auto|0|PXPK", + help="de-tilt slope in px per row; 'auto' measures it (default auto)") + p.add_argument("--output", default=None, metavar="DIR", + help="output directory (default geometry/)") + p.add_argument("--module-dir", default=None, + help="directory containing hq_spectrum.py (default this folder)") + p.add_argument("--quiet", action="store_true") + return p + + +def measure(hq, image, pattern, roi, black) -> dict: + """Measure the tilt on every usable line and take the median.""" + + planes = hq.extract_planes(image, pattern, roi) + columns = planes["G"].shape[1] + profile = np.mean(planes["G"], axis=0, dtype=np.float64) - black + net = profile - core.estimate_baseline(profile, 50, hq.smooth_vector) + peaks = core.find_peak_candidates(net, 3, 0.10 * net.max(), 0.02 * net.max(), 8) + peaks = [peak for peak in peaks if net[peak["sample"]] > 0.10 * net.max()] + if not peaks: + raise ValueError("no strong line to measure tilt; pass --shear 0 to skip") + + samples = [] + for peak in sorted(peaks, key=lambda item: -net[item["sample"]]): + index = peak["sample"] + if any(abs(other["sample"] - index) * 2.0 < 30 for other in peaks if other is not peak): + continue # a neighbour would bias the profile + width = relative_half_width(net, index) + if width > 10: # broad continuum, not a line + continue + try: + result = core.measure_tilt(planes, index, 3 * width, black) + except ValueError: + continue + if result["sharpness_ratio"] < 1.05: + continue # straightening the rows buys nothing + result["x_px"] = float(hq.channel_x_positions(roi, pattern, columns, columns)["G"][index]) + samples.append(result) + if not samples: + raise ValueError("could not measure tilt; pass --shear 0 to skip") + + slopes = np.array([sample["slope"] for sample in samples]) + median = float(np.median(slopes)) + agreed = [sample for sample in samples if abs(sample["slope"] - median) <= 0.010] + slopes = np.array([sample["slope"] for sample in agreed]) + span = float(np.median([sample["span_px"] for sample in agreed])) + return { + "slope": float(np.median(slopes)), + "angle_deg": float(np.degrees(np.arctan(np.median(slopes)))), + "spread_deg": float(np.degrees(np.arctan(np.std(slopes)))), + "span_px": span, + "pp_shift_px": float(np.median(slopes) * span), + "lines": len(agreed), + "lines_measured": len(samples), + "samples": [{key: sample[key] for key in + ("x_px", "slope", "angle_deg", "pp_shift_px", "sharpness_ratio", "bins")} + for sample in agreed], + } + + +def relative_half_width(net: np.ndarray, index: int, limit: int = 60) -> int: + peak = net[index] + left = index + while left > 0 and net[left] > 0.5 * peak and index - left < limit: + left -= 1 + right = index + while right < net.size - 1 and net[right] > 0.5 * peak and right - index < limit: + right += 1 + return max(2, (right - left) // 2) + + +def plot_preview(path: Path, hq, image, pattern, geometry: core.Geometry, tilt: dict | None, + full_size) -> None: + import matplotlib + matplotlib.use("Agg") + import matplotlib.pyplot as plt + from matplotlib.patches import Rectangle + + preview = hq.make_preview(image, pattern) + width, height = full_size + figure, axis = plt.subplots(figsize=(11, 5.6), constrained_layout=True) + axis.imshow(preview, extent=(0, width, height, 0), interpolation="nearest") + x0, y0, x1, y1 = geometry.roi + axis.add_patch(Rectangle((x0, y0), x1 - x0, y1 - y0, fill=False, + edgecolor="#f39c12", lw=1.2)) + if tilt is not None: + sample = (tilt.get("samples") or [{}])[0] + x_centre = float(sample.get("x_px", 0.5 * (x0 + x1))) + row_top, row_bottom = float(y0), float(y1) + origin = geometry.row_origin + axis.plot([x_centre + geometry.shear * (row_top - origin), + x_centre + geometry.shear * (row_bottom - origin)], + [row_top, row_bottom], color="#00e5ff", lw=1.4) + axis.annotate(f"tilt {tilt['angle_deg']:+.2f} deg, {tilt['pp_shift_px']:+.1f} px " + f"over {tilt['span_px']:.0f} rows, {tilt['lines']} line(s)", + (x_centre, row_top), xytext=(8, -12), textcoords="offset points", + color="#00e5ff", fontsize=9) + axis.set_title(f"geometry: ROI ({x0},{y0})-({x1},{y1}), " + f"shear {geometry.shear:+.4f} px/row", fontsize=10) + axis.set_xlabel("Sensor column / px") + axis.set_ylabel("Sensor row / px") + figure.savefig(path, dpi=150) + plt.close(figure) + + + +def run(args) -> int: + hq = core.load_hq(args.module_dir) + tell = (lambda *a, **k: None) if args.quiet else print + source = hq.RecordingSource(args.input) + try: + source.start() + image, frame_meta = source.read_at(args.frame) + full_size = tuple(source.full_size) + pattern = source.pattern + black, full_scale = hq.resolve_levels( + {**source.base_meta, **{k: v for k, v in frame_meta.items() if not str(k).startswith("_")}}, + source.bit_depth, None, None) + start_geometry = core.Geometry.from_spec(args.roi, full_size, lambda: hq.detect_auto_roi( + hq.make_preview(image, pattern), *full_size)) + roi = hq.parse_roi(",".join(str(value) for value in start_geometry.roi), *full_size) + if args.rows: + parts = [piece for piece in str(args.rows).split(",") if piece.strip()] + if len(parts) != 2: + raise ValueError("--rows needs Y0,Y1") + row0, row1 = sorted(int(float(piece)) for piece in parts) + roi = roi._replace(y0=max(roi.y0, row0), y1=min(roi.y1, row1)) + if roi.y0 >= roi.y1: + raise ValueError("--rows does not overlap the crop") + tilt = None + if str(args.shear).lower() == "auto": + try: + tilt = measure(hq, image, pattern, roi, black) + except ValueError as exc: + tell(f"tilt : not measured ({exc})") + shear = tilt["slope"] if tilt and abs(tilt["angle_deg"]) >= TILT_LIMIT_DEG else 0.0 + if str(args.shear).lower() not in ("auto",): + shear = float(args.shear) + finally: + source.close() + + geometry = core.Geometry(roi, shear, row_origin_px=0.5 * (roi.y0 + roi.y1), + source=str(Path(args.input).expanduser().resolve()), + method="auto ROI + measured tilt" if tilt else "auto ROI", + tilt_deg=tilt["angle_deg"] if tilt else float("nan")) + out = Path(args.output) if args.output else Path("geometry") / Path(args.input).name + out.mkdir(parents=True, exist_ok=True) + core.write_json(out / "geometry.json", { + **geometry.as_dict(), + "frame": args.frame, "frames": [args.frame], + "full_size": list(full_size), "pattern": pattern, + "raw_format": source.raw_format, "bit_depth": source.bit_depth, + "black_adu": black, "full_scale_adu": full_scale, + "tilt_measurement": tilt, + "created": __import__("datetime").datetime.now().isoformat(timespec="seconds"), + }) + plot_preview(out / "geometry_preview.png", hq, image, pattern, geometry, tilt, full_size) + + tell(f"recording : {Path(args.input).expanduser().resolve()}") + tell(f"frame : {args.frame}") + tell(f"ROI : ({roi.x0},{roi.y0})-({roi.x1},{roi.y1}) {roi.width} x {roi.height} px") + if tilt is not None: + tell(f"tilt : {tilt['angle_deg']:+.2f} deg ({tilt['slope']:+.4f} px/row, " + f"{tilt['pp_shift_px']:+.1f} px over {tilt['span_px']:.0f} rows; " + f"{tilt['lines']}/{tilt['lines_measured']} lines agree within " + f"{tilt['spread_deg']:.2f} deg)") + tell(f"shear : {shear:+.4f} px/row" + + (" (disabled: below %.1f deg)" % TILT_LIMIT_DEG + if tilt and abs(tilt["angle_deg"]) < TILT_LIMIT_DEG else "")) + tell(f"written : {out.resolve()}") + return 0 + + +def main(argv=None) -> int: + args = build_parser().parse_args(argv) + try: + return run(args) + except (ValueError, RuntimeError, OSError) as exc: + print(f"Geometry error: {exc}", file=sys.stderr) + return 2 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/test_geometry.py b/test_geometry.py new file mode 100644 index 0000000..eb65180 --- /dev/null +++ b/test_geometry.py @@ -0,0 +1,132 @@ +"""Tests for the geometry stage: crop + de-tilt + provenance.""" +from __future__ import annotations + +import json +import os +import sys +import tempfile +import unittest +import uuid +from pathlib import Path + +import numpy as np + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) + +import spectro_core as core # noqa: E402 + +TEST_TMP = os.environ.get("PEAK_FINDER_TEST_TMP") + + +def scratch() -> Path: + base = Path(TEST_TMP) if TEST_TMP else Path(tempfile.gettempdir()) + base.mkdir(parents=True, exist_ok=True) + path = base / f"geo_case_{uuid.uuid4().hex[:10]}" + path.mkdir() + return path + + +def tilted_plane(rows: int, cols: int, slope_px_per_row: float, black: float = 4096.0, + amplitude: float = 9000.0, sigma: float = 1.6) -> np.ndarray: + """A Gaussian line whose centre moves by slope_px_per_row per sensor row.""" + plane = np.zeros((rows, cols)) + columns = np.arange(cols) + for index in range(rows): + row = 2.0 * index # sensor row of this plane row + centre = 0.4 * cols + slope_px_per_row * (row - rows) / 2.0 + plane[index] = amplitude * np.exp(-0.5 * ((columns - centre) / sigma) ** 2) + black + return plane + + +class GeometryTests(unittest.TestCase): + def test_roundtrip_json(self): + directory = scratch() + try: + geometry = core.Geometry((24, 474, 1558, 964), 0.031, row_origin_px=719.0, + source="rec", method="test", tilt_deg=1.2) + path = directory / "geometry.json" + core.write_json(path, {**geometry.as_dict(), "frame": 0}) + loaded = core.Geometry.load(path) + self.assertEqual(loaded.roi, (24, 474, 1558, 964)) + self.assertAlmostEqual(loaded.shear, 0.031, places=6) + self.assertAlmostEqual(loaded.row_origin, 719.0, places=3) + self.assertAlmostEqual(loaded.tilt_deg, 1.2, places=3) + finally: + import shutil + shutil.rmtree(directory, ignore_errors=True) + + def test_from_spec_accepts_crop_and_file(self): + directory = scratch() + try: + path = directory / "g.json" + path.write_text(json.dumps({"format": "spectro-geometry", + "roi": [10, 20, 100, 200], + "shear_px_per_row": 0.02}), encoding="utf-8") + loaded = core.Geometry.from_spec(str(path), (2028, 1520), lambda: None) + self.assertEqual(loaded.roi, (10, 20, 100, 200)) + self.assertAlmostEqual(loaded.shear, 0.02, places=6) + + crop = core.Geometry.from_spec("0,100,500,900", (2028, 1520), lambda: None) + self.assertEqual(crop.roi, (0, 100, 500, 900)) + self.assertEqual(crop.shear, 0.0) + finally: + import shutil + shutil.rmtree(directory, ignore_errors=True) + + def test_invalid_roi_is_rejected(self): + with self.assertRaises(ValueError): + core.Geometry((100, 200, 100, 300)) + with self.assertRaises(ValueError): + core.Geometry.from_spec("1,2,3", (2028, 1520), lambda: None) + + def test_shear_removes_a_known_tilt(self): + rows, cols = 120, 200 + slope = 0.05 # px per sensor row + plane = tilted_plane(rows, cols, slope) + + def drift(data): + columns = np.arange(cols) + centres = [] + for row in data: + weight = np.clip(row.astype(float) - 4096.0, 0.0, None) + centres.append(float((weight * columns).sum() / max(weight.sum(), 1e-9))) + return centres + + before = drift(plane) + geometry = core.Geometry((0, 0, 2 * cols, 2 * rows), slope, row_origin_px=rows) + fixed = core.shear_planes({"G": plane}, geometry, 2.0, 0)["G"] + after = drift(fixed) + self.assertGreater(abs(before[-1] - before[0]), 5.0) + self.assertLess(abs(after[-1] - after[0]), 1.0) + + def test_shear_of_zero_returns_the_input(self): + plane = np.zeros((10, 20)) + geometry = core.Geometry((0, 0, 40, 20)) + self.assertIs(core.shear_planes({"G": plane}, geometry)["G"], plane) + + +class TiltTests(unittest.TestCase): + def test_measure_tilt_recovers_a_known_slope(self): + rows, cols = 240, 160 + slope = 0.06 + plane = tilted_plane(rows, cols, slope) + result = core.measure_tilt({"G": plane}, 64, 12, 4096.0) + self.assertAlmostEqual(result["slope"], slope, delta=0.01) + self.assertAlmostEqual(result["angle_deg"], np.degrees(np.arctan(slope)), delta=0.6) + self.assertGreater(result["sharpness_ratio"], 1.05) + + def test_measure_tilt_reports_no_tilt_for_a_vertical_line(self): + plane = tilted_plane(240, 160, 0.0) + result = core.measure_tilt({"G": plane}, 64, 12, 4096.0) + self.assertAlmostEqual(result["slope"], 0.0, delta=0.01) + self.assertLess(result["sharpness_ratio"], 1.05) + + def test_measure_tilt_needs_rows(self): + plane = tilted_plane(8, 160, 0.05) + with self.assertRaises(ValueError): + core.measure_tilt({"G": plane}, 64, 12, 4096.0) + + +if __name__ == "__main__": + unittest.main()