"""A submodule defining the base class for emission generators."""
from abc import ABC, abstractmethod
from synthesizer import exceptions
from synthesizer.emission_models.utils import get_emission_label, get_params
[docs]
class Generator(ABC):
"""An abstract base class defining the Generator interface.
A Generator defines an object which generates emissions based on
some input parameters. This could include generation of spectra/lines
based on component properties or existing emissions.
Any Generator class should inherit from this class and implement the
required methods.
Everything associated to this class is private and should not be accessed
by the user. The child classes can present a public interface to the user
which makes more sense in the specific context of the generation. We only
need to define these methods for use behind the scenes when generating
emissions with an EmissionModel.
Attributes:
_required_params (tuple):
The name of any required parameters needed by the generator
when generating an emission. These should either be
available from an emitter or from the EmissionModel itself.
If they are missing an exception will be raised.
_required_emissions (tuple):
The required emissions needed by the generator when generating
an emission. These can be given either as labels or as the
EmissionModels themselves. They should be available from the
emitter in either "spectra", "particle_spectra", "lines",
or "particle_lines" (which is dictated by the current method
called). If they are missing an exception will be raised.
"""
def __init__(self, required_params=(), required_emissions=()):
"""Initialise the Generator.
Args:
required_params (tuple, optional):
The name of any required parameters needed by the generator
when generating an emission. These should either be
available from an emitter or from the EmissionModel itself.
If they are missing an exception will be raised.
required_emissions (tuple, optional):
The required emissions needed by the generator when
generating an emission. These can be given either as labels
or as the EmissionModels themselves. They should be
available from the emitter in either "spectra",
"particle_spectra", "lines", or "particle_lines" (which is
dictated by the current method called). If they are missing
an exception will be raised.
"""
# Store the parameters this generator will need
self._required_params = required_params
self._required_emissions = required_emissions
@abstractmethod
def _generate_spectra(self, lams, emitter, model, *args, **kwargs):
"""Generate spectra based on the emitter and model.
This method must look for the required parameters in
model.fixed_parameters, on the generator itself (if not None) and on
the emitter (in that order of importance). If any of the required
parameters are missing an exception will be raised. This can be
achieved by using the base class's _extract_params method.
For example, a generator that generates a blackbody spectrum based on
component temperature would need to look for the temperature parameter
on the emitter or model and then use this to generate the spectrum.
If an input emission is needed then the same behaviour should be
implemented using the _extract_spectra method.
Args:
lams (unyt_array):
The wavelengths at which to generate the spectra.
emitter (Emitter):
The emitter for which to generate the spectra.
model (EmissionModel):
The emission model calling this generator.
*args:
Additional positional arguments.
**kwargs:
Additional keyword arguments.
Returns:
spectra (dict):
A dictionary of generated spectra.
"""
raise NotImplementedError(
"_generate_spectra method must be implemented in child classes."
)
@abstractmethod
def _generate_lines(self, lams, emitter, model, *args, **kwargs):
"""Generate lines based on the emitter and model.
This method must look for the required parameters in
model.fixed_parameters, on the generator itself (if not None) and on
the emitter (in that order of importance). If any of the required
parameters are missing an exception will be raised. This can be
achieved by using the base class's _extract_params method.
For example, a generator that generates emission lines based on
component metallicity would need to look for the metallicity parameter
on the emitter or model and then use this to generate the lines.
If an input emission is needed then the same behaviour should be
implemented using the _extract_lines method.
Args:
lams (unyt_array):
The wavelengths at which to generate the lines.
emitter (Emitter):
The emitter for which to generate the lines.
model (EmissionModel):
The emission model calling this generator.
*args:
Additional positional arguments.
**kwargs:
Additional keyword arguments.
Returns:
lines (dict):
A dictionary of generated lines.
"""
raise NotImplementedError(
"_generate_lines method must be implemented in child classes."
)
def _extract_params(self, model, emitter):
"""Extract the required parameters for the generation.
Values set on the generator are fixed overrides, so the priority is:
1. The EmissionModel's fixed parameters
2. The generator itself (if not None)
3. The emitter object
Note that this differs from Transformers, where values on the
transformer are the final fallback after the emitter.
Args:
model (EmissionModel):
The emission model generating the emission.
emitter (Emitter):
The object emitting the emission.
Returns:
dict
A dictionary containing the required parameters.
Raises:
MissingAttribute
If any of the required parameters can't be found.
"""
# Parameters set on the generator override the emitter, but not the
# model's fixed parameters
model_fixed = model.fixed_parameters if model is not None else {}
overrides = {
param: getattr(self, param)
for param in self._required_params
if getattr(self, param, None) is not None
and param not in model_fixed
}
# Look up everything else on the model and then the emitter (never
# falling back to the generator, where these are None)
params = get_params(
[p for p in self._required_params if p not in overrides],
model,
None,
emitter,
preserve_units=True,
)
params.update(overrides)
# Check if any of the required parameters are missing (iterating
# _required_params keeps the reported order stable)
missing_params = [
param
for param in self._required_params
if params.get(param) is None
]
if len(missing_params) > 0:
missing_strs = [f"'{s}'" for s in missing_params]
raise exceptions.MissingAttribute(
f"{', '.join(missing_strs)} can't be "
"found on the EmissionModel, emitter or "
f"{self.__class__.__name__} generator "
f"(required by {self.__class__.__name__})"
)
return params
def _extract_emissions(self, emission_dict):
"""Extract the required emissions for the generation.
The returned dictionary is keyed by the label of each required
emission. If any of the required emissions are missing an exception
will be raised.
Args:
emission_dict (dict):
The dictionary containing all emissions generated so far. These
can be spectra, particle_spectra, lines, or particle_lines.
Returns:
dict
A dictionary containing the required emissions.
"""
# Extract the emissions (Missing emissions will return None)
emissions = {}
for emission in self._required_emissions:
label = get_emission_label(emission)
emissions[label] = emission_dict.get(label, None)
# Check if any of the required emissions are missing
missing_emissions = [
name for name, value in emissions.items() if value is None
]
if len(missing_emissions) > 0:
missing_strs = [f"'{s}'" for s in missing_emissions]
raise exceptions.MissingAttribute(
f"{', '.join(missing_strs)} can't be "
"found on the emitter "
f"(required by {self.__class__.__name__})"
)
return emissions