.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/api/model_management.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_api_model_management.py: Modelling ========= Multiple datasets and models interaction in Gammapy. Aim --- The main aim of this tutorial is to illustrate model management in Gammapy, specially how to distribute multiple models across multiple datasets. We also show some convenience functions built in gammapy for handling multiple model components. **Note: Since gammapy v0.18, the responsibility of model management is left totally upon the user. All models, including background models, have to be explicitly defined.** To keep track of the used models, we define a global `~gammapy.modeling.models.Models` object (which is a collection of `~gammapy.modeling.models.SkyModel` objects) to which we append and delete models. Prerequisites ------------- - Knowledge of 3D analysis, dataset reduction and fitting see the :doc:`/tutorials/starting/analysis_2` tutorial. - Understanding of gammapy models, see the :doc:`/tutorials/api/models` tutorial. - Analysis of the Galactic Center with Fermi-LAT, shown in the :doc:`/tutorials/data/fermi_lat` tutorial. - Analysis of the Galactic Center with CTA-DC1 , shown in the :doc:`/tutorials/analysis-3d/analysis_3d` tutorial. Proposed approach ----------------- To show how datasets interact with models, we use two pre-computed datasets on the galactic center, one from Fermi-LAT and the other from simulated CTA (DC1) data. We demonstrate - Adding background models for each dataset - Sharing a model between multiple datasets We then load models from the Fermi 3FHL catalog to show some convenience handling for multiple `~gammapy.modeling.models.Models` together - accessing models from a catalog - selecting models contributing to a given region - adding and removing models - freezing and thawing multiple model parameters together - serialising models For computational purposes, we do not perform any fitting in this notebook. Setup ----- .. GENERATED FROM PYTHON SOURCE LINES 57-76 .. code-block:: Python from astropy import units as u from astropy.coordinates import SkyCoord from regions import CircleSkyRegion import matplotlib.pyplot as plt # %matplotlib inline from IPython.display import display from gammapy.datasets import Datasets, MapDataset from gammapy.maps import Map from gammapy.modeling.models import ( FoVBackgroundModel, Models, PowerLawNormSpectralModel, SkyModel, TemplateSpatialModel, create_fermi_isotropic_diffuse_model, ) .. GENERATED FROM PYTHON SOURCE LINES 77-79 Check setup ----------- .. GENERATED FROM PYTHON SOURCE LINES 79-84 .. code-block:: Python from gammapy.utils.check import check_tutorials_setup check_tutorials_setup() .. rst-class:: sphx-glr-script-out .. code-block:: none System: python_executable : /home/runner/work/gammapy-docs/gammapy-docs/gammapy/.tox/build_docs/bin/python python_version : 3.9.19 machine : x86_64 system : Linux Gammapy package: version : 1.3.dev241+g0271bebfc path : /home/runner/work/gammapy-docs/gammapy-docs/gammapy/.tox/build_docs/lib/python3.9/site-packages/gammapy Other packages: numpy : 1.26.4 scipy : 1.13.0 astropy : 5.2.2 regions : 0.8 click : 8.1.7 yaml : 6.0.1 IPython : 8.18.1 jupyterlab : not installed matplotlib : 3.8.4 pandas : not installed healpy : 1.16.6 iminuit : 2.25.2 sherpa : 4.16.0 naima : 0.10.0 emcee : 3.1.6 corner : 2.2.2 ray : 2.20.0 Gammapy environment variables: GAMMAPY_DATA : /home/runner/work/gammapy-docs/gammapy-docs/gammapy-datasets/dev .. GENERATED FROM PYTHON SOURCE LINES 85-91 Read the datasets ----------------- First, we read some precomputed Fermi and CTA datasets, and create a `~gammapy.datasets.Datasets` object containing the two. .. GENERATED FROM PYTHON SOURCE LINES 91-101 .. code-block:: Python fermi_dataset = MapDataset.read( "$GAMMAPY_DATA/fermi-3fhl-gc/fermi-3fhl-gc.fits.gz", name="fermi_dataset" ) cta_dataset = MapDataset.read( "$GAMMAPY_DATA/cta-1dc-gc/cta-1dc-gc.fits.gz", name="cta_dataset" ) datasets = Datasets([fermi_dataset, cta_dataset]) .. GENERATED FROM PYTHON SOURCE LINES 102-104 Plot the counts maps to see the region .. GENERATED FROM PYTHON SOURCE LINES 104-121 .. code-block:: Python plt.figure(figsize=(15, 5)) ax1 = plt.subplot(121, projection=fermi_dataset.counts.geom.wcs) ax2 = plt.subplot(122, projection=cta_dataset.counts.geom.wcs) datasets[0].counts.sum_over_axes().smooth(0.05 * u.deg).plot( ax=ax1, stretch="sqrt", add_cbar=True ) datasets[1].counts.sum_over_axes().smooth(0.05 * u.deg).plot( ax=ax2, stretch="sqrt", add_cbar=True ) ax1.set_title("Fermi counts") ax2.set_title("CTA counts") plt.show() .. image-sg:: /tutorials/api/images/sphx_glr_model_management_001.png :alt: Fermi counts, CTA counts :srcset: /tutorials/api/images/sphx_glr_model_management_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 123-126 .. code-block:: Python display(datasets.info_table(cumulative=False)) .. rst-class:: sphx-glr-script-out .. code-block:: none name counts excess ... n_fit_bins stat_type stat_sum ... ------------- ------ -------------- ... ---------- --------- -------- fermi_dataset 27727 3792.244140625 ... 633600 cash nan cta_dataset 104317 12809.3046875 ... 691680 cash nan .. GENERATED FROM PYTHON SOURCE LINES 128-132 .. code-block:: Python print(datasets) .. rst-class:: sphx-glr-script-out .. code-block:: none Datasets -------- Dataset 0: Type : MapDataset Name : fermi_dataset Instrument : Models : Dataset 1: Type : MapDataset Name : cta_dataset Instrument : Models : .. GENERATED FROM PYTHON SOURCE LINES 133-137 Note that while the datasets have an associated background map, they currently do not have any associated background model. This will be added in the following section .. GENERATED FROM PYTHON SOURCE LINES 140-156 Assigning background models to datasets --------------------------------------- For any IACT dataset (in this case `cta_dataset`) , we have to create a `~gammapy.modeling.models.FoVBackgroundModel`. Note that `~gammapy.modeling.models.FoVBackgroundModel` must be specified to one dataset only For Fermi-LAT, the background contribution is taken from a diffuse isotropic template. To convert this into a gammapy `~gammapy.modeling.models.SkyModel`, use the helper function `~gammapy.modeling.models.create_fermi_isotropic_diffuse_model` To attach a model on a particular dataset it is necessary to specify the ``datasets_names``. Otherwise, by default, the model will be applied to all the datasets in ``datasets`` .. GENERATED FROM PYTHON SOURCE LINES 159-162 First, we must create a global `~gammapy.modeling.models.Models` object which acts as the container for all models used in a particular analysis .. GENERATED FROM PYTHON SOURCE LINES 162-186 .. code-block:: Python models = Models() # global models object # Create the FoV background model for CTA data bkg_model = FoVBackgroundModel(dataset_name=cta_dataset.name) models.append(bkg_model) # Add the bkg_model to models() # Read the fermi isotropic diffuse background model diffuse_iso = create_fermi_isotropic_diffuse_model( filename="$GAMMAPY_DATA/fermi_3fhl/iso_P8R2_SOURCE_V6_v06.txt", ) diffuse_iso.datasets_names = fermi_dataset.name # specifying the dataset name models.append(diffuse_iso) # Add the fermi_bkg_model to models() # Now, add the models to datasets datasets.models = models # You can see that each dataset lists the correct associated models print(datasets) .. rst-class:: sphx-glr-script-out .. code-block:: none Datasets -------- Dataset 0: Type : MapDataset Name : fermi_dataset Instrument : Models : ['fermi-diffuse-iso'] Dataset 1: Type : MapDataset Name : cta_dataset Instrument : Models : ['cta_dataset-bkg'] .. GENERATED FROM PYTHON SOURCE LINES 187-198 Add a model on multiple datasets -------------------------------- In this section, we show how to add a model to multiple datasets. For this, we specify a list of `datasets_names` to the model. Alternatively, not specifying any `datasets_names` will add it to all the datasets. For this example, we use a template model of the galactic diffuse emission to be shared between the two datasets. .. GENERATED FROM PYTHON SOURCE LINES 198-224 .. code-block:: Python # Create the diffuse model diffuse_galactic_fermi = Map.read("$GAMMAPY_DATA/fermi-3fhl-gc/gll_iem_v06_gc.fits.gz") template_diffuse = TemplateSpatialModel( diffuse_galactic_fermi, normalize=False ) # the template model in this case is already a full 3D model, it should not be normalised diffuse_iem = SkyModel( spectral_model=PowerLawNormSpectralModel(), spatial_model=template_diffuse, name="diffuse-iem", datasets_names=[ cta_dataset.name, fermi_dataset.name, ], # specifying list of dataset names ) # A power law spectral correction is applied in this case # Now, add the diffuse model to the global models list models.append(diffuse_iem) # add it to the datasets, and inspect datasets.models = models print(datasets) .. rst-class:: sphx-glr-script-out .. code-block:: none Datasets -------- Dataset 0: Type : MapDataset Name : fermi_dataset Instrument : Models : ['fermi-diffuse-iso', 'diffuse-iem'] Dataset 1: Type : MapDataset Name : cta_dataset Instrument : Models : ['cta_dataset-bkg', 'diffuse-iem'] .. GENERATED FROM PYTHON SOURCE LINES 225-229 The `diffuse-iem` model is correctly present on both. Now, you can proceed with the fit. For computational purposes, we skip it in this notebook .. GENERATED FROM PYTHON SOURCE LINES 231-234 fit2 = Fit() result2 = fit2.run(datasets) print(result2.success) .. GENERATED FROM PYTHON SOURCE LINES 237-244 Loading models from a catalog ----------------------------- We now load the Fermi 3FHL catalog and demonstrate some convenience functions. For more details on using Gammapy catalog, see the :doc:`/tutorials/api/catalog` tutorial. .. GENERATED FROM PYTHON SOURCE LINES 244-250 .. code-block:: Python from gammapy.catalog import SourceCatalog3FHL catalog = SourceCatalog3FHL() .. GENERATED FROM PYTHON SOURCE LINES 251-254 We first choose some relevant models from the catalog and create a new `~gammapy.modeling.models.Models` object. .. GENERATED FROM PYTHON SOURCE LINES 254-262 .. code-block:: Python gc_sep = catalog.positions.separation(SkyCoord(0, 0, unit="deg", frame="galactic")) models_3fhl = [_.sky_model() for k, _ in enumerate(catalog) if gc_sep[k].value < 8] models_3fhl = Models(models_3fhl) print(len(models_3fhl)) .. rst-class:: sphx-glr-script-out .. code-block:: none 20 .. GENERATED FROM PYTHON SOURCE LINES 263-271 Selecting models contributing to a given region ----------------------------------------------- We now use `~gammapy.modeling.models.Models.select_region` to get a subset of models contributing to a particular region. You can also use `~gammapy.modeling.models.Models.select_mask` to get models lying inside the `True` region of a mask map` .. GENERATED FROM PYTHON SOURCE LINES 271-280 .. code-block:: Python region = CircleSkyRegion( center=SkyCoord(0, 0, unit="deg", frame="galactic"), radius=3.0 * u.deg ) models_selected = models_3fhl.select_region(region) print(len(models_selected)) .. rst-class:: sphx-glr-script-out .. code-block:: none 8 .. GENERATED FROM PYTHON SOURCE LINES 281-286 We now want to assign `models_3fhl` to the Fermi dataset, and `models_selected` to both the CTA and Fermi datasets. For this, we explicitly mention the `datasets_names` to the former, and leave it `None` (default) for the latter. .. GENERATED FROM PYTHON SOURCE LINES 286-295 .. code-block:: Python for model in models_3fhl: if model not in models_selected: model.datasets_names = fermi_dataset.name # assign the models to datasets datasets.models = models_3fhl .. GENERATED FROM PYTHON SOURCE LINES 296-298 To see the models on a particular dataset, you can simply see .. GENERATED FROM PYTHON SOURCE LINES 298-303 .. code-block:: Python print("Fermi dataset models: ", datasets[0].models.names) print("\n CTA dataset models: ", datasets[1].models.names) .. rst-class:: sphx-glr-script-out .. code-block:: none Fermi dataset models: ['3FHL J1731.7-3003', '3FHL J1732.6-3131', '3FHL J1741.8-2536', '3FHL J1744.5-2609', '3FHL J1745.6-2900', '3FHL J1745.8-3028e', '3FHL J1746.2-2852', '3FHL J1747.2-2959', '3FHL J1747.2-2822', '3FHL J1748.0-2446', '3FHL J1748.1-2903', '3FHL J1748.6-2816', '3FHL J1753.8-2537', '3FHL J1800.5-2343e', '3FHL J1800.7-2357', '3FHL J1801.5-2450', '3FHL J1801.6-2327', '3FHL J1802.3-3043', '3FHL J1809.8-2332', '3FHL J1811.2-2800'] CTA dataset models: ['3FHL J1744.5-2609', '3FHL J1745.6-2900', '3FHL J1745.8-3028e', '3FHL J1746.2-2852', '3FHL J1747.2-2959', '3FHL J1747.2-2822', '3FHL J1748.1-2903', '3FHL J1748.6-2816'] .. GENERATED FROM PYTHON SOURCE LINES 304-309 Combining two Models -------------------- `~gammapy.modeling.models.Models` can be extended simply as python lists .. GENERATED FROM PYTHON SOURCE LINES 309-314 .. code-block:: Python models.extend(models_selected) print(len(models)) .. rst-class:: sphx-glr-script-out .. code-block:: none 11 .. GENERATED FROM PYTHON SOURCE LINES 315-321 Selecting models from a list ---------------------------- A `~gammapy.modeling.models.Model` can be selected from a list of `~gammapy.modeling.models.Models` by specifying its index or its name. .. GENERATED FROM PYTHON SOURCE LINES 321-330 .. code-block:: Python model = models_3fhl[0] print(model) # Alternatively model = models_3fhl["3FHL J1731.7-3003"] print(model) .. rst-class:: sphx-glr-script-out .. code-block:: none SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.742 +/- 0.50 amplitude : 2.59e-12 +/- 7.6e-13 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 : 262.949 +/- 0.02 deg lat_0 : -30.051 +/- 0.02 deg SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.742 +/- 0.50 amplitude : 2.59e-12 +/- 7.6e-13 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 : 262.949 +/- 0.02 deg lat_0 : -30.051 +/- 0.02 deg .. GENERATED FROM PYTHON SOURCE LINES 331-335 `~gammapy.modeling.models.Models.select` can be used to select all models satisfying a list of conditions. To select all models applied on the ``cta_dataset`` with the characters `1748` in the name .. GENERATED FROM PYTHON SOURCE LINES 335-340 .. code-block:: Python models = models_3fhl.select(datasets_names=cta_dataset.name, name_substring="1748") print(models) .. rst-class:: sphx-glr-script-out .. code-block:: none Models Component 0: SkyModel Name : 3FHL J1748.1-2903 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 4.687 +/- 1.02 amplitude : 1.24e-11 +/- 3.3e-12 1 / (cm2 GeV s) reference (frozen): 12.596 GeV lon_0 : 267.037 +/- 0.02 deg lat_0 : -29.062 +/- 0.02 deg Component 1: SkyModel Name : 3FHL J1748.6-2816 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 4.345 +/- 0.66 amplitude : 1.76e-11 +/- 3.6e-12 1 / (cm2 GeV s) reference (frozen): 13.199 GeV lon_0 : 267.160 +/- 0.02 deg lat_0 : -28.278 +/- 0.01 deg .. GENERATED FROM PYTHON SOURCE LINES 341-346 Note that `~gammapy.modeling.models.Models.select` combines the different conditions with an `AND` operator. If one needs to combine conditions with a `OR` operator, the `~gammapy.modeling.models.Models.selection_mask` method can generate a boolean array that can be used for selection. For example: .. GENERATED FROM PYTHON SOURCE LINES 346-355 .. code-block:: Python selection_mask = models_3fhl.selection_mask( name_substring="1748" ) | models_3fhl.selection_mask(name_substring="1731") models_OR = models_3fhl[selection_mask] print(models_OR) .. rst-class:: sphx-glr-script-out .. code-block:: none Models Component 0: SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.742 +/- 0.50 amplitude : 2.59e-12 +/- 7.6e-13 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 : 262.949 +/- 0.02 deg lat_0 : -30.051 +/- 0.02 deg Component 1: SkyModel Name : 3FHL J1748.0-2446 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 3.480 +/- 0.58 amplitude : 7.17e-12 +/- 1.6e-12 1 / (cm2 GeV s) reference (frozen): 14.796 GeV lon_0 : 267.012 +/- 0.02 deg lat_0 : -24.775 +/- 0.01 deg Component 2: SkyModel Name : 3FHL J1748.1-2903 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 4.687 +/- 1.02 amplitude : 1.24e-11 +/- 3.3e-12 1 / (cm2 GeV s) reference (frozen): 12.596 GeV lon_0 : 267.037 +/- 0.02 deg lat_0 : -29.062 +/- 0.02 deg Component 3: SkyModel Name : 3FHL J1748.6-2816 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 4.345 +/- 0.66 amplitude : 1.76e-11 +/- 3.6e-12 1 / (cm2 GeV s) reference (frozen): 13.199 GeV lon_0 : 267.160 +/- 0.02 deg lat_0 : -28.278 +/- 0.01 deg .. GENERATED FROM PYTHON SOURCE LINES 356-363 Removing a model from a dataset ------------------------------- Any addition or removal of a model must happen through the global models object, which must then be re-applied on the dataset/s. Note that operations **cannot** be directly performed on `dataset.models()`. .. GENERATED FROM PYTHON SOURCE LINES 363-375 .. code-block:: Python # cta_dataset.models.remove() # * this is forbidden * # Remove the model '3FHL J1744.5-2609' models_3fhl.remove("3FHL J1744.5-2609") len(models_3fhl) # After any operation on models, it must be re-applied on the datasets datasets.models = models_3fhl .. GENERATED FROM PYTHON SOURCE LINES 376-378 To see the models applied on a dataset, you can simply .. GENERATED FROM PYTHON SOURCE LINES 378-382 .. code-block:: Python print(datasets.models.names) .. rst-class:: sphx-glr-script-out .. code-block:: none ['3FHL J1731.7-3003', '3FHL J1732.6-3131', '3FHL J1741.8-2536', '3FHL J1745.6-2900', '3FHL J1745.8-3028e', '3FHL J1746.2-2852', '3FHL J1747.2-2959', '3FHL J1747.2-2822', '3FHL J1748.0-2446', '3FHL J1748.1-2903', '3FHL J1748.6-2816', '3FHL J1753.8-2537', '3FHL J1800.5-2343e', '3FHL J1800.7-2357', '3FHL J1801.5-2450', '3FHL J1801.6-2327', '3FHL J1802.3-3043', '3FHL J1809.8-2332', '3FHL J1811.2-2800'] .. GENERATED FROM PYTHON SOURCE LINES 383-391 Plotting models on a (counts) map ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The spatial regions of `~gammapy.modeling.models.Models` can be plotted on a given geom using `~gammapy.modeling.models.Models.plot_regions`. You can also use `~gammapy.modeling.models.Models.plot_positions` to plot the centers of each model. .. GENERATED FROM PYTHON SOURCE LINES 391-405 .. code-block:: Python plt.figure(figsize=(16, 5)) ax1 = plt.subplot(121, projection=fermi_dataset.counts.geom.wcs) ax2 = plt.subplot(122, projection=cta_dataset.counts.geom.wcs) for ax, dataset in zip([ax1, ax2], datasets): dataset.counts.sum_over_axes().smooth(0.05 * u.deg).plot( ax=ax, stretch="sqrt", add_cbar=True, cmap="afmhot" ) dataset.models.plot_regions(ax=ax, color="white") ax.set_title(dataset.name) plt.show() .. image-sg:: /tutorials/api/images/sphx_glr_model_management_002.png :alt: fermi_dataset, cta_dataset :srcset: /tutorials/api/images/sphx_glr_model_management_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 406-413 Freezing and unfreezing model parameters ---------------------------------------- For a given model, any parameter can be (un)frozen individually. Additionally, `model.freeze` and `model.unfreeze` can be used to freeze and unfreeze all parameters in one go. .. GENERATED FROM PYTHON SOURCE LINES 413-433 .. code-block:: Python model = models_3fhl[0] print(model) # To freeze a single parameter model.spectral_model.index.frozen = True print(model) # index is now frozen # To unfreeze a parameter model.spectral_model.index.frozen = False # To freeze all parameters of a model model.freeze() print(model) # To unfreeze all parameters (except parameters which must remain frozen) model.unfreeze() print(model) .. rst-class:: sphx-glr-script-out .. code-block:: none SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.742 +/- 0.50 amplitude : 2.59e-12 +/- 7.6e-13 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 : 262.949 +/- 0.02 deg lat_0 : -30.051 +/- 0.02 deg SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index (frozen): 2.742 amplitude : 2.59e-12 +/- 7.6e-13 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 : 262.949 +/- 0.02 deg lat_0 : -30.051 +/- 0.02 deg SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index (frozen): 2.742 amplitude (frozen): 2.59e-12 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 (frozen): 262.949 deg lat_0 (frozen): -30.051 deg SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.742 +/- 0.50 amplitude : 2.59e-12 +/- 7.6e-13 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 : 262.949 +/- 0.02 deg lat_0 : -30.051 +/- 0.02 deg .. GENERATED FROM PYTHON SOURCE LINES 434-437 Only spectral or spatial or temporal components of a model can also be frozen .. GENERATED FROM PYTHON SOURCE LINES 437-443 .. code-block:: Python # To freeze spatial components model.freeze("spatial") print(model) .. rst-class:: sphx-glr-script-out .. code-block:: none SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.742 +/- 0.50 amplitude : 2.59e-12 +/- 7.6e-13 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 (frozen): 262.949 deg lat_0 (frozen): -30.051 deg .. GENERATED FROM PYTHON SOURCE LINES 444-446 To check if all the parameters of a model are frozen, .. GENERATED FROM PYTHON SOURCE LINES 446-452 .. code-block:: Python print(model.frozen) # False because spectral components are not frozen print(model.spatial_model.frozen) # all spatial components are frozen .. rst-class:: sphx-glr-script-out .. code-block:: none False True .. GENERATED FROM PYTHON SOURCE LINES 453-456 The same operations can be performed on `~gammapy.modeling.models.Models` directly - to perform on a list of models at once, e.g. .. GENERATED FROM PYTHON SOURCE LINES 456-465 .. code-block:: Python models_selected.freeze() # freeze all parameters of all models models_selected.unfreeze() # unfreeze all parameters of all models # print the free parameters in the models print(models_selected.parameters.free_parameters.names) .. rst-class:: sphx-glr-script-out .. code-block:: none ['index', 'amplitude', 'lon_0', 'lat_0', 'index', 'amplitude', 'lon_0', 'lat_0', 'index', 'amplitude', 'lon_0', 'lat_0', 'r_0', 'index', 'amplitude', 'lon_0', 'lat_0', 'index', 'amplitude', 'lon_0', 'lat_0', 'index', 'amplitude', 'lon_0', 'lat_0', 'index', 'amplitude', 'lon_0', 'lat_0', 'index', 'amplitude', 'lon_0', 'lat_0'] .. GENERATED FROM PYTHON SOURCE LINES 466-472 There are more functionalities which you can explore. In general, using `help()` on any function is a quick and useful way to access the documentation. For ex, `Models.unfreeze_all` will unfreeze all parameters, even those which are fixed by default. To see its usage, you can simply type .. GENERATED FROM PYTHON SOURCE LINES 472-476 .. code-block:: Python help(models_selected.unfreeze) .. rst-class:: sphx-glr-script-out .. code-block:: none Help on method unfreeze in module gammapy.modeling.models.core: unfreeze(model_type=None) method of gammapy.modeling.models.core.Models instance 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. .. GENERATED FROM PYTHON SOURCE LINES 477-480 Serialising models ------------------ .. GENERATED FROM PYTHON SOURCE LINES 483-488 `~gammapy.modeling.models.Models` can be (independently of `~gammapy.datasets.Datasets`) written to/ read from a disk as yaml files. Datasets are always serialised along with their associated models, ie, with yaml and fits files. eg: .. GENERATED FROM PYTHON SOURCE LINES 488-504 .. code-block:: Python # To save only the models models_3fhl.write("3fhl_models.yaml", overwrite=True) # To save datasets and models datasets.write( filename="datasets-gc.yaml", filename_models="models_gc.yaml", overwrite=True ) # To read only models models = Models.read("3fhl_models.yaml") print(models) # To read datasets with models datasets_read = Datasets.read("datasets-gc.yaml", filename_models="models_gc.yaml") print(datasets_read) .. rst-class:: sphx-glr-script-out .. code-block:: none Models Component 0: SkyModel Name : 3FHL J1731.7-3003 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.742 +/- 0.50 amplitude : 2.59e-12 +/- 7.6e-13 1 / (cm2 GeV s) reference (frozen): 17.603 GeV lon_0 (frozen): 262.949 deg lat_0 (frozen): -30.051 deg Component 1: SkyModel Name : 3FHL J1732.6-3131 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 5.151 +/- 0.84 amplitude : 2.78e-11 +/- 4.5e-12 1 / (cm2 GeV s) reference (frozen): 12.216 GeV lon_0 : 263.156 +/- 0.01 deg lat_0 : -31.518 +/- 0.01 deg Component 2: SkyModel Name : 3FHL J1741.8-2536 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.196 +/- 0.30 amplitude : 1.25e-12 +/- 3.2e-13 1 / (cm2 GeV s) reference (frozen): 25.438 GeV lon_0 : 265.457 +/- 0.02 deg lat_0 : -25.610 +/- 0.02 deg Component 3: SkyModel Name : 3FHL J1745.6-2900 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.727 +/- 0.10 amplitude : 4.54e-11 +/- 2.7e-12 1 / (cm2 GeV s) reference (frozen): 18.370 GeV lon_0 : 266.419 +/- 0.01 deg lat_0 : -29.011 +/- 0.00 deg Component 4: SkyModel Name : 3FHL J1745.8-3028e Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : DiskSpatialModel Temporal model type : Parameters: index : 1.988 +/- 0.11 amplitude : 6.30e-12 +/- 7.0e-13 1 / (cm2 GeV s) reference (frozen): 34.165 GeV lon_0 : 266.453 +/- 0.00 deg lat_0 : -30.475 +/- 0.00 deg r_0 : 0.528 +/- 0.00 deg e (frozen): 0.000 phi (frozen): 0.000 deg edge_width (frozen): 0.010 Component 5: SkyModel Name : 3FHL J1746.2-2852 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 3.253 +/- 0.31 amplitude : 2.43e-11 +/- 3.2e-12 1 / (cm2 GeV s) reference (frozen): 15.207 GeV lon_0 : 266.564 +/- 0.01 deg lat_0 : -28.878 +/- 0.01 deg Component 6: SkyModel Name : 3FHL J1747.2-2959 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 3.704 +/- 0.71 amplitude : 6.59e-12 +/- 1.8e-12 1 / (cm2 GeV s) reference (frozen): 14.624 GeV lon_0 : 266.802 +/- 0.02 deg lat_0 : -29.995 +/- 0.02 deg Component 7: SkyModel Name : 3FHL J1747.2-2822 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.680 +/- 0.38 amplitude : 4.91e-12 +/- 1.2e-12 1 / (cm2 GeV s) reference (frozen): 18.330 GeV lon_0 : 266.824 +/- 0.02 deg lat_0 : -28.367 +/- 0.01 deg Component 8: SkyModel Name : 3FHL J1748.0-2446 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 3.480 +/- 0.58 amplitude : 7.17e-12 +/- 1.6e-12 1 / (cm2 GeV s) reference (frozen): 14.796 GeV lon_0 : 267.012 +/- 0.02 deg lat_0 : -24.775 +/- 0.01 deg Component 9: SkyModel Name : 3FHL J1748.1-2903 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 4.687 +/- 1.02 amplitude : 1.24e-11 +/- 3.3e-12 1 / (cm2 GeV s) reference (frozen): 12.596 GeV lon_0 : 267.037 +/- 0.02 deg lat_0 : -29.062 +/- 0.02 deg Component 10: SkyModel Name : 3FHL J1748.6-2816 Datasets names : None Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 4.345 +/- 0.66 amplitude : 1.76e-11 +/- 3.6e-12 1 / (cm2 GeV s) reference (frozen): 13.199 GeV lon_0 : 267.160 +/- 0.02 deg lat_0 : -28.278 +/- 0.01 deg Component 11: SkyModel Name : 3FHL J1753.8-2537 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 3.658 +/- 0.48 amplitude : 1.50e-11 +/- 2.4e-12 1 / (cm2 GeV s) reference (frozen): 14.320 GeV lon_0 : 268.450 +/- 0.01 deg lat_0 : -25.630 +/- 0.01 deg Component 12: SkyModel Name : 3FHL J1800.5-2343e Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : DiskSpatialModel Temporal model type : Parameters: index : 2.346 +/- 0.07 amplitude : 4.34e-11 +/- 2.4e-12 1 / (cm2 GeV s) reference (frozen): 23.149 GeV lon_0 : 270.144 +/- 0.00 deg lat_0 : -23.719 +/- 0.00 deg r_0 : 0.638 +/- 0.00 deg e (frozen): 0.000 phi (frozen): 0.000 deg edge_width (frozen): 0.010 Component 13: SkyModel Name : 3FHL J1800.7-2357 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 3.576 +/- 0.61 amplitude : 1.08e-11 +/- 2.8e-12 1 / (cm2 GeV s) reference (frozen): 14.223 GeV lon_0 : 270.184 +/- 0.02 deg lat_0 : -23.953 +/- 0.02 deg Component 14: SkyModel Name : 3FHL J1801.5-2450 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 4.524 +/- 1.03 amplitude : 9.48e-12 +/- 2.7e-12 1 / (cm2 GeV s) reference (frozen): 12.892 GeV lon_0 : 270.381 +/- 0.03 deg lat_0 : -24.837 +/- 0.02 deg Component 15: SkyModel Name : 3FHL J1801.6-2327 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 5.050 +/- 1.23 amplitude : 2.42e-11 +/- 5.8e-12 1 / (cm2 GeV s) reference (frozen): 12.124 GeV lon_0 : 270.403 +/- 0.02 deg lat_0 : -23.465 +/- 0.02 deg Component 16: SkyModel Name : 3FHL J1802.3-3043 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 6.222 +/- 1.29 amplitude : 1.48e-11 +/- 3.5e-12 1 / (cm2 GeV s) reference (frozen): 11.876 GeV lon_0 : 270.599 +/- 0.02 deg lat_0 : -30.722 +/- 0.02 deg Component 17: SkyModel Name : 3FHL J1809.8-2332 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 4.220 +/- 0.26 amplitude : 6.26e-11 +/- 4.8e-12 1 / (cm2 GeV s) reference (frozen): 13.446 GeV lon_0 : 272.459 +/- 0.01 deg lat_0 : -23.539 +/- 0.01 deg Component 18: SkyModel Name : 3FHL J1811.2-2800 Datasets names : fermi_dataset Spectral model type : PowerLawSpectralModel Spatial model type : PointSpatialModel Temporal model type : Parameters: index : 2.557 +/- 0.49 amplitude : 1.30e-12 +/- 4.2e-13 1 / (cm2 GeV s) reference (frozen): 19.437 GeV lon_0 : 272.804 +/- 0.02 deg lat_0 : -28.003 +/- 0.02 deg Datasets -------- Dataset 0: Type : MapDataset Name : fermi_dataset Instrument : Models : ['3FHL J1731.7-3003', '3FHL J1732.6-3131', '3FHL J1741.8-2536', '3FHL J1745.6-2900', '3FHL J1745.8-3028e', '3FHL J1746.2-2852', '3FHL J1747.2-2959', '3FHL J1747.2-2822', '3FHL J1748.0-2446', '3FHL J1748.1-2903', '3FHL J1748.6-2816', '3FHL J1753.8-2537', '3FHL J1800.5-2343e', '3FHL J1800.7-2357', '3FHL J1801.5-2450', '3FHL J1801.6-2327', '3FHL J1802.3-3043', '3FHL J1809.8-2332', '3FHL J1811.2-2800'] Dataset 1: Type : MapDataset Name : cta_dataset Instrument : Models : ['3FHL J1745.6-2900', '3FHL J1745.8-3028e', '3FHL J1746.2-2852', '3FHL J1747.2-2959', '3FHL J1747.2-2822', '3FHL J1748.1-2903', '3FHL J1748.6-2816'] .. _sphx_glr_download_tutorials_api_model_management.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/gammapy/gammapy-webpage/main?urlpath=lab/tree/notebooks/dev/tutorials/api/model_management.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: model_management.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: model_management.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_