synthesizer.emission_models.parameters¶
A submodule containing the parameter types used by emission models.
Emission model parameters can be passed as simple values, as strings (which point at an attribute to read from the emitter), or as one of the parameter types defined in this module. The parameter types defined here describe parameters which need some extra machinery before a value can be used:
ParameterFunction: a value computed at run time from other parameters.
ParameterList: a list of values, each of which produces a variant model.
ParameterDistribution: a distribution which is sampled to produce a list of values, each of which produces a variant model. This is an abstract base class, use one of its flavours (e.g. ParameterUniformDist).
Example usage:
def compute_metallicity(mass, age, fixed_param):
return (mass * 0.01) + (age * 0.001) + fixed_param
param_func = ParameterFunction(
func=compute_metallicity,
sets="metallicity",
func_args=["mass", "age", "fixed_param"],
)
model = EmissionModel(
label="custom_model",
grid=grid,
fixed_param=0.02,
metallicity=param_func,
)
# Vary the escape fraction over 3 values. This produces nothing until
# expand_models is called, at which point the model (and everything
# depending on it) is duplicated once per value.
varied = EmissionModel(
label="transmitted",
grid=grid,
extract="transmitted",
fesc=ParameterList([0.0, 0.1, 0.5], label_modifier="fesc_%.2f"),
)
expanded = varied.expand_models()
Functions
- synthesizer.emission_models.parameters.find_variations(model)[source]¶
Return the variation markers attached to a model.
A variation can be declared in three places: on a fixed parameter, on the transformer or generator the model uses, or on an argument of that transformer or generator. The last of these is how the arguments of a dust curve or a dust emission model are varied, e.g. the slope of a PowerLaw, which belong to the curve rather than to the model.
All three are reported keyed by the name the expansion should set, so it does not have to care which is which. An argument of a transformer or generator is keyed by a dotted name, e.g. “transformer.slope”.
- Parameters:
model (EmissionModel) – The model to inspect.
- Returns:
A dictionary of the form {<name>: <marker>} containing every ParameterList and ParameterDistribution declared on the model.
- Return type:
dict
- synthesizer.emission_models.parameters.set_variation_value(model, name, value)[source]¶
Set the value a variation resolved to on a model.
- Parameters:
model (EmissionModel) – The model to set it on.
name (str) – The name the variation was declared under, as reported by find_variations.
value – The value this variant uses.
Classes
- class synthesizer.emission_models.parameters.ParameterDistribution(n, seed=None, label_modifier=None, labels=None, units=None)[source]¶
A distribution of parameter values, sampled to produce variant models.
This is the base class for the distribution flavours (e.g. ParameterUniformDist, ParameterNormalDist). It behaves exactly like a ParameterList except that the values are sampled from a distribution rather than stated explicitly.
Sampling happens once, at the start of expansion, via realise(). This matters: if the distribution were sampled per variant then a model downstream of another varied model would draw different values in each branch, rather than sharing one set of samples.
Note that this samples one scalar per variant model. It does NOT sample a value per particle. Use a ParameterFunction for per particle stochasticity.
- n¶
The number of values to sample.
- Type:
int
- seed¶
The seed for the random number generator. Pass a seed for a reproducible set of samples.
- Type:
int
- label_modifier¶
A printf style format string used to construct the label suffix for each variant model.
- Type:
str
- units¶
The units the sampled values carry, or None for a dimensionless parameter.
- Type:
unyt.Unit
- realise()[source]¶
Sample the distribution and return the values as a ParameterList.
This is called once at the start of an expansion, converting every distribution in the tree into a concrete list of values before any models are duplicated.
- Returns:
The sampled values, ready to be expanded.
- Return type:
- class synthesizer.emission_models.parameters.ParameterFunction(func, sets, func_args)[source]¶
A class for wrapping functions that compute parameters for emitters.
This class can be used to wrap functions which take emitter attributes as inputs and return a computed parameter value or array of values. This class is designed as a dependency injection mechanism to be passed to EmissionModel arguments that require dynamic parameter computation from an emitter. As such, this is mostly designed for internal use within the Synthesizer package, but it can also be used by an experienced user to create custom parameter functions.
- Any function wrapped by this class must:
Follow this signature: func(**kwargs) -> value
Return a single value or numpy/unyt array of values. If an array is returned, it must be the same shape as arrays on the emitter (i.e. nstar in length for per star properties etc.).
Have kwargs which are either attributes of the emitter object or fixed parameters on an EmissionModel.
Have kwargs which are all defined in the “func_args” list (set during initialization).
Example
- def compute_metallicity(mass, age, fixed_param):
# Compute metallicity based on mass, age, and a fixed parameter return (mass * 0.01) + (age * 0.001) + fixed_param
- param_func = ParameterFunction(
func=compute_metallicity, func_args=[‘mass’, ‘age’, ‘fixed_param’]
)
# Define an emission model that fixes ‘fixed_param’ to 0.02 model = EmissionModel(
label=’custom_model’, fixed_param=0.02, grid=grid, metallicity_param=param_func,
)
# Later… call get spectra on an emitter which will use the function # to compute metallicity dynamically. emitter.get_spectra(model)
# And you can see the cached value to was used print(emitter.model_param_cache[‘custom_model’][‘metallicity_param’])
- class synthesizer.emission_models.parameters.ParameterList(values, label_modifier=None, labels=None)[source]¶
A list of parameter values, each producing a variant emission model.
Unlike the other parameter types, a ParameterList does not describe a single value to use during an emission calculation. Instead it declares that the model should be varied over a set of values. This variation is only realised when EmissionModel.expand_models is called, at which point the model carrying the list (and every model which depends on it) is duplicated once per value, with each duplicate labelled to say which value produced it.
Those labels are how the resulting emissions are addressed, so every value needs one. There are two ways to provide them, and exactly one must be used:
label_modifier, a printf style format string applied to each value. This is the natural choice for numbers: “fesc_%.2f” gives “_fesc_0.10”.
labels, one name per value. This is the choice for anything a format string cannot sensibly render, such as a list of dust curves or of ParameterFunctions, whose repr would make for an unusable label.
Because a ParameterList is never a usable value, encountering one during an emission calculation is an error (get_param will complain and tell the user to call expand_models first).
- values¶
The values to vary the parameter over.
- Type:
list
- label_modifier¶
The format string used to name each variant, or None if explicit labels were given instead.
- Type:
str
- labels¶
The name given to each variant, or None if a format string was given instead.
- Type:
list
- suffixes¶
The suffix appended to a model’s label for each value, including the leading underscore.
- Type:
list
- class synthesizer.emission_models.parameters.ParameterLogNormalDist(mean, sigma, n, seed=None, label_modifier=None, labels=None, units=None)[source]¶
A log normal distribution of parameter values.
Values are sampled from a normal distribution in log10 space, where mean and sigma are given in log10 space, and returned in linear space.
Since mean and sigma are in log10 space they cannot carry the units of a sample. Give those with the units argument.
- class synthesizer.emission_models.parameters.ParameterLogUniformDist(low, high, n, seed=None, label_modifier=None, labels=None, units=None)[source]¶
A log uniform distribution of parameter values.
Values are sampled uniformly in log10 space between low and high, which are given in linear space.