sax.utils module#

SAX General Utilities

block_diag(*arrs)[source]#

create block diagonal matrix with arbitrary batch dimensions

Parameters:

arrs (ComplexArrayND) –

Return type:

ComplexArrayND

clean_string(s, dot='p', minus='m', other='_')[source]#

clean a string such that it is a valid python identifier

Parameters:

s (str) –

Return type:

str

copy_settings(settings)[source]#

copy a parameter dictionary

Parameters:

settings (Settings) –

Return type:

Settings

validate_settings(settings)[source]#

Validate a parameter dictionary

Parameters:

settings (Settings) –

Return type:

Settings

try_complex_float(f)[source]#

try converting an object to float, return unchanged object on fail

Parameters:

f (Any) –

Return type:

Any

flatten_dict(dic, sep=',')[source]#

flatten a nested dictionary

Parameters:
  • dic (Dict[str, Any]) –

  • sep (str) –

Return type:

Dict[str, Any]

unflatten_dict(dic, sep=',')[source]#

unflatten a flattened dictionary

get_ports(S)[source]#

get port names of a model or an stype

Note

if a Model function is given in stead of an SDict, the function will be traced by JAX to obtain the port combinations of the resulting SType. Although this tracing of the function is ‘cheap’ in comparison to evaluating the model/circuit. It is not for free! Use this function sparingly on your large Model or circuit!

Parameters:

S (Union[Model, SType]) –

Return type:

Tuple[str, …]

get_port_combinations(S)[source]#

get port combinations of a model or an stype

Parameters:

S (Union[Model, SType]) –

Return type:

Tuple[Tuple[str, str], …]

get_settings(model)[source]#

Get the parameters of a SAX model function

Parameters:

model (Union[Model, ModelFactory]) –

Return type:

Settings

grouped_interp(wl, wls, phis)[source]#

Grouped phase interpolation

Note

Grouped interpolation is useful to interpolate phase values where each datapoint is doubled (very close together) to give an indication of the phase variation at that point.

Warning

this interpolation is only accurate in the range [wls[0], wls[-2]) (wls[-2] not included). Any extrapolation outside these bounds can yield unexpected results!

(Source code, png, hires.png, pdf)

_images/sax-utils-1.png
Parameters:
  • wl (FloatArrayND) –

  • wls (FloatArrayND) –

  • phis (FloatArrayND) –

Return type:

FloatArrayND

merge_dicts(*dicts)[source]#

merge (possibly deeply nested) dictionaries

Parameters:

dicts (Dict) –

Return type:

Dict

mode_combinations(modes, cross=False)[source]#

create mode combinations for a collection of given modes

Parameters:
  • modes (Iterable[str]) –

  • cross (bool) –

Return type:

Tuple[Tuple[str, str], …]

reciprocal(sdict)[source]#

Make an SDict reciprocal

Parameters:

sdict (SDict) –

Return type:

SDict

rename_params(model: ModelFactory, renamings: Dict[str, str]) ModelFactory[source]#
rename_params(model: Model, renamings: Dict[str, str]) Model

rename the parameters of a Model or ModelFactory given a renamings mapping old parameter names to new.

rename_ports(S: SDict, renamings: Dict[str, str]) SDict[source]#
rename_ports(S: SCoo, renamings: Dict[str, str]) SCoo
rename_ports(S: SDense, renamings: Dict[str, str]) SDense
rename_ports(S: Model, renamings: Dict[str, str]) Model
rename_ports(S: ModelFactory, renamings: Dict[str, str]) ModelFactory

rename the ports of an SDict, Model or ModelFactory given a renamings mapping old port names to new.

update_settings(settings, *compnames, **kwargs)[source]#

update a nested settings dictionary

Note

1. Even though it’s possible to update parameter dictionaries in place, this function is convenient to apply certain parameters (e.g. wavelength ‘wl’ or temperature ‘T’) globally. 2. This operation never updates the given settings dictionary inplace. 3. Any non-float keyword arguments will be silently ignored.

Parameters:
  • settings (Settings) –

  • compnames (str) –

  • kwargs (Any) –

Return type:

Settings

validate_not_mixedmode(S)[source]#

validate that an stype is not ‘mixed mode’ (i.e. invalid)

Parameters:

S (SType) – the stype to validate

validate_multimode(S, modes=('te', 'tm'))[source]#

validate that an stype is multimode and that the given modes are present.

Parameters:

S (SType) –

Return type:

None

validate_sdict(sdict)[source]#

Validate an SDict

Parameters:

sdict (Any) –

Return type:

None

get_inputs_outputs(ports)[source]#
Parameters:

ports (Tuple[str, ...]) –

hash_dict(dic)[source]#
Parameters:

dic (Dict) –

Return type:

int

class Normalization(mean, std)[source]#

Bases: NamedTuple

Parameters:
  • mean (ComplexArrayND) –

  • std (ComplexArrayND) –

mean: ComplexArrayND#

Alias for field number 0

std: ComplexArrayND#

Alias for field number 1

normalization(x, axis=None)[source]#
Parameters:

x (ComplexArrayND) –

cartesian_product(*arrays)[source]#

calculate the n-dimensional cartesian product of an arbitrary number of arrays

Parameters:

arrays (ComplexArrayND) –

Return type:

ComplexArrayND

normalize(x, normalization)[source]#

normalize an array with a given mean and standard deviation

Parameters:
Return type:

Tuple[ComplexArrayND]

denormalize(x, normalization)[source]#

denormalize an array with a given mean and standard deviation

Parameters:
Return type:

ComplexArrayND