1+ using SMLMSim. InteractionDiffusion
2+ using MicroscopePSFs
3+ using Random
4+
5+ function test_imaging_and_analysis()
6+ @testset " Image Generation" begin
7+ # Create a simple system with one molecule
8+ camera = IdealCamera(1 : 50 , 1 : 50 , 0.1 ) # 5μm x 5μm field with 100nm pixels
9+ emitter = Emitter2D{Float64}(2.5 , 2.5 , 1000.0 ) # Center of field
10+ molecule = DiffusingMolecule(emitter, 1 , 1 , nothing , false )
11+
12+ system = DiffusingMoleculeSystem(
13+ [molecule], camera, 5.0 , 1 , 1 , Dict{String,Any}()
14+ )
15+
16+ # Create PSF and generate image
17+ psf = Gaussian2D(0.15 )
18+ img = gen_image(psf, system, 1 , photons= 1000.0 , bg= 5.0 , poisson_noise= false )
19+
20+ # Test basic image properties
21+ @test size(img) == (length(camera. pixel_edges_y)- 1 , length(camera. pixel_edges_x)- 1 )
22+ @test all(img .>= 5.0 ) # At least background everywhere
23+
24+ # Test image sequence generation
25+ # Create a minimal simulation
26+ params = SmoluchowskiParams(
27+ density = 1.0 ,
28+ box_size = 2.0 ,
29+ dt = 0.01 ,
30+ t_max = 0.03 # Just 3 frames
31+ )
32+ systems = simulate(params)
33+
34+ # Generate image sequence
35+ imgs = gen_image_sequence(psf, systems,
36+ photons= 1000.0 , bg= 5.0 ,
37+ frame_integration= 1 , poisson_noise= false )
38+
39+ # Test basic sequence properties
40+ @test size(imgs, 3 ) == length(systems)
41+
42+ # Test frame integration
43+ imgs_integrated = gen_image_sequence(psf, systems,
44+ photons= 1000.0 , bg= 5.0 ,
45+ frame_integration= 3 , poisson_noise= false )
46+
47+ @test size(imgs_integrated, 3 ) == 1 # Should have just one frame with integration=3
48+ end
49+
50+ @testset " Dimer Imaging" begin
51+ # Create a system with dimers
52+ params = SmoluchowskiParams(
53+ density = 5.0 ,
54+ box_size = 1.0 ,
55+ dt = 0.01 ,
56+ t_max = 0.01 # Just one frame
57+ )
58+ system = initialize_system(params)
59+
60+ # Make some molecules into dimers
61+ n_molecules = length(system. molecules)
62+ n_dimers = min(2 , div(n_molecules, 2 ))
63+ for i in 1 : n_dimers
64+ mol1 = system. molecules[2 * i- 1 ]
65+ mol2 = system. molecules[2 * i]
66+ mol1. state = 2
67+ mol2. state = 2
68+ mol1. link = mol2. id
69+ mol2. link = mol1. id
70+ end
71+
72+ # Create a sequence with just this one system
73+ systems = [system]
74+
75+ # Create PSF and generate dimer images
76+ psf = Gaussian2D(0.15 )
77+ dimer_images = gen_dimer_images(systems, psf,
78+ photons= 1000.0 , bg= 5.0 ,
79+ frame_integration= 1 , poisson_noise= false )
80+
81+ # Test basic image properties
82+ @test size(dimer_images, 3 ) == 1
83+ end
84+
85+ @testset " Full Simulation Pipeline" begin
86+ # Test the complete workflow with minimal simulation
87+ # This is a crucial test as it ensures all components work together
88+
89+ # Create simulation parameters
90+ params = SmoluchowskiParams(
91+ density = 1.0 ,
92+ box_size = 1.0 ,
93+ dt = 0.01 ,
94+ t_max = 0.02 # Just 2 frames
95+ )
96+
97+ # Create PSF
98+ psf = Gaussian2D(0.15 )
99+
100+ # Run simulation and imaging
101+ images, systems = simulate_and_image(params, psf,
102+ photons= 1000.0 , bg= 5.0 ,
103+ frame_integration= 1 , poisson_noise= false )
104+
105+ # Test basic outputs
106+ @test isa(images, Array{Float64, 3 })
107+ @test length(systems) == 2
108+ @test size(images, 3 ) == 2
109+ end
110+
111+ @testset " Core Interface" begin
112+ # Test the main simulate interfaces
113+ camera = IdealCamera(1 : 20 , 1 : 20 , 0.1 )
114+
115+ # Basic interface
116+ smld_true, smld_model, smld_noisy = SMLMSim. simulate(
117+ ρ= 1.0 ,
118+ camera= camera,
119+ nframes= 5 # Minimal
120+ )
121+
122+ # Test basic outputs
123+ @test smld_true isa BasicSMLD
124+ @test smld_model isa BasicSMLD
125+ @test smld_noisy isa BasicSMLD
126+
127+ # With explicit pattern
128+ pattern = Nmer2D(n= 4 , d= 0.1 )
129+ smld_true, smld_model, smld_noisy = SMLMSim. simulate(
130+ pattern,
131+ ρ= 1.0 ,
132+ camera= camera,
133+ nframes= 5
134+ )
135+
136+ @test smld_true isa BasicSMLD
137+ end
138+
139+ # Skip visualization tests in normal test runs as they're slow and mostly check for errors
140+ if get(ENV , " RUN_VISUALIZATION_TESTS" , " false" ) == " true"
141+ @testset " Visualization" begin
142+ # Create a small simulation
143+ params = SmoluchowskiParams(
144+ density = 2.0 ,
145+ box_size = 1.0 ,
146+ dt = 0.01 ,
147+ t_max = 0.03 # Just 3 frames
148+ )
149+ systems = simulate(params)
150+
151+ # Test with a temporary file (no assertion, just checking for errors)
152+ temp_file = " test_vis_$(rand(1 : 1000 )) .mp4"
153+
154+ try
155+ visualize_sequence(systems, filename= temp_file, framerate= 10 )
156+ finally
157+ if isfile(temp_file)
158+ rm(temp_file)
159+ end
160+ end
161+ end
162+ end
163+ end
0 commit comments