diff --git a/bin/debug/scan_artefact_check.py b/bin/debug/scan_artefact_check.py index 760ce151..0552e26d 100755 --- a/bin/debug/scan_artefact_check.py +++ b/bin/debug/scan_artefact_check.py @@ -167,6 +167,30 @@ def infer_grid(n: int, meta: dict) -> tuple[int, int] | None: return None +def line_breaks(arr: np.ndarray) -> np.ndarray | None: + """Indices where a scanned axis starts a new line. + + Returns the boundaries themselves rather than a count, because the monitored + stream is sampled opportunistically: the number of samples per line varies, + so the trace cannot be reshaped into a rectangle. Segmenting on the actual + boundaries handles ragged lines, which is what these files contain. + """ + d = np.diff(arr) + nz = np.abs(d[d != 0]) + if nz.size < 4: + return None + typical = float(np.median(nz)) + if typical <= 0: + return None + for cand in (np.where(np.abs(d) > 5 * typical)[0] + 1, # raster flyback + np.where(np.diff(np.sign(np.where(d == 0, 1, d))) != 0)[0] + 1): # snake + if cand.size >= 2: + sizes = np.diff(np.concatenate(([0], cand, [arr.size]))) + if sizes.min() >= 2 and sizes.size >= 3: + return cand + return None + + def detect_lines(arr: np.ndarray) -> int | None: """Recover the number of lines from a scanned axis, without metadata. @@ -266,8 +290,6 @@ def check_end_of_line(signals, grid, monitored, fast_axis) -> None: if arr.size != lines * per_line: continue dev = name.split("/")[0] - if monitored and dev not in monitored: - continue if fast_axis and name == fast_axis: continue block = arr.reshape(lines, per_line) @@ -296,9 +318,6 @@ def check_end_of_line(signals, grid, monitored, fast_axis) -> None: for name, arr in sorted(signals.items()): if arr.size != lines * per_line: continue - dev = name.split("/")[0] - if monitored and dev not in monitored: - continue block = arr.reshape(lines, per_line) if monotonicity(block) > 0.9: continue @@ -351,25 +370,59 @@ def check_line_motion_from_motor(signals, motors, meta) -> None: return expected = int(meta["num_lines"]) if meta.get("num_lines") else None - best = None + + # Segment on the SLOW axis, not the fast one. The fast axis cannot define its + # own line boundaries here: a line where motion failed has no flyback, so it + # merges into its neighbour and the resulting segment looks perfectly healthy + # -- the stall erases the very evidence of itself. The slow axis steps once per + # line regardless of what the fast axis did, so it stays a valid ruler. + slow_name, slow_breaks = None, None for name, arr in sorted(cands.items()): - found = detect_lines(arr) - if found and (best is None or (expected and abs(found - expected) < abs(best[1] - expected))): - best = (name, found, arr) - if best is None: + changes = np.where(np.abs(np.diff(arr)) > 0)[0] + 1 + if changes.size == 0: + continue + if expected and abs(changes.size - (expected - 1)) <= max(1, 0.05 * expected): + slow_name, slow_breaks = name, changes + break + if slow_breaks is not None: + print(f" lines delimited by the slow axis {slow_name}" + f" ({slow_breaks.size + 1} segments)") + + scored = [] + for name, arr in sorted(cands.items()): + if name == slow_name: + continue + breaks = slow_breaks if slow_breaks is not None else line_breaks(arr) + if breaks is None: + continue + starts = np.concatenate(([0], breaks, [arr.size])) + segs = [arr[a:b] for a, b in zip(starts[:-1], starts[1:])] + spans = np.array([np.ptp(sg) for sg in segs]) + if np.median(spans) <= 0: + continue + scored.append((name, arr, segs, spans)) + if not scored: print(f" positioner traces found ({', '.join(sorted(cands))}) but none shows") - print(" a line structure -- the axis may not have been the scanned one.") + print(" a line structure. For a continuous scan the position is sampled") + print(" while the line runs, so an irregular or too-sparse trace can hide it.") return - name, found, arr = best - per_line = arr.size // found - print(f" fast axis : {name} ({arr.size} samples, {found} lines detected" - + (f", metadata says {expected}" if expected else "") + ")") - if expected and found != expected: - print(f" NOTE: detected {found} lines but metadata says {expected}." - " Treat the per-line numbers below with care.") - block = arr[: found * per_line].reshape(found, per_line) - spans = np.ptp(block, axis=1) + # Prefer the trace whose travel per line is largest -- that is the scanned axis; + # the slow axis steps a little and the rest sit still. + scored.sort(key=lambda t: -float(np.median(t[3]))) + if len(scored) > 1: + others = ", ".join(f"{n} (median span {np.median(sp):.4g}, {len(sg)} segments)" + for n, _, sg, sp in scored[1:4]) + print(f" other positioner traces: {others}") + name, arr, segs, spans = scored[0] + found = len(segs) + per_line = int(np.median([len(sg) for sg in segs])) + print(f" fast axis : {name} ({arr.size} samples, {found} line segments" + + (f", metadata says {expected} lines" if expected else "") + ")") + if expected and abs(found - expected) > max(1, 0.02 * expected): + print(f" NOTE: segmented into {found} but metadata says {expected}. The extra" + " or missing") + print(" boundaries are themselves suspicious -- a stalled line has no flyback.") median = float(np.median(spans)) if median <= 0: print(" the axis does not move -- check that this is the scanned motor")