This is a fixed-text formatted version of a Jupyter notebook

Recipe to show the interactively edit the Sky model on the notebook

Example showing how to transform the gammapy model to a pandas dataframe, a widely used tabular format and modifying the parameters interactively on the notebook.
Plain text models in the XML or yaml format can not be sorted or filtered. Here we propose to use qgrid to explore and edit the Model.

Qgrid is a Jupyter notebook widget which uses SlickGrid to render pandas DataFrames within a Jupyter notebook. This allows you to explore your DataFrames with intuitive scrolling, sorting, and filtering controls, as well as edit your DataFrames by double clicking cells. Github

See GIF movie demo here.

Qgrid can be installed via conda:
conda install -c conda-forge qgrid
Works using Jupyter notebook. Currently doesn’t work using Jupyter Lab 3.0.

There are alternatives to qgrid like ipysheet or ipyaggrid.

[1]:
from gammapy.modeling.models import Models
from astropy.table import Table

# read gammapy models
models = Models.read("$GAMMAPY_DATA/tests/models/gc_example_models.yaml")
df = models.to_parameters_table().to_pandas()

import qgrid

qgrid_widget = qgrid.show_grid(df, grid_options={"maxVisibleRows": 10})
qgrid_widget
# You can sort, filter, freeze/thaw parameters
# e.g show all spectral indices, only parameters of a given source, only the spatial parameters, etc
# all changes values are recorded and will need to be loaded in the gammapy model in a separated cell.

Load the changed parameters and verify that the model has been updated

[2]:
qgrid_df = qgrid_widget.get_changed_df()  # get changed values
models.update_parameters_from_table(
    Table.from_pandas(qgrid_df)
)  # update model with changed values
models.to_parameters_table()  # print model to verify that values were updated
[2]:
Table length=23
modeltypenamevalueuniterrorminmaxfrozenlink
str18str8str9float64str14int64float64float64boolstr18
gcspectralindex2.0000e+000.000e+00nannanFalse
gcspectralamplitude3.0000e-12cm-2 s-1 TeV-10.000e+00nannanFalse
gcspectralreference1.0000e+00TeV0.000e+00nannanTruereference@lQO8TIDg
gcspectrallambda_1.0000e-01TeV-10.000e+00nannanFalse
gcspectralalpha1.0000e+000.000e+00nannanTrue
gcspatiallon_00.0000e+00deg0.000e+00nannanFalse
gcspatiallat_00.0000e+00deg0.000e+00-9.000e+019.000e+01False
gll_iem_v06_cutoutspectralnorm1.0000e+000.000e+00nannanFalse
gll_iem_v06_cutoutspectraltilt0.0000e+000.000e+00nannanTrue
gll_iem_v06_cutoutspectralreference1.0000e+00TeV0.000e+00nannanTrue
gc-bkgspectralnorm1.0000e+000.000e+00nannanFalse
gc-bkgspectraltilt0.0000e+000.000e+00nannanTrue
gc-bkgspectralreference1.0000e+00TeV0.000e+00nannanTrue
g09spectralindex2.0000e+000.000e+00nannanFalse
g09spectralamplitude3.0000e-12cm-2 s-1 TeV-10.000e+00nannanFalse
g09spectralreference1.0000e+00TeV0.000e+00nannanTruereference@lQO8TIDg
g09spectrallambda_1.0000e-01TeV-10.000e+00nannanFalse
g09spectralalpha1.0000e+000.000e+00nannanTrue
g09spatiallon_09.0000e-01deg0.000e+00nannanFalse
g09spatiallat_01.0000e-01deg0.000e+00-9.000e+019.000e+01False
g09-bkgspectralnorm1.0000e+000.000e+00nannanFalse
g09-bkgspectraltilt0.0000e+000.000e+00nannanTrue
g09-bkgspectralreference1.0000e+00TeV0.000e+00nannanTrue
[3]:
# Display thumbnails for nbsphinx-thumbnail
from IPython.display import Image

display(Image(filename="edit-interactive.png"))
../../_images/notebooks_interactive-model-editing_interactive-model-editing_5_0.png