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.
Free-space propagation between surfaces and to the sensor is handled by
ComplexWave.prop, which selects band-limited ASM or single-FFT
Fresnel diffraction based on the propagation distance. 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 first surface (z=0) to the sensor plane [mm]. |
Note
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 or None
|
Path to the lens configuration JSON file. If provided, loads the lens configuration from file; otherwise an empty surface list and a default 8x8 mm, 2000x2000 px sensor are used. Defaults to None. |
None
|
device
|
str or None
|
Computation device ('cpu' or
'cuda'). When None, resolved by the base |
None
|
dtype
|
dtype
|
Data type for the lens parameters.
Pass |
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 [R, G, B] in µm. Defaults to
|
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
surf_d
Return the derived global vertex position of DOE idx [mm].
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__
Propagate a wave through the lens system (alias of forward).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wave
|
ComplexWave
|
Input wave field entering the lens system. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
wave |
ComplexWave
|
Output wave field at the sensor plane. |
Source code in deeplens-src/deeplens/diffraclens.py
forward
Propagate a wave through the diffractive lens system to the sensor.
Sequentially applies each phase modulation and its following d_next
free-space propagation. The final surface's d_next reaches the sensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wave
|
ComplexWave
|
Input wave field entering the lens system. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
wave |
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 [µm]. When None (default),
falls back to |
None
|
ks
|
int
|
PSF kernel size in pixels. When None (default),
the full sensor resolution ( |
None
|
method
|
str
|
Convolution backend passed to |
'fft'
|
Returns:
| Name | Type | Description |
|---|---|---|
img_render |
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
Lens.psf and GeoLens.psf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor or list
|
Point source coordinates, shape [N, 3] or [3]. x, y are normalised to [-1, 1] (relative to the sensor half-width/height); z is the depth in mm (negative; -inf for an object at infinity). |
required |
wvln
|
float
|
Wavelength [µm]. When None (default), falls
back to |
None
|
ks
|
int
|
PSF kernel size in pixels. Pass |
PSF_KS
|
**kwargs
|
Model-specific options: - 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). - upsample_factor (int): Field upsampling factor to meet the Nyquist sampling constraint. When None (default), a factor is chosen so the field resolution is close to 4000 x 4000. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf |
Tensor
|
PSF intensity map (normalised to sum 1), shape [ks, ks] for a single point or [N, ks, ks] for a batch. |
Note
A single (non-tiled) propagation 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
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
draw_layout
Draw a 2D layout diagram of the diffractive lens.
Each diffractive surface is drawn at the prefix sum returned by
surf_d, and the sensor at the sum of all surface d_next values.
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 [mm]. When None
(default), falls back to |
None
|
ks
|
int
|
Size of the PSF kernel in pixels. When None
(default), the full sensor resolution ( |
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:
| Name | Type | Description |
|---|---|---|
optimizer |
Optimizer
|
Adam optimizer over the selected surfaces' phase parameters. |