Algorithmresearch
Super-resolution optical fluctuation imaging
Also called SOFI.
Compute the n-th order cumulant of the blinking at each pixel of a movie. This raises the PSF to the n-th power, which narrows a Gaussian PSF by the square root of n.
- Time
- Θ(n N T) worst · average · best
- Space
- Θ(N + n T) extra
- Where
- N = the number of pixels; T = the number of frames; n = the order of the cumulant
- Model
- Real RAM
- Properties
- background freedeterministicnot iterativenot linearoptical method
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Thomas Dertinger, Ryan Colyer, Gopal Iyer, Shimon Weiss, Jörg Enderlein, 2009
Super-resolution optical fluctuation imaging (SOFI) is an optical method that makes a sharper image from a movie of fluorescent emitters that blink on and off independently. It does not need special optics or isolated emitters. At each pixel it computes a cumulant of order of the intensity over time, with small time lags. Because the emitters blink independently, the cumulants of different emitters add without cross terms, and each emitter contributes the -th power of its point spread function (PSF), times the -th power of its brightness. A constant background drops out exactly, and noise that is independent from frame to frame drops out when the lags differ. For a Gaussian PSF of width , the -th power is a Gaussian of width , so the image gets sharper by ; for other PSFs the narrowing factor is different, but the support of the spectrum grows by . The gain comes from the physical blinking, which the computation measures; a movie of emitters that do not fluctuate gives no gain.
In plain words
Many small lamps sit closer together than a microscope can separate. Each lamp flickers on its own. The camera sees one blur. At one pixel, the light goes up and down as the nearby lamps flicker. If you measure how strongly the light at a pixel goes up and down, a pixel near the center of one lamp moves a lot, and a pixel between two lamps moves less, because its two lamps flicker independently and partly cancel in the statistics. Higher order statistics sharpen this effect. A steady glow from the background does not go up and down, so it disappears, and camera noise that is new in every frame disappears when you compare different frames.
The problem
The input is a movie of frames of pixels. The model is : a background , emitters at with brightness and PSF , independent stationary blinking processes , and noise that is independent from frame to frame. The goal is an image of the emitter positions sharper than the time mean of the movie, which is the widefield image with the on fractions .
The algorithm
Choose the order and distinct lags , for example frames.
SOFI(movie, n, lags)- 1
for each pixel r - 2
s ← the series F(r, 0), ..., F(r, T - 1) - 3
X_q(t) ← s(t + τ_q) for q = 1, ..., n and t = 0, ..., T - 1 - max τ - 4
center each X_q by its mean over the window - 5
C(r) ← κ_n(X_1, ..., X_n) by the moment formula - 6
return C
The moment formulas on the centered series are , and , where is the mean over the window. In expectation the result is
Example
Two emitters of equal brightness sit apart under a Gaussian PSF of width . In units of the peak of one PSF:
| image | at an emitter | midway | dip? |
|---|---|---|---|
| widefield, | no | ||
| SOFI order 2, | yes, ratio |
The widefield image has its maximum midway, so it shows one blob. The second order image has a dip of about percent between the emitters. The test simulates this case with frames, a background of , Gaussian noise and Markov blinking with ; the measured ratio is , close to the expected (a measurement, with the error of a finite movie).
For nm, the width of is , and nm for , and the full width at half maximum, , falls from nm for the widefield PSF to , and nm.
Correctness
The proof is in proofs/correctness.md. The facts:
- Algebra (theorems). The moment formulas equal the partition formula for the joint cumulant. Cumulants of order ignore added constants, scale with the product of the factors, add over independent sums, and vanish when the variables split into two independent groups.
- SOFI formula (theorem, under the model). With independent stationary emitters, a constant background, noise independent between frames and distinct lags, . With equal lags, white noise adds its own cumulant, for example its variance at .
- One emitter (theorem, exact). For a single emitter and a constant background the computed image is exactly times the sample cumulant of the blinking.
- Narrowing (theorem). For a Gaussian PSF, is a Gaussian of width . For a PSF whose OTF has the cutoff , the spectrum of has the cutoff .
- Blinking cumulants (theorem). For a two-state Markov chain with on fraction , the zero-lag cumulants are those of a Bernoulli law, is negative for , and the lag-1 covariance is .
The computed values are cumulants of the sample, which tend to the cumulants of the model as the movie grows (a standard fact for ergodic processes, not proved here).
Proof: The SOFI cumulant removes the background and the white noise and keeps the n-th power of the PSF of each independent emitterfull
Setting. The intensity at pixel and frame is
with a constant background , emitters at with brightness , a PSF , blinking processes and noise . The model assumptions are: the processes and are mutually independent and stationary, and the noise values at different frames are independent (white in time). For the lags , the SOFI value of order is the joint cumulant
The joint cumulant of is , summed over the partitions of . Equivalently, it is the coefficient of in the cumulant generating function , when this function exists, as it does for bounded variables. The code computes the cumulant of the empirical law, which puts the mass on each of the vectors in the window. All the algebraic claims below hold for any law, so they hold exactly for the empirical one too.
Claim 1 (moment formulas). For the code's formulas on the centered series equal the partition formula: , , .
Proof. By Claim 2, adding constants does not change for , so we may center. For centered variables every partition with a block of one element contributes . For only the partition with one block is left. For the one block gives and the three partitions into two pairs have the coefficient .
The test checks the identity in exact rational arithmetic against a direct sum over all partitions.
Claim 2 (constants and scaling). For , , and .
Proof. of the shifted variables is , which has no term in when . Scaling by replaces by in .
Claim 3 (independent sums). If and the families and are independent, then .
Proof. By independence, , so the cumulant generating functions add.
Claim 4 (independent groups). If splits into two nonempty groups and such that is independent of , then .
Proof. is then the sum of a function of and a function of , and neither has a term with all of .
Claim 5 (the SOFI formula). Under the model assumptions, if the lags are distinct,
Proof. The background is a constant, which Claim 2 removes. The emitters and the noise are independent, so by Claim 3 the cumulant is the sum of one cumulant per emitter and one for the noise. The noise values at the distinct frames are independent, so by Claim 4 the noise cumulant is . For emitter , Claim 2 with gives the factor .
With equal lags, the noise cumulant is the -th cumulant of the noise at one frame, for example its variance when . The test measures this bias for and checks that the lag removes it.
Claim 6 (single emitter, exact). For one emitter and a constant background, the empirical SOFI image is exactly times the empirical cumulant of the lagged blinking series, for any lags, at every pixel.
Proof. Without noise, for every . Apply Claim 2 to the empirical law.
The test checks this in exact rational arithmetic for .
Claim 7 (narrowing). If , then , a Gaussian of width . For any PSF whose OTF vanishes outside a disk of radius , the Fourier transform of vanishes outside the disk of radius .
Proof. The first part is algebra. The Fourier transform of is the -fold convolution of the OTF with itself, whose support lies in the -fold sum of the disk, the disk of radius .
So the narrowing is a statement about a Gaussian PSF. For another PSF, is narrower too, but by a different factor, and the support of its spectrum grows by ; a later deconvolution can use the larger support.
Claim 8 (blinking cumulants). For a stationary two-state process with , and the on fraction :
- with all lags equal, , and , the cumulants of a Bernoulli law;
- with the lags and , .
Proof. At one time is Bernoulli with mean . Since , all raw moments are , and the centered moments are , and . Claim 1 gives . For the lag , , so , and gives .
So when , that is for : the fourth order image can change sign between emitters with different on fractions. The test checks the Bernoulli values exactly for , where they are , and .
Remark (estimation). The claims about are about the cumulant of the process. The code computes the cumulant of the empirical law of a finite window, which is a biased but consistent estimate for ergodic blinking and noise: the cross terms between emitters and the noise terms are only in the limit of long movies. This is the role of the measurement tests with long series.
Complexity
Each pixel needs lagged copies of its series, of length at most , and the moment formulas cost per product, with a number of products that depends only on . Over pixels this is time for the orders to , and extra space for the output and the series of one pixel. The partition formula for a general order has a number of terms that grows like the Bell number of , which is why codes use explicit formulas for small .
Properties
- Background-free. A constant background has no fluctuations and drops out exactly.
- Noise-free in expectation with distinct lags. Noise independent between frames has zero cumulant across different frames.
- Nonlinear in brightness. Emitter enters with . Bright or strongly blinking emitters dominate, and dim ones vanish as grows.
- Sign changes. Odd and higher cumulants can be negative. For and on fractions between and the factor is negative, so emitters with different blinking can give values of opposite sign.
- Needs many frames. The cumulant estimates have a variance that grows quickly with , so higher orders need longer movies.
Variants and improvements
- Cross-cumulants. Use different pixels for the factors. For a Gaussian PSF, the cross-cumulant of pixels is centered at the mean of the pixel positions, times a factor that depends only on the distances between the pixels. So it adds new virtual pixels between the real ones and removes the noise even at zero lag, as the noise of different pixels is independent.
- Deconvolution after SOFI. Since the spectrum of reaches , a deconvolution of the cumulant image can push the resolution gain from toward .
- Balanced SOFI. Combine cumulants of several orders to cancel the brightness and the blinking factor, and so estimate the brightness, the on fraction and the positions.
- Lags in time. Longer lags reduce the effect of slow drifts but need blinking that stays correlated over the lag.
When to use it
Use SOFI when the emitters blink independently, as quantum dots and many dyes do, but are too dense for single-molecule localization, or when the frame rate and the photon budget favor a simple pixelwise computation. It works with a plain widefield microscope, removes the background, and needs no fitting. For the highest resolution with sparse emitters, localization methods give tens of nanometers. For samples that do not blink, use SIM or deconvolution.
Pitfalls
- Correlated blinking or drift. The formula needs independent emitters and a still sample. Drift, bleaching or a common flicker of the lamp add cross terms. Correct the drift and split long movies into blocks.
- Equal lags. With zero lag, white noise adds to the cumulant. Use distinct lags or cross-cumulants.
- Short movies. Finite movies leave cross terms between emitters of the order of the statistical error. Check that the image does not change much with half of the frames.
- Reading values as densities. The value at a pixel scales with , not with the number of emitters. Use balanced SOFI or another method for counting.
- Expecting for any PSF. The factor holds for a Gaussian PSF.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/sofi/. The authors are listed in prov/70NF24.jsonl.
"""Super-resolution optical fluctuation imaging (SOFI): pixelwise cumulants.
A movie is a list of T frames, each an image movie[t][i][j]. The SOFI image
of order n with the time lags (tau_1, ..., tau_n) is, at each pixel, the
sample joint cumulant of the n series X_q(t) = F(t + tau_q), for
t = 0, ..., T - 1 - max(tau). Sample moments are means over these windows.
With distinct lags, noise that is independent from frame to frame adds
nothing to the cumulant in expectation; a constant background adds nothing
exactly. Works with float and fractions.Fraction values.
"""
import math
def _mean(xs):
return sum(xs) / len(xs)
def _centered(xs):
m = _mean(xs)
return [x - m for x in xs]
def cumulant(series):
"""The sample joint cumulant of 2, 3 or 4 equal-length series.
On the centered series a, b, c, d:
order 2: E[ab]
order 3: E[abc]
order 4: E[abcd] - E[ab] E[cd] - E[ac] E[bd] - E[ad] E[bc]
"""
n = len(series)
if n not in (2, 3, 4):
raise ValueError("order must be 2, 3 or 4")
z = [_centered(s) for s in series]
length = len(z[0])
if n == 2:
return _mean([z[0][t] * z[1][t] for t in range(length)])
if n == 3:
return _mean([z[0][t] * z[1][t] * z[2][t] for t in range(length)])
def m2(p, q):
return _mean([z[p][t] * z[q][t] for t in range(length)])
m4 = _mean([z[0][t] * z[1][t] * z[2][t] * z[3][t] for t in range(length)])
return m4 - m2(0, 1) * m2(2, 3) - m2(0, 2) * m2(1, 3) - m2(0, 3) * m2(1, 2)
def set_partitions(items):
"""All partitions of the list items into nonempty blocks."""
if not items:
yield []
return
first, rest = items[0], items[1:]
for part in set_partitions(rest):
yield [[first]] + part
for i in range(len(part)):
yield part[:i] + [[first] + part[i]] + part[i + 1:]
def cumulant_by_partitions(series):
"""The sample joint cumulant of any order by the partition formula.
kappa = sum over partitions pi of (-1)^(|pi| - 1) (|pi| - 1)! prod_B E[prod_{q in B} X_q],
with raw (not centered) sample moments.
"""
length = len(series[0])
total = 0
for part in set_partitions(list(range(len(series)))):
k = len(part)
term = (-1) ** (k - 1) * math.factorial(k - 1)
for block in part:
prod = [1] * length
for q in block:
prod = [p * v for p, v in zip(prod, series[q])]
term = term * _mean(prod)
total += term
return total
def lagged(s, lags):
"""The series X_q(t) = s(t + lags[q]) for t = 0, ..., len(s) - 1 - max(lags)."""
length = len(s) - max(lags)
if length < 2:
raise ValueError("the movie is too short for these lags")
return [s[tau:tau + length] for tau in lags]
def sofi_image(movie, order=2, lags=None):
"""The SOFI image of the given order: the joint cumulant of lagged series at each pixel.
lags defaults to (0, 1, ..., order - 1), so all lags are distinct.
"""
if lags is None:
lags = tuple(range(order))
if len(lags) != order:
raise ValueError("one lag per factor")
n1, n2 = len(movie[0]), len(movie[0][0])
out = [[0] * n2 for _ in range(n1)]
for i in range(n1): # @line L1
for j in range(n2): # @line L1
s = [frame[i][j] for frame in movie] # @line L2
xs = lagged(s, lags) # @line L3
out[i][j] = cumulant(xs) # @line L4 L5
return out # @line L6
def mean_image(movie):
"""The widefield image: the time mean of each pixel."""
n1, n2 = len(movie[0]), len(movie[0][0])
return [[_mean([f[i][j] for f in movie]) for j in range(n2)] for i in range(n1)]
def markov_blinking(rng, length, a, b, on=None):
"""A two-state blinking series: P(off -> on) = a, P(on -> off) = b.
The stationary on fraction is rho = a / (a + b), and the lag-1
covariance is rho (1 - rho) (1 - a - b). The first state is drawn from
the stationary law unless on is given.
"""
if on is None:
on = rng.random() < a / (a + b)
out = []
for _ in range(length):
out.append(1 if on else 0)
u = rng.random()
on = (u >= b) if on else (u < a)
return out
def gaussian_psf(dist2, sigma):
"""exp(-d^2 / (2 sigma^2)) for the squared distance d^2."""
return math.exp(-dist2 / (2 * sigma * sigma))
def simulate_movie(rng, shape, emitters, sigma, length, a, b, background=0.0, noise=0.0):
"""A movie of blinking emitters with a Gaussian PSF.
emitters is a list of (row, column, brightness) with real coordinates.
Each frame is background + sum_k brightness_k s_k(t) U(r - r_k) plus
independent N(0, noise^2) values.
"""
n1, n2 = shape
weights = [[[gaussian_psf((i - r) ** 2 + (j - c) ** 2, sigma) * e for j in range(n2)]
for i in range(n1)] for r, c, e in emitters]
states = [markov_blinking(rng, length, a, b) for _ in emitters]
movie = []
for t in range(length):
frame = [[background] * n2 for _ in range(n1)]
for k, w in enumerate(weights):
if states[k][t]:
for i in range(n1):
for j in range(n2):
frame[i][j] += w[i][j]
if noise > 0:
for i in range(n1):
for j in range(n2):
frame[i][j] += rng.gauss(0.0, noise)
movie.append(frame)
return movie
test_sofi.py 96 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/sofi/. The authors are listed in prov/70NF24.jsonl.
import math
import random
from fractions import Fraction as F
from sofi import (
cumulant,
cumulant_by_partitions,
lagged,
markov_blinking,
mean_image,
simulate_movie,
sofi_image,
)
def test_moment_formulas_match_partition_formula():
rng = random.Random(1)
for order in (2, 3, 4):
xs = [[F(rng.randint(-5, 9)) for _ in range(12)] for _ in range(order)]
assert cumulant(xs) == cumulant_by_partitions(xs)
def test_bernoulli_cumulants():
s = [F(1)] * 3 + [F(0)] * 7 # the empirical law is Bernoulli(3/10)
rho = F(3, 10)
assert cumulant([s, s]) == rho * (1 - rho) == F(21, 100)
assert cumulant([s, s, s]) == rho * (1 - rho) * (1 - 2 * rho) == F(84, 1000)
k4 = rho * (1 - rho) * (1 - 6 * rho * (1 - rho))
assert cumulant([s, s, s, s]) == k4 == F(-546, 10000)
def test_single_emitter_is_exact_and_background_free():
rng = random.Random(2)
blink = [F(rng.randint(0, 1)) for _ in range(40)]
psf = [F(1, 9), F(1, 2), F(1), F(1, 2), F(1, 9)]
eps, bg = F(7), F(3)
movie = [[[bg + eps * u * st for u in psf]] for st in blink]
for order in (2, 3, 4):
img = sofi_image(movie, order)
k = cumulant(lagged(blink, tuple(range(order))))
for j, u in enumerate(psf):
assert img[0][j] == u ** order * eps ** order * k
def test_lags_remove_white_noise():
rng = random.Random(3)
a, b, sigma = 0.2, 0.2, 0.5
s = markov_blinking(rng, 40000, a, b)
noisy = [v + rng.gauss(0.0, sigma) for v in s]
rho = a / (a + b)
var = rho * (1 - rho)
lag1 = var * (1 - a - b)
c0 = cumulant(lagged(noisy, (0, 0)))
c1 = cumulant(lagged(noisy, (0, 1)))
assert abs(c0 - (var + sigma ** 2)) < 0.02
assert abs(c1 - lag1) < 0.02
def test_markov_lag_covariance():
rng = random.Random(4)
a, b = 0.1, 0.2
s = markov_blinking(rng, 50000, a, b)
rho = a / (a + b)
assert abs(sum(s) / len(s) - rho) < 0.02
assert abs(cumulant(lagged(s, (0, 1))) - rho * (1 - rho) * (1 - a - b)) < 0.015
def test_second_order_resolves_two_emitters():
# Two emitters 2 sigma apart: the mean image has no dip between them,
# the second order SOFI image has one.
rng = random.Random(5)
sigma = 2.0
movie = simulate_movie(rng, (1, 15), [(0, 5, 1.0), (0, 9, 1.0)], sigma, 4000,
0.3, 0.3, background=2.0, noise=0.05)
wf = mean_image(movie)[0]
s2 = sofi_image(movie, 2)[0]
assert wf[7] > wf[5] and wf[7] > wf[9]
assert s2[7] < 0.85 * min(s2[5], s2[9])
def test_order_n_narrows_a_gaussian_by_sqrt_n():
# One emitter: the SOFI image of order n is U^n times a constant, a
# Gaussian of width sigma / sqrt(n).
rng = random.Random(6)
sigma = 1.5
blink = markov_blinking(rng, 200, 0.3, 0.3)
psf = [math.exp(-(j - 6) ** 2 / (2 * sigma ** 2)) for j in range(13)]
movie = [[[u * st for u in psf]] for st in blink]
for order in (2, 3, 4):
img = sofi_image(movie, order)[0]
for j in range(13):
want = math.exp(-order * (j - 6) ** 2 / (2 * sigma ** 2))
assert abs(img[j] / img[6] - want) < 1e-9
History
Dertinger and coworkers introduced SOFI in 2009, using blinking quantum dots. They showed that the cumulant image of order is the sum of the -th powers of the PSFs, free of background, and they also showed 3D images. Later work developed cross-cumulants for extra pixels, deconvolution of the cumulant images, balanced SOFI for the brightness and the on fraction, and SOFI with dyes and fluorescent proteins.
References
- T. Dertinger, R. Colyer, G. Iyer, S. Weiss and J. Enderlein, Fast, background-free, 3D super-resolution optical fluctuation imaging (SOFI), Proceedings of the National Academy of Sciences 106(52), 2009, 22287 to 22292, the model of independent blinking emitters, the cumulant of order n as a sum of n-th powers of the PSF, the removal of background and noise, and the narrowing by the square root of n for a Gaussian PSF
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 70NF24 Edit this page History Report an error Markdown