Radynversion API Documentation
Submodules
smug.radynversion_adapter module
- class smug.radynversion_adapter.RadynversionAdapter(model: RadynversionModel, atmos_params: List[str], line_profiles: List[str], line_half_width: List[float], z_stratification: ndarray, dev: Optional[device] = None)[source]
Bases:
object
Run a RADYNVERSION model: prepare the data, run the model, and extract the output.
The parameters for standard uses of this class are present in the smug.radynversion_model.model_params dict. When passing multiple instances of a parameter to any of the methods of this class (unless otherwise specified) they are expected to have leading dimension the number of instances, and second dimension the size of the parameter.
Transformation of different parameters is controlled by the transform_{param} and inv_transform_{param} static methods on the class, and these are looked up dynamically, i.e. this can be used with updated models taking different parameters by subclassing/monkeypatching.
- Parameters
model (RadynversionModel) – The model to use.
atmos_params (list of str) – The names of the atmospheric parameters in the order expected by the model.
line_profiles (list of str) – The names of the spectral lines in the order expected by the model.
line_half_width (list of float) – The half width (in Angstrom) of the lines expected by the model.
z_stratification (array-like) – The z_stratification used in the model.
dev (torch.device, optional) – The device to run the model on. Default will use the first CUDA device (if available), or CPU.
- forward_model(atmos: Tensor, cpu: bool = True) Dict[str, Tensor] [source]
Compute the line profiles associated with atmospheric samples and return in a dict by line name. This result also contains the associated latent space under key LatentSpace.
- Parameters
atmos (torch.Tensor) – The pre-packed atmosphere tensor (see transform_atmosphere).
cpu (bool, optional) – Whether to return the line profile tensors to the cpu. Default: True.
- Returns
The line profiles associated with these atmospheres.
- Return type
dict of str to torch.Tensor
- interpolate_lines(lines: Dict[str, ndarray], delta_lambdas: Dict[str, ndarray]) Dict[str, Tensor] [source]
Interpolate lines from their current grid to the one expected by the model.
- Parameters
lines (dict of str to array-like) – The lines to interpolate, with their expected name as key (as detailed in self.line_profiles)
delta_lambdas (dict of str to array-like) – The wavelength grids for the lines (in Angstrom), relative to the rest wavelength.
- Returns
The interpolated lines on the grids given by self.line_grids().
- Return type
dict of str to torch.Tensor
- inv_transform_atmosphere(atmos: Tensor, ignore: Optional[List[str]] = None) Dict[str, Tensor] [source]
Convert and unpack a transformed atmosphere into a dict by parameter.
- Parameters
atmos (torch.Tensor) – The packed atmosphere to expand
ignore (list of str, optional) – Parameters to ignore in the inverse transformation step.
- Returns
The atmospheric parameters
- Return type
dict of str to torch.Tensor
- static inv_transform_ne(ne: Tensor) Tensor [source]
Inverse transform ne for output from the network.
- static inv_transform_temperature(temp: Tensor) Tensor [source]
Inverse transform temperature for output from the network.
- static inv_transform_vel(v: Tensor) Tensor [source]
Inverse transform velocity for output from the network.
- invert_dual_cubes(crisp_seq: CRISPSequence, batch_size: int = 256, latent_draws: int = 128, seed: Optional[int] = None, rotate: bool = False, crispy_line_mapping: Optional[Dict[str, str]] = None, progress: bool = True, inverse_trans_ignore: Optional[List[str]] = None) Inversion [source]
Invert observations of spectral lines as loaded by the sst-crispy package.
- Parameters
crisp_seq (CRISPSequence) – Sequence of spectral lines in the order expected by the model (see self.line_profiles).
batch_size (int, optional) – The number of samples to put through the network at once. Default: 256 (quite conservative). Must be a multiple of latent_draws.
latent_draws (int, optional) – The number of latent draws for the inversion of each pixel. Default: 128.
seed (int, optional) – Manual random seed for PyTorch to control the latent space draws. Default: None (do not adjust the random state).
rotate (bool, optional) – Whether to use the rotate_crop method of CRISPSequence prior to inverting. Default: False.
crispy_line_mapping (dict of str to str, optional) – Map of strs used for line profile names by the Radynversion model to those used by Crispy. Default works with default pretrained Radynversion models.
progress (bool, optional) – Whether to show a progress bar for the inversion. Default: True.
inverse_trans_ignore (list of str, optional) – Atmospheric parameters to ignore the when inverse transforming the inverted atmospheres (passed to inv_transform_atmosphere). Default (‘ne’, ‘temperature’), works with default pretrained Radynversion models.
- Returns
crispy.Inversion containing the inverted data including errors in the
form of median absolute deviation.
- invert_lines(lines: Dict[str, Tensor], latent_space: Optional[Union[ndarray, Tensor]] = None, batch_size: Optional[int] = None, cpu: bool = True, seed: Optional[int] = None) Tensor [source]
Invert spectral lines. The batch size is inferred from the length of the line arrays (i.e. one latent sample per line), or can be used to set the number latent draws for a _single_ observation.
- linesdict of str to torch.Tensor
The transformed lines (see transform_lines).
- latent_spacearray-like or torch.Tensor, optional
Fixed values to use for the latent space. Default: Draw new random samples.
- batch_sizeint, optional
The number of samples to draw if only one pixel of line profiles was provided. Default: infer from shape of lines.
- cpubool, optional
Whether to return the final data to the cpu. Default: True
- seedint, optional
Manual random seed for PyTorch to control the latent space draws. Default: None (do not adjust the random state).
- Returns
Tensor containing the associated packed atmospheres (see
inv_transform_atmosphere).
- line_grids() Dict[str, ndarray] [source]
The wavelength grid expected for each line, in a dict by line name. Wavelengths are in Angstrom relative to rest wavelength.
- line_slice(idx: int) slice [source]
The slice associated with line of index idx in the packed [latent_space, lines] vector for the reverse process.
- static strip_units_ne(ne: ndarray) ndarray [source]
Convert ne to astropy units of cm^{-3} and return the value.
- static strip_units_temperature(vel: ndarray) ndarray [source]
Convert temperature to astropy units of K and return the value.
- static strip_units_vel(vel: ndarray) ndarray [source]
Convert velocity to astropy units of km/s and return the value.
- static to_tensor(x: ndarray) Tensor [source]
Convert a numpy array to torch Tensor and ensure float32 dtype.
- transform_atmosphere(**kwargs) Tensor [source]
Construct a tensor of transformed atmospheric parameters for input into the network.
Default units (if not using astropy units): ne: cm^{-3}, temperature: K, velocity: km/s
- Parameters
**kwargs – key, value mapping of the atmospheric parameters. Optionally using astropy units which will be converted.
- Returns
atmosphere – The atmospheric input prepared for the network.
- Return type
torch.Tensor
- transform_lines(lines: Dict[str, ndarray], delta_lambdas: Dict[str, ndarray]) Dict[str, Tensor] [source]
Interpolate and transform (normalise) lines ready for use in the network.
- Parameters
lines (dict of str to array-like) – The lines to interpolate, with their expected name as key (as detailed in self.line_profiles)
delta_lambdas (dict of str to array-like) – The wavelength grids for the lines (in Angstrom), relative to the rest wavelength.
- Returns
The transformed lines on the grids given by self.line_grids().
- Return type
dict of str to array-like
smug.radynversion_model module
- class smug.radynversion_model.F_fully_connected_leaky(size_in: int, size_out: int, internal_size: Optional[int] = None, dropout: float = 0.0, batch_norm: bool = False, leaky_slope: float = 0.01)[source]
Bases:
Module
Fully connected transformation, not reversible, but used in the RNVP blocks in the Radynversion model. This is a standard five-layer feed-forward fully connected network, with leaky ReLU’s after the first three layers, and a ReLU after the fourth.
- Parameters
size_in (int) – The size of the network input layer.
size_out (int) – The size of the network output layer.
internal_size (int, optional) – The size of the network’s three hidden layers (Default: 2 * size_out).
dropout (float, optional) – The dropout fraction to use during training. (Default: 0.0)
batch_norm (bool, optional) – Whether to apply batch normalisation after the fully connected layers (Default: False).
leaky_slope (float, optional) – The slope to use in the leaky ReLU activations (after the first 3 FC layers).
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class smug.radynversion_model.RadynversionModel(in_out_size: int, num_lines: int, line_profile_size: int, atmos_size: int, num_atmos_params: int, latent_size: int, num_inv_layers: int = 5, version: Optional[str] = None)[source]
Bases:
Module
RADYNVERSION Invertible Neural Network. Constructs the model object to the requisite dimensions using building blocks from the FrEIA package.
Forward process: atmosphere -> line profiles x latent space Backward process: line profiles x latent space -> atmosphere.
- Parameters
in_out_size (int) – The size of the input and output layers.
num_lines (int) – The number of spectral lines used in the model (e.g. 2 for the standard Radynversion).
line_profile_size (int) – The number of points across each spectral line (assumed the same for all).
atmos_size (int) – The number of points in the atmospheric stratification.
num_atmos_params (int) – The number of stratified atmospheric parameters.
latent_size (int) – The size of the latent space associated with associated with information lost in the forward process.
version (str or None) – A version identifier associated with the current model.
- model
The model describing the INN.
- Type
FrEIA.SequenceINN
- in_out_size
The size of the input and output layers.
- Type
int
- num_lines
The number of spectral lines used in the model (e.g. 2 for the standard Radynversion).
- Type
int
- line_profile_size
The number of points across each spectral line (assumed the same for all).
- Type
int
- atmos_size
The number of points in the atmospheric stratification.
- Type
int
- num_atmos_params
The number of stratified atmospheric parameters.
- Type
int
- latent_size
The size of the latent space associated with associated with information lost in the forward process.
- Type
int
- version
A version identifier associated with the current model.
- Type
str or None
- forward(*args, **kwargs)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- smug.radynversion_model.model_params = {'1.0.1': {'atmos_params': ['ne', 'temperature', 'vel'], 'line_half_width': [1.4, 1.0], 'line_profiles': ['Halpha', 'CaII8542'], 'z_stratification': array([-0.065, 0.016, 0.097, 0.178, 0.259, 0.34 , 0.421, 0.502, 0.583, 0.664, 0.745, 0.826, 0.907, 0.988, 1.069, 1.15 , 1.231, 1.312, 1.393, 1.474, 1.555, 1.636, 1.718, 1.799, 1.88 , 1.961, 2.042, 2.123, 2.204, 2.285, 2.366, 2.447, 2.528, 2.609, 2.69 , 2.771, 2.852, 2.933, 3.014, 3.095, 3.176, 3.257, 3.338, 3.419, 3.5 , 4.36 , 5.431, 6.766, 8.429, 10.5 ], dtype=float32)}, '1.1.1': {'atmos_params': ['ne', 'temperature', 'vel'], 'line_half_width': [1.4, 1.0], 'line_profiles': ['Halpha', 'CaII8542'], 'z_stratification': array([-0.065, 0.016, 0.097, 0.178, 0.259, 0.34 , 0.421, 0.502, 0.583, 0.664, 0.745, 0.826, 0.907, 0.988, 1.069, 1.15 , 1.231, 1.312, 1.393, 1.474, 1.555, 1.636, 1.718, 1.799, 1.88 , 1.961, 2.042, 2.123, 2.204, 2.285, 2.366, 2.447, 2.528, 2.609, 2.69 , 2.771, 2.852, 2.933, 3.014, 3.095, 3.176, 3.257, 3.338, 3.419, 3.5 , 4.36 , 5.431, 6.766, 8.429, 10.5 ], dtype=float32)}}
additional parameters needed by the RadynversionAdapter for each pre-trained versioned Radynversion model.
- Type
dict of str to dict
- smug.radynversion_model.model_urls = {'1.0.1': 'https://www.astro.gla.ac.uk/users/USER-MANAGED/solar_model_weights/Radynversion_1.0.1.pth.tar', '1.1.1': 'https://www.astro.gla.ac.uk/users/USER-MANAGED/solar_model_weights/Radynversion_1.1.1.pth.tar'}
urls to the weights for the pre-trained versioned Radynversion models
- Type
dict of str to str
- smug.radynversion_model.original_z = array([-0.065, 0.016, 0.097, 0.178, 0.259, 0.34 , 0.421, 0.502, 0.583, 0.664, 0.745, 0.826, 0.907, 0.988, 1.069, 1.15 , 1.231, 1.312, 1.393, 1.474, 1.555, 1.636, 1.718, 1.799, 1.88 , 1.961, 2.042, 2.123, 2.204, 2.285, 2.366, 2.447, 2.528, 2.609, 2.69 , 2.771, 2.852, 2.933, 3.014, 3.095, 3.176, 3.257, 3.338, 3.419, 3.5 , 4.36 , 5.431, 6.766, 8.429, 10.5 ], dtype=float32)
altitude grid used in the original Radynversion models
- Type
array of float32
- smug.radynversion_model.pretrained_kwargs = {'1.0.1': {'atmos_size': 50, 'in_out_size': 384, 'latent_size': 150, 'line_profile_size': 30, 'num_atmos_params': 3, 'num_lines': 2}, '1.1.1': {'atmos_size': 50, 'in_out_size': 384, 'latent_size': 150, 'line_profile_size': 31, 'num_atmos_params': 3, 'num_lines': 2}}
kwargs needed to instantiate the standard pre-trained versioned Radynversion models.
- Type
dict of str to dict of str
- smug.radynversion_model.pretrained_radynversion(version: str = '1.1.1', map_location: Optional[device] = None) RadynversionModel [source]
Load a pretrained RADYNVERSION model. The weights will be be cached as described by torch.hub. See their documentation for details.
- Parameters
version (str, optional) – The version number of the model to load. Default: 1.1.1
map_location (torch.device, optional) – Where to remap arrays during the loading process, by default this is set to “CPU” to allow loading on any platform.