elisa.models.model#
The spectral model bases.
- class Model(name: str, latex: str, comps: list[Component])[source]#
Bases:
ABCBase model class.
Attributes
Component names.
Get side-effect free model evaluation function.
\(\LaTeX\) format of the model.
Model name.
Model type.
Methods
compile(*[, model_info])Compile the model for fast evaluation.
- compile(*, model_info: ModelInfo | None = None) CompiledModel[source]#
Compile the model for fast evaluation.
- Parameters:
- model_info
ModelInfo, optional Optional model information used to compile the model.
- model_info
- Returns:
CompiledModelThe compiled model.
- class CompiledModel(name: str, params_id: Sequence[str], fixed_id: Sequence[str], fn: Callable[[Array, dict[str, Array]], Array], additive_fn: Callable[[Array, dict[str, Array]], dict[tuple[str, str], Array]] | None, mtype: Literal['add', 'mul'], model_info: ModelInfo)[source]#
Bases:
objectModel with fast evaluation and fixed configuration.
Attributes
Whether the model has additive subcomponents.
Parameter names.
Model type.
Methods
ce(egrid, resp_matrix, channel_width[, ...])Calculate the folded model, i.e. \(C(E)\).
eene(egrid[, params, comps])Calculate \(E^2 N(E)\), i.e. \(\nu F(\nu)\).
eiso(emin_rest, emax_rest, z, duration, ...)Calculate the isotropic emission energy of model.
ene(egrid[, params, comps])Calculate \(E N(E)\), i.e. \(F(\nu)\).
eval(egrid[, params])Evaluate the model.
flux(emin, emax[, params, energy, comps, ...])Calculate the flux of model between emin and emax.
lumin(emin_rest, emax_rest, z, params, ...)Calculate the luminosity of model.
ne(egrid[, params, comps])Calculate \(N(E)\).
simulate(photon_egrid, channel_emin, ...[, ...])Simulate observation data.
simulate_from_data(data[, spec_exposure, ...])Simulate observation based on the configuration of existing data.
- eval(egrid: Array | ndarray | bool | number | bool | int | float | complex, params: Sequence | Mapping | None = None) Array[source]#
Evaluate the model.
- ne(egrid: Array | ndarray | bool | number | bool | int | float | complex, params: Sequence | Mapping | None = None, comps: bool = False) Array | dict[str, Array][source]#
Calculate \(N(E)\).
- Parameters:
- Returns:
- ene(egrid: Array | ndarray | bool | number | bool | int | float | complex, params: Sequence | Mapping | None = None, comps: bool = False) Array | dict[str, Array][source]#
Calculate \(E N(E)\), i.e. \(F(\nu)\).
- Parameters:
- Returns:
- eene(egrid: Array | ndarray | bool | number | bool | int | float | complex, params: Sequence | Mapping | None = None, comps: bool = False) Array | dict[str, Array][source]#
Calculate \(E^2 N(E)\), i.e. \(\nu F(\nu)\).
- Parameters:
- Returns:
- ce(egrid: Array | ndarray | bool | number | bool | int | float | complex, resp_matrix: Array | ndarray | bool | number | bool | int | float | complex, channel_width: Array | ndarray | bool | number | bool | int | float | complex, params: Sequence | Mapping | None = None, comps: bool = False) Array | dict[str, Array][source]#
Calculate the folded model, i.e. \(C(E)\).
- Parameters:
- egrid
ndarray Photon energy grid of resp_matrix, in units of keV.
- resp_matrix
ndarray Instrumental response matrix used to fold the model.
- channel_width
ndarray Measured energy channel width of resp_matrix.
- paramssequence or
mapping, optional Parameter sequence or mapping for the model.
- compsbool, optional
Whether to return the result of each component. Defaults to False.
- egrid
- Returns:
- flux(emin: float | int | Array, emax: float | int | Array, params: Sequence | Mapping | None = None, energy: bool = True, comps: bool = False, ngrid: int = 1000, log: bool = True) Array | dict[str, Array][source]#
Calculate the flux of model between emin and emax.
Warning
The flux is calculated by trapezoidal rule, which may not be accurate if not enough energy bins are used when the difference between emin and emax is large.
- Parameters:
- emin
floatorint Minimum value of energy range, in units of keV.
- emax
floatorint Maximum value of energy range, in units of keV.
- paramssequence or
mapping, optional Parameter sequence or mapping for the model.
- energybool, optional
When True, calculate energy flux in units of erg cm⁻² s⁻¹; otherwise calculate photon flux in units of ph cm⁻² s⁻¹. The default is True.
- compsbool, optional
Whether to return the result of each component. The default is False.
- ngrid
int, optional The energy grid number to use in integration. The default is 1000.
- logbool, optional
Whether to use logarithmically regular energy grid. The default is True.
- emin
- Returns:
- lumin(emin_rest: float | int, emax_rest: float | int, z: float | int, params: Sequence | Mapping | None = None, comps: bool = False, ngrid: int = 1000, log: bool = True, cosmo: LambdaCDM = FlatLambdaCDM(name='Planck18', H0=<Quantity 67.66 km / (Mpc s)>, Om0=0.30966, Tcmb0=<Quantity 2.7255 K>, Neff=3.046, m_nu=<Quantity [0., 0., 0.06] eV>, Ob0=0.04897)) Array | dict[str, Array][source]#
Calculate the luminosity of model.
Warning
The luminosity is calculated by trapezoidal rule, and is accurate only if enough numbers of energy grids are used.
- Parameters:
- emin_rest
floatorint Minimum value of rest-frame energy range, in units of keV.
- emax_rest
floatorint Maximum value of rest-frame energy range, in units of keV.
- z
floatorint Redshift of the source.
- paramssequence or
mapping, optional Parameter sequence or mapping for the model.
- compsbool, optional
Whether to return the result of each component. The default is False.
- ngrid
int, optional The energy grid number to use in integration. The default is 1000.
- logbool, optional
Whether to use logarithmically regular energy grid. The default is True.
- cosmo
LambdaCDM, optional Cosmology model used to calculate luminosity. The default is Planck18.
- emin_rest
- Returns:
- eiso(emin_rest: float | int, emax_rest: float | int, z: float | int, duration: float | int, params: Sequence | Mapping | None = None, comps: bool = False, ngrid: int = 1000, log: bool = True, cosmo: LambdaCDM = FlatLambdaCDM(name='Planck18', H0=<Quantity 67.66 km / (Mpc s)>, Om0=0.30966, Tcmb0=<Quantity 2.7255 K>, Neff=3.046, m_nu=<Quantity [0., 0., 0.06] eV>, Ob0=0.04897)) Array | dict[str, Array][source]#
Calculate the isotropic emission energy of model.
Warning
The \(E_\mathrm{iso}\) is calculated by trapezoidal rule, and is accurate only if enough numbers of energy grids are used.
- Parameters:
- emin_rest
floatorint Minimum value of rest-frame energy range, in units of keV.
- emax_rest
floatorint Maximum value of rest-frame energy range, in units of keV.
- z
floatorint Redshift of the source.
- duration
floatorint Observed duration of the source, in units of second.
- compsbool, optional
Whether to return the result of each component. The default is False.
- ngrid
int, optional The energy grid number to use in integration. The default is 1000.
- logbool, optional
Whether to use logarithmically regular energy grid. The default is True.
- params
dict, optional Parameters dict to overwrite the fitted parameters.
- cosmo
LambdaCDM, optional Cosmology model used to calculate luminosity. The default is Planck18.
- emin_rest
- Returns:
- simulate_from_data(data: ObservationData, spec_exposure: float | None = None, back_exposure: float | None = None, params: Sequence | Mapping | None = None, seed: int = 42, **kwargs: dict) ObservationData[source]#
Simulate observation based on the configuration of existing data.
- Parameters:
- data
ObservationData Observation data to read observation configuration.
- spec_exposure
float, optional Exposure time of the source. Defaults to the exposure time of spec_data.
- back_exposure
float, optional Exposure time of the background. Defaults to the exposure time of back_data.
- paramssequence or
mapping, optional Parameter sequence or mapping for the model.
- seed
int, optional Random seed for the simulation. The default is 42.
- **kwargs
dict, optional Additional keyword arguments passed to
ObservationData.
- data
- Returns:
ObservationDataSimulated observation data.
- simulate(photon_egrid: ndarray, channel_emin: ndarray, channel_emax: ndarray, response_matrix: ndarray | sparray, spec_exposure: float, spec_poisson: bool = True, spec_errors: ndarray | None = None, back_counts: ndarray | None = None, back_errors: ndarray | None = None, back_exposure: float | None = None, back_poisson: bool | None = None, spec_area_scale: float | ndarray = 1.0, spec_back_scale: float | ndarray = 1.0, back_area_scale: float | ndarray = 1.0, back_back_scale: float | ndarray = 1.0, quality: ndarray | None = None, grouping: ndarray | None = None, channel: ndarray | None = None, channel_type: str = 'Ch', response_sparse: bool = False, params: Sequence | Mapping | None = None, name: str = 'simulation', seed: int = 42, **kwargs: dict) ObservationData[source]#
Simulate observation data.
- Parameters:
- photon_egrid
ndarray Photon energy grid in units of keV.
- channel_emin
ndarray Lower energy bounds of the detector channels.
- channel_emax
ndarray Upper energy bounds of the detector channels.
- response_matrix
ndarray Response matrix of the detector.
- spec_exposure
float Exposure time of the source.
- spec_poissonbool, optional
Whether the source spectrum is Poisson distributed. If false, spec_errors must be provided. The default is True.
- spec_errors
ndarray, optional Errors of the source spectrum. Must be provided if spec_poisson is False.
- back_counts
ndarray, optional Background counts in each channel.
- back_errors
ndarray, optional Errors of the background counts. Must be provided if back_poisson is False.
- back_exposure
float, optional Exposure time of the background.
- back_poissonbool, optional
Whether the background spectrum is Poisson distributed. If false, back_errors must be provided.
- spec_area_scale
floatorndarray, optional Area scale factor of the source. The default is 1.0.
- spec_back_scale
floatorndarray, optional Background scale factor of the source. The default is 1.0.
- back_area_scale
floatorndarray, optional Area scale factor of the background. The default is 1.0.
- back_back_scale
floatorndarray, optional Background scale factor of the background. The default is 1.0.
- quality
ndarray, optional Quality flags of the data.
- grouping
ndarray, optional Grouping flags of the data.
- channel
ndarray, optional Channel numbers.
- channel_type
str, optional Channel type. The default is ‘Ch’.
- response_sparsebool, optional
Whether the response matrix is sparse. The default is False.
- paramssequence or
mapping, optional Parameter sequence or mapping for the model.
- name
str, optional Name of the simulation data. The default is ‘simulation’.
- seed
int, optional Random seed for simulation. The default is 42.
- **kwargs
dict, optional Additional keyword arguments passed to
ObservationData.
- photon_egrid
- Returns:
ObservationDataSimulated observation data.
- class UniComponentModel(component: Component)[source]#
Bases:
ModelModel defined by a single additive or multiplicative component.
Attributes
comp_namesComponent names.
Get side-effect free model evaluation function.
latex\(\LaTeX\) format of the model.
nameModel name.
Model type.
Methods
compile(*[, model_info])Compile the model for fast evaluation.
- class CompositeModel(lhs: Model, rhs: Model, op: Literal['+', '*'])[source]#
Bases:
ModelModel defined by sum or product of two models.
Attributes
comp_namesComponent names.
Get side-effect free model evaluation function.
latex\(\LaTeX\) format of the model.
nameModel name.
Model type.
Methods
compile(*[, model_info])Compile the model for fast evaluation.
- class ComponentMeta(name, bases, dct, **kwargs) None[source]#
Bases:
ABCMetaAvoid cumbersome coding for subclass
__init__.Methods
__call__(*args, **kwargs)Call self as a function.
mro(/)Return a type's method resolution order.
register(subclass)Register a virtual subclass of an ABC.
- class ParamConfig(name: str, latex: str, unit: str, default: float, min: float, max: float, log: bool = False, fixed: bool = False)[source]#
Bases:
NamedTupleConfiguration of a uniform parameter for spectral component.
Methods
count(value, /)Return number of occurrences of value.
index(value[, start, stop])Return first index of value.
- class Component(params: dict, latex: str | None)[source]#
Bases:
ABCBase class to define a spectral component.
Attributes
Get side-effect free component evaluation function.
\(\LaTeX\) format of the component.
Component name.
Component's parameter names.
Component type.
- class AdditiveComponent(params: dict, latex: str | None)[source]#
Bases:
ComponentPrototype class to define an additive component.
Attributes
evalGet side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
nameComponent name.
param_namesComponent's parameter names.
Component type is additive.
- class MultiplicativeComponent(params: dict, latex: str | None)[source]#
Bases:
ComponentPrototype class to define a multiplicative component.
Attributes
evalGet side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
nameComponent name.
param_namesComponent's parameter names.
Component type is multiplicative.
- class AnalyticalIntegral(params: dict, latex: str | None)[source]#
Bases:
ComponentPrototype component to calculate model integral analytically.
Attributes
Get side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type.
Methods
integral(*args, **kwargs)Calculate the model value over grid.
- class NumericalIntegral(params: dict, latex: str | None, method: Literal['trapz', 'simpson'] | None = None)[source]#
Bases:
ComponentPrototype component to calculate model integral numerically.
Attributes
Get side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
Numerical integration method.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type.
Methods
continuum(*args, **kwargs)Calculate the model value at the energy grid.
- property eval: Callable[[Array, dict[str, Array]], Array]#
Get side-effect free component evaluation function.
- class AnaIntAdditive(params: dict, latex: str | None)[source]#
Bases:
AnalyticalIntegral,AdditiveComponentPrototype additive component with integral expression defined.
Attributes
evalGet side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type is additive.
Methods
integral(*args, **kwargs)Calculate the photon flux integrated over the energy grid.
- class NumIntAdditive(params: dict, latex: str | None, method: Literal['trapz', 'simpson'] | None = None)[source]#
Bases:
NumericalIntegral,AdditiveComponentPrototype additive component with continuum expression defined.
Attributes
evalGet side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
methodNumerical integration method.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type is additive.
Methods
continuum(*args, **kwargs)Calculate the photon flux at the energy grid.
- class AnaIntMultiplicative(params: dict, latex: str | None)[source]#
Bases:
AnalyticalIntegral,MultiplicativeComponentPrototype multiplicative component with integral expression defined.
Attributes
evalGet side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type is multiplicative.
Methods
integral(*args, **kwargs)Calculate the average value of the model between the grid.
- class NumIntMultiplicative(params: dict, latex: str | None, method: Literal['trapz', 'simpson'] | None = None)[source]#
Bases:
NumericalIntegral,MultiplicativeComponentPrototype multiplicative component with continuum expression defined.
Attributes
evalGet side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
methodNumerical integration method.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type is multiplicative.
Methods
continuum(*args, **kwargs)Calculate the model value at the energy grid.
- class ConvolutionModel(component: ConvolutionComponent)[source]#
Bases:
ModelModel defined by a convolution component.
Attributes
comp_namesComponent names.
Get side-effect free model evaluation function.
latex\(\LaTeX\) format of the model.
nameModel name.
Model type is convolution.
Methods
__call__(model)Call self as a function.
compile(*[, model_info])Compile the model for fast evaluation.
- property eval#
Get side-effect free model evaluation function.
- class ConvolvedModel(op: ConvolutionComponent, model: Model)[source]#
Bases:
ModelModel created by convolution.
Attributes
comp_namesComponent names.
Get side-effect free model evaluation function.
latex\(\LaTeX\) format of the model.
nameModel name.
Model type.
Methods
compile(*[, model_info])Compile the model for fast evaluation.
- class ConvolutionComponent(params: dict, latex: str | None)[source]#
Bases:
ComponentPrototype class to define a convolution component.
Attributes
Get side-effect free component evaluation function.
latex\(\LaTeX\) format of the component.
nameComponent name.
param_namesComponent's parameter names.
Component type.
Methods
convolve(*args, **kwargs)Convolve a model function.
- property eval: Callable[[Array, dict[str, Array], Callable[[Array], Array]], Array]#
Get side-effect free component evaluation function.
- abstractmethod static convolve(*args, **kwargs) Array[source]#
Convolve a model function.
- Parameters:
- egrid
ndarray Photon energy grid in units of keV.
- params
dict Parameter dict for the convolution model.
- model_fn
callable() The model function to be convolved, which takes the energy grid as input and returns the model value over the grid.
- egrid
- Returns:
jax.ArrayThe convolved model value over egrid.
- class PyComponent(params: dict, latex: str | None, grad_method: Literal['central', 'forward'] | None)[source]#
Bases:
ComponentPrototype component with pure Python expression defined.
Attributes
evalGet side-effect free component evaluation function.
Numerical differentiation method.
latex\(\LaTeX\) format of the component.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type.
- class PyAnaInt(params: dict, latex: str | None, grad_method: Literal['central', 'forward'] | None)[source]#
Bases:
PyComponent,AnalyticalIntegralPrototype component with python integral expression defined.
Attributes
Get side-effect free component evaluation function.
grad_methodNumerical differentiation method.
latex\(\LaTeX\) format of the component.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type.
Methods
integral(*args, **kwargs)Calculate the model value over grid.
- class PyNumInt(params: dict, latex: str | None, method: Literal['trapz', 'simpson'] | None, grad_method: Literal['central', 'forward'] | None)[source]#
Bases:
PyComponent,NumericalIntegralPrototype component with python continuum expression defined.
Attributes
Get side-effect free component evaluation function.
grad_methodNumerical differentiation method.
latex\(\LaTeX\) format of the component.
methodNumerical integration method.
nameComponent name.
param_namesComponent's parameter names.
typeComponent type.
Methods
continuum(*args, **kwargs)Calculate the model value at the energy grid.
- get_mtype_str(model: Model | CompiledModel) str[source]#
Get the model type string.
- get_model_info(comps: Sequence[Component], cid_to_name: dict[str, str], cid_to_latex: dict[str, str]) ModelInfo[source]#
Get the model information.
- class ModelInfo(info: tuple[tuple[str, str, str, str, str, str], ...], name: ParamIDStrMapping, latex: ParamIDStrMapping, unit: ParamIDStrMapping, sample: dict[ParamID, Distribution], default: ParamIDValMapping, deterministic: dict[ParamID, Callable[[ParamIDValMapping], JAXFloat]], fixed: ParamIDValMapping, log: dict[ParamID, bool], pid_to_comp_latex: dict[ParamID, str], cid_to_name: dict[CompID, CompName], cid_to_latex: dict[CompID, str], cid_to_params: dict[CompID, Callable[[ParamIDValMapping], NameValMapping]], integrate: dict[ParamID, IntegralFactory], setup: dict[CompParamName, tuple[ParamID, ParamSetup]])[source]#
Bases:
NamedTupleModel information.
Methods
count(value, /)Return number of occurrences of value.
index(value[, start, stop])Return first index of value.
- info: tuple[tuple[str, str, str, str, str, str], ...]#
The model parameter information.
Each row contains (No., Component, Parameter, Value, Bound, Prior).
- sample: dict[str, Distribution]#
The mapping of free parameter id to numpyro Distribution.
- deterministic: dict[str, Callable[[dict[str, Array]], Array]]#
The mapping of deterministic parameters id to value getter.
- cid_to_params: dict[str, Callable[[dict[str, Array]], dict[str, Array]]]#
The mapping of component id to parameter value getter function.
- class ParamSetup(*values)[source]#
Bases:
EnumModel parameter setup.
- Free = 0#
Parameter is free to vary.
- Composite = 1#
Parameter is composed of other free parameters.
- Forwarded = 2#
Parameter is directly forwarded to another model parameter.
- Fixed = 3#
Parameter is fixed to a value.
- Integrated = 4#
Parameter is integrated out.