Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions benchmarks/uipsf/real/m2_pairs/edge_vs_time.py
Original file line number Diff line number Diff line change
@@ -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_<MODEL>.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()
112 changes: 112 additions & 0 deletions benchmarks/uipsf/real/m2_pairs/heights.py
Original file line number Diff line number Diff line change
@@ -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_<MODEL>.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 ``<name>_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()
83 changes: 83 additions & 0 deletions benchmarks/uipsf/real/m2_pairs/score_by_height.py
Original file line number Diff line number Diff line change
@@ -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_<MODEL>.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()
Loading
Loading