Skip to content

jyangch/bayspec_app

Repository files navigation

BaySpec App

A Bayesian spectral fitting workbench for high-energy astrophysics.
OGIP spectra in. Composed models. Posterior out. All in your browser.

Live demo on Hugging Face Spaces Powered by bayspec Python 3.12+ License GPL-3.0


BaySpec App is a browser front-end for the bayspec library — drop in OGIP spectra, compose models from several component libraries, and run a Bayesian sampler or maximum-likelihood optimizer without writing a line of glue code. Inference progress streams back live over SSE, and a whole session export/imports as a single JSON.

Try it nowhuggingface.co/spaces/jyangch/bayspec

Highlights

  • Drag-in OGIP spectra. Source / background / response triplets parse on upload; metadata, counts, exposure, and effective area surface immediately.
  • Four component libraries. Local (bayspec), astromodels, Xspec (when available), and your own Python functions registered live from the Model page. Compose with simple algebra: tbabs * cpl.
  • Auto-paired inference. Every Data ↔ Model binding becomes a fitting pair. Tweak, freeze, or link parameters across pairs.
  • Four inference backends. Two Bayesian samplers — emcee (MCMC) and MultiNest (nested sampling) — plus two maximum-likelihood optimisers (lmfit and iminuit) for quick checks. Progress streams over SSE.
  • Recent runs at a glance. Last 3 inferences as chips in the Inference panel, with a stale-binding warning if the displayed run no longer matches the current setup.
  • Compare runs. Side-by-side best-fit ± 1σ across history entries, CSV-exportable.
  • Save & resume. Bundle every UI choice and parameter into a portable JSON file. FITS uploads stay local — re-attach them after import.
  • Reset, never restart. Wipe Data / Models / Pairs / Inference in one click; custom-model registrations survive.
  • Workflow at a glance. A five-stage Data → Model → Pairs → Inference → Analyzer indicator in the sidebar tracks where you are.

Quickstart

Requires Python 3.12+.

git clone https://github.com/jyangch/bayspec_app.git
cd bayspec_app

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

uvicorn main:app --reload

Open http://127.0.0.1:8000 and follow the workflow: Data (upload spectra) → Model (pick or compose components, bind to a Data container) → Inference (configure sampler, run, inspect posterior).

Optional components

  • Xspec model library — the XSPEC tab activates only if xspec is importable in your Python environment.
  • MultiNest sampler — requires pymultinest; without it, emcee, lmfit, and iminuit remain available.

Run on Hugging Face Spaces

A live, public deployment lives at https://huggingface.co/spaces/jyangch/bayspec. The Space is a Docker SDK build of this repo — the included Dockerfile plus a small YAML header on the Space-side README are everything HF needs to build and serve the app.

The HF free tier resets the filesystem on container restart, so uploaded FITS files and exported fits are lost between sleeps. Enable Persistent Storage in the Space settings if that matters.

Under the hood

main.py              FastAPI app — every route, helper, and SSE worker
state.py             Per-session in-memory state store (cookie-keyed)
templates/           Jinja2 templates (base.html + one per page + partials)
static/style.css     UI styles (design tokens, components, layout)
static/app.js        Tiny client-side glue (HTMX is the main driver)
static/logo.svg      The badge above
docs/                Internal conventions (docstring standard, etc.)

A single FastAPI process, no JS framework — HTMX swaps server-rendered fragments into the page. All session data lives in a per-process dict keyed by a cookie; restarting the server clears every session.

Citation and upstream

BaySpec App is a front-end. The fitting machinery lives in the bayspec library — please cite that project when publishing results.

License

GPL-3.0 — see LICENSE.

About

An application of BaySpec implemented using streamlit

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors