Aperture

class dnois.optics.rt.Aperture(*args, **kwargs)

Base class for aperture shapes. Aperture refers to the region on a surface where rays can transmit. The region outside the aperture is assumed to be completely opaque. Note that the region inside is not necessarily completely transparent. The most common aperture type is CircularAperture.

Mathematically, an aperture is defined by a set of 2D points on the baseline plane of associated surface. See Surface for more details.

abstract evaluate(x: Tensor, y: Tensor) BoolTensor

Returns a boolean tensor representing whether each point \((x,y)\) is inside the aperture. To jointly represent 2D coordinates, x and y must be broadcastable.

Parameters:
  • x (Tensor) – x coordinates of the points.

  • y (Tensor) – y coordinates of the points.

Returns:

See description above. The shape of returned tensor is the broadcast result of x and y.

Return type:

Tensor

forward(ray: BatchedRay) BatchedRay

Similar to evaluate(), but operates on rays.

Parameters:

ray (BatchedRay) – Incident rays.

Returns:

New rays among which those outside the aperture are marked as invalid.

Return type:

BatchedRay

classmethod from_dict(d: dict)

Constructs an instance of cls from a dict.

Parameters:

d (dict) – A dict typically returned by to_dict().

Returns:

An instance of cls.

classmethod load_json(file, **kwargs) Self

Constructs an instance of cls through loading JSON from a file, converting it to a dict and then calling from_dict().

Parameters:
  • file (str or pathlib.Path or file-like object) – The JSON file to load. Either its path (str or pathlib.Path) or a file-like object.

  • kwargs – Keyword arguments passed to json.load().

Returns:

An instance of cls.

pass_ray(ray: BatchedRay) BoolTensor

Similar to evaluate(), but operates on rays.

Parameters:

ray (BatchedRay) – Incident rays.

Returns:

A mask tensor indicating whether corresponding rays can pass the aperture.

Return type:

torch.BoolTensor

register_latent_parameter(name: str, param: Parameter | None, transform: Transform)

Similar to register_parameter(), but takes as input the latent value rather than nominal value. In this way, the inverse transformation need not be provided since the initial latent value is known.

If name corresponds to a vanilla parameter (i.e. not transformed parameter) it will be converted to a transformed one.

Parameters:
  • name (str) – Name of the parameter.

  • param (Parameter) – Latent torch.nn.Parameter instance to be registered.

  • transform (Transform) – Transformation object.

register_parameter(name: str, param: Parameter | None, transform: Transform = None) None

Similar to torch.nn.Module.register_parameter(), but allows you to register a transformed parameter as long as transform is given.

If name corresponds to a vanilla parameter (i.e. not transformed parameter) but transform is given, it will be converted to a transformed one.

Parameters:
  • name (str) – Name of the parameter.

  • param (Parameter) – Nominal torch.nn.Parameter instance to be registered.

  • transform (Transform) – Transformation object.

remove_transform(name: str)

Remove transformation for parameter name.

Parameters:

name (str) – Name of the parameter.

sample(mode: str, *args, **kwargs) tuple[Tensor, Tensor]

Samples points on this aperture, i.e. baseline plane of associated surface. Specific distribution depends on mode.

Parameters:

mode (str) – Sampling mode. Calling object of this method should possess a sample_{mode} method.

Returns:

Two 1D tensors of representing x and y coordinates of the points.

Return type:

tuple[Tensor, Tensor]

sample_center() tuple[Tensor, Tensor]

Return the central point of the aperture.

Returns:

Two [0.] tensors.

Return type:

tuple[Tensor, Tensor]

abstract sample_random(n: int) tuple[Tensor, Tensor]

Returns n points randomly sampled on this aperture.

Parameters:

n (int) – Number of points.

Returns:

Two 1D tensors of length n, representing x and y coordinates of the points.

Return type:

tuple[Tensor, Tensor]

sampler(mode: str, *args, **kwargs) Callable[[], tuple[Tensor, Tensor]]

Returns a callable object that can be used to sample points on this aperture. When it is called, it will call sample() with given arguments and return its return value. See sample() for more details.

Returns:

A callable object that can be used to sample points on this aperture.

Return type:

Callable

save_json(file, **kwargs)

Save self into a JSON file file.

Parameters:
  • file – The JSON file to save. Either its path (str or pathlib.Path) or a file-like object.

  • kwargs – Keyword arguments passed to json.dump().

set_transform(name: str, transform: Transform)

Set transformation for parameter name.

If name corresponds to a vanilla parameter (i.e. not transformed parameter) it will be converted to a transformed one.

Parameters:
  • name (str) – Name of the parameter.

  • transform (Transform) – Transformation object.

to_dict(keep_tensor=True) dict[str, Any]

Converts self into a dict which recursively contains only primitive Python objects.

Return type:

dict

to_json(**kwargs) str

Converts self into a JSON string.

Parameters:

kwargs – Keyword arguments passed to json.dumps().

Return type:

str

property device: device

Device of this object.

Type:

torch.device

property dtype: dtype

Data type of this object.

Type:

torch.dtype

property nominal_values: dict[str, Tensor]

A dict whose keys are names of all parameters of this module and values are their values. The values are nominal ones for transformed parameters.

Type:

dict[str, Tensor]

property transformed_parameters: dict[str, tuple[Parameter, Transform]]

A dict whose keys are names of all transformed parameters of this module. The value corresponding to each key is a tuple containing:

  • The latent value, a torch.nn.Parameter instance;

  • Corresponding transformation object.

Type:

dict[str, tuple[Parameter, Transform]]