Ray & Light API Reference
The src.light module contains ray and wave optics representations for geometric and physical optics simulation.
Ray
Batched geometric ray bundle carrying origin, direction, wavelength, validity mask, energy, and optical path length.
src.light.Ray
Bases: DeepObj
Batched ray bundle for optical simulation.
Stores ray origins, directions, wavelength, validity mask, energy, obliquity,
and (in coherent mode) optical path length. All tensor attributes share the
same batch shape (*batch_size, num_rays).
Attributes:
| Name | Type | Description |
|---|---|---|
o |
Tensor
|
Ray origins, shape |
d |
Tensor
|
Unit ray directions, shape |
wvln |
Tensor
|
Wavelength scalar [µm]. |
is_valid |
Tensor
|
Binary validity mask, shape |
en |
Tensor
|
Energy weight, shape |
obliq |
Tensor
|
Obliquity factor, shape |
opl |
Tensor
|
Optical path length (coherent mode only),
shape |
coherent |
bool
|
Whether OPL tracking is enabled. |
Initialize a ray object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
o
|
Tensor
|
Ray origin, shape |
required |
d
|
Tensor
|
Ray direction, shape |
required |
wvln
|
float
|
Ray wavelength [µm]. |
DEFAULT_WAVE
|
coherent
|
bool
|
Enable optical path length tracking for coherent
tracing. Defaults to |
False
|
device
|
str
|
Compute device. Defaults to |
'cpu'
|
Source code in src/light/ray.py
prop_to
Ray propagates to a given depth plane.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
float
|
depth. |
required |
n
|
float
|
refractive index. Defaults to 1. |
1.0
|
Source code in src/light/ray.py
centroid
Calculate the centroid of the ray, shape (..., num_rays, 3)
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Centroid of the ray, shape (..., 3) |
Source code in src/light/ray.py
rms_error
Calculate the RMS error of the ray.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
center_ref
|
Tensor
|
Reference center of the ray, shape (..., 3). If None, use the centroid of the ray as reference. |
None
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: average RMS error of the ray |
Source code in src/light/ray.py
flip_xy
Flip the x and y coordinates of the ray.
This function is used when calculating point spread function and wavefront distribution.
Source code in src/light/ray.py
clone
Clone the ray.
Can spercify which device we want to clone. Sometimes we want to store all rays in CPU, and when using it, we move it to GPU.
Source code in src/light/ray.py
squeeze
Squeeze the ray.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
dimension to squeeze. Defaults to None. |
None
|
Source code in src/light/ray.py
unsqueeze
Unsqueeze the ray.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
dimension to unsqueeze. Defaults to None. |
None
|
Source code in src/light/ray.py
| Parameter | Type | Description |
|---|---|---|
o |
Tensor |
Ray origins, shape (*batch, num_rays, 3) in mm |
d |
Tensor |
Ray directions, shape (*batch, num_rays, 3) (auto-normalized) |
wvln |
float |
Wavelength in micrometers (must be 0.1 -- 10) |
coherent |
bool |
Enable optical path length tracking |
Key Attributes
| Attribute | Type | Shape | Description |
|---|---|---|---|
o |
Tensor |
(*batch, N, 3) |
Ray origins (mm) |
d |
Tensor |
(*batch, N, 3) |
Unit ray directions |
wvln |
Tensor |
scalar | Wavelength (\(\mu\)m) |
is_valid |
Tensor |
(*batch, N) |
Binary validity mask |
en |
Tensor |
(*batch, N, 1) |
Energy weight |
obliq |
Tensor |
(*batch, N, 1) |
Accumulated obliquity factor |
opl |
Tensor |
(*batch, N, 1) |
Optical path length (mm, coherent mode) |
Key Methods
prop_to(z, n=1.0)
Propagate all rays to a given z-plane.
centroid()
Weighted mean position of valid rays.
rms_error(center_ref=None)
RMS spot radius relative to a reference center.
clone(device=None)
Deep copy, optionally moving to a new device.
flip_xy()
Negate x and y components (used for PSF centering).
Wave Optics
Complex electromagnetic field representations with scalar diffraction propagation.
src.light.ComplexWave
Bases: DeepObj
Complex scalar wave field for diffraction simulation.
Represents a monochromatic, coherent complex amplitude on a uniform
rectangular grid. Propagation methods (ASM, Fresnel, Fraunhofer) are
implemented as member functions and use torch.fft for efficiency.
Attributes:
| Name | Type | Description |
|---|---|---|
u |
Tensor
|
Complex amplitude, shape |
wvln |
float
|
Wavelength [µm]. |
k |
float
|
Wave number |
phy_size |
tuple
|
Physical aperture size (W, H) [mm]. |
ps |
float
|
Pixel pitch [mm] (must be square). |
res |
tuple
|
Grid resolution (H, W) in pixels. |
z |
float
|
Current axial position [mm]. |
Initialize a complex wave field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Tensor or None
|
Initial complex amplitude.
Accepted shapes: |
None
|
wvln
|
float
|
Wavelength [µm]. Defaults to |
0.55
|
z
|
float
|
Initial axial position [mm]. Defaults to
|
0.0
|
phy_size
|
tuple
|
Physical aperture (W, H) [mm].
Defaults to |
(4.0, 4.0)
|
res
|
tuple
|
Grid resolution (H, W) [pixels]. Only
used when u is |
(2000, 2000)
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the pixel pitch is not square or the
wavelength is outside the range |
Source code in src/light/wave.py
point_wave
classmethod
point_wave(point=(0, 0, -1000.0), wvln=0.55, z=0.0, phy_size=(4.0, 4.0), res=(2000, 2000), valid_r=None)
Create a spherical wave field on x0y plane originating from a point source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
tuple
|
Point source position in object space. [mm]. Defaults to (0, 0, -1000.0). |
(0, 0, -1000.0)
|
wvln
|
float
|
Wavelength. [um]. Defaults to 0.55. |
0.55
|
z
|
float
|
Field z position. [mm]. Defaults to 0.0. |
0.0
|
phy_size
|
tuple
|
Valid plane on x0y plane. [mm]. Defaults to (2, 2). |
(4.0, 4.0)
|
res
|
tuple
|
Valid plane resoltution. Defaults to (1000, 1000). |
(2000, 2000)
|
valid_r
|
float
|
Valid circle radius. [mm]. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
field |
ComplexWave
|
Complex field on x0y plane. |
Source code in src/light/wave.py
plane_wave
classmethod
Create a planar wave field on x0y plane.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wvln
|
float
|
Wavelength. [um]. |
0.55
|
z
|
float
|
Field z position. [mm]. |
0.0
|
phy_size
|
tuple
|
Physical size of the field. [mm]. |
(4.0, 4.0)
|
res
|
tuple
|
Resolution. |
(2000, 2000)
|
valid_r
|
float
|
Valid circle radius. [mm]. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
field |
ComplexWave
|
Complex field. |
Source code in src/light/wave.py
image_wave
classmethod
Initialize a complex wave field from an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Tensor
|
Input image with shape [H, W] or [B, C, H, W]. Data range is [0, 1]. |
required |
wvln
|
float
|
Wavelength. [um]. |
0.55
|
z
|
float
|
Field z position. [mm]. |
0.0
|
phy_size
|
tuple
|
Physical size of the field. [mm]. |
(4.0, 4.0)
|
Returns:
| Name | Type | Description |
|---|---|---|
field |
ComplexWave
|
Complex field. |
Source code in src/light/wave.py
prop
Propagate the field by distance z. Can only propagate planar wave.
Reference
[1] Modeling and propagation of near-field diffraction patterns: A more complete approach. Table 1. [2] https://github.com/kaanaksit/odak/blob/master/odak/wave/classical.py [3] https://spie.org/samples/PM103.pdf [4] "Non-approximated Rayleigh Sommerfeld diffraction integral: advantages and disadvantages in the propagation of complex wave fields"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_dist
|
float
|
propagation distance, unit [mm]. |
required |
n
|
float
|
refractive index. |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
propagated complex wave field. |
Source code in src/light/wave.py
prop_to
Propagate the field to plane z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
float
|
destination plane z coordinate. |
required |
Source code in src/light/wave.py
gen_xy_grid
Generate the x and y grid.
Source code in src/light/wave.py
gen_freq_grid
load_npz
Load data from npz file.
Source code in src/light/wave.py
save
save_npz
Save the complex wave field to a npz file.
Source code in src/light/wave.py
show
Save the field as an image.
Source code in src/light/wave.py
pad
Pad the input field by (Hpad, Hpad, Wpad, Wpad). This step will also expand physical size of the field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Hpad
|
int
|
Number of pixels to pad on the top and bottom. |
required |
Wpad
|
int
|
Number of pixels to pad on the left and right. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
Padded complex wave field. |
Source code in src/light/wave.py
flip
Flip the field horizontally and vertically.
Propagation Methods
| Class | Method | Use Case |
|---|---|---|
AngularSpectrumMethod |
Fourier-domain propagation | Near-field, high-NA |
ScalableASM |
Scalable ASM with chirp-z transform | Large propagation distances |
FresnelDiffraction |
Fresnel approximation | Moderate distances |
FraunhoferDiffraction |
Fraunhofer (far-field) | Far-field patterns |
RayleighSommerfeld |
Rayleigh-Sommerfeld integral | Reference method |
Utility Functions
| Function | Description |
|---|---|
Nyquist_ASM_zmax(wvln, L, N) |
Maximum valid propagation distance for ASM |
Fresnel_zmin(wvln, L, N) |
Minimum valid distance for Fresnel approximation |
Default Wavelengths
Defined in src/config.py:
| Constant | Value | Description |
|---|---|---|
DEFAULT_WAVE |
0.587 \(\mu\)m | d-line (green) |
WAVE_RGB |
[0.656, 0.587, 0.486] \(\mu\)m | C-line, d-line, F-line (R, G, B) |
DEPTH |
-20000.0 mm | Approximate optical infinity |