DiffractiveLens
Pure wave-optics lens built from diffractive surfaces with scalar diffraction propagation. Use it to model flat DOE / metasurface imaging systems end-to-end.
deeplens.DiffractiveLens
DiffractiveLens(filename=None, device=None, dtype=torch.float32, primary_wvln=DEFAULT_WAVE, wvln_rgb=WAVE_RGB, obj_depth=DEPTH)
Bases: Lens
Paraxial diffractive lens in which each element is modelled as a phase surface.
Every optical element (converging lens, DOE, metasurface, …) is represented by a phase function applied to an incoming complex wavefront. Propagation between surfaces uses the Angular Spectrum Method (ASM). This model is simple and fast, but accurate only in the paraxial regime (it does not account for higher-order geometric aberrations).
Attributes:
| Name | Type | Description |
|---|---|---|
surfaces |
list
|
Ordered list of diffractive/phase surfaces. |
d_sensor |
Tensor
|
Distance from the last surface to the sensor plane [mm]. |
Notes
Lens parameters default to torch.float32; pass
dtype=torch.float64 for higher-precision wave propagation.
Initialize a diffractive lens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the lens configuration JSON file. If provided, loads the lens configuration from file. Defaults to None. |
None
|
device
|
str
|
Computation device ('cpu' or 'cuda'). Defaults to 'cpu'. |
None
|
dtype
|
dtype
|
Data type for the lens parameters. Defaults to torch.float32; pass torch.float64 for higher-precision wave propagation. |
float32
|
primary_wvln
|
float
|
Primary design wavelength [µm].
Used as fallback when a method is called without an explicit
|
DEFAULT_WAVE
|
wvln_rgb
|
sequence of float
|
Three wavelengths used
for RGB computations, ordered |
WAVE_RGB
|
obj_depth
|
float
|
Default object depth [mm], used
when a method is called without an explicit |
DEPTH
|
Source code in deeplens-src/deeplens/diffraclens.py
read_lens_json
Load the lens configuration from a JSON file.
Reads lens parameters including sensor configuration and diffractive surfaces from the specified JSON file. If sensor_size or sensor_res are not provided, defaults of 8mm x 8mm and 2000x2000 pixels will be used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the JSON configuration file. |
required |
Source code in deeplens-src/deeplens/diffraclens.py
write_lens_json
Write the lens configuration to a JSON file.
Saves all lens parameters including sensor configuration and diffractive surface data to the specified file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output path for the JSON file. |
required |
Source code in deeplens-src/deeplens/diffraclens.py
__call__
forward
Propagate a wave through the diffractive lens system to the sensor.
Sequentially applies phase modulation from each diffractive surface, then propagates the wave to the sensor plane using wave optics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wave
|
ComplexWave
|
Input wave field entering the lens system. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ComplexWave |
Output wave field at the sensor plane. |
Source code in deeplens-src/deeplens/diffraclens.py
render_mono
Simulate monochromatic lens blur by convolving an image with the point spread function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Tensor
|
Input image. Shape: (B, 1, H, W) |
required |
wvln
|
float
|
Wavelength in µm. When |
None
|
ks
|
int
|
PSF kernel size. When |
None
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Rendered image after applying lens blur with shape (B, 1, H, W). |
Source code in deeplens-src/deeplens/diffraclens.py
psf
Calculate the monochromatic PSF for one or more point sources.
Off-axis point sources are supported. The signature follows
psf and psf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor or list
|
Point source coordinates, shape
|
required |
wvln
|
float
|
Wavelength in µm. When |
None
|
ks
|
int
|
PSF kernel size in pixels. When |
None
|
recenter
|
bool
|
How the ks x ks kernel is centered (both options keep off-axis PSFs centered in the kernel). If True, crop around the measured peak (argmax of the sensor-plane intensity). If False (default), crop around the perspective (pinhole) image of the field point. The lens forms a physically inverted image, but the result is flipped so the PSF is reported in the sensor/source-sign convention (a +x source -> +x). |
False
|
upsample_factor
|
int
|
Field upsampling factor to meet the Nyquist sampling constraint. Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: PSF intensity map, shape |
|
|
point or |
Note
A single Angular Spectrum Method (ASM) window is used, so very large off-axis fields can suffer from the shifted-phase/aliasing issue; see "Modeling off-axis diffraction with the least-sampling angular spectrum method".
Source code in deeplens-src/deeplens/diffraclens.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
draw_layout
Draw a 2D layout diagram of the diffractive lens.
Each diffractive surface is drawn as a vertical dashed line at its axial
position z = surface.d, and the sensor as a solid rectangle at
z = d_sensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_name
|
str
|
Path to save the figure. Defaults to './doelens.png'. |
'./doelens.png'
|
Source code in deeplens-src/deeplens/diffraclens.py
draw_psf
Draw on-axis RGB PSF.
Computes and saves a visualization of the RGB PSF for a given depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
float
|
Depth of the point source. When |
None
|
ks
|
int
|
Size of the PSF kernel in pixels. When |
None
|
save_name
|
str
|
Path to save the PSF image. Defaults to './psf_doelens.png'. |
'./psf_doelens.png'
|
log_scale
|
bool
|
If True, display PSF in log scale. Defaults to True. |
True
|
eps
|
float
|
Small value for log scale to avoid log(0). Defaults to 1e-4. |
0.0001
|
Source code in deeplens-src/deeplens/diffraclens.py
get_optimizer
Build an Adam optimizer over the trainable diffractive surfaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float
|
Learning rate. |
required |
optim_surf_ls
|
list[int]
|
Indices of the surfaces to optimize. If None, all diffractive surfaces are optimized. |
None
|
Returns:
| Type | Description |
|---|---|
|
torch.optim.Optimizer: Adam optimizer over the selected surfaces' |
|
|
phase parameters. |