SpecFit is designed to be a versatile spectrum-fitting tool. Utilizing the
Python package lmfit, this tool performs non-linear least-squares
minimization to accurately model astrophysical spectra. SpecFit streamlines
the process of deriving physical parameters such as blackbody temperature by
integrating an internal preprocessing pipeline. With the support of
pybind11 and C++, SpecFit allows for
the iteration of complex (non-vectorizable) models within lmfit, combining
Python's simplicity to perform data analysis with the computational efficiency
of C++.
Prior to installing SpecFit, it is important to note that some data is
necessary for some models:
- Free-free emission
To retrieve this data, after cloning this repository, run the './retrieve_data.py' script. Currently, the script downloads the following:
- Gaunt factor table from https://data.nublado.org/gauntff
The data will be downloaded and subsequently copied into the Python
site-packages SpecFit directory upon installation (the following step).
The Python bindings of the C++ modeling code must be built first before using
this package. The 'setup.py' and 'pyproject.toml' handle this automatically;
to install SpecFit, simply do so using:
python -m pip install path/to/SpecFitPerforming this step will automatically install its other Python dependencies, which are:
NumPySciPylmfitSpectrumCore(My core repository for spectrum operations)
See the examples in the 'SpecFit/examples' directory.
Using SpecFit is intended to be simple and uses the following general
approach:
-
Instantiate the
SpecFitobject by providing the file name (for simple data with wave/flux/error as the first three columns) or just aNumPyarray of the with wave/flux/error(optional) in the first 3 columns:from SpecFit import SpecFit fn = 'my_spectrum.dat' optional_read_params = { # ... } spec = SpecFit(fn, **optional_read_params)
-
Add submodels to the composite model to be processed by
lmfit. Custom functions may be passed, or strings corresponding to the available premade models in C++:# Adds a "blackbody" (Planck function) model spec.add_model('bb') # Adds a "free-free emission" model spec.add_model('free_free')
Default parameters are determined during this step for premade models; however, one may optionally provide initial parameters to
add_modelin the same manner as forlmfit. See examples for details. If providing a custom callable function toadd_model, providing initial guesses for parameters will be necessary. -
Fit the parameters of the provided model.
spec.fit()
This method essentially calls the
lmfit.Model.fit()method with appropriate arguments provided to it. However,SpecFit.fit()passes any additional keyword-arguments given to it directly to thelmfit.Model.fit()method (see examples). -
After fitting, data analysis can occur. Best-fit parameters can be viewed with
SpecFit.fit_report(), or a plot may be retrieved withSpecFit.plot(). See examples for more details.