-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtest_optics.py
More file actions
260 lines (232 loc) · 8.5 KB
/
test_optics.py
File metadata and controls
260 lines (232 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Use this only when running the test locally.
# import sys
# sys.path.append(".") # Adds the module to path
import unittest
import numpy as np
from deeptrack import optics
from deeptrack.scatterers import PointParticle, Sphere
from deeptrack import units_registry as u
from deeptrack.backend import TORCH_AVAILABLE, xp
from deeptrack.tests import BackendTestBase
if TORCH_AVAILABLE:
import torch
class TestOptics_NumPy(BackendTestBase):
BACKEND = "numpy"
@property
def array_type(self):
if self.BACKEND == "numpy":
return np.ndarray
elif self.BACKEND == "torch":
return torch.Tensor
else:
raise ValueError(f"Unsupported backend: {self.BACKEND}")
def test_Microscope(self):
microscope_type = optics.Fluorescence()
scatterer = PointParticle()
microscope = optics.Microscope(
sample=scatterer, objective=microscope_type,
)
output_image = microscope.get(None)
self.assertIsInstance(output_image, self.array_type)
self.assertEqual(output_image.shape, (128, 128, 1))
def test_Optics(self):
microscope = optics.Optics()
scatterer = PointParticle()
image = microscope(scatterer)
self.assertIsInstance(image, optics.Microscope)
def test_Fluorescence(self):
microscope = optics.Fluorescence(
NA=0.7,
wavelength=660e-9,
resolution=1e-6,
magnification=10,
refractive_index_medium=1.33,
upscale=2,
padding=(10, 10, 10, 10),
output_region=(0, 0, 64, 64),
aberration=None,
)
scatterer = PointParticle(
intensity=100, # Squared magnitude of the field.
position_unit="pixel", # Units of position (default meter)
position=(32, 32), # Position of the particle
)
imaged_scatterer = microscope(scatterer)
output_image = imaged_scatterer.resolve()
self.assertIsInstance(output_image, self.array_type)
self.assertEqual(microscope.NA(), 0.7)
self.assertEqual(output_image.shape, (64, 64, 1))
def test_Brightfield(self):
microscope = optics.Brightfield(
NA=0.7,
wavelength=660e-9,
resolution=1e-6,
magnification=10,
refractive_index_medium=1.33,
upscale=2,
output_region=(0, 0, 64, 64),
padding=(10, 10, 10, 10),
aberration=None,
)
scatterer = PointParticle(
refractive_index=1.45 + 0.1j,
position_unit="pixel",
position=(32, 32),
)
imaged_scatterer = microscope(scatterer)
output_image = imaged_scatterer.resolve()
self.assertIsInstance(output_image, self.array_type)
self.assertEqual(output_image.shape, (64, 64, 1))
def test_Holography(self):
microscope = optics.Holography(
NA=0.7,
wavelength=660e-9,
resolution=1e-6,
magnification=10,
refractive_index_medium=1.33,
upscale=2,
output_region=(0, 0, 64, 64),
padding=(10, 10, 10, 10),
aberration=None,
)
scatterer = PointParticle(
refractive_index=1.45 + 0.1j,
position_unit="pixel",
position=(32, 32),
)
imaged_scatterer = microscope(scatterer)
output_image = imaged_scatterer.resolve()
self.assertIsInstance(output_image, self.array_type)
self.assertEqual(output_image.shape, (64, 64, 1))
def test_ISCAT(self):
microscope = optics.ISCAT(
NA=0.7,
wavelength=660e-9,
resolution=1e-6,
magnification=10,
refractive_index_medium=1.33,
upscale=2,
output_region=(0, 0, 64, 64),
padding=(10, 10, 10, 10),
aberration=None,
)
scatterer = PointParticle(
refractive_index=1.45 + 0.1j,
position_unit="pixel",
position=(32, 32),
)
imaged_scatterer = microscope(scatterer)
output_image = imaged_scatterer.resolve()
self.assertEqual(microscope.illumination_angle(), 3.141592653589793)
self.assertIsInstance(output_image, self.array_type)
self.assertEqual(output_image.shape, (64, 64, 1))
def test_Darkfield(self):
microscope = optics.Darkfield(
NA=0.7,
wavelength=660e-9,
resolution=1e-6,
magnification=10,
refractive_index_medium=1.33,
upscale=2,
output_region=(0, 0, 64, 64),
padding=(10, 10, 10, 10),
aberration=None,
)
scatterer = PointParticle(
refractive_index=1.45 + 0.1j,
position_unit="pixel",
position=(32, 32),
)
imaged_scatterer = microscope(scatterer)
output_image = imaged_scatterer.resolve()
self.assertEqual(microscope.illumination_angle(), 1.5707963267948966)
self.assertIsInstance(output_image, self.array_type)
self.assertEqual(output_image.shape, (64, 64, 1))
def test_IlluminationGradient(self):
illumination_gradient = optics.IlluminationGradient(gradient=(5e-5, 5e-5))
microscope = optics.Brightfield(
NA=0.7,
wavelength=660e-9,
resolution=1e-6,
magnification=10,
refractive_index_medium=1.33,
upscale=2,
output_region=(0, 0, 64, 64),
padding=(10, 10, 10, 10),
aberration=None,
illumination=illumination_gradient,
)
scatterer = PointParticle(
refractive_index=1.45 + 0.1j,
position_unit="pixel",
position=(32, 32),
)
imaged_scatterer = microscope(scatterer)
output_image = imaged_scatterer.resolve()
self.assertIsInstance(output_image, self.array_type)
self.assertEqual(output_image.shape, (64, 64, 1))
def test_upscale_fluorescence(self):
microscope = optics.Brightfield(
NA=0.7,
wavelength=660e-9,
resolution=1e-6,
magnification=5,
refractive_index_medium=1.33,
upscale=2,
output_region=(0, 0, 64, 64),
padding=(10, 10, 10, 10),
aberration=None,
)
scatterer = Sphere(
refractive_index=1.45,
radius=1e-6,
z=2 * u.um,
position_unit="pixel",
position=(32, 32),
)
imaged_scatterer = microscope(scatterer)
output_image_no_upscale = imaged_scatterer.update()(upscale=1)
output_image_2x_upscale = imaged_scatterer.update()(upscale=(2, 2, 2))
self.assertEqual(output_image_no_upscale.shape, (64, 64, 1))
self.assertEqual(output_image_2x_upscale.shape, (64, 64, 1))
# Ensure the upscaled image is almost the same as the original image
error = np.abs(
output_image_2x_upscale - output_image_no_upscale
).mean() # Mean absolute error
self.assertLess(error, 0.01)
def test_upscale_brightfield(self):
microscope = optics.Fluorescence(
NA=0.5,
wavelength=660e-9,
resolution=1e-6,
magnification=10,
refractive_index_medium=1.33,
upscale=2,
output_region=(0, 0, 64, 64),
padding=(10, 10, 10, 10),
aberration=None,
)
scatterer = Sphere(
intensity=100,
radius=1e-6,
z=2 * u.um,
position_unit="pixel",
position=(32, 32),
)
imaged_scatterer = microscope(scatterer)
output_image_no_upscale = imaged_scatterer.update()(upscale=1)
output_image_2x_upscale = imaged_scatterer.update()(upscale=(2, 2, 1))
self.assertEqual(output_image_no_upscale.shape, (64, 64, 1))
self.assertEqual(output_image_2x_upscale.shape, (64, 64, 1))
# Ensure the upscaled image is almost the same as the original image
error = np.abs(
output_image_2x_upscale - output_image_no_upscale
).mean() # Mean absolute error
self.assertLess(error, 0.01)
# TODO: Extending the test and setting the backend to torch
# @unittest.skipUnless(TORCH_AVAILABLE, "PyTorch is not installed.")
# class TestOptics_PyTorch(TestOptics_NumPy):
# BACKEND = "torch"
# pass
if __name__ == "__main__":
unittest.main()