Spline 3D

Localize · version 2 · has a preview

Detect and fit with an experimental spline PSF from a bead calibration: adds z.

What it does

This is the 3D fitter. It does what Gaussian 2D does – reads the camera frames, finds the single fluorophores that are on, and fits each one to get its position far below the size of a pixel – but the model it fits is not a Gaussian. It is the measured image of a point source on this microscope, at every height, taken from a bead calibration and interpolated with cubic splines – the method of Li et al. 2018. Because that image changes with the emitter's distance from the focal plane, the fit also tells how far it was: every localization gets a z_nm and a z_err_nm besides x, y and the photons.

The shape has to change with z in a way that tells above from below. The usual way is a weak cylindrical lens in the detection path (astigmatism): the spot is then stretched along one axis above focus, along the other below, and round in between. Any other engineered PSF works as well, as long as the calibration was measured with it – and so do the aberrations of an ordinary objective, which a Gaussian would get wrong and a measured model does not.

What it needs:

For 2D data without a calibration, use Gaussian 2D; for two channels on one camera, Spline 3D 2C.

How it works

The detection, the ROIs and the fit itself are those of Gaussian 2D, which explains them step by step: the counts are converted to photons, candidates are found in a difference-of-Gaussians filtered image, a square ROI is cut around each, and the ROI is fitted by maximum likelihood with Levenberg-Marquardt. What is different here is the model, and where it comes from.

1. The calibration. Beads are small and bright, so a bead's image is the PSF (the point spread function: what the microscope makes of a single point of light). The Bead calibration tool steps the objective through focus over a few beads, finds them, aligns their stacks to each other in x, y and z below a pixel, averages them, and smooths the average a little along z. The result is the PSF sampled on a grid: one image every dz nanometres (the step of the bead stack) over a range of about a micrometre or more.

2. A smooth model between the samples. An emitter is almost never exactly on the grid. To get the PSF anywhere – a fraction of a pixel off centre, and between two planes – the grid is interpolated with a cubic spline in x, y and z: within each small box of the grid, a smooth polynomial that passes through the measured values and joins its neighbours without a kink. That makes the model, and how it changes with x, y and z, available everywhere, which is what a fit needs.

The calibration. Top: the spline model at five heights, as the fitter sees it in a 13-pixel ROI. Bottom: the PSF the simulated beads were drawn with. The spot is stretched along x below focus and along y above it; the model has learnt that from nine beads.

3. The fit. Each ROI is compared with the model: the calibrated PSF at position and height , scaled to photons, on a flat background per pixel. The five parameters that make the observed photons most probable are the result. z is found the same way x and y are: the shape of the spot is what depends on it, and the fit changes z until the model's shape matches the spot's.

The fit starts at the centre of mass of the ROI, at the background of its border pixels, and at the start z – the focal plane of the calibration unless set otherwise. From there Levenberg-Marquardt walks to the best z; with an astigmatic PSF the shape changes monotonically over the range, so there is one best z to find.

4. Precision. As for the Gaussian fitter, the Cramér-Rao lower bound (CRLB) from the model says how precisely each parameter can be known from the photons there are, and it is written as x_err_nm, y_err_nm and z_err_nm. It depends on z: where the spot is small it is sharp and precise; far from focus it spreads over more pixels and more background, and the precision falls. z is typically two to four times less precise than x and y.

The fit gives z back. Spots simulated at known heights (2000 photons, 10 background photons per pixel) from the true PSF, not from the spline, and fitted with the calibration of the figure above. Left: fitted against true z, with the line of perfect agreement. Right: the scatter of each coordinate about the truth (dots) against the precision the fitter reported (lines) – x and y trade places above and below focus, and z is best near focus.

5. The table. Positions are converted to nanometres with the pixel size (the default units here are nm), fits that failed or whose z is not a number are dropped, and the table is saved as <acquisition>_locs.hdf5.

In detail

The spline. The calibration is a grid of boxes (voxels): one pixel wide laterally, deep. Within each box the PSF is a tricubic polynomial of the fractional offsets of the point from the box's corner, in x, y and z,

so 64 coefficients per box, stored as an array of shape in the order . The Bead calibration tool computes them by not-a-knot cubic interpolation of the averaged bead stack along each axis in turn, after normalising it so that the brightest plane sums to 1 over the calibration ROI; is then the photons in that plane. A SMAP file brings its own coefficients.

The model. For pixel of a ROI centred on the candidate,

where the ROI sits in the middle of the (larger) calibration grid. Its derivatives are those of the polynomial,

and the same for y; z is fitted in units of grid planes and converted at the end. The likelihood, the Levenberg-Marquardt steps (with the expected information as curvature) and the CRLB are exactly those of the Gaussian fitter, with these five derivatives: the Fisher information

at the fitted , and . Because the model is the measured PSF, the CRLB includes everything the PSF does with z – which is what makes the z precision curve of the figure above come out of the fit rather than from a formula. A calibration made from few, noisy beads has bumps of its own that the fitter takes for information, and then reports a z precision that is too good; smoothing the calibration along z (the Bead calibration's setting) is what prevents it.

z. The spline's z index is converted with the calibration's plane spacing and its focal plane (z0 in the file),

The minus sign is the convention SMAP uses: the calibration records where the objective was, and a bead is below the focus by as much as the objective was raised. z is therefore relative to the calibration's focal plane, not to the coverslip, and in the nanometres the objective moved – no correction for the different refractive index of the sample is made (for an oil objective imaging into water, real distances are shorter, by a factor of roughly 0.7 to 0.8; Math Parser can apply one).

The start and the limits. x and y start at the ROI's centre of mass, the background at the mean of the ROI's border pixels, the photons at the brightest pixel above background divided by the model's central value, times 4, and z at start z (0 nm by default, the plane ). A z step is capped at a third of the calibration's depth (at least two planes) at first, and the cap is halved whenever the step reverses. z is held inside the calibration: a fit that tries to leave it stops at an end. During the fit and ; x and y are not held.

Mirroring. A SMAP calibration made from mirrored bead images (emmirror, for beads read out through the other port of an EMCCD) records that; each ROI is then flipped in x before it is fitted and the fitted x flipped back, so the table is in the orientation of the data. Calibrations from the Bead calibration tool are never mirrored.

EM gain. A calibration records whether the beads were taken with EM gain – the Bead calibration tool reads it from the bead files' metadata, a SMAP calibration carries it – and if the data's camera says otherwise, the fit warns, in the plugin's output: the EM register of many EMCCDs reads out mirrored, so a model from beads on the other port is mirrored against the data, and every fit is then subtly wrong. The fit is not stopped. The EM gain's excess noise is handled as in the Gaussian fitter. So is the camera's read noise: its variance is added to data and model, one electron without EM gain by default (read noise), which also keeps a pixel whose spline model nears zero from dominating a fit at low background (see Gaussian 2D).

Compared with the paper. The method is that of Li et al. 2018; what the code does differently (the first two concern the Bead calibration tool; a SMAP calibration brings its own coefficients):

Parameters

settingdefaultwhat it does
source – Where the frames come from.
   file
   source.path
–Any file of the acquisition: a Micro-Manager TIFF series, an NDTiff directory, one image out of a folder written one file per frame, or a simulation (*.sim.yaml, File > Simulate).
   first frame (more)
   source.start
0Frames before this one are skipped.
   last frame (more)
   source.stop
autoAuto: to the end. at least 1
   live
   source.live
offThe file is still being written: fit what is there and keep watching for new frames.
   stop after (more)
   source.live_timeout
30 s idleLive: give up after this long without a new frame. at least 1 s idle
   frames per block (more)
   source.chunk
200Frames read and detected at once; only memory and speed depend on it. at least 1
camera – ADU -> photons and pixel -> nm. Empty fields come from the file.
   camera
   camera.camera
autoAuto: identified from the file's metadata.
   preset
   camera.preset
-A camera YAML; fills the fields below.
   conversion
   camera.conversion
autoPhotoelectrons per count; auto: from the file or the camera database.
   offset
   camera.offset
autoThe count of a pixel that saw no light.
   pixel size
   camera.pixelsize_um
autoThe pixel's size in the sample, after the magnification.
   pixel size y (more)
   camera.pixelsize_y_um
autoAuto: square pixels, the same as in x.
   EM gain on
   camera.em_on
autoAn EMCCD with its electron multiplication on: the counts are divided by the gain, and the noise is doubled.

Also compared with the calibration's EM setting; see EM gain above.

   EM gain
   camera.emgain
autoThe EM gain the camera was set to.
   read noise (more)
   camera.read_noise_e
autoRms per pixel; auto: 1 without EM gain (an sCMOS), 0 with it.
detection – Finding candidates in the filtered image.
   filter
   detection.filter
difference of GaussiansWhat the frame is smoothed with before maxima are looked for; the difference of Gaussians also removes a smooth background. Choices: difference of Gaussians; Gaussian
   filter sigma
   detection.sigma
1.2 pixThe width of the smoothing: about the PSF's. at least 0.1 pix
   cutoff
   detection.cutoff_mode
dynamic (x noise)Dynamic: set per frame from the noise of the filtered image; absolute: a fixed value. Choices: dynamic (x noise); absolute (photons)
   cutoff value
   detection.cutoff
1.7Lower finds dimmer molecules, and more noise.
model – An experimental PSF from a bead calibration (.h5, or SMAP's _3dcal.mat): adds z.
   calibration
   model.calibration
–The bead calibration of this microscope: an .h5 from Tools > Bead calibration, or a SMAP _3dcal.mat.

Made once per microscope configuration, with the objective, filters and camera settings of the data.

   start z (more)
   model.z_start_nm
0 nmWhere each fit starts in z, relative to the calibration's focal plane.

Worth moving only when most of the data is far from focus on one side, where a start at the focal plane has the farthest to go.

fit – Everything about the fit that is not the camera or the PSF model.
   ROI size
   fit.roisize
13 pixThe square cut around each candidate and fitted.

At least 2 pixels smaller than the calibration laterally (the Bead calibration's ROI size, 27 by default), or the fit refuses to start: beyond the grid the model would only repeat its edge. Large enough for the widest, most defocused spot; the default 13 holds the spots of the figures above, over 600 nm.

at least 5 pix
   iterations (more)
   fit.iterations
50The most steps a fit may take.

Defocused spots can take more steps than in 2D; 50 is usually plenty, and the iterations column shows a fit that ran out.

at least 1
   ROIs per fit (more)
   fit.max_block_rois
15000How many are collected before they are fitted together; speed and memory only. at least 100
   threads (more)
   fit.n_threads
00: one per core.
   max fit distance (more)
   fit.max_fit_distance
autoReject fits that ran off; auto: keep all.
   units
   fit.output_unit
nmThe unit of the positions in the table. Choices: nm; pixel; pixel+nm
output
   save HDF5
   output.save
onWrite the table to a file as it is fitted.
   file
   output.path
–Filled from the source: <acquisition>_locs.hdf5 next to the folder the images are in.
   raw frames (more)
   output.raw_frames
50Camera frames kept with the table, in photons, after their average; 0: none.

50 is enough to see what the camera saw at the start, the end and a few points between. Each kept frame costs its size in the file: for a 256 x 256 ROI about a quarter of a megabyte, for a full 2048 x 2048 sCMOS chip 16 MB, so fifty of those are 800 MB – set fewer there.

   show image tags
   output.show_tags
PIZStageImage tags drawn when the fit is done, names or parts of names; empty: none (Analysis/Process/Image Tags).

PIZStage draws the piezo position, which shows at once whether the focus lock held. On a microscope whose piezo has another name, put that name (or part of it) here. Every tag that changed is kept in the file whatever this says; Analysis/Process/Image Tags draws any of them later.

Output

The table has the columns of Gaussian 2D with z instead of the PSF width:

column meaning
x_nm, y_nmthe position
z_nmheight relative to the calibration's focal plane, in objective nanometres
z_err_nmits precision (CRLB)
x_err_nm, y_err_nm, xy_err_nmthe lateral precision (CRLB)
photons, background, photons_err, background_err, and their CRLB
logl, logl_relthe log-likelihood, and per pixel
peak_x_pix, peak_y_pix, iterationsthe candidate, and the steps taken

The file also records the calibration used: its path, , , the grid and whether it was mirrored.

What to check:

The camera frames. The file also keeps a few of the frames the table was fitted from, in photons (counts minus offset, times the conversion): first the average of every frame fitted, then the first fitted frame, then the rest spaced evenly up to the last one, as many as raw frames asks for, each with its frame number – the same number as in frame. In the Render tab they are a source of an image layer, placed in the table's coordinates, so they lie under the localizations: whether a structure is really there, where the cell edge is, or whether the focus was lost can be checked without the original stack. The average is the one to start with; a localization of frame 17 should sit on a spot in frame 17.

Differences from SMAP

Based on SMAP's fit_fastsimple workflow and its MLE_GPU_Yiming fitter in Spline mode (Ries 2020). The main changes:

References