Models

class gammapy.modeling.models.Models(models=None)[source]

Bases: gammapy.modeling.models.core.DatasetModels, collections.abc.MutableSequence

Sky model collection.

Parameters
modelsSkyModel, list of SkyModel or Models

Sky models

Attributes Summary

covariance

frozen

Boolean mask, True if all parameters of a given model are frozen

names

parameters

parameters_unique_names

List of unique parameter names as model_name.par_type.par_name

positions

Positions of the models (SkyCoord)

wcs_geom

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()

A 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 dict.

from_yaml(yaml_str[, path])

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)

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_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

to_dict([full_output])

Convert to dict.

to_parameters_table()

Convert Models parameters to an astropy Table.

to_regions()

Returns a list of the regions for the spatial models

to_template_sky_model(geom[, …])

Merge a list of models into a single SkyModel

to_yaml([full_output])

Convert to YAML string.

unfreeze([model_type])

Restore parameters frozen status to default depending on model type

update_parameters_from_table(t)

Update Models from an astropy Table.

write(path[, overwrite, full_output, …])

Write to YAML file.

write_covariance(filename, **kwargs)

Write covariance to file

Attributes Documentation

covariance
frozen

Boolean mask, True if all parameters of a given model are frozen

names
parameters
parameters_unique_names

List of unique parameter names as model_name.par_type.par_name

positions

Positions of the models (SkyCoord)

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()

A deep copy.

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

classmethod from_dict(data, path='')

Create from dict.

classmethod from_yaml(yaml_str, path='')

Create from YAML string.

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.

insert(idx, model)[source]

S.insert(index, value) – insert value before index

plot_positions(ax=None, **kwargs)

“Plot the centers of the spatial models on a given wcs axis

Parameters
axWCSAxes

Axes to plot on. If no axes are given, an all-sky wcs is chosen using a CAR projection

**kwargsdict

Keyword arguments passed to scatter

Returns
axWcsAxes

Wcs axes

plot_regions(ax=None, kwargs_point=None, path_effect=None, **kwargs)

Plot extent of the spatial models on a given wcs axis

Parameters
axWCSAxes

Axes to plot on. If no axes are given, an all-sky wcs is chosen using a CAR projection

kwargs_pointdict

Keyword arguments passed to Line2D for plotting of point sources

path_effectPathEffect

Path effect applied to artists and lines.

**kwargsdict

Keyword arguments passed to Artist

Returns
axWcsAxes

WCS axes

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)

Read from YAML file.

read_covariance(path, filename='_covariance.dat', **kwargs)

Read covariance data from file

Parameters
filenamestr

Filename

**kwargsdict

Keyword arguments passed to read

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 lenght, 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

Restore values if True, otherwise restore only frozen status and covariance matrix.

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

Substring contained in the model name

datasets_namesstr or list

Name of the dataset

tagstr or list

Model tag

model_type{None, spatial, spectral}

Type of model, used together with “tag”, if the tag is not unique.

frozenbool

Select models with all parameters frozen if True, exclude them if False.

Returns
modelsDatasetModels

Selected models

select_mask(mask, margin='0 deg', use_evaluation_region=True)

Check if sky models contribute within a mask map.

Parameters
maskWcsNDMap of boolean type

Map containing a boolean mask

marginQuantity

Add a margin in degree to the source evaluation radius. Used to take into account PSF width.

use_evaluation_regionbool

Account for the extension of the model or not. The default is True.

Returns
modelsDatasetModels

Selected models contributing inside the region where mask==True

select_region(regions, wcs=None)

Select sky models with center position contained within a given region

Parameters
regionsstr, Region or list of Region

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.

wcsWCS

World coordinate system transformation

Returns
modelsDatasetModels

Selected 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

Substring contained in the model name

datasets_namesstr or list of str

Name of the dataset

tagstr or list of str

Model tag

model_type{None, spatial, spectral}

Type of model, used together with “tag”, if the tag is not unique.

frozenbool

Select models with all parameters frozen if True, exclude them if False.

Returns
masknumpy.array

Boolean mask, True for selected models

set_parameters_bounds(tag, model_type, parameters_names, 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”}

type of models

parameters_namesstr or list

parameters names

minfloat

min value

maxfloat

max value

valuefloat

init value

to_dict(full_output=False)

Convert to dict.

to_parameters_table()

Convert Models parameters to an astropy Table.

to_regions()

Returns a list of the regions for the spatial models

Returns
regions: list of SkyRegion

Regions

to_template_sky_model(geom, spectral_model=None, name=None)

Merge a list of models into a single SkyModel

Parameters
spectral_modelSpectralModel

One of the NormSpectralMdel

namestr

Name of the new model

to_yaml(full_output=False)

Convert to YAML string.

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

update_parameters_from_table(t)

Update Models from an astropy Table.

write(path, overwrite=False, full_output=False, write_covariance=True)

Write to YAML file.

Parameters
pathpathlib.Path or str

path to write files

overwritebool

overwrite files

write_covariancebool

save covariance or not

write_covariance(filename, **kwargs)

Write covariance to file

Parameters
filenamestr

Filename

**kwargsdict

Keyword arguments passed to write