diff --git a/benchmarks/uipsf/real/m2_pairs/edge_vs_time.py b/benchmarks/uipsf/real/m2_pairs/edge_vs_time.py new file mode 100644 index 00000000..a841caa4 --- /dev/null +++ b/benchmarks/uipsf/real/m2_pairs/edge_vs_time.py @@ -0,0 +1,98 @@ +"""Did the focus drift over the movie? The coverslip's edge per block of time, one model held. + + python edge_vs_time.py OUTDIR MODEL [MODEL ...] [--blocks 10] [--json FILE] + +The nucleus movie is ~72 000 frames under the focus lock. The in situ models learn one focus for all of it; if +the focus wandered, the molecules' heights wandered with it, the depth signal that tells the even aberrations +apart is blurred, and the learning may put the blur into the pupil (an open hypothesis of architecture.md, "In +situ against molecule stacks on M2"). With the pupil held, a drift of the focus shows directly: the molecules +on the coverslip are found at a height that moves. Each block's edge (`coverslip_edge`) is that height. + +Per model (``heights_.npz`` from `heights.py`): the units ordered by movie and frame, cut into ``blocks`` +of equal count, each block's edge with its error, the spike fraction, and the median and 90th percentile of its +heights; then a weighted line through the edges, in nm per 10 000 frames. The frame is counted across the movies +(each movie's frames after the previous movie's last frame seen), so the line's axis is the acquisition's order. + +What to read: edges that stay within ~10 nm say the focus held; a trend or a jump of tens of nm says it did not, +and then a focus per block (or per movie) in the learning is the next thing to try. The median height follows +where the molecules are as well as the focus, so it is shown, not fitted. +""" +import argparse +import json +import os +import sys + +import numpy as np + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from heights import coverslip_edge, load # noqa: E402 + + +def acquisition_frame(frame, source) -> np.ndarray: + """The frame counted across the movies: each movie after the previous one's last frame seen.""" + frame, source = np.asarray(frame, int), np.asarray(source, int) + out = frame.copy() + offset = 0 + for s in np.unique(source): + sel = source == s + out[sel] = frame[sel] + offset + offset += int(frame[sel].max()) + 1 + return out + + +def block_edges(t, h, sd, blocks: int): + order = np.argsort(t, kind="stable") + rows = [] + for part in np.array_split(order, blocks): + e = coverslip_edge(h[part], sd[part]) + if e is None or not np.isfinite(e.sd_nm): + continue + rows.append(dict(frames=(int(t[part].min()), int(t[part].max())), t=float(np.median(t[part])), + edge_nm=e.shift_nm, sd_nm=e.sd_nm, spike=e.spike, n=len(part), + median_nm=float(np.nanmedian(h[part])), p90_nm=float(np.nanpercentile(h[part], 90)))) + return rows + + +def trend(rows): + """Weighted line edge = a + b·t: b in nm per 10 000 frames, its error (χ²-scaled), χ² per degree of freedom.""" + t = np.array([r["t"] for r in rows]) / 1e4 + z = np.array([r["edge_nm"] for r in rows]) + w = 1 / np.maximum(np.array([r["sd_nm"] for r in rows]), 1.0) ** 2 + A = np.c_[np.ones_like(t), t] + cov = np.linalg.inv(A.T @ (A * w[:, None])) + coef = cov @ (A.T @ (w * z)) + dof = max(len(z) - 2, 1) + chi2 = float(np.sum(w * (z - A @ coef) ** 2) / dof) + return float(coef[1]), float(np.sqrt(cov[1, 1] * max(chi2, 1.0))), chi2 + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("out") + ap.add_argument("names", nargs="+") + ap.add_argument("--blocks", type=int, default=10) + ap.add_argument("--json") + a = ap.parse_args() + for name in a.names: + d = load(a.out, name) + t = acquisition_frame(d["frame"], d["source"]) + rows = block_edges(t, d["h"], d["sd_h"], a.blocks) + if len(rows) < 3: + print(f"{name}: only {len(rows)} blocks with an edge; fewer --blocks") + continue + slope, err, chi2 = trend(rows) + edges = np.array([r["edge_nm"] for r in rows]) + print(f"\n{name}: {len(rows)} blocks; edge {slope:+.1f} ± {err:.1f} nm per 10 000 frames, range " + f"{edges.min():+.0f} to {edges.max():+.0f} nm, χ²/dof about the line {chi2:.1f}") + for r in rows: + print(f" frames {r['frames'][0]:6d}-{r['frames'][1]:6d}: edge {r['edge_nm']:+7.1f} ± {r['sd_nm']:5.1f} nm" + f" spike {r['spike']:.2f} median {r['median_nm']:6.0f} p90 {r['p90_nm']:6.0f} n {r['n']}") + if a.json: + with open(a.json, "a") as f: + f.write(json.dumps(dict(kind="edge_vs_time", model=name, blocks=a.blocks, + slope_nm_per_10k_frames=slope, slope_err=err, chi2_dof=chi2, + block_rows=rows)) + "\n") + + +if __name__ == "__main__": + main() diff --git a/benchmarks/uipsf/real/m2_pairs/heights.py b/benchmarks/uipsf/real/m2_pairs/heights.py new file mode 100644 index 00000000..a23a1289 --- /dev/null +++ b/benchmarks/uipsf/real/m2_pairs/heights.py @@ -0,0 +1,112 @@ +"""Every molecule of M2's nucleus movie localised once per model, saved, for the analyses that read heights. + + python heights.py OUTDIR MODEL [MODEL ...] [--states "used,held out,not drawn"] + +Writes ``OUTDIR/heights_.npz`` per model: each unit's id and state, its height above the coverslip +``h`` (nm, the in situ geometry's), the height's CRLB ``sd_h``, ½ Σ deviance (the score `cross_score.py` uses), +photons, its place on the chip (``x_px``, ``y_px``) and its frame and movie. Each unit is localised from +`STARTS_NM` with x, y, z, photons and background free, the best start kept (`cross_score.insitu_scores`'s +procedure). A MODEL is a saved ``_3dcal.h5`` in OUTDIR; an in situ model uses its own focus, so a stack +model enters through the in situ run that held every aberration of it (``insitu_all_n1345_ab_n1345``). + +`movie_rois` with its defaults is deterministic, so every model's file holds the same units in the same order +(``units`` is checked by the readers). Units the selection rejected are not localised: they are pairs, +neighbours or off centre, and would only add noise to an edge. + +The readers: `tilt_from_edge.py` (the coverslip's edge per tile of the field), `edge_vs_time.py` (per time +block) and `score_by_height.py` (two models' scores per height bin). +""" +import argparse +import glob +import os + +import numpy as np +import torch + +# the data are not in git: M2_DATA names the folder that holds 261112_new's sets +ROOT = os.environ.get("M2_DATA", os.path.expanduser("~/Library/Mobile Documents/com~apple~CloudDocs/Vienna/Data/" + "smappy test/261112_new")) +MOVIE = "261112_MR_Intercalib2_BGAAF647_50ms_30i_nucleus_1/*_MMStack_Pos0.ome.tif" +PIXEL_NM = (127.0, 116.0) # iXon on M2's astigmatic path (docs/uipsf/microscopes.md) +STARTS_NM = (-200.0, 200.0, 600.0, 1000.0, 1400.0) +STATES = ("used", "held out", "not drawn") + + +def localise_all(model, shared: dict, batch, noise, starts=STARTS_NM) -> dict: + """Each emitter of ``batch`` localised from every height in ``starts``, the best (lowest ½ Σ deviance) + kept: {"h", "sd_h", "half_deviance", "photons"}, numpy [E].""" + from uipsf.solve import Problem + from uipsf.workflows.insitu import INSITU_CHUNK + from uipsf.workflows.scoring import localise + + E = batch.n_emitters + out = {k: np.full(E, np.nan) for k in ("h", "sd_h", "photons")} + out["half_deviance"] = np.full(E, np.inf) + for z0 in starts: + xyz = torch.zeros(E, 3, dtype=torch.float64) + xyz[:, 2] = z0 + fit, problem = localise(model, shared, batch, noise, start={"emitter.xyz": xyz}, chunk_size=INSITU_CHUNK) + dev = Problem(model, noise, problem.batch, INSITU_CHUNK).deviance(fit.params) + e = 0.5 * dev.reshape(E, -1).sum(1).double().cpu().numpy() + phys = fit.params.physical() + photons = phys["emitter.photons"] if "emitter.photons" in phys else phys["emitter.photons_per_slice"].mean(1) + better = e < out["half_deviance"] + out["half_deviance"][better] = e[better] + out["h"][better] = phys["emitter.xyz"][:, 2].detach().cpu().double().numpy()[better] + out["sd_h"][better] = fit.std["emitter.xyz"][:, 2].detach().cpu().double().numpy()[better] + out["photons"][better] = photons.detach().cpu().double().numpy()[better] + return out + + +def unit_table(rois, ids) -> dict: + """Where and when each unit ``ids`` of ``rois`` (`InSituROIs`) is: its state, chip pixel, first frame, + movie.""" + u = rois.units + source = np.zeros(len(u.reason), int) if u.source is None else np.asarray(u.source) + return {"units": np.asarray(ids), "state": np.array([rois.state[i] for i in ids]), + "x_px": np.asarray(u.x_px)[ids], "y_px": np.asarray(u.y_px)[ids], + "frame": np.array([int(np.min(u.frames[i])) for i in ids]), "source": source[ids]} + + +def path(out: str, name: str) -> str: + return os.path.join(out, f"heights_{name}.npz") + + +def load(out: str, name: str) -> dict: + with np.load(path(out, name), allow_pickle=False) as f: + return {k: f[k] for k in f.files} + + +def coverslip_edge(h, sd): + """The heights' lower edge (`uipsf.workflows.insitu_rounds.coverslip_edge`, the rounds' own fit), or None.""" + from uipsf.workflows.insitu_rounds import coverslip_edge as edge + ok = np.isfinite(h) & np.isfinite(sd) & (sd > 0) + return edge(np.asarray(h)[ok], np.asarray(sd)[ok]) + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("out") + ap.add_argument("names", nargs="+") + ap.add_argument("--states", default=",".join(STATES), help="comma-separated unit states to localise") + a = ap.parse_args() + from smappy.calibrate.insitu_run import movie_rois + from smappy.calibrate.pupil import load_start + + rois = movie_rois(sorted(glob.glob(os.path.join(ROOT, MOVIE)))) + states = [s.strip() for s in a.states.split(",")] + ids = np.sort(np.concatenate([rois.ids(s) for s in states])) + batch, noise = rois.batch(ids=ids) + table = unit_table(rois, ids) + print(f"{len(ids)} units ({', '.join(f'{s}: {len(rois.ids(s))}' for s in states)})", flush=True) + for name in a.names: + m = load_start(os.path.join(a.out, f"{name}_3dcal.h5")) + shared = {k: torch.as_tensor(v) for k, v in m.shared.items()} + found = localise_all(m.model, shared, batch, noise) + np.savez(path(a.out, name), **table, **found, focus_nm=float(torch.as_tensor(shared["geometry.focus"]))) + print(f" {name:44s} heights p5/50/95 {np.nanpercentile(found['h'], [5, 50, 95]).round(0).tolist()} nm, " + f"½Σdeviance {found['half_deviance'].mean():.2f} per unit -> {path(a.out, name)}", flush=True) + + +if __name__ == "__main__": + main() diff --git a/benchmarks/uipsf/real/m2_pairs/score_by_height.py b/benchmarks/uipsf/real/m2_pairs/score_by_height.py new file mode 100644 index 00000000..ff2acdc3 --- /dev/null +++ b/benchmarks/uipsf/real/m2_pairs/score_by_height.py @@ -0,0 +1,83 @@ +"""Where in z does each pupil fit the cell better? Models' scores on the same molecules, per height bin. + + python score_by_height.py OUTDIR REFERENCE MODEL [MODEL ...] [--bins -200,0,200,...] [--json FILE] + +`cross_score.py` found that on the nucleus movie's 223 *held out* molecules the in situ pupils beat the stack's +(held, the focus learnt: ``insitu_all_n1345_ab_n1345``) by 1.75-2.75 nats per molecule, while on the 4105 +*not drawn* -- the z bins' surplus, crowded where molecules are plentiful -- they lose by 0.36-0.50. Which +heights the surplus crowds at, and so where the stack's pupil is the better one, was not checked. This script +does: an excess the in situ pupils carry because of depth (the index, the nucleus) would win deep and lose near +the glass; one from something else would not depend on height. + +Every MODEL's ½ Σ deviance per molecule (``heights_.npz`` from `heights.py`), each molecule placed in a +height bin by REFERENCE's height (so a bin holds the same molecules for every model), and per bin the mean +difference against the first MODEL, with its standard error and count, for the *held out* and *not drawn* +molecules apart and together. The *used* are left out: the in situ models learnt from them. + +Negative: the model fits that bin's molecules better than the first MODEL (nats per molecule; 1 ≈ moving each +molecule by 1.4 CRLB). +""" +import argparse +import json +import os +import sys + +import numpy as np + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from heights import load # noqa: E402 + +BINS_NM = (-200.0, 0.0, 200.0, 400.0, 600.0, 800.0, 1000.0, 1200.0, 1600.0) +UNSEEN = ("held out", "not drawn") + + +def by_bin(height, delta, edges): + """Per bin of ``height``: (count, mean of ``delta``, its standard error).""" + idx = np.digitize(height, edges) - 1 + out = [] + for b in range(len(edges) - 1): + d = delta[(idx == b) & np.isfinite(delta)] + out.append((len(d), float(d.mean()) if len(d) else float("nan"), + float(d.std(ddof=1) / np.sqrt(len(d))) if len(d) > 1 else float("nan"))) + return out + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("out") + ap.add_argument("reference", help="the model whose heights place the molecules in bins") + ap.add_argument("names", nargs="+", help="models to compare; differences against the first") + ap.add_argument("--bins", default=",".join(f"{b:g}" for b in BINS_NM)) + ap.add_argument("--json") + a = ap.parse_args() + edges = np.array([float(b) for b in a.bins.split(",")]) + ref = load(a.out, a.reference) + data = {n: load(a.out, n) for n in a.names} + for n, d in data.items(): + if not np.array_equal(d["units"], ref["units"]): + raise SystemExit(f"{n} holds other units than {a.reference}: run heights.py with the same --states") + state, height = ref["state"], ref["h"] + base = data[a.names[0]]["half_deviance"] + labels = [f"{edges[b]:g}..{edges[b + 1]:g}" for b in range(len(edges) - 1)] + for n in a.names[1:]: + delta = data[n]["half_deviance"] - base + print(f"\n{n} - {a.names[0]}, nats per molecule by {a.reference}'s height (nm):") + print(f" {'height':>12s} " + " ".join(f"{s + ' (n)':>22s}" for s in (*UNSEEN, "both"))) + parts = {s: by_bin(height[state == s], delta[state == s], edges) for s in UNSEEN} + unseen = np.isin(state, UNSEEN) + parts["both"] = by_bin(height[unseen], delta[unseen], edges) + for b, label in enumerate(labels): + cells = [] + for s in (*UNSEEN, "both"): + k, m, se = parts[s][b] + cells.append(f"{m:+7.2f} ± {se:4.2f} ({k:5d})" if k else f"{'-':>22s}") + print(f" {label:>12s} " + " ".join(cells)) + if a.json: + with open(a.json, "a") as f: + f.write(json.dumps(dict(kind="score_by_height", model=n, against=a.names[0], + reference=a.reference, bins=edges.tolist(), + **{s.replace(" ", "_"): parts[s] for s in parts})) + "\n") + + +if __name__ == "__main__": + main() diff --git a/benchmarks/uipsf/real/m2_pairs/tilt_from_edge.py b/benchmarks/uipsf/real/m2_pairs/tilt_from_edge.py new file mode 100644 index 00000000..af2bb050 --- /dev/null +++ b/benchmarks/uipsf/real/m2_pairs/tilt_from_edge.py @@ -0,0 +1,121 @@ +"""Is the coverslip tilted? Its edge measured per tile of the field, a plane through the tiles, no pupil tilt. + + python tilt_from_edge.py OUTDIR MODEL [MODEL ...] [--tiles 3] [--stack STACK_MODEL] [--json FILE] + +The in situ runs on M2's nucleus movie learn a coverslip tilt of 1.6° about y (2.9° with every aberration of +the stack held), which takes most of their excess astigmatism, coma and spherical over the molecule stack +(architecture.md, "In situ against molecule stacks on M2"). That is a tilt of the interfaces in the *pupil*. +A real tilt would also raise the coverslip across the field: the field is ~26 µm wide, so 1.6° is ~28 nm per µm, +~700 nm across. The molecules' heights show that without any pupil: the lowest molecules of each tile lie on the +coverslip, and their edge (`coverslip_edge`, the rounds' own fit) moves with it. + +Per model (``heights_.npz`` from `heights.py`): the field cut into ``tiles`` × ``tiles``, each tile's edge +and its error, then a weighted plane through the tiles, edge = a + b_x·u + b_y·v (u, v in µm from the field's +centre). Reported: b_x, b_y in nm per µm with their errors (scaled by the fit's χ² where it exceeds 1), the +slope's size as an angle, and each tile's edge. A model without a learnt tilt should be used (the edge then +carries the whole tilt); the edge's slope barely depends on the pupil, since it is the molecules' positions. + +``--stack``: the molecule stack's own focal-plane slope (``geometry.plane``, nm per µm, `CoverslipStackGeometry`) +printed beside it -- the coverslip's tilt as the z sweeps saw it, two hours later. + +What to read: a slope of ~28-50 nm per µm along x would say the tilt is real; a slope within a few nm per µm +of 0 says the learnt tilt stands in for something else (the nucleus' index, focus drift, the pores' own +orientation). A tile without molecules on the glass (its spike fraction near 0) gives the lower edge of the +sample instead: inside a thick nucleus that is not the coverslip, so look at the spike column. +""" +import argparse +import json +import math +import os +import sys + +import numpy as np + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from heights import PIXEL_NM, coverslip_edge, load # noqa: E402 + +MIN_PER_TILE = 60 + + +def tile_edges(x_px, y_px, h, sd, tiles: int, pixel_nm=PIXEL_NM): + """Each tile's (u, v) centre in µm from the field's centre, its edge, error, spike fraction and count.""" + x0, x1 = np.nanmin(x_px), np.nanmax(x_px) + 1e-9 + y0, y1 = np.nanmin(y_px), np.nanmax(y_px) + 1e-9 + cx, cy = (x0 + x1) / 2, (y0 + y1) / 2 + ix = np.minimum(((x_px - x0) / (x1 - x0) * tiles).astype(int), tiles - 1) + iy = np.minimum(((y_px - y0) / (y1 - y0) * tiles).astype(int), tiles - 1) + rows = [] + for j in range(tiles): + for i in range(tiles): + sel = (ix == i) & (iy == j) + if sel.sum() < MIN_PER_TILE: + continue + e = coverslip_edge(h[sel], sd[sel]) + if e is None or not np.isfinite(e.sd_nm): + continue + u = (np.mean(x_px[sel]) - cx) * pixel_nm[0] / 1000 + v = (np.mean(y_px[sel]) - cy) * pixel_nm[1] / 1000 + rows.append(dict(tile=(i, j), u_um=float(u), v_um=float(v), edge_nm=e.shift_nm, sd_nm=e.sd_nm, + spike=e.spike, n=int(sel.sum()))) + return rows + + +def plane(rows): + """Weighted least squares edge = a + b_x u + b_y v: (a, b_x, b_y), their errors and χ² per degree of freedom.""" + u = np.array([r["u_um"] for r in rows]) + v = np.array([r["v_um"] for r in rows]) + z = np.array([r["edge_nm"] for r in rows]) + w = 1 / np.maximum(np.array([r["sd_nm"] for r in rows]), 1.0) ** 2 + A = np.c_[np.ones_like(u), u, v] + cov = np.linalg.inv(A.T @ (A * w[:, None])) + coef = cov @ (A.T @ (w * z)) + dof = max(len(z) - 3, 1) + chi2 = float(np.sum(w * (z - A @ coef) ** 2) / dof) + err = np.sqrt(np.diag(cov) * max(chi2, 1.0)) + return coef, err, chi2 + + +def angle_deg(bx, by) -> float: + return math.degrees(math.atan(math.hypot(bx, by) / 1000)) + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("out") + ap.add_argument("names", nargs="+") + ap.add_argument("--tiles", type=int, default=3) + ap.add_argument("--stack", help="a molecule stack model in OUTDIR: its geometry.plane printed beside") + ap.add_argument("--json") + a = ap.parse_args() + print(f"a tilt of 1.6° is {1000 * math.tan(math.radians(1.6)):.0f} nm per µm, 2.9° is " + f"{1000 * math.tan(math.radians(2.9)):.0f}") + if a.stack: + import torch + from smappy.calibrate.pupil import load_start + m = load_start(os.path.join(a.out, f"{a.stack}_3dcal.h5")) + p = torch.as_tensor(m.shared["geometry.plane"]).double().reshape(-1).tolist() + print(f"stack {a.stack}: focal plane's slope {p[0]:+.1f}, {p[1]:+.1f} nm per µm (x, y), " + f"{angle_deg(*p):.2f}°") + for name in a.names: + d = load(a.out, name) + rows = tile_edges(d["x_px"], d["y_px"], d["h"], d["sd_h"], a.tiles) + if len(rows) < 4: + print(f"{name}: only {len(rows)} tiles with an edge; fewer --tiles") + continue + coef, err, chi2 = plane(rows) + print(f"\n{name}: {len(rows)} tiles of {a.tiles}x{a.tiles}; edge's slope b_x {coef[1]:+.1f} ± {err[1]:.1f}, " + f"b_y {coef[2]:+.1f} ± {err[2]:.1f} nm per µm -> {angle_deg(coef[1], coef[2]):.2f}°; " + f"χ²/dof {chi2:.1f}") + for r in rows: + print(f" tile {r['tile']}: u {r['u_um']:+6.1f} v {r['v_um']:+6.1f} µm edge {r['edge_nm']:+7.1f} ± " + f"{r['sd_nm']:5.1f} nm spike {r['spike']:.2f} n {r['n']}") + if a.json: + with open(a.json, "a") as f: + f.write(json.dumps(dict(kind="tilt_from_edge", model=name, tiles=a.tiles, + b_x_nm_per_um=float(coef[1]), b_y_nm_per_um=float(coef[2]), + b_x_err=float(err[1]), b_y_err=float(err[2]), chi2_dof=chi2, + angle_deg=angle_deg(coef[1], coef[2]), tile_rows=rows)) + "\n") + + +if __name__ == "__main__": + main() diff --git a/docs/uipsf/architecture.md b/docs/uipsf/architecture.md index e1e92fc8..892c75c5 100644 --- a/docs/uipsf/architecture.md +++ b/docs/uipsf/architecture.md @@ -2644,6 +2644,18 @@ this is not yet a clean test of linearity. their printout). - `npc_rings.chain.yaml`, `rings.batch.yaml` and `rings_vs_z.py`: the NPC rings (`m2_pairs_npc_rings.jsonl`). +**Next, scripts written, not yet run** (the open hypotheses above, tested from the molecules' heights alone; each +script's docstring says how to read it): +- `heights.py`: every molecule of the movie (*used*, *held out*, *not drawn*) localised once per model, saved as + `heights_.npz` -- height, CRLB, ½ Σ deviance, chip position, frame. The three below read these files. +- `tilt_from_edge.py`: the coverslip's edge per tile of the field and a plane through it. A real tilt of 1.6° is + ~28 nm per µm, ~700 nm across the field; with `--stack` the molecule stack's own focal-plane slope beside it. +- `edge_vs_time.py`: the edge per block of the 72 000 frames, with one model held: focus drift. +- `score_by_height.py`: two models' scores on the same unseen molecules per height bin: where the stack's pupil + and the in situ pupils each fit better. +- The stack again with h held at 5 nm (`HEIGHT=5`), and in situ started from it, to see whether the stack's + h of 75 nm is part of the gap. + ## 4Pi from beads (October 2026; `docs/4pi.md` D7 and §8) ```