Elements
Оптические элементы: линзы, апертуры, дифракционные слои, SLM и др.
from svetlanna.elements import ...Классы
MulElement
Наследует:Element ABC
Class that generalize all elements with E->T@E like forward function, where T is transmission function
Свойства
transmission_function_axesAxes of the transmission function.
Методы
get_transmission_function
get_transmission_function(self) -> torch.TensorMethod which returns the transmission function of the element
📤 Возвращает
torch.Tensor
transmission function
forward
forward(self, incident_wavefront: Wavefront) -> WavefrontCalculate the field after propagating through the element
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
input_field | Wavefront | Field incident on the aperture |
📤 Возвращает
Wavefront
The field after propagating through the element
Aperture
Наследует:MulElement
Aperture of the optical element with transmission function, which takes the value 0 or 1
Свойства
transmission_function_axesМетоды
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, mask: OptimizableTensor)Aperture of the optical element defined by mask tensor.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | Class exemplar that describes the optical system |
mask | torch.Tensor | Two-dimensional tensor representing the aperture mask. Each element must be either 0 (blocks light) or 1 (allows light). |
get_transmission_function
get_transmission_function(self) -> torch.Tensorto_specs
to_specs(self) -> Iterable[ParameterSpecs]RectangularAperture
Наследует:MulElement
A rectangle-shaped aperture with a transmission function taking either a value of 0 or 1
Свойства
transmission_function_axesМетоды
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, height: float, width: float)Constructor method
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | Class exemplar, that describes optical system |
height | float | aperture height |
width | float | aperture width |
get_transmission_function
get_transmission_function(self) -> torch.Tensorto_specs
to_specs(self) -> Iterable[ParameterSpecs]RoundAperture
Наследует:MulElement
A round-shaped aperture with a transmission function taking either a value of 0 or 1
Свойства
transmission_function_axesМетоды
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, radius: float)Constructor method
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | Class exemplar, that describes optical system |
radius | float | Radius of the round-shaped aperture |
get_transmission_function
get_transmission_function(self) -> torch.Tensorto_specs
to_specs(self) -> Iterable[ParameterSpecs]DiffractiveLayer
Наследует:Element
A class that described the field after propagating through the passive diffractive layer with a given phase mask
Свойства
transmission_functionМетоды
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, mask: OptimizableTensor, mask_norm: float = 2 * torch.pi)Constructor method
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | Simulation parameters |
mask | OptimizableTensor | Phase mask |
mask_norm | float, optional | This value will be used as following: the phase addition is equal to 2*torch.pi * mask / mask_norm. By default, 2*torch.pi |
forward
forward(self, incident_wavefront: Wavefront) -> WavefrontMethod that calculates the field after propagating through the SLM
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
input_field | Wavefront | Field incident on the SLM |
📤 Возвращает
Wavefront
The field after propagating through the SLM
reverse
reverse(self, transmission_wavefront: Wavefront) -> WavefrontMethod that calculates the field after passing the SLM in back propagation
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
transmitted_field | Wavefront | Field incident on the SLM in back propagation (transmitted field in forward propagation) |
📤 Возвращает
Wavefront
Field transmitted on the SLM in back propagation (incident field in forward propagation)
to_specs
to_specs(self) -> Iterable[ParameterSpecs]_BufferedValueContainer
Наследует:tuple
Internal class that marks buffered values.
It is used to prevent double setattr calls with the same value in
patterns like self.x = self.make_buffer('x', x_value).
Inheriting from tuple is used for performance reasons, so the __slots__.
This approach was identified by GPT as the fastest one.
Element
Наследует:nn.Module
A class that describes each element of the system
Методы
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters) -> NoneA class that describes each element of the system
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | Class exemplar that describes the optical system |
forward
forward(self, incident_wavefront: Wavefront) -> WavefrontForward propagation through the optical element
to_specs
to_specs(self) -> Iterable[ParameterSpecs | SubelementSpecs]Create specs
make_buffer
make_buffer(self, name: str, value: _T, persistent: bool = False) -> _TMake buffer for internal use.
Use case:
self.mask = make_buffer('mask', some_tensor)This allow torch to properly process .to method on Element
by marking that mask should be transferred to required device.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
name | str | name of the new buffer (it is more convenient to use name of new attribute) |
value | _T | tensor to be buffered |
persistent | bool, optional | see torch docs on buffers, by default False |
📤 Возвращает
_T
the value passed to the method
process_parameter
process_parameter(self, name: str, value: _V) -> _VProcess element parameter passed by user. Automatically registers buffer for non-parametric tensors.
Use case:
self.mask = process_parameter('mask', some_tensor)📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
name | str | name of the new buffer (it is more convenient to use name of new attribute) |
value | _V | the value of the element parameter |
📤 Возвращает
_V
the value passed to the method
FreeSpace
Наследует:Element
A class that describes a propagation of the field in free space between two optical elements
Методы
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, distance: OptimizableFloat, method: Literal['fresnel', 'AS'])Free space element.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | An instance describing the optical system’s simulation parameters. |
distance | float | The distance of the free space propagation. |
method | Literal['fresnel', 'AS'] | Method describing propagation in free space (1) ‘AS’ - angular spectrum method, (2) ‘fresnel’ - fresnel approximation, |
impulse_response_angular_spectrum
impulse_response_angular_spectrum(self) -> torch.TensorCreates the impulse response function for angular spectrum method
📤 Возвращает
torch.Tensor
2d impulse response function for angular spectrum method
impulse_response_fresnel
impulse_response_fresnel(self) -> torch.TensorCreates the impulse response function for fresnel approximation
📤 Возвращает
torch.Tensor
2d impulse response function for fresnel approximation
forward
forward(self, incident_wavefront: Wavefront) -> WavefrontCalculates the field after propagating in the free space
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
input_field | Wavefront | Field before propagation in free space |
📤 Возвращает
Wavefront
Field after propagation in free space
⚠️ Исключения
ValueError— Occurs when a non-existent direct distribution method is chosen
reverse
reverse(self, transmission_wavefront: Wavefront) -> WavefrontCalculate the field after it propagates in the free space in the backward direction.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
transmission_field | Wavefront | Field to be propagated in the backward direction |
📤 Возвращает
Wavefront
Propagated in the backward direction field
to_specs
to_specs(self) -> Iterable[ParameterSpecs]ThinLens
Наследует:Element
A class that described the field after propagating through the thin lens.
Свойства
transmission_functionМетоды
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, focal_length: OptimizableFloat, radius: float = torch.inf)Thin lens element.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | An instance describing the optical system’s simulation parameters. |
focal_length | OptimizableFloat | The focal length of the lens. Must be greater than 0 for a converging lens. |
radius | float | The radius of the thin lens. |
get_transmission_function
get_transmission_function(self) -> torch.TensorReturns the transmission function of the thin lens.
📤 Возвращает
torch.Tensor
The transmission function of the thin lens.
forward
forward(self, incident_wavefront: Wavefront) -> WavefrontCalculates the field after propagation through the thin lens.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
input_field | Wavefront | The field incident on the thin lens. |
📤 Возвращает
Wavefront
The field after propagation through the thin lens.
reverse
reverse(self, transmission_wavefront: Wavefront) -> WavefrontCalculates the field after passing through the lens during back propagation.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
transmission_field | Wavefront | The field incident on the lens during back propagation. This corresponds to the transmitted field in forward propagation. |
📤 Возвращает
Wavefront
The field transmitted through the lens during back propagation. This corresponds to the incident field in forward propagation.
to_specs
to_specs(self) -> Iterable[ParameterSpecs]FunctionModule
Наследует:torch.nn.Module
A class for transforming an arbitrary function with multiple parameters. Allows training function parameters
Методы
__init__ constructor
__init__(self, function: Callable[[torch.Tensor], torch.Tensor], function_parameters: Dict | None) -> NoneConstructor method
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
function | Callable[[torch.Tensor], torch.Tensor] | Arbitrary function with several parameters |
function_parameters | `Dict | None` |
forward
forward(self, function_argument: torch.Tensor) -> torch.Tensorforward method for a class inherited from torch.nn.Module
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
function_argument | torch.Tensor | Argument of the function |
📤 Возвращает
torch.Tensor
Function with trainable parameters
NonlinearElement
Наследует:Element
A class representing a nonlinear optical element with a given amplitude response function. Preserves the phase distribution of the incident wavefront
Методы
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, response_function: Callable[[torch.Tensor], torch.Tensor], response_parameters: Dict | None = None)Constructor method
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | Class exemplar, that describes optical system |
response_function | Callable[[torch.Tensor], torch.Tensor] | Intensity response function |
response_parameters | Dict, optional | Parameters of the response_function. Parameters converted to svetlanna.Parameter(value) will be trained, by default None |
forward
forward(self, incident_wavefront: Wavefront) -> WavefrontMethod calculating the wavefront after passing a nonlinear optical element
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
incident_wavefront | Wavefront | Wavefront before the nonlinear optical element |
📤 Возвращает
Wavefront
Wavefront passing through a nonlinear optical element
SimpleReservoir
Наследует:Element
Reservoir element.
Методы
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, nonlinear_element: Union[Element, 'LinearOpticalSetup'], delay_element: Union[Element, 'LinearOpticalSetup'], feedback_gain: OptimizableFloat, input_gain: OptimizableFloat, delay: int) -> NoneReservoir element. The main idea is explained in https://doi.org/10.1364/OE.20.022783 . The governing formula is:
x_{out}[i] = F_{NL}(eta x_{in}[i] + lpha F_{D}(x_{out}[i- au]))where F_{NL} is the nonlinear element, F_{D} is the delay element, lpha is the feedback_gain, eta is the input_gain, is the delay in samples. The user should match the delay in samples with the actual light propagation time in F_{D}.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
simulation_parameters | SimulationParameters | An instance describing the optical system’s simulation parameters. |
nonlinear_element | `Element | LinearOpticalSetup` |
delay_element | `Element | LinearOpticalSetup` |
feedback_gain | OptimizableFloat | The feedback (delay line) gain (lpha). |
input_gain | OptimizableFloat | The input gain eta |
delay | int | The delay time, measured in samples, that the light spends in the delay line. |
append_feedback_queue
append_feedback_queue(self, field: Wavefront)Append a new wavefront to the feedback queue.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
field | Wavefront | The new wavefront to be added to the end of the queue. |
pop_feedback_queue
pop_feedback_queue(self) -> None | WavefrontRetrieve and remove the first element from the feedback queue if available.
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
field | Wavefront | The first wavefront in the queue, or None if the queue is empty or not full yet. |
drop_feedback_queue
drop_feedback_queue(self) -> NoneClear all elements from the feedback queue.
forward
forward(self, incident_wavefront: Wavefront) -> Wavefrontto_specs
to_specs(self) -> Iterable[ParameterSpecs | SubelementSpecs]SpatialLightModulator
Наследует:Element
A class that described the field after propagating through the Spatial Light Modulator with a given phase mask
📥 Параметры
| Параметр | Тип | Описание |
|---|---|---|
Element | _type_ | description |
Свойства
get_apertureresized_masktransmission_functionМетоды
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, mask: torch.Tensor, height: OptimizableFloat, width: OptimizableFloat, location: Tuple = (0.0, 0.0), number_of_levels: int = 256, step_function: Callable[[torch.Tensor], torch.Tensor] = relu, mode: Literal['nearest', 'bilinear', 'bicubic', 'area', 'nearest-exact'] = 'nearest')forward
forward(self, incident_wavefront: Wavefront) -> Wavefrontreverse
reverse(self, transmission_wavefront: Wavefront) -> Wavefront