pysrx.camera¶
pysrx.camera
¶
sCMOS camera fixed-pattern noise: per-pixel and per-column offsets that are identical in every frame.
Why it matters: Poisson statistics (binomial split FRC, Poisson likelihoods) assume counts above a uniform offset. A fixed pattern is not photon noise. It is split into both binomial halves and correlates them up to Nyquist (FRC never crosses 1/7), and decorrelation reads its column stripes as fine structure.
Estimate from the data itself (no dark frames needed): high-pass every frame (drops the smooth sample signal and the offset level), then take the per-pixel median over all frames (phases x planes): the fixed pattern survives, photon noise and frame-dependent structure do not. FPN(y, x) = median over k of (f_k - G_sigma * f_k), zero-mean. Subtract FPN from every frame.
Calibration (calibrate): the sCMOS model of Mandracchia et al., Nat Commun 11, 94 (2020),
raw = g * Poisson(electrons) + N(0, sigma_read^2) + o (per pixel g, sigma_read^2, o),
measured once per camera from a dark stack (o, sigma_read^2) and flat stacks at several light levels (g: per-pixel
photon transfer). Calibration.to_electrons then turns raw counts into photoelectrons.
Calibration
dataclass
¶
Per-pixel sCMOS maps from calibrate: (Y, X) float32 NumPy in raw counts (ADU), plus per-level summaries.
Attributes:
| Name | Type | Description |
|---|---|---|
offset |
ndarray
|
o, the mean of the dark stack. |
read_var |
ndarray
|
sigma_read^2 (counts^2), the variance of the dark stack. |
gain |
ndarray
|
g (counts per photoelectron), the photon-transfer slope; NaN where fewer than 2 levels survive the filter. |
intercept |
ndarray
|
c, the fitted variance at zero signal after removing sigma_read^2 (about 0 when the model holds). |
chi2 |
ndarray
|
Reduced chi-square of the kept levels (about 1 when the model holds; NaN with no degree of freedom). |
n_levels |
ndarray
|
Number of light levels kept per pixel (uint8). |
level_mean |
ndarray
|
(L,) median signal of each flat level above the offset (counts). |
level_kept |
ndarray
|
(L,) fraction of pixels that kept each level. |
seconds, peak_vram_gb |
float
|
Wall time and peak CUDA memory (0 on the CPU). |
fixed_pattern(frames, sigma=6.0, columns=True)
¶
frames (..., Y, X), any leading axes (phases, planes, time) -> zero-mean fixed pattern (Y, X) in counts.
Each frame is high-passed (minus a Gaussian blur of sigma pixels: larger than the pattern, smaller than the
sample's blur), then the per-pixel MEDIAN over frames is taken. The pattern is the same in every frame, the photon
noise and the (frame-dependent) in-focus structure are not, so the median keeps the former. Estimator noise
~ 1.25 noise_per_frame / sqrt(n_frames): use many frames (all phases x planes). columns additionally replaces the
column component by its per-column median (the dominant sCMOS term, estimated over all rows). Memory: the
high-passed stack as float32 (n_frames x Y x X).
remove_fixed_pattern(frames, pattern)
¶
Subtract the (Y, X) fixed pattern from every frame (..., Y, X).
photon_transfer(frames, axis=0, n_bins=60, quadratic=True)
¶
Gain (counts per photon) and read-noise variance (counts^2) from repeated frames of the same scene.
Per pixel, the mean m and variance v over axis (e.g. the 13 SIM phases of one plane, offset and fixed pattern
removed). Photon transfer: v = g m + sigma_read^2 (+ a m^2 for the lattice modulation between phases, which is not
noise). Fitted to the per-bin medians of v over quantile bins of m (robust to structure edges).
pixel_stats(stack, *, device=None, frame_chunk=16)
¶
stack (N, Y, X) repeated frames of one light level -> per-pixel mean and variance (ddof=1), (Y, X) float32.
Streams frame_chunk frames at a time to device (default: CUDA if available, else CPU) and sums in float64.
calibrate(dark, flats, *, device=None, saturation=None, outlier=4.0, intercept=True, frame_chunk=16)
¶
Per-pixel sCMOS calibration: offset, read variance and gain maps from a dark stack and flat stacks.
dark (N, Y, X): frames with no light (shutter closed, the exposure used for imaging). flats: a sequence of
(N_l, Y, X) stacks, or one (L, N, Y, X) array, each a uniform, steady illumination at a different level (L >= 2;
5 to 10 levels spanning the useful range are typical). Per pixel:
offset o = mean(dark), sigma_read^2 = var(dark),
var_l - sigma_read^2 = g (mean_l - o) + c (photon transfer, one point per level l)
fitted by closed-form weighted least squares for every pixel at once on device (default: CUDA if available,
else CPU). Each point is weighted by the inverse variance of a sample variance, (N_l - 1) / (2 var^2), with var
the model variance g (mean_l - o) + c + sigma_read^2: the fit starts from the per-pixel median of
(var_l - sigma_read^2) / (mean_l - o) and is refined twice. intercept=False forces c = 0.
The filter: with saturation (raw counts), a level is dropped in a pixel if any of its frames reached it
(clipped or non-linear). With outlier, each refinement also drops points whose standardised residual exceeds
outlier (e.g. a level near full well, where the variance falls). Set both to None for the unfiltered fit.
Memory: frame_chunk frames on the device at a time (12 bytes per value; 0.8 GB for 16 frames of
2048 x 2048), plus about 6 (L + 2) Y X float64 values for the maps.
Means and variances are summed in float64; the maps are returned as float32.