Models#
- class gammapy.modeling.models.Models(models=None, covariance_data=None)[source]#
Bases:
DatasetModels
,MutableSequence
Sky model collection.
Attributes Summary
Covariance as a
Covariance
object.Boolean mask, True if all parameters of a given model are frozen.
List of model names.
Parameters as a
Parameters
object.List of unique parameter names.
Positions of the models as a
SkyCoord
.Minimum WCS geom in which all the models are contained.
Methods Summary
append
(value)S.append(value) -- append value to the end of the sequence
clear
()copy
([copy_data])Deep copy.
count
(value)extend
(values)S.extend(iterable) -- extend sequence by appending elements from the iterable
freeze
([model_type])Freeze parameters depending on model type.
from_dict
(data[, path])Create from dictionary.
from_yaml
(yaml_str[, path, checksum])Create from YAML string.
index
(value, [start, [stop]])Raises ValueError if the value is not present.
insert
(idx, model)S.insert(index, value) -- insert value before index
plot_positions
([ax])Plot the centers of the spatial models on a given WCS axis.
plot_regions
([ax, kwargs_point, path_effect])Plot extent of the spatial models on a given WCS axis.
pop
([index])Raise IndexError if list is empty or index is out of range.
read
(filename[, checksum])Read from YAML file.
read_covariance
(path[, filename])Read covariance data from file.
reassign
(dataset_name, new_dataset_name)Reassign a model from one dataset to another.
remove
(value)S.remove(value) -- remove first occurrence of value.
restore_status
([restore_values])Context manager to restore status.
reverse
()S.reverse() -- reverse IN PLACE
select
([name_substring, datasets_names, ...])Select models that meet all specified conditions.
select_from_geom
(geom, **kwargs)Select models that fall inside a given geometry.
select_mask
(mask[, margin, ...])Check if sky models contribute within a mask map.
select_region
(regions[, wcs])Select sky models with center position contained within a given region.
selection_mask
([name_substring, ...])Create a mask of models, that meet all specified conditions.
set_parameters_bounds
(tag, model_type[, ...])Set bounds for the selected models types and parameters names.
set_prior
(parameters, priors)to_dict
([full_output, overwrite_templates])Convert to dictionary.
Convert model parameters to a
Table
.Return a list of the regions for the spatial models.
to_template_sky_model
(geom[, ...])Merge a list of models into a single
SkyModel
.to_template_spectral_model
(geom[, mask])Merge a list of models into a single
TemplateSpectralModel
.to_yaml
([full_output, overwrite_templates])Convert to YAML string.
unfreeze
([model_type])Restore parameters frozen status to default depending on model type.
Update linked parameters labels used for serialisation and print.
Update models from a
Table
.write
(path[, overwrite, full_output, ...])Write to YAML file.
write_covariance
(filename, **kwargs)Write covariance to file.
Attributes Documentation
- covariance#
Covariance as a
Covariance
object.
- frozen#
Boolean mask, True if all parameters of a given model are frozen.
- names#
List of model names.
- parameters#
Parameters as a
Parameters
object.
- parameters_unique_names#
List of unique parameter names. Return formatted as model_name.par_type.par_name.
- wcs_geom#
Minimum WCS geom in which all the models are contained.
Methods Documentation
- append(value)#
S.append(value) – append value to the end of the sequence
- clear() None -- remove all items from S #
- copy(copy_data=False)#
Deep copy.
- Parameters:
- copy_databool
Whether to copy data attached to template models.
- Returns:
- models:
Models
Copied models.
- models:
- count(value) integer -- return number of occurrences of value #
- extend(values)#
S.extend(iterable) – extend sequence by appending elements from the iterable
- freeze(model_type=None)#
Freeze parameters depending on model type.
- Parameters:
- model_type{None, “spatial”, “spectral”}
Freeze all parameters or only spatial or only spectral. Default is None.
- classmethod from_dict(data, path='')#
Create from dictionary.
- classmethod from_yaml(yaml_str, path='', checksum=False)#
Create from YAML string.
- Parameters:
- yaml_strstr
yaml str
- pathstr
base path of model files
- checksumbool, optional
Whether to perform checksum verification. Default is False.
- index(value[, start[, stop]]) integer -- return first index of value. #
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- plot_positions(ax=None, **kwargs)#
Plot the centers of the spatial models on a given WCS axis.
- Parameters:
- ax
WCSAxes
, optional Axes to plot on. If no axes are given, an all-sky WCS is chosen using a CAR projection. Default is None.
- **kwargsdict
Keyword arguments passed to
scatter
.
- ax
- Returns:
- ax
WcsAxes
WCS axes.
- ax
- plot_regions(ax=None, kwargs_point=None, path_effect=None, **kwargs)#
Plot extent of the spatial models on a given WCS axis.
- Parameters:
- ax
WCSAxes
, optional Axes to plot on. If no axes are given, an all-sky WCS is chosen using a CAR projection. Default is None.
- kwargs_pointdict, optional
Keyword arguments passed to
Line2D
for plotting of point sources. Default is None.- path_effect
PathEffect
, optional Path effect applied to artists and lines. Default is None.
- **kwargsdict
Keyword arguments passed to
Artist
.
- ax
- Returns:
- ax
WcsAxes
WCS axes.
- ax
- pop([index]) item -- remove and return item at index (default last). #
Raise IndexError if list is empty or index is out of range.
- classmethod read(filename, checksum=False)#
Read from YAML file.
- Parameters:
- filenamestr
input filename
- checksumbool, optional
Whether to perform checksum verification. Default is False.
- read_covariance(path, filename='_covariance.dat', **kwargs)#
Read covariance data from file.
- Parameters:
- pathstr or
Path
Base path.
- filenamestr
Filename.
- **kwargsdict
Keyword arguments passed to
read
.
- pathstr or
- reassign(dataset_name, new_dataset_name)#
Reassign a model from one dataset to another.
- Parameters:
- dataset_namestr or list
Name of the datasets where the model is currently defined.
- new_dataset_namestr or list
Name of the datasets where the model should be defined instead. If multiple names are given the two list must have the save length, as the reassignment is element-wise.
- remove(value)#
S.remove(value) – remove first occurrence of value. Raise ValueError if the value is not present.
- restore_status(restore_values=True)#
Context manager to restore status.
A copy of the values is made on enter, and those values are restored on exit.
- Parameters:
- restore_valuesbool, optional
Restore values if True, otherwise restore only frozen status and covariance matrix. Default is True.
- reverse()#
S.reverse() – reverse IN PLACE
- select(name_substring=None, datasets_names=None, tag=None, model_type=None, frozen=None)#
Select models that meet all specified conditions.
- Parameters:
- name_substringstr, optional
Substring contained in the model name. Default is None.
- datasets_namesstr or list, optional
Name of the dataset. Default is None.
- tagstr or list, optional
Model tag. Default is None.
- model_type{‘None’, ‘spatial’, ‘spectral’}
Type of model, used together with “tag”, if the tag is not unique. Default is None.
- frozenbool, optional
If True, select models with all parameters frozen; if False, exclude them. Default is None.
- Returns:
- models
DatasetModels
Selected models.
- models
- select_from_geom(geom, **kwargs)#
Select models that fall inside a given geometry.
- Parameters:
- geom
Geom
Geometry to select models from.
- **kwargsdict
Keyword arguments passed to
select_mask
.
- geom
- Returns:
- models
DatasetModels
Selected models.
- models
- select_mask(mask, margin='0 deg', use_evaluation_region=True)#
Check if sky models contribute within a mask map.
- Parameters:
- mask
WcsNDMap
of boolean type Map containing a boolean mask.
- margin
Quantity
, optional Add a margin in degree to the source evaluation radius. Used to take into account PSF width. Default is “0 deg”.
- use_evaluation_regionbool, optional
Account for the extension of the model or not. Default is True.
- mask
- Returns:
- models
DatasetModels
Selected models contributing inside the region where mask==True.
- models
- select_region(regions, wcs=None)#
Select sky models with center position contained within a given region.
- Parameters:
- regionsstr,
Region
or list ofRegion
Region or list of regions (pixel or sky regions accepted). A region can be defined as a string ind DS9 format as well. See http://ds9.si.edu/doc/ref/region.html for details.
- wcs
WCS
, optional World coordinate system transformation. Default is None.
- regionsstr,
- Returns:
- models
DatasetModels
Selected models.
- models
- selection_mask(name_substring=None, datasets_names=None, tag=None, model_type=None, frozen=None)#
Create a mask of models, that meet all specified conditions.
- Parameters:
- name_substringstr, optional
Substring contained in the model name. Default is None.
- datasets_namesstr or list of str, optional
Name of the dataset. Default is None.
- tagstr or list of str, optional
Model tag. Default is None.
- model_type{‘None’, ‘spatial’, ‘spectral’}
Type of model, used together with “tag”, if the tag is not unique. Default is None.
- frozenbool, optional
Select models with all parameters frozen if True, exclude them if False. Default is None.
- Returns:
- mask
numpy.array
Boolean mask, True for selected models.
- mask
- set_parameters_bounds(tag, model_type, parameters_names=None, min=None, max=None, value=None)#
Set bounds for the selected models types and parameters names.
- Parameters:
- tagstr or list
Tag of the models.
- model_type{“spatial”, “spectral”, “temporal”}
Type of model.
- parameters_namesstr or list, optional
Parameters names. Default is None.
- minfloat, optional
Minimum value. Default is None.
- maxfloat, optional
Maximum value. Default is None.
- valuefloat, optional
Initial value. Default is None.
- to_dict(full_output=False, overwrite_templates=False)#
Convert to dictionary.
- to_regions()#
Return a list of the regions for the spatial models.
- Returns:
- regions: list of
SkyRegion
Regions.
- regions: list of
- to_template_sky_model(geom, spectral_model=None, name=None)#
Merge a list of models into a single
SkyModel
.- Parameters:
- geom
Geom
Map geometry of the result template model.
- spectral_model
SpectralModel
, optional One of the NormSpectralModel. Default is None.
- namestr, optional
Name of the new model. Default is None.
- geom
- Returns:
- model
SkyModel
Template sky model.
- model
- to_template_spectral_model(geom, mask=None)#
Merge a list of models into a single
TemplateSpectralModel
.For each model the spatial component is integrated over the given geometry where the mask is true and multiplied by the spectral component value in each energy bin.
- Parameters:
- Returns:
- model
TemplateSpectralModel
Template spectral model.
- model
- to_yaml(full_output=False, overwrite_templates=False)#
Convert to YAML string.
- Parameters:
- full_outputbool, optional
Store full parameter output. Default is False.
- overwrite_templatesbool, optional
Overwrite templates FITS files. Default is False.
- unfreeze(model_type=None)#
Restore parameters frozen status to default depending on model type.
- Parameters:
- model_type{None, “spatial”, “spectral”}
Restore frozen status to default for all parameters or only spatial or only spectral. Default is None.
- update_link_label()#
Update linked parameters labels used for serialisation and print.
- write(path, overwrite=False, full_output=False, overwrite_templates=False, write_covariance=True, checksum=False)#
Write to YAML file.
- Parameters:
- path
pathlib.Path
or str Path to write files.
- overwritebool, optional
Overwrite existing file. Default is False.
- full_outputbool, optional
Store full parameter output. Default is False.
- overwrite_templatesbool, optional
Overwrite templates FITS files. Default is False.
- write_covariancebool, optional
Whether to save the covariance. Default is True.
- checksumbool, optional
When True adds a CHECKSUM entry to the file. Default is False.
- path