Emission Spectra
This page covers the emission spectrum of a fluorophore: how to give a fluorophore a spectrum, how to get the photons of each emitter in each wavelength bin, and how to render a sample of several species into a cube over camera pixels and wavelength. The cube is the input a simulated spectral instrument (a spectrometer behind a slit, a filter set, a spectral unmixing test) applies its own optics to.
Wavelengths are in μm, as in MicroscopePSFs: 520 nm is 0.520.
Spectrum Types
An emission spectrum says how a fluorophore's emitted photons are divided over wavelength. It is normalized: over all wavelengths the fraction is 1.
using SMLMSim
# Gaussian in wavelength: peak position and full width at half maximum
green = GaussianSpectrum(center=0.520, fwhm=0.035)
# A sampled curve, such as a measured spectrum: linear between samples, zero outside them,
# normalized on construction (the intensity units do not matter)
measured = TabulatedSpectrum(wavelength=[0.50, 0.52, 0.56, 0.62],
intensity=[0.0, 1.0, 0.4, 0.0])
emission_fraction(green, 0.50, 0.54) # photons between 500 and 540 nm: about 0.82
emission_fraction(measured, 0.50, 0.52) # 0.2bin_fractions gives the fraction in each bin of a wavelength grid. The edges must increase and need not be evenly spaced, so a grid from a spectrometer's pixel-to-wavelength calibration can be used as it is:
pixel = 0:541
calibrated = @. 0.50 + 6.2e-4 * pixel + 6.0e-8 * pixel^2 - 4.0e-11 * pixel^3 # 541 bins
f = bin_fractions(green, calibrated)
sum(f) # below 1: part of this spectrum lies under 500 nm, outside the gridWavelength edges and tabulated samples are used as Float64, whatever number type they are given in.
A Fluorophore with a Spectrum
GenericFluor takes the spectrum as a keyword, and emission_spectrum reads it. The spectrum does not change blinking or photon counts: a simulation with it gives the same emitters as one without.
green_dye = GenericFluor(photons=1e4, k_off=1.0, k_on=10.0, spectrum=green) # on about 90%
emission_spectrum(green_dye) # the spectrum
# A fluorophore with any rate matrix: on, a short off state and a long-lived dark state
Q = [-60.0 50.0 10.0; 5.0 -5.0 0.0; 0.1 0.0 -0.1]
three_state_dye = GenericFluor(photons=1e4, q=Q, spectrum=green)Species and Samples
A data set holds one species. Wherever a spectrum is needed, the data set is paired with what its emitters emit: smld => spectrum, or smld => dye for a fluorophore that has a spectrum. A sample with several species is several pairs, each simulated with its own fluorophore on the same camera:
red = GaussianSpectrum(center=0.670, fwhm=0.040)
red_dye = GenericFluor(photons=1e4, k_off=1.0, k_on=10.0, spectrum=red)
camera = IdealCamera(64, 64, 0.1)
params = StaticSMLMConfig(nframes=10)
_, info_green = simulate(params; molecule=green_dye, camera=camera)
_, info_red = simulate(params; molecule=red_dye, camera=camera)
smld_green, smld_red = info_green.smld_model, info_red.smld_modelinfo.smld_model holds the blinking emitters at their true positions, which is what an image is rendered from; the data set simulate returns first has localization noise added.
Photons per Wavelength Bin
spectral_photons gives the expected photons of every emitter record in every bin: the record's photons times the bin fractions. No noise is drawn.
edges = range(0.48, 0.75; length=28) # 27 bins of 10 nm
p = spectral_photons(smld_green => green_dye, edges) # n_emitters by 27
p = spectral_photons(smld_green => measured, edges) # the same records, another spectrumThe Cube: Pixels by Wavelength
gen_spectral_images renders any number of species into a cube of expected photons, cube[y, x, bin, frame]. Each record's photons are divided over the bins by its species' spectrum, and each bin is imaged through the PSF.
using MicroscopePSFs
cube, info = gen_spectral_images(GaussianPSF(0.13), smld_green => green_dye,
smld_red => red_dye; wavelength_edges=edges)
size(cube) # (64, 64, 27, 10)
sum(cube; dims=3) # over wavelength: the ordinary image, less the light outside the
# bins and in bins cut by min_fraction (below)
info.n_photons_total # photons of the selected records, at all wavelengthsThe cube holds expected photons, with no noise and no background: an instrument's optics act on the expected light, and noise belongs on the image the camera finally records. After applying your optics, use poisson_noise or scmos_noise (see Microscope Image Generation).
The keywords dataset, frames, support, sampling and threaded are those of gen_images. A species adds nothing to a bin that holds less than min_fraction of its photons (default 1e-12): the far tails of a Gaussian are tiny but never zero, and this keeps them from costing renders. min_fraction=0 renders every bin that holds any light.
A PSF That Follows Wavelength
The first argument can be a function of wavelength (μm) that returns the PSF of a bin. It is called once per bin with the bin's center wavelength:
# Airy patterns for a 1.4 NA objective, one for every 20 nm
airy = Dict(n => AiryPSF(1.4, 0.02n) for n in 24:38)
cube, info = gen_spectral_images(smld_green => green_dye, smld_red => red_dye;
wavelength_edges=edges) do λ
airy[round(Int, λ / 0.02)]
end
info.n_psfs # 14: the number of different PSFs over the 27 binsRendering costs one image per frame for each species and each different PSF its spectrum reaches, not one per bin: bins for which the function returns the same PSF object (===) share the image. A function that changes the PSF in steps coarser than the bins, as above, therefore saves renders, while one that builds a new PSF for every bin costs as many renders as there are bins. Build the PSFs once and look them up, as above: a PSF that holds arrays (a spline PSF, a pupil-based PSF) is the same object only if the function returns the one it built before.
Rendering a Strip of a Field
A number given as support is the half-width of the square region around each emitter over which its PSF is computed, as in gen_images. An emitter outside the pixel grid still contributes the part of that region that lies on the grid, and one whose region misses the grid contributes nothing.
For a narrow strip of pixels (a slit, a line scan) much of the light at the strip's edge comes from emitters just outside it. Simulate on the whole field and give the strip as the camera to render onto, built from explicit pixel edges, which place it in the field:
field = IdealCamera(512, 512, 0.1) # 51.2 μm square
_, info_field = simulate(StaticSMLMConfig(density=2.0, nframes=1);
molecule=green_dye, camera=field)
# 8 rows by 512 columns, starting 20 μm up the field
strip = IdealCamera(collect(field.pixel_edges_x), collect(20.0:0.1:20.8))
cube, info = gen_spectral_images(GaussianPSF(0.13), info_field.smld_model => green_dye;
wavelength_edges=edges, camera=strip, support=1.0)
size(cube) # (8, 512, 27, 1)Simulating on the strip itself would not do: simulate keeps only the molecules on its camera, so the strip's edge rows would lack the light from outside and come out dimmer. (IdealCamera given pixel-center ranges, as in IdealCamera(1:512, 201:208, 0.1), starts its edges at 0 whatever the ranges; only explicit edges place a camera.)
Speed and Threads
Time grows with the number of emitters within support of the pixel grid, the number of different PSFs and the number of pixels. For a strip of 8 by 512 pixels with 541 wavelength bins, 4 species, 10,000 emitters in the field and 13 different PSFs, one call with support=1.0 takes several hundredths of a second on one thread of a workstation, and about half that on 8 threads.
With threaded=true and several different PSFs, the PSFs are rendered at the same time, each PSF object in one render at a time. This asks more of a PSF type than gen_images does, which never runs two pixel integrations at once: a PSF type of your own whose evaluation or pixel integration writes to storage shared between calls needs threaded=false. The PSFs of MicroscopePSFs need nothing.
Writing Your Own Spectrum
A spectrum type needs one method, emission_fraction(spectrum, lo, hi): the fraction of emitted photons with wavelength λ in lo <= λ < hi. Everything on this page is built on it, so a type defined in your own package works wherever the stock ones do.
using SMLMSim
# A single emission line, as from a calibration lamp
struct LineSpectrum <: AbstractEmissionSpectrum
wavelength::Float64
end
SMLMSim.emission_fraction(s::LineSpectrum, lo::Real, hi::Real) =
Float64(lo <= s.wavelength < hi)
bin_fractions(LineSpectrum(0.6328), [0.60, 0.63, 0.64, 0.70]) # [0.0, 1.0, 0.0]
cube, info = gen_spectral_images(GaussianPSF(0.13), smld_red => LineSpectrum(0.6328);
wavelength_edges=edges)The fraction must be between 0 and 1 and add over neighboring bands: the fractions of a set of bins that tile a range sum to the fraction of the range. That is why the band is half-open: a line that sits exactly on a bin edge belongs to the bin above the edge and is counted once. bin_fractions refuses a spectrum whose bin fractions are negative or sum to more than 1.
A fluorophore type of your own (a subtype of Molecule) declares its spectrum by adding a method SMLMSim.emission_spectrum(::MyDye); without one it has no spectrum.
What Is Not Modeled
- Excitation spectra. The excitation efficiency of a species at a laser line is one number, which scales its photons.
- A spectrum that changes with a fluorophore's state or in time.
- A species on each emitter record: a data set holds one species.