synthesizer.emission_models.utils¶
A submodule containing utility functions for the emission models.
Functions
- synthesizer.emission_models.utils.cache_model_params(model, emitter)[source]¶
Cache all model specific parameters on to the emitter.
- This function stores all predefined parameters from the model including:
emitter: The emitter type for this model (stellar/blackhole/galaxy).
extract: The key that will extracted from the Grid.
combine: The models that will be combined to create the emission.
apply_to: The label of the model the transformation applies to.
transformer: The transformer’s repr.
generator: The generator’s repr.
mask parameters: Any parameters used in masking.
Note that all fixed parameters that are used will automatically be associated at the point of use via calls to get_param.
- Parameters:
model (EmissionModel) – The model object containing the parameters to cache.
emitter (Stars/Gas/BlackHoles/Galaxy) – The emitter object where the parameters will be cached.
- Return type:
None
- synthesizer.emission_models.utils.cache_param(param, emitter, model_label, value)[source]¶
Cache a parameter value on an emitter for a given model.
This function stores a computed parameter value in the emitter’s model_param_cache under the specified model label.
NOTE: This does not duplicate the value, it simply stores a reference to it. Under the hood this just increases the reference count by one.
- Parameters:
param (str) – The name of the parameter to cache.
emitter (Stars/Gas/BlackHoles/Galaxy) – The emitter object where the parameter will be cached.
model_label (str) – The label of the model associated with the parameter.
value (
Any) – The value to cache for the parameter.
- Return type:
None
- synthesizer.emission_models.utils.get_param(param, model, emission, emitter, obj=None, default=<object object>, _singular_attempted=False, _plural_attempted=False, _visited=None)[source]¶
Extract a parameter from a model, emission, emitter, or object.
- The priority of extraction is:
Model (EmissionModel)
Emission (Sed/LineCollection)
Emitter (Stars/Gas/Galaxy)
Object (any object)
If we find a string value this should mean the parameter points to another attribute, so we will recursively look for that attribute.
- Parameters:
param (str) – The parameter to extract.
model (EmissionModel) – The model object.
emission (Sed/LineCollection) – The emission object.
emitter (Stars/Gas/Galaxy) – The emitter object.
obj (object, optional) – An optional additional object to look for the parameter on last.
default (object, optional) – The default value to return if the parameter is not found.
_singular_attempted (bool, optional) – Internal flag to track if singular version has been attempted.
_plural_attempted (bool, optional) – Internal flag to track if plural version has been attempted.
_visited (set, optional) – Internal set to track visited parameters and detect cycles.
- Returns:
- value
The value of the parameter extracted from the appropriate object.
- Raises:
MissingAttribute – If the parameter is not found in the model, emission, or emitter. This is only raised if no default is passed.
- synthesizer.emission_models.utils.get_params(params, model, emission, emitter, obj=None)[source]¶
Extract a list of parameters from a model, emission, emitter, or object.
- The priority of extraction is:
Model (EmissionModel)
Emission (Sed/LineCollection)
Emitter (Stars/Gas/Galaxy)
Object (any object)
- Parameters:
params (list) – The parameters to extract.
model (EmissionModel) – The model object.
emission (Sed/LineCollection) – The emission object.
emitter (Stars/BlackHoles/Gas/Galaxy) – The emitter object.
obj (object, optional) – An optional additional object to look for parameters on last.
- Returns:
A dictionary of the values of the parameters extracted from the appropriate object.
- Return type:
values (dict)
Classes
- class synthesizer.emission_models.utils.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’])