Galaxy Spectrum Fitting
GalSpec is a Python package for fitting astronomical spectra, specifically designed for AGN and galaxy spectra with complex emission and absorption line features.
- Astropy Integration: All models inherit from Astropy's modeling framework
- Multiple Fitting Methods: LSQ, MCMC (emcee), and nested sampling (dynesty)
- Comprehensive Line Database: Built-in UV/optical emission lines
- Template Support: Fe II and stellar population templates
- Flexible Model Composition: Easily combine continuum, line, and template components
Full documentation is available at: https://jyshangguan.github.io/GalSpec/
The documentation includes:
- Installation instructions
- Quick start tutorial with real SDSS data
- API reference for all modules
- Examples for continuum fitting, emission lines, absorption lines, and advanced techniques
Install from source:
git clone https://github.com/jyshangguan/GalSpec.git
cd GalSpec
pip install -e .GalSpec includes a Claude Code skill that allows you to use GalSpec directly within Claude Code for interactive spectral fitting assistance.
To install the skill:
# From the GalSpec repository root
./install_skill.shThis will:
- Create a symlink from
~/.claude/skills/galspecto theskills/directory - Enable GalSpec commands in Claude Code
To use the skill in Claude Code:
/skills
Use GalSpec to fit the spectrum
To uninstall:
rm ~/.claude/skills/galspecimport numpy as np
from astropy.modeling.models import Linear1D
from galspec import Line_Gaussian
from astropy.modeling.fitting import LevMarLSQFitter
# Load your spectrum
wave = np.linspace(6400, 6700, 1000)
flux = np.random.randn(len(wave)) * 0.1 # Your data here
# Define model: continuum + Hα line
continuum = Linear1D(slope=0.0, intercept=1.0)
halpha = Line_Gaussian(amplitude=5.0, dv=0, sigma=200, wavec=6563)
model = continuum + halpha
# Fit
fitter = LevMarLSQFitter()
fitted_model = fitter(model, wave, flux)
print(f"Hα amplitude: {fitted_model[1].amplitude.value:.3f}")The example/ directory contains detailed notebooks demonstrating:
- Continuum fitting with stellar templates
- Emission line fitting for AGN
- Absorption line and BAL fitting
- Physical parameter estimation
- Python ≥ 3.9
- numpy, scipy, matplotlib, astropy, pandas
- emcee, dynesty (for Bayesian fitting)
Note: When using ReadSpectrum class for extinction correction, SFD dust map data (4 FITS files, ~167MB total) will be automatically downloaded from kbarbary/sfddata on first use.
MIT License - see LICENSE file for details
- Documentation: https://jyshangguan.github.io/GalSpec/
- GitHub Repository: https://github.com/jyshangguan/GalSpec
- Issue Tracker: https://github.com/jyshangguan/GalSpec/issues