Skip to content

Commit 80a8728

Browse files
committed
update main pacakge readme and make a test file that runs the examples.
1 parent 465d439 commit 80a8728

File tree

2 files changed

+367
-14
lines changed

2 files changed

+367
-14
lines changed

README.md

Lines changed: 213 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,225 @@
55
[![Build Status](https://github.com/JuliaSMLM/SMLMSim.jl/workflows/CI/badge.svg)](https://github.com/JuliaSMLM/SMLMSim.jl/actions)
66
[![Coverage](https://codecov.io/gh/JuliaSMLM/SMLMSim.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaSMLM/SMLMSim.jl)
77

8-
Generate simulated SMLM coordinate data. Patterns, Cameras, Fluorophores, and SMLD data organization can be configured.
8+
## Overview
99

10-
Simulation parameters use physical units. Resulting `SMLMData.SMLD` structures are in units of pixels and frames.
10+
SMLMSim is a Julia package for simulating Single Molecule Localization Microscopy (SMLM) data with realistic physical properties. The package builds upon SMLMData, reexporting essential types and functions so you typically don't need to import SMLMData directly.
1111

12+
The package provides tools for:
1213

13-
The high level interface for simulating SMLM super-resolution coordinate data is the `SMLMSim.sim()` function.
14+
- Generating spatial patterns of fluorophores in 2D and 3D
15+
- Simulating fluorophore photophysics with stochastic kinetic models
16+
- Adding realistic localization uncertainty based on photon counts
17+
- Simulating diffusion and interactions between molecules
18+
- Generating microscope images with configurable PSFs
1419

20+
All simulations use physical units, with coordinates in microns and time in seconds. The resulting data is organized into `SMLMData.SMLD` structures compatible with the broader JuliaSMLM ecosystem.
21+
22+
## Installation
23+
24+
```julia
25+
using Pkg
26+
Pkg.add("SMLMSim")
1527
```
28+
29+
## Basic Usage
30+
31+
The high-level interface for simulating SMLM super-resolution coordinate data is the `simulate()` function (or the alias `sim()`).
32+
33+
```julia
1634
using SMLMSim
17-
using SMLMData
1835

19-
smld_true, smld_model, smld_noisy = SMLMSim.sim(;
20-
ρ=1.0,
21-
σ_PSF=0.13,
22-
minphotons=50,
23-
ndatasets=10,
24-
nframes=1000,
25-
framerate=50.0, # 1/s
26-
pattern=SMLMSim.Nmer2D(),
27-
molecule=SMLMSim.GenericFluor(; q=[0 50; 1e-2 0]), #1/s
28-
camera=SMLMSim.IdealCamera(; ypixels=256, xpixels=128, pixelsize=0.1) #pixelsize is microns
36+
# Basic simulation with default parameters
37+
camera = IdealCamera(1:128, 1:128, 0.1) # 128×128 pixels, 100nm pixels
38+
smld_true, smld_model, smld_noisy = simulate(
39+
camera=camera
2940
)
3041
```
42+
43+
This basic example creates a 2D simulation using default parameters:
44+
- 8-molecule circular patterns (Nmer2D with n=8, d=0.1μm)
45+
- 1 pattern per square micron (ρ=1.0)
46+
- PSF width of 130nm (σ_psf=0.13μm)
47+
- Two-state fluorophore kinetics with realistic blinking behavior
48+
- 1000 frames at 50 frames per second
49+
- Minimum photon threshold of 50 for detection
50+
51+
The function returns three SMLD objects with SMLM coordinates:
52+
- `smld_true`: Ground truth emitter positions (spatial coordinates only)
53+
- `smld_model`: Positions with simulated blinking kinetics (subset of true positions appearing in different frames)
54+
- `smld_noisy`: Positions with both blinking and localization uncertainty (realistic SMLM data with position errors)
55+
56+
For more control, you can customize the parameters:
57+
58+
```julia
59+
# More customized simulation
60+
smld_true, smld_model, smld_noisy = simulate(;
61+
ρ=1.0, # emitters per μm²
62+
σ_psf=0.13, # PSF width in μm (130nm)
63+
minphotons=50, # minimum photons for detection
64+
ndatasets=10, # number of independent datasets
65+
nframes=1000, # frames per dataset
66+
framerate=50.0, # frames per second
67+
pattern=Nmer2D(n=6, d=0.2), # hexamer with 200nm diameter
68+
molecule=GenericFluor(; q=[0 50; 1e-2 0]), # rates in 1/s
69+
camera=IdealCamera(; ypixels=256, xpixels=128, pixelsize=0.1) # pixelsize in μm
70+
)
71+
```
72+
73+
This customized example:
74+
- Creates hexagonal patterns (6 molecules in a 200nm circle)
75+
- Uses 10 independent datasets of 1000 frames each
76+
- Simulates fluorophores with specific on/off transition rates
77+
- Uses a rectangular camera field of view (128×256 pixels)
78+
79+
In both cases, the output SMLD objects contain emitter information (x, y coordinates, photon counts, frame numbers, etc.) that can be used for further analysis or visualization.
80+
81+
## Pattern Types
82+
83+
SMLMSim includes several built-in pattern types for positioning fluorophores:
84+
85+
### 2D Patterns
86+
87+
```julia
88+
# N molecules arranged in a circle
89+
nmer = Nmer2D(n=8, d=0.1) # 8 molecules in a 100nm diameter circle
90+
91+
# Linear pattern with random positions
92+
line = Line2D(λ=5.0, endpoints=[(-2.0, 0.0), (2.0, 0.0)]) # 5 molecules per μm along line
93+
```
94+
95+
### 3D Patterns
96+
97+
```julia
98+
# N molecules arranged in a circle at z=0
99+
nmer3d = Nmer3D(n=8, d=0.1) # 8 molecules in a 100nm diameter circle
100+
101+
# 3D line with random positions
102+
line3d = Line3D(λ=5.0, endpoints=[(-1.0, 0.0, -0.5), (1.0, 0.0, 0.5)])
103+
```
104+
105+
## Molecule Models
106+
107+
SMLMSim supports different fluorophore photophysical models:
108+
109+
```julia
110+
# Generic fluorophore with two-state kinetics
111+
fluor = GenericFluor(
112+
γ=10000.0, # photon emission rate in Hz
113+
q=[0 10; 1e-2 0] # transition rate matrix: state 1 ↔ state 2
114+
)
115+
```
116+
117+
## Diffusion and Interaction Simulation
118+
119+
The package includes tools for simulating diffusion and interactions between molecules:
120+
121+
```julia
122+
# Set up parameters for Smoluchowski diffusion simulation
123+
params = SmoluchowskiParams(
124+
density = 0.5, # molecules per μm²
125+
box_size = 10.0, # μm
126+
diff_monomer = 0.1, # μm²/s
127+
diff_dimer = 0.05, # μm²/s
128+
k_off = 0.2, # s⁻¹
129+
r_react = 0.01, # μm
130+
d_dimer = 0.05, # μm
131+
dt = 0.01, # s
132+
t_max = 10.0 # s
133+
)
134+
135+
# Run simulation
136+
systems = simulate(params)
137+
138+
# Visualize the simulation
139+
visualize_sequence(systems, filename="diffusion.mp4", framerate=30)
140+
141+
# Generate microscope images
142+
psf = Gaussian2D(0.15) # 150nm PSF width
143+
images = gen_image_sequence(
144+
psf,
145+
systems,
146+
frame_integration=10
147+
)
148+
149+
# Extract only dimers
150+
dimer_systems = get_dimers(systems)
151+
dimer_images = gen_image_sequence(
152+
psf,
153+
dimer_systems,
154+
frame_integration=10
155+
)
156+
```
157+
158+
## Example Workflows
159+
160+
### 2D Simulation with Visualization
161+
162+
```julia
163+
using SMLMSim
164+
using CairoMakie
165+
166+
# Create camera with physical pixel size
167+
camera = IdealCamera(1:128, 1:256, 0.1) # 128×256 pixels, 100nm pixels
168+
169+
# Simulation parameters in physical units
170+
smld_true, smld_model, smld_noisy = simulate(;
171+
ρ=1.0, # emitters per μm²
172+
σ_psf=0.13, # PSF width in μm
173+
pattern=Nmer2D(n=6, d=0.2), # hexamer with 200nm diameter
174+
camera=camera
175+
)
176+
177+
# Extract coordinates from emitters
178+
x_noisy = [e.x for e in smld_noisy.emitters]
179+
y_noisy = [e.y for e in smld_noisy.emitters]
180+
photons = [e.photons for e in smld_noisy.emitters]
181+
182+
# Create figure and plot results
183+
fig = Figure(size=(800, 600))
184+
ax = Axis(fig[1, 1],
185+
title="Simulated SMLM Localizations",
186+
xlabel="x (μm)",
187+
ylabel="y (μm)",
188+
aspect=DataAspect()
189+
)
190+
191+
# Scatter plot with photon counts as color
192+
scatter!(ax, x_noisy, y_noisy,
193+
color=photons,
194+
colormap=:viridis,
195+
markersize=4,
196+
alpha=0.6
197+
)
198+
199+
Colorbar(fig[1, 2], colormap=:viridis, label="Photons")
200+
201+
# Show or save the figure
202+
# display(fig)
203+
# save("smlm_simulation.png", fig)
204+
```
205+
206+
### 3D Simulation
207+
208+
```julia
209+
using SMLMSim
210+
211+
# Create camera with physical pixel size
212+
camera = IdealCamera(1:128, 1:256, 0.1) # 128×256 pixels, 100nm pixels
213+
214+
# Simulation parameters in physical units
215+
smld_true, smld_model, smld_noisy = simulate(;
216+
ρ=0.5, # emitters per μm²
217+
pattern=Nmer3D(n=8, d=0.3), # 3D pattern
218+
camera=camera,
219+
zrange=[-2.0, 2.0] # 4μm axial range
220+
)
221+
```
222+
223+
## Contributors
224+
225+
- [JuliaSMLM Team](https://github.com/JuliaSMLM)
226+
227+
## License
228+
229+
This project is licensed under the MIT License - see the LICENSE file for details. The MIT License is a permissive license that allows for reuse with few restrictions. It permits use, modification, distribution, and private use while preserving copyright and license notices.

dev/test_README.jl

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# using Pkg
2+
# Pkg.activate("dev")
3+
4+
# using Revise
5+
using SMLMSim # must be 'dev'ed in the current environment
6+
using CairoMakie
7+
8+
#==========================================================================
9+
Basic Usage
10+
==========================================================================#
11+
12+
# Basic simulation with default parameters
13+
camera = IdealCamera(1:128, 1:128, 0.1) # 128×128 pixels, 100nm pixels
14+
smld_true, smld_model, smld_noisy = simulate(
15+
camera=camera
16+
)
17+
18+
# More customized simulation
19+
smld_true, smld_model, smld_noisy = simulate(;
20+
ρ=1.0, # emitters per μm²
21+
σ_psf=0.13, # PSF width in μm (130nm)
22+
minphotons=50, # minimum photons for detection
23+
ndatasets=10, # number of independent datasets
24+
nframes=1000, # frames per dataset
25+
framerate=50.0, # frames per second
26+
pattern=Nmer2D(n=6, d=0.2), # hexamer with 200nm diameter
27+
molecule=GenericFluor(; q=[0 50; 1e-2 0]), # rates in 1/s
28+
camera=IdealCamera(; ypixels=256, xpixels=128, pixelsize=0.1) # pixelsize in μm
29+
)
30+
31+
#==========================================================================
32+
Pattern Types
33+
==========================================================================#
34+
35+
# 2D Patterns
36+
# N molecules arranged in a circle
37+
nmer = Nmer2D(n=8, d=0.1) # 8 molecules in a 100nm diameter circle
38+
39+
# Linear pattern with random positions
40+
line = Line2D(λ=5.0, endpoints=[(-2.0, 0.0), (2.0, 0.0)]) # 5 molecules per μm along line
41+
42+
# 3D Patterns
43+
# N molecules arranged in a circle at z=0
44+
nmer3d = Nmer3D(n=8, d=0.1) # 8 molecules in a 100nm diameter circle
45+
46+
# 3D line with random positions
47+
line3d = Line3D(λ=5.0, endpoints=[(-1.0, 0.0, -0.5), (1.0, 0.0, 0.5)])
48+
49+
#==========================================================================
50+
Molecule Models
51+
==========================================================================#
52+
53+
# Generic fluorophore with two-state kinetics
54+
fluor = GenericFluor(
55+
γ=10000.0, # photon emission rate in Hz
56+
q=[0 10; 1e-2 0] # transition rate matrix: state 1 ↔ state 2
57+
)
58+
59+
#==========================================================================
60+
Diffusion and Interaction Simulation
61+
==========================================================================#
62+
63+
# Set up parameters for Smoluchowski diffusion simulation
64+
params = SmoluchowskiParams(
65+
density = 0.5, # molecules per μm²
66+
box_size = 10.0, # μm
67+
diff_monomer = 0.1, # μm²/s
68+
diff_dimer = 0.05, # μm²/s
69+
k_off = 0.2, # s⁻¹
70+
r_react = 0.01, # μm
71+
d_dimer = 0.05, # μm
72+
dt = 0.01, # s
73+
t_max = 10.0 # s
74+
)
75+
76+
# Run simulation
77+
systems = simulate(params)
78+
79+
# Visualize the simulation
80+
visualize_sequence(systems, filename="diffusion.mp4", framerate=30)
81+
82+
# Generate microscope images
83+
psf = Gaussian2D(0.15) # 150nm PSF width
84+
images = gen_image_sequence(
85+
psf,
86+
systems,
87+
frame_integration=10
88+
)
89+
90+
# Extract only dimers
91+
dimer_systems = get_dimers(systems)
92+
dimer_images = gen_image_sequence(
93+
psf,
94+
dimer_systems,
95+
frame_integration=10
96+
)
97+
98+
#==========================================================================
99+
2D Simulation with Visualization
100+
==========================================================================#
101+
102+
# Create camera with physical pixel size
103+
camera_viz = IdealCamera(1:128, 1:256, 0.1) # 128×256 pixels, 100nm pixels
104+
105+
# Simulation parameters in physical units
106+
smld_true_viz, smld_model_viz, smld_noisy_viz = simulate(;
107+
ρ=1.0, # emitters per μm²
108+
σ_psf=0.13, # PSF width in μm
109+
pattern=Nmer2D(n=6, d=0.2), # hexamer with 200nm diameter
110+
camera=camera_viz
111+
)
112+
113+
# Extract coordinates from emitters
114+
x_noisy = [e.x for e in smld_noisy_viz.emitters]
115+
y_noisy = [e.y for e in smld_noisy_viz.emitters]
116+
photons = [e.photons for e in smld_noisy_viz.emitters]
117+
118+
# Create figure and plot results
119+
fig = Figure(size=(800, 600))
120+
ax = Axis(fig[1, 1],
121+
title="Simulated SMLM Localizations",
122+
xlabel="x (μm)",
123+
ylabel="y (μm)",
124+
aspect=DataAspect()
125+
)
126+
127+
# Scatter plot with photon counts as color
128+
scatter!(ax, x_noisy, y_noisy,
129+
color=photons,
130+
colormap=:viridis,
131+
markersize=4,
132+
alpha=0.6
133+
)
134+
135+
Colorbar(fig[1, 2], colormap=:viridis, label="Photons")
136+
137+
# Show or save the figure
138+
display(fig)
139+
# save("smlm_simulation.png", fig)
140+
141+
#==========================================================================
142+
3D Simulation
143+
==========================================================================#
144+
145+
# Create camera with physical pixel size
146+
camera_3d = IdealCamera(1:128, 1:256, 0.1) # 128×256 pixels, 100nm pixels
147+
148+
# Simulation parameters in physical units
149+
smld_true_3d, smld_model_3d, smld_noisy_3d = simulate(;
150+
ρ=0.5, # emitters per μm²
151+
pattern=Nmer3D(n=8, d=0.3), # 3D pattern
152+
camera=camera_3d,
153+
zrange=[-2.0, 2.0] # 4μm axial range
154+
)

0 commit comments

Comments
 (0)