[![CI - Python Package](https://github.com/Anselmoo/spectrafit/actions/workflows/python-ci.yml/badge.svg?branch=main)](https://github.com/Anselmoo/spectrafit/actions/workflows/python-ci.yml)
[![codecov](https://codecov.io/gh/Anselmoo/spectrafit/branch/main/graph/badge.svg?token=pNIMKwWsO2)](https://codecov.io/gh/Anselmoo/spectrafit)
[![PyPI](https://img.shields.io/pypi/v/spectrafit?logo=PyPi&logoColor=yellow)](https://pypi.org/project/spectrafit/)
[![Conda](https://img.shields.io/conda/v/conda-forge/spectrafit?label=Anaconda.org&logo=anaconda)](https://github.com/conda-forge/spectrafit-feedstock)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/spectrafit?color=gree&logo=Python&logoColor=yellow)](https://pypi.org/project/spectrafit/)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Anselmoo/spectrafit/main.svg)](https://results.pre-commit.ci/latest/github/Anselmoo/spectrafit/main)
[![doi](https://img.shields.io/badge/10.1021/acsomega.3c09262-blue?logo=DOI&logoColor=white)](https://pubs.acs.org/doi/full/10.1021/acsomega.3c09262)
<p align="center">
<img src="https://github.com/Anselmoo/spectrafit/blob/c5f7ee05e5610fb8ef4e237a88f62977b6f832e5/docs/images/spectrafit_synopsis.png?raw=true">
</p>
# SpectraFit
---
> Data Analysis Tool for All Kinds of Spectra
`SpectraFit` is a Python tool for quick data fitting based on the regular
expression of distribution and linear functions via the command line (CMD) or
[Jupyter Notebook](https://jupyter.org) It is designed to be easy to use and
supports all common ASCII data formats. SpectraFit runs on **Linux**,
**Windows**, and **MacOS**.
## Scope
- Fitting of 2D data, also with multiple columns as _global fitting_
- Using established and advanced solver methods
- Extensibility of the fitting function
- Guarantee traceability of the fitting results
- Saving all results in a _SQL-like-format_ (`CSV`) for publications
- Saving all results in a _NoSQL-like-format_ (`JSON`) for project management
- Having an API interface for Graph-databases
`SpectraFit` is a tool designed for researchers and scientists who require
immediate data fitting to a model. It proves to be especially beneficial for
individuals working with vast datasets or who need to conduct numerous fits
within a limited time frame. `SpectraFit's` adaptability to various platforms
and data formats makes it a versatile tool that caters to a broad spectrum of
scientific applications.
## Installation
via pip:
```bash
pip install spectrafit
# with support for Jupyter Notebook
pip install spectrafit[jupyter]
# with support for the dashboard in the Jupyter Notebook
pip install spectrafit[jupyter-dash]
# with support to visualize pkl-files as graph
pip install spectrafit[graph]
# with all upcomming features
pip install spectrafit[all]
# Upgrade
pip install spectrafit --upgrade
```
via conda, see also
[conda-forge](https://github.com/conda-forge/spectrafit-feedstock):
```bash
conda install -c conda-forge spectrafit
# with support for Jupyter Notebook
conda install -c conda-forge spectrafit-jupyter
# with all upcomming features
conda install -c conda-forge spectrafit-all
```
## Usage
`SpectraFit` needs as command line tool only two things:
1. The reference data, which should be fitted.
2. The input file, which contains the initial model.
As model files [json](https://en.wikipedia.org/wiki/JSON),
[toml](https://en.wikipedia.org/wiki/TOML), and
[yaml](https://en.wikipedia.org/wiki/YAML) are supported. By making use of the
python `**kwargs` feature, the input file can call most of the following
functions of [LMFIT](https://lmfit.github.io/lmfit-py/index.html). LMFIT is the
workhorse for the fit optimization, which is macro wrapper based on:
1. [NumPy](https://www.numpy.org/)
2. [SciPy](https://www.scipy.org/)
3. [uncertainties](https://pythonhosted.org/uncertainties/)
In case of `SpectraFit`, we have further extend the package by:
1. [Pandas](https://pandas.pydata.org/)
2. [statsmodels](https://www.statsmodels.org/stable/index.html)
3. [numdifftools](https://github.com/pbrod/numdifftools)
4. [Matplotlib](https://matplotlib.org/) in combination with
[Seaborn](https://seaborn.pydata.org/)
```bash
spectrafit data_file.txt -i input_file.json
```
```bash
usage: spectrafit [-h] [-o OUTFILE] [-i INPUT] [-ov] [-e0 ENERGY_START]
[-e1 ENERGY_STOP] [-s SMOOTH] [-sh SHIFT] [-c COLUMN COLUMN]
[-sep { ,,,;,:,|, ,s+}] [-dec {.,,}] [-hd HEADER]
[-g {0,1,2}] [-auto] [-np] [-v] [-vb {0,1,2}]
infile
Fast Fitting Program for ascii txt files.
positional arguments:
infile Filename of the spectra data
optional arguments:
-h, --help show this help message and exit
-o OUTFILE, --outfile OUTFILE
Filename for the export, default to set to
'spectrafit_results'.
-i INPUT, --input INPUT
Filename for the input parameter, default to set to
'fitting_input.toml'.Supported fileformats are:
'*.json', '*.yml', '*.yaml', and '*.toml'
-ov, --oversampling Oversampling the spectra by using factor of 5;
default to False.
-e0 ENERGY_START, --energy_start ENERGY_START
Starting energy in eV; default to start of energy.
-e1 ENERGY_STOP, --energy_stop ENERGY_STOP
Ending energy in eV; default to end of energy.
-s SMOOTH, --smooth SMOOTH
Number of smooth points for lmfit; default to 0.
-sh SHIFT, --shift SHIFT
Constant applied energy shift; default to 0.0.
-c COLUMN COLUMN, --column COLUMN COLUMN
Selected columns for the energy- and intensity-values;
default to '0' for energy (x-axis) and '1' for intensity
(y-axis). In case of working with header, the column
should be set to the column names as 'str'; default
to 0 and 1.
-sep { ,,,;,:,|, ,s+}, --separator { ,,,;,:,|, ,s+}
Redefine the type of separator; default to ' '.
-dec {.,,}, --decimal {.,,}
Type of decimal separator; default to '.'.
-hd HEADER, --header HEADER
Selected the header for the dataframe; default to None.
-cm COMMENT, --comment COMMENT
Lines with comment characters like '#' should not be
parsed; default to None.
-g {0,1,2}, --global_ {0,1,2}
Perform a global fit over the complete dataframe. The
options are '0' for classic fit (default). The
option '1' for global fitting with auto-definition
of the peaks depending on the column size and '2'
for self-defined global fitting routines.
-auto, --autopeak Auto detection of peaks in the spectra based on `SciPy`.
The position, height, and width are used as estimation
for the `Gaussian` models.The default option is 'False'
for manual peak definition.
-np, --noplot No plotting the spectra and the fit of `SpectraFit`.
-v, --version Display the current version of `SpectraFit`.
-vb {0,1,2}, --verbose {0,1,2}
Display the initial configuration parameters and fit
results, as a table '1', as a dictionary '2', or not in
the terminal '0'. The default option is set to 1 for
table `printout`.
```
### Jupyter Notebook
Open the `Jupyter Notebook` and run the following code:
```bash
spectrafit-jupyter
```
or via Docker Image for `<cpu>` with `amd64` and `arm64`:
```bash
docker pull ghcr.io/anselmoo/spectrafit-<cpu>:latest
docker run -it -p 8888:8888 spectrafit-<cpu>:latest
```
or just:
```bash
docker run -p 8888:8888 ghcr.io/anselmoo/spectrafit-<cpu>:latest
```
Next define your initial model and the reference data:
```python
from spectrafit.plugins.notebook import SpectraFitNotebook
import pandas as pd
df = pd.read_csv(
"https://raw.githubusercontent.com/Anselmoo/spectrafit/main/Examples/data.csv"
)
initial_model = [
{
"pseudovoigt": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 1},
"center": {"max": 2, "min": -2, "vary": True, "value": 0},
"fwhmg": {"max": 0.4, "min": 0.1, "vary": True, "value": 0.21},
"fwhml": {"max": 0.4, "min": 0.1, "vary": True, "value": 0.21},
}
},
{
"pseudovoigt": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 1},
"center": {"max": 2, "min": -2, "vary": True, "value": 1},
"fwhmg": {"max": 0.4, "min": 0.1, "vary": True, "value": 0.21},
"fwhml": {"max": 0.4, "min": 0.1, "vary": True, "value": 0.21},
}
},
{
"pseudovoigt": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 1},
"center": {"max": 2, "min": -2, "vary": True, "value": 1},
"fwhmg": {"max": 0.4, "min": 0.1, "vary": True, "value": 0.21},
"fwhml": {"max": 0.4, "min": 0.1, "vary": True, "value": 0.21},
}
},
]
spf = SpectraFitNotebook(df=df, x_column="Energy", y_column="Noisy")
spf.solver_model(initial_model)
```
Which results in the following output:
![img_jupyter](https://github.com/Anselmoo/spectrafit/blob/8962a277b0c3d2aa05970617f0ac323a07de2fec/docs/images/jupyter_plot.png?raw=true)
## Documentation
Please see the [extended documentation](https://anselmoo.github.io/spectrafit/)
for the full usage of `SpectraFit`.
The documentation is generated by
<a href="https://squidfunk.github.io/mkdocs-material/">
<img src="https://img.shields.io/badge/Material_for_MkDocs-526CFE?style=for-the-badge&logo=MaterialForMkDocs&logoColor=white" alt="Built with Material for MkDocs" style="vertical-align: middle; height: 20px;">
</a>.
Raw data
{
"_id": null,
"home_page": "https://pypi.org/project/spectrafit/",
"name": "SpectraFit",
"maintainer": "Anselm Hahn",
"docs_url": null,
"requires_python": "<3.13,>=3.8",
"maintainer_email": "anselm.hahn@gmail.com",
"keywords": "2D-Spectra, 3D-Spectra, fitting, curve-fitting, peak-fitting, spectrum",
"author": "Anselm Hahn",
"author_email": "anselm.hahn@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/09/ce/9f6fac950212a819472175507b7c2e48c018958cb078a4d127887c7fd332/spectrafit-1.1.0.tar.gz",
"platform": null,
"description": "[![CI - Python Package](https://github.com/Anselmoo/spectrafit/actions/workflows/python-ci.yml/badge.svg?branch=main)](https://github.com/Anselmoo/spectrafit/actions/workflows/python-ci.yml)\n[![codecov](https://codecov.io/gh/Anselmoo/spectrafit/branch/main/graph/badge.svg?token=pNIMKwWsO2)](https://codecov.io/gh/Anselmoo/spectrafit)\n[![PyPI](https://img.shields.io/pypi/v/spectrafit?logo=PyPi&logoColor=yellow)](https://pypi.org/project/spectrafit/)\n[![Conda](https://img.shields.io/conda/v/conda-forge/spectrafit?label=Anaconda.org&logo=anaconda)](https://github.com/conda-forge/spectrafit-feedstock)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/spectrafit?color=gree&logo=Python&logoColor=yellow)](https://pypi.org/project/spectrafit/)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Anselmoo/spectrafit/main.svg)](https://results.pre-commit.ci/latest/github/Anselmoo/spectrafit/main)\n[![doi](https://img.shields.io/badge/10.1021/acsomega.3c09262-blue?logo=DOI&logoColor=white)](https://pubs.acs.org/doi/full/10.1021/acsomega.3c09262)\n\n<p align=\"center\">\n<img src=\"https://github.com/Anselmoo/spectrafit/blob/c5f7ee05e5610fb8ef4e237a88f62977b6f832e5/docs/images/spectrafit_synopsis.png?raw=true\">\n</p>\n\n# SpectraFit\n\n---\n\n> Data Analysis Tool for All Kinds of Spectra\n\n`SpectraFit` is a Python tool for quick data fitting based on the regular\nexpression of distribution and linear functions via the command line (CMD) or\n[Jupyter Notebook](https://jupyter.org) It is designed to be easy to use and\nsupports all common ASCII data formats. SpectraFit runs on **Linux**,\n**Windows**, and **MacOS**.\n\n## Scope\n\n- Fitting of 2D data, also with multiple columns as _global fitting_\n- Using established and advanced solver methods\n- Extensibility of the fitting function\n- Guarantee traceability of the fitting results\n- Saving all results in a _SQL-like-format_ (`CSV`) for publications\n- Saving all results in a _NoSQL-like-format_ (`JSON`) for project management\n- Having an API interface for Graph-databases\n\n`SpectraFit` is a tool designed for researchers and scientists who require\nimmediate data fitting to a model. It proves to be especially beneficial for\nindividuals working with vast datasets or who need to conduct numerous fits\nwithin a limited time frame. `SpectraFit's` adaptability to various platforms\nand data formats makes it a versatile tool that caters to a broad spectrum of\nscientific applications.\n\n## Installation\n\nvia pip:\n\n```bash\npip install spectrafit\n\n# with support for Jupyter Notebook\n\npip install spectrafit[jupyter]\n\n# with support for the dashboard in the Jupyter Notebook\n\npip install spectrafit[jupyter-dash]\n\n# with support to visualize pkl-files as graph\n\npip install spectrafit[graph]\n\n# with all upcomming features\n\npip install spectrafit[all]\n\n# Upgrade\n\npip install spectrafit --upgrade\n```\n\nvia conda, see also\n[conda-forge](https://github.com/conda-forge/spectrafit-feedstock):\n\n```bash\nconda install -c conda-forge spectrafit\n\n# with support for Jupyter Notebook\n\nconda install -c conda-forge spectrafit-jupyter\n\n# with all upcomming features\n\nconda install -c conda-forge spectrafit-all\n```\n\n## Usage\n\n`SpectraFit` needs as command line tool only two things:\n\n1. The reference data, which should be fitted.\n2. The input file, which contains the initial model.\n\nAs model files [json](https://en.wikipedia.org/wiki/JSON),\n[toml](https://en.wikipedia.org/wiki/TOML), and\n[yaml](https://en.wikipedia.org/wiki/YAML) are supported. By making use of the\npython `**kwargs` feature, the input file can call most of the following\nfunctions of [LMFIT](https://lmfit.github.io/lmfit-py/index.html). LMFIT is the\nworkhorse for the fit optimization, which is macro wrapper based on:\n\n1. [NumPy](https://www.numpy.org/)\n2. [SciPy](https://www.scipy.org/)\n3. [uncertainties](https://pythonhosted.org/uncertainties/)\n\nIn case of `SpectraFit`, we have further extend the package by:\n\n1. [Pandas](https://pandas.pydata.org/)\n2. [statsmodels](https://www.statsmodels.org/stable/index.html)\n3. [numdifftools](https://github.com/pbrod/numdifftools)\n4. [Matplotlib](https://matplotlib.org/) in combination with\n [Seaborn](https://seaborn.pydata.org/)\n\n```bash\nspectrafit data_file.txt -i input_file.json\n```\n\n```bash\nusage: spectrafit [-h] [-o OUTFILE] [-i INPUT] [-ov] [-e0 ENERGY_START]\n [-e1 ENERGY_STOP] [-s SMOOTH] [-sh SHIFT] [-c COLUMN COLUMN]\n [-sep { ,,,;,:,|, ,s+}] [-dec {.,,}] [-hd HEADER]\n [-g {0,1,2}] [-auto] [-np] [-v] [-vb {0,1,2}]\n infile\n\nFast Fitting Program for ascii txt files.\n\npositional arguments:\n infile Filename of the spectra data\n\noptional arguments:\n -h, --help show this help message and exit\n -o OUTFILE, --outfile OUTFILE\n Filename for the export, default to set to\n 'spectrafit_results'.\n -i INPUT, --input INPUT\n Filename for the input parameter, default to set to\n 'fitting_input.toml'.Supported fileformats are:\n '*.json', '*.yml', '*.yaml', and '*.toml'\n -ov, --oversampling Oversampling the spectra by using factor of 5;\n default to False.\n -e0 ENERGY_START, --energy_start ENERGY_START\n Starting energy in eV; default to start of energy.\n -e1 ENERGY_STOP, --energy_stop ENERGY_STOP\n Ending energy in eV; default to end of energy.\n -s SMOOTH, --smooth SMOOTH\n Number of smooth points for lmfit; default to 0.\n -sh SHIFT, --shift SHIFT\n Constant applied energy shift; default to 0.0.\n -c COLUMN COLUMN, --column COLUMN COLUMN\n Selected columns for the energy- and intensity-values;\n default to '0' for energy (x-axis) and '1' for intensity\n (y-axis). In case of working with header, the column\n should be set to the column names as 'str'; default\n to 0 and 1.\n -sep { ,,,;,:,|, ,s+}, --separator { ,,,;,:,|, ,s+}\n Redefine the type of separator; default to ' '.\n -dec {.,,}, --decimal {.,,}\n Type of decimal separator; default to '.'.\n -hd HEADER, --header HEADER\n Selected the header for the dataframe; default to None.\n -cm COMMENT, --comment COMMENT\n Lines with comment characters like '#' should not be\n parsed; default to None.\n -g {0,1,2}, --global_ {0,1,2}\n Perform a global fit over the complete dataframe. The\n options are '0' for classic fit (default). The\n option '1' for global fitting with auto-definition\n of the peaks depending on the column size and '2'\n for self-defined global fitting routines.\n -auto, --autopeak Auto detection of peaks in the spectra based on `SciPy`.\n The position, height, and width are used as estimation\n for the `Gaussian` models.The default option is 'False'\n for manual peak definition.\n -np, --noplot No plotting the spectra and the fit of `SpectraFit`.\n -v, --version Display the current version of `SpectraFit`.\n -vb {0,1,2}, --verbose {0,1,2}\n Display the initial configuration parameters and fit\n results, as a table '1', as a dictionary '2', or not in\n the terminal '0'. The default option is set to 1 for\n table `printout`.\n```\n\n### Jupyter Notebook\n\nOpen the `Jupyter Notebook` and run the following code:\n\n```bash\nspectrafit-jupyter\n```\n\nor via Docker Image for `<cpu>` with `amd64` and `arm64`:\n\n```bash\ndocker pull ghcr.io/anselmoo/spectrafit-<cpu>:latest\ndocker run -it -p 8888:8888 spectrafit-<cpu>:latest\n```\n\nor just:\n\n```bash\ndocker run -p 8888:8888 ghcr.io/anselmoo/spectrafit-<cpu>:latest\n```\n\nNext define your initial model and the reference data:\n\n```python\nfrom spectrafit.plugins.notebook import SpectraFitNotebook\nimport pandas as pd\n\ndf = pd.read_csv(\n \"https://raw.githubusercontent.com/Anselmoo/spectrafit/main/Examples/data.csv\"\n)\n\ninitial_model = [\n {\n \"pseudovoigt\": {\n \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 1},\n \"center\": {\"max\": 2, \"min\": -2, \"vary\": True, \"value\": 0},\n \"fwhmg\": {\"max\": 0.4, \"min\": 0.1, \"vary\": True, \"value\": 0.21},\n \"fwhml\": {\"max\": 0.4, \"min\": 0.1, \"vary\": True, \"value\": 0.21},\n }\n },\n {\n \"pseudovoigt\": {\n \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 1},\n \"center\": {\"max\": 2, \"min\": -2, \"vary\": True, \"value\": 1},\n \"fwhmg\": {\"max\": 0.4, \"min\": 0.1, \"vary\": True, \"value\": 0.21},\n \"fwhml\": {\"max\": 0.4, \"min\": 0.1, \"vary\": True, \"value\": 0.21},\n }\n },\n {\n \"pseudovoigt\": {\n \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 1},\n \"center\": {\"max\": 2, \"min\": -2, \"vary\": True, \"value\": 1},\n \"fwhmg\": {\"max\": 0.4, \"min\": 0.1, \"vary\": True, \"value\": 0.21},\n \"fwhml\": {\"max\": 0.4, \"min\": 0.1, \"vary\": True, \"value\": 0.21},\n }\n },\n]\nspf = SpectraFitNotebook(df=df, x_column=\"Energy\", y_column=\"Noisy\")\nspf.solver_model(initial_model)\n```\n\nWhich results in the following output:\n\n![img_jupyter](https://github.com/Anselmoo/spectrafit/blob/8962a277b0c3d2aa05970617f0ac323a07de2fec/docs/images/jupyter_plot.png?raw=true)\n\n## Documentation\n\nPlease see the [extended documentation](https://anselmoo.github.io/spectrafit/)\nfor the full usage of `SpectraFit`.\n\nThe documentation is generated by\n<a href=\"https://squidfunk.github.io/mkdocs-material/\">\n<img src=\"https://img.shields.io/badge/Material_for_MkDocs-526CFE?style=for-the-badge&logo=MaterialForMkDocs&logoColor=white\" alt=\"Built with Material for MkDocs\" style=\"vertical-align: middle; height: 20px;\">\n</a>.\n\n",
"bugtrack_url": null,
"license": "BSD-3-Clause",
"summary": "Fast fitting of 2D- and 3D-Spectra with established routines",
"version": "1.1.0",
"project_urls": {
"Documentation": "https://anselmoo.github.io/spectrafit/",
"Homepage": "https://pypi.org/project/spectrafit/",
"Repository": "https://github.com/Anselmoo/spectrafit"
},
"split_keywords": [
"2d-spectra",
" 3d-spectra",
" fitting",
" curve-fitting",
" peak-fitting",
" spectrum"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f3d32a12ce20073b1caf20316a2ad1a745d307669bea98f8063a555df31e1f71",
"md5": "610fd40e7597a7d6eb00a839499ada34",
"sha256": "3e6b3e12b2abb1af08485a91a101eee47ceaa14ef26b2c9696b3d8c388e6cdcf"
},
"downloads": -1,
"filename": "spectrafit-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "610fd40e7597a7d6eb00a839499ada34",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.13,>=3.8",
"size": 96374,
"upload_time": "2024-11-24T14:02:57",
"upload_time_iso_8601": "2024-11-24T14:02:57.299693Z",
"url": "https://files.pythonhosted.org/packages/f3/d3/2a12ce20073b1caf20316a2ad1a745d307669bea98f8063a555df31e1f71/spectrafit-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "09ce9f6fac950212a819472175507b7c2e48c018958cb078a4d127887c7fd332",
"md5": "3a43b28f391ac1662bfd6738a88b0d56",
"sha256": "8e46900e7c9926cbc27e96925338e32bbdd6c51cb46501d0ae2b4765688c028c"
},
"downloads": -1,
"filename": "spectrafit-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "3a43b28f391ac1662bfd6738a88b0d56",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.13,>=3.8",
"size": 88828,
"upload_time": "2024-11-24T14:02:58",
"upload_time_iso_8601": "2024-11-24T14:02:58.459705Z",
"url": "https://files.pythonhosted.org/packages/09/ce/9f6fac950212a819472175507b7c2e48c018958cb078a4d127887c7fd332/spectrafit-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-24 14:02:58",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Anselmoo",
"github_project": "spectrafit",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "spectrafit"
}