Skip to content

Commit 465d439

Browse files
committed
refactor interfaces and code - this is a major breaking change even within this branch. Still needs testing.
1 parent 4183fbd commit 465d439

File tree

11 files changed

+1054
-145
lines changed

11 files changed

+1054
-145
lines changed

src/SMLMSim.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ include("diffusion/InteractionDiffusion.jl")
1515

1616
using .InteractionDiffusion
1717

18+
# Export molecule types
19+
export
20+
# Abstract molecule types
21+
Molecule,
22+
23+
# Concrete molecule types
24+
GenericFluor
25+
26+
# Export simulation functions
27+
export
28+
# Kinetic simulation
29+
intensitytrace,
30+
kineticmodel,
31+
noise,
32+
33+
# CTMC type and functions
34+
CTMC,
35+
getstate,
36+
getnext
1837

1938
# Core types and functions for diffusion simulation
2039
export
@@ -59,5 +78,4 @@ export
5978
visualize_simulation
6079

6180

62-
end
63-
81+
end

src/diffusion/InteractionDiffusion.jl

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
"""
2-
This module provides simulation tools for diffusion
3-
and interaction between particles.
2+
InteractionDiffusion
3+
4+
This module provides simulation tools for diffusion and interaction between particles.
5+
6+
# Overview
7+
Simulates diffusion and interaction dynamics between particles in 2D/3D space.
8+
Includes functionality for generating microscope images, analyzing dimers, and
9+
visualizing particle dynamics.
10+
11+
# Components
12+
- Core simulation types (DiffusingMolecule, DiffusingMoleculeSystem)
13+
- Smoluchowski dynamics simulation
14+
- Visualization tools
15+
- Microscope image generation
16+
- Dimer analysis and extraction
17+
18+
# Examples
19+
```julia
20+
# Set up simulation parameters
21+
params = SmoluchowskiParams(
22+
density = 0.5, # molecules per μm²
23+
box_size = 10.0, # μm
24+
diff_monomer = 0.1, # μm²/s
25+
diff_dimer = 0.05, # μm²/s
26+
k_off = 0.2, # s⁻¹
27+
r_react = 0.01, # μm
28+
d_dimer = 0.05, # μm
29+
dt = 0.01, # s
30+
t_max = 10.0 # s
31+
)
32+
33+
# Run simulation
34+
systems = simulate(params)
35+
36+
# Visualize the simulation
37+
visualize_sequence(systems, filename="diffusion.mp4")
38+
```
439
"""
540
module InteractionDiffusion
641

@@ -12,7 +47,6 @@ using SMLMSim
1247
using Printf
1348
using CairoMakie
1449

15-
1650
include("types.jl")
1751
include("smoluchowski.jl")
1852
include("visualize.jl")
@@ -33,10 +67,15 @@ export
3367
# Analysis functions
3468
get_dimers,
3569
gen_dimer_images,
70+
analyze_dimer_fraction,
3671

72+
# Visualization functions
3773
show_frame,
3874
visualize_sequence,
39-
visualize_simulation
40-
75+
visualize_simulation,
76+
77+
# Microscope image generation
78+
gen_image,
79+
gen_image_sequence
4180

42-
end
81+
end

src/diffusion/dimer.jl

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
"""
32
get_dimers(system::DiffusingMoleculeSystem)
43
@@ -9,6 +8,12 @@ Extract a new DiffusingMoleculeSystem containing only molecules in dimer state.
98
109
# Returns
1110
- `DiffusingMoleculeSystem`: New system containing only dimers
11+
12+
# Example
13+
```julia
14+
# Extract only dimers from a system
15+
dimer_system = get_dimers(system)
16+
```
1217
"""
1318
function get_dimers(system::DiffusingMoleculeSystem)
1419
# Extract molecules in dimer state
@@ -35,31 +40,61 @@ Extract dimers from a sequence of system states.
3540
3641
# Returns
3742
- `Vector{DiffusingMoleculeSystem}`: Sequence of dimer-only states
43+
44+
# Example
45+
```julia
46+
# Run simulation
47+
params = SmoluchowskiParams()
48+
systems = simulate(params)
49+
50+
# Extract dimers at each timepoint
51+
dimer_systems = get_dimers(systems)
52+
```
3853
"""
3954
function get_dimers(systems::Vector{<:DiffusingMoleculeSystem})
4055
[get_dimers(system) for system in systems]
4156
end
4257

4358
"""
44-
gen_dimer_images(systems::Vector{DiffusingMoleculeSystem}, psf::AbstractPSF; kwargs...)
59+
gen_dimer_images(systems::Vector{DiffusingMoleculeSystem}, psf::AbstractPSF;
60+
photons::Float64=1000.0, bg::Float64=5.0,
61+
frame_integration::Int=1, poisson_noise::Bool=true)
4562
4663
Generate microscope images showing only dimers.
4764
4865
# Arguments
4966
- `systems::Vector{DiffusingMoleculeSystem}`: Sequence of system states
50-
- `psf::PSF`: Point spread function model
67+
- `psf::AbstractPSF`: Point spread function model
5168
52-
# Optional kwargs
69+
# Keyword Arguments
70+
- `photons::Float64=1000.0`: Base photon count for emitters
71+
- `bg::Float64=5.0`: Background photon count per pixel
5372
- `frame_integration::Int=1`: Number of frames to integrate
5473
- `poisson_noise::Bool=true`: Whether to add Poisson noise
5574
5675
# Returns
5776
- `Array{Float64,3}`: Stack of dimer-only images [ny, nx, frames]
77+
78+
# Example
79+
```julia
80+
# Run simulation
81+
params = SmoluchowskiParams()
82+
systems = simulate(params)
83+
84+
# Generate images showing only dimers
85+
psf = Gaussian2D(0.15)
86+
dimer_images = gen_dimer_images(systems, psf;
87+
photons=2000.0,
88+
frame_integration=5)
89+
```
5890
"""
5991
function gen_dimer_images(systems::Vector{<:DiffusingMoleculeSystem}, psf::AbstractPSF;
60-
frame_integration::Int=1, poisson_noise::Bool=true)
92+
photons::Float64=1000.0, bg::Float64=5.0,
93+
frame_integration::Int=1, poisson_noise::Bool=true)
6194
dimer_systems = get_dimers(systems)
6295
gen_image_sequence(psf, dimer_systems;
96+
photons=photons,
97+
bg=bg,
6398
frame_integration=frame_integration,
6499
poisson_noise=poisson_noise)
65100
end
@@ -74,6 +109,24 @@ Calculate the fraction of molecules in dimer state over time.
74109
75110
# Returns
76111
- `Vector{Float64}`: Dimer fraction at each timepoint
112+
113+
# Example
114+
```julia
115+
# Run simulation
116+
params = SmoluchowskiParams()
117+
systems = simulate(params)
118+
119+
# Calculate dimer fraction over time
120+
dimer_fractions = analyze_dimer_fraction(systems)
121+
122+
# Plot the results
123+
using Plots
124+
times = range(0, params.t_max, length=length(systems))
125+
plot(times, dimer_fractions,
126+
xlabel="Time (s)",
127+
ylabel="Fraction of molecules in dimers",
128+
title="Dimer formation dynamics")
129+
```
77130
"""
78131
function analyze_dimer_fraction(systems::Vector{<:DiffusingMoleculeSystem})
79132
map(systems) do system
@@ -83,3 +136,31 @@ function analyze_dimer_fraction(systems::Vector{<:DiffusingMoleculeSystem})
83136
end
84137
end
85138

139+
"""
140+
analyze_dimer_lifetime(systems::Vector{DiffusingMoleculeSystem})
141+
142+
Analyze the lifetime distribution of dimers.
143+
144+
# Arguments
145+
- `systems::Vector{DiffusingMoleculeSystem}`: Sequence of system states
146+
147+
# Returns
148+
- `Vector{Float64}`: Distribution of dimer lifetimes
149+
150+
# Note
151+
This function is not yet fully implemented.
152+
"""
153+
function analyze_dimer_lifetime(systems::Vector{<:DiffusingMoleculeSystem})
154+
# Get time step from metadata
155+
if !haskey(systems[1].metadata, "simulation_parameters")
156+
error("System metadata missing simulation parameters")
157+
end
158+
159+
params = systems[1].metadata["simulation_parameters"]
160+
dt = params.dt
161+
162+
# TODO: Implement dimer lifetime tracking
163+
@warn "Dimer lifetime analysis not yet fully implemented"
164+
165+
return Float64[]
166+
end

0 commit comments

Comments
 (0)