{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Recipe to show the interactively edit the Sky model on the notebook \n", "\n", "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. \n", "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. \n", "\n", "\n", "*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](https://github.com/quantopian/qgrid) \n", "\n", "\n", "See GIF movie demo [here](https://github.com/quantopian/qgrid/blob/master/docs/images/filtering_demo.gif).\n", "\n", "Qgrid can be installed via conda: \n", "`conda install -c conda-forge qgrid` \n", "Works using Jupyter notebook.\n", "Currently doesn't work using Jupyter Lab 3.0. \n", "\n", "There are alternatives to qgrid like [ipysheet](https://github.com/QuantStack/ipysheet) or [ipyaggrid](https://dgothrek.gitlab.io/ipyaggrid)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2021-11-24T21:35:29.686884Z", "iopub.status.busy": "2021-11-24T21:35:29.686057Z", "iopub.status.idle": "2021-11-24T21:35:30.598619Z", "shell.execute_reply": "2021-11-24T21:35:30.597049Z" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "08ce2794b221465a9a196c840c3eb7cc", "version_major": 2, "version_minor": 0 }, "text/plain": [ "QgridWidget(grid_options={'fullWidthRows': True, 'syncColumnCellResize': True, 'forceFitColumns': True, 'defau…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from gammapy.modeling.models import Models\n", "from astropy.table import Table\n", "\n", "# read gammapy models\n", "models = Models.read(\"$GAMMAPY_DATA/tests/models/gc_example_models.yaml\")\n", "df = models.to_parameters_table().to_pandas()\n", "\n", "import qgrid\n", "\n", "qgrid_widget = qgrid.show_grid(df, grid_options={\"maxVisibleRows\": 10})\n", "qgrid_widget\n", "# You can sort, filter, freeze/thaw parameters\n", "# e.g show all spectral indices, only parameters of a given source, only the spatial parameters, etc\n", "# all changes values are recorded and will need to be loaded in the gammapy model in a separated cell." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load the changed parameters and verify that the model has been updated " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2021-11-24T21:35:30.607481Z", "iopub.status.busy": "2021-11-24T21:35:30.607117Z", "iopub.status.idle": "2021-11-24T21:35:30.608612Z", "shell.execute_reply": "2021-11-24T21:35:30.608787Z" } }, "outputs": [ { "data": { "text/html": [ "
model | type | name | value | unit | error | min | max | frozen | link |
---|---|---|---|---|---|---|---|---|---|
str18 | str8 | str9 | float64 | str14 | int64 | float64 | float64 | bool | str18 |
gc | spectral | index | 2.0000e+00 | 0.000e+00 | nan | nan | False | ||
gc | spectral | amplitude | 3.0000e-12 | cm-2 s-1 TeV-1 | 0.000e+00 | nan | nan | False | |
gc | spectral | reference | 1.0000e+00 | TeV | 0.000e+00 | nan | nan | True | reference@lQO8TIDg |
gc | spectral | lambda_ | 1.0000e-01 | TeV-1 | 0.000e+00 | nan | nan | False | |
gc | spectral | alpha | 1.0000e+00 | 0.000e+00 | nan | nan | True | ||
gc | spatial | lon_0 | 0.0000e+00 | deg | 0.000e+00 | nan | nan | False | |
gc | spatial | lat_0 | 0.0000e+00 | deg | 0.000e+00 | -9.000e+01 | 9.000e+01 | False | |
gll_iem_v06_cutout | spectral | norm | 1.0000e+00 | 0.000e+00 | nan | nan | False | ||
gll_iem_v06_cutout | spectral | tilt | 0.0000e+00 | 0.000e+00 | nan | nan | True | ||
gll_iem_v06_cutout | spectral | reference | 1.0000e+00 | TeV | 0.000e+00 | nan | nan | True | |
gc-bkg | spectral | norm | 1.0000e+00 | 0.000e+00 | nan | nan | False | ||
gc-bkg | spectral | tilt | 0.0000e+00 | 0.000e+00 | nan | nan | True | ||
gc-bkg | spectral | reference | 1.0000e+00 | TeV | 0.000e+00 | nan | nan | True | |
g09 | spectral | index | 2.0000e+00 | 0.000e+00 | nan | nan | False | ||
g09 | spectral | amplitude | 3.0000e-12 | cm-2 s-1 TeV-1 | 0.000e+00 | nan | nan | False | |
g09 | spectral | reference | 1.0000e+00 | TeV | 0.000e+00 | nan | nan | True | reference@lQO8TIDg |
g09 | spectral | lambda_ | 1.0000e-01 | TeV-1 | 0.000e+00 | nan | nan | False | |
g09 | spectral | alpha | 1.0000e+00 | 0.000e+00 | nan | nan | True | ||
g09 | spatial | lon_0 | 9.0000e-01 | deg | 0.000e+00 | nan | nan | False | |
g09 | spatial | lat_0 | 1.0000e-01 | deg | 0.000e+00 | -9.000e+01 | 9.000e+01 | False | |
g09-bkg | spectral | norm | 1.0000e+00 | 0.000e+00 | nan | nan | False | ||
g09-bkg | spectral | tilt | 0.0000e+00 | 0.000e+00 | nan | nan | True | ||
g09-bkg | spectral | reference | 1.0000e+00 | TeV | 0.000e+00 | nan | nan | True |