Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 42 additions & 126 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,152 +1,68 @@
name: Docs
on: [release]
on:
push:
branches:
- main
- dev
tags:
- "v*"
pull_request:
branches:
- main
- dev
release:

permissions:
contents: write # 'write' access to repository contents
pull-requests: write # 'write' access to pull requests

jobs:
notebooks:
build-notebooks:
name: "Build the notebooks for the docs"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Set SHA
run: |
SHA="${{ github.event.pull_request.head.sha || github.sha }}"
echo "SHA=$SHA" >> "$GITHUB_ENV"
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: 3.11
- uses: conda-incubator/setup-miniconda@v4
with:
auto-activate-base: true
activate-environment: ""
auto-update-conda: true
python-version: ${{ matrix.python-version }}
channels: conda-forge, threeml, defaults

python-version: 3.13
- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel
pip install numpy==2.1
pip install matplotlib==3.8.4
pip install black
pip install ipython jupyter cython astropy
conda install jupytext jupyterthemes emcee pymultinest ultranest nbconvert ipykernel astropy regions threeML astromodels gammapy
pip install .
pip install black ultranest
pip install -e .
pip install -r docs/requirements.txt

- name: Execute the notebooks
shell: bash -l {0}
run: |
pip install .
export GAMMAPY_DATA=$HOME/data
export GAMMAPY_DATA="/home/runner/work/gammapy_data"
mkdir -p $GAMMAPY_DATA
git clone https://github.com/gammapy/gammapy-data.git $GAMMAPY_DATA
jupytext --to ipynb --pipe black --execute docs/md/*.md
jupytext --to ipynb --pipe black --execute docs/md_docs/*.md
mkdir -p docs/notebooks
mv docs/md/*.ipynb docs/notebooks

mv docs/md_docs/*.ipynb docs/notebooks
echo "Next - uploading artifacts for sha ${SHA}"
- uses: actions/upload-artifact@v6
with:
name: notebooks-for-${{ github.sha }}
name: notebooks-for-${{ env.SHA }}
path: docs/notebooks

api_doc:
name: "Create the API stubs"
runs-on: macos-latest
steps:
- uses: actions/checkout@v6
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
fetch-depth: 0 # otherwise, you will failed to push refs to dest repo

- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: 3.11

- name: Build the API doc
run: |

brew install c-blosc
brew install hdf5

pip3 install --upgrade pip
pip3 install cython
pip3 install numpy==2.1
pip3 install --upgrade scipy numba astropy
pip3 install matplotlib==3.8.4
pip3 install .

brew install sphinx-doc pandoc

pip3 install wheel
pip3 install mock recommonmark
pip3 install sphinx_rtd_theme
pip3 install -U sphinx nbsphinx sphinx-gallery



sphinx-apidoc -f -o docs/api/ gammapy_plugin

- uses: actions/upload-artifact@v6
with:
name: api-stubs-for-${{ github.sha }}
path: docs/api

build_docs:
name: "Build the Documentation"
runs-on: macos-latest
needs: [notebooks, api_doc]
steps:
- uses: actions/checkout@v6
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
fetch-depth: 0 # otherwise, you will failed to push refs to dest repo

- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: 3.11

- name: Install package
- name: Trigger RTD
shell: bash -l {0}
run: |
curl -X POST \
"https://app.readthedocs.org/api/v3/projects/gammapy-plugin/versions/${PR_NUMBER}/builds/" \
-H "Authorization: Token $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"branch\": \"${BRANCH}\"
}"

brew install c-blosc
brew install hdf5
pip3 install numpy scipy numba astropy matplotlib==3.8.4
brew install sphinx-doc pandoc

pip3 install wheel
pip3 install mock recommonmark
pip3 install sphinx_rtd_theme
pip3 install -U sphinx nbsphinx sphinx-gallery
pip3 install threeML
pip3 install --upgrade git+https://github.com/threeml/astromodels.git
pip3 install --upgrade git+https://github.com/threeml/threeML.git
pip3 install --upgrade gammapy --use-pep517


pip3 install -e .

rm -rf docs/md/*
env:
ISDEV: ${{contains(github.rev,'dev') || contains(github.base_ref, 'dev')}}

- uses: actions/download-artifact@master
with:
name: notebooks-for-${{ github.sha }}
path: docs/notebooks

- uses: actions/download-artifact@master
with:
name: api-stubs-for-${{ github.sha }}
path: docs/notebooks/api

- name: Build and Commit
uses: sphinx-notes/pages@master
with:
documentation_path: docs
sphinx_version: 5.1.1
requirements_path: docs/requirements.txt

- name: Push changes
if: github.event_name == 'push'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: gh-pages
TOKEN: ${{ secrets.RTD_API }}
BRANCH: ${{ github.event.pull_request.head.ref || github.ref_name }}
PR_NUMBER: ${{ github.event.number || 'latest' }}
18 changes: 7 additions & 11 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
# Read the Docs configuration file for Sphinx projects
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
os: ubuntu-24.04
tools:
python: "3.12"
# You can also specify other tool versions:
# nodejs: "20"
# rust: "1.70"
# golang: "1.20"
jobs:
pre_build:
- sphinx-apidoc . -o docs/api
- bash docs/download_notebooks.sh


# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/conf.py
fail_on_warning: false

# Optionally build your docs in additional formats such as PDF and ePub
# formats:
# - pdf
# - epub

python:
install:
- requirements: docs/requirements.txt
- method: pip
path: .
extra_requirements:
- docs

63 changes: 63 additions & 0 deletions docs/download_notebooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash
set -euo pipefail

OWNER="threeML"
REPO="gammapy-plugin"
SHA="${READTHEDOCS_GIT_COMMIT_HASH}"
TOKEN="${GITHUB_TOKEN}"

API="https://api.github.com/repos/$OWNER/$REPO"

echo "Searching artifacts for SHA: $SHA"

ARTIFACT_ID=$(
python3 - <<PY
import os, json, urllib.request

owner = "$OWNER"
repo = "$REPO"
sha = "$SHA"
token = "$TOKEN"

url = f"https://api.github.com/repos/{owner}/{repo}/actions/artifacts?per_page=100"

req = urllib.request.Request(url)
req.add_header("Accept", "application/vnd.github+json")
req.add_header("Authorization", f"Bearer {token}")

with urllib.request.urlopen(req) as r:
data = json.load(r)

for a in data.get("artifacts", []):
if a.get("expired"):
continue
name = a.get("name", "")
if sha in name:
print(a["id"])
break
PY
)

if [[ -z "$ARTIFACT_ID" ]]; then
echo "No artifact found for SHA: $SHA"
exit 1
fi

echo "Artifact ID: $ARTIFACT_ID"

echo "Downloading artifact..."

curl -fL \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/vnd.github+json" \
"$API/actions/artifacts/$ARTIFACT_ID/zip" \
-o artifact.zip

echo "Downloaded artifact.zip"

mkdir -p docs/notebooks
unzip -o artifact.zip -d docs/notebooks

rm artifact.zip

echo "Done unzipping"
2 changes: 1 addition & 1 deletion docs/md_docs/converter.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jupyter:
kernelspec:
display_name: threeML
language: python
name: threeml
name: python3

---
# Converting a astromodels Model
Expand Down
6 changes: 3 additions & 3 deletions docs/md_docs/crab_spectrum.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jupyter:
kernelspec:
display_name: threeML
language: python
name: threeml
name: python3

---
# Spectrum Fitting
Expand Down Expand Up @@ -148,8 +148,8 @@ gl.set_datasets(datasets)
gl.set_model(model, converted_model=conv)

ba = BayesianAnalysis(model, DataList(gl))
ba.set_sampler("multinest")
ba.sampler.setup(resume = False)
ba.set_sampler("ultranest")
ba.sampler.setup()
ba.sample()
res = ba.results
res
Expand Down
12 changes: 12 additions & 0 deletions gammapy_plugin/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def _update_parameters(self) -> None:
for name, source in self._converted_sources.items():
source._update_parameters()

def update(self) -> None:
"""Update all parameters of all SkyModels witht the current values from the
astromodels model. Public method for _update_parameters"""
self._update_parameters()

@property
def gammapy_models(self) -> list[SkyModel]:
"""
Expand Down Expand Up @@ -221,6 +226,13 @@ def _create_skymodel(self) -> None:
)
self._gather_mappings()

def update(self) -> None:
"""
This invokes the updating of all parameters of the converted model in the
gammapy SkyModels
"""
self._update_parameters()

@property
def skymodel(self) -> SkyModel:
"""Returns the Gammapy skymodel for this source.
Expand Down
Loading