A modern, standalone GUI application built with CustomTkinter for loading, visualizing, and fitting Gaussian peaks to spectrum data. This tool is specifically designed to work with data files from applications like the ESRF PyMca, supporting its common data structures.
- Modern Interface: Clean and theme-able UI built with
customtkinter. - Data Loading: Loads
.txtand.csvfiles, automatically detecting common delimiters (comma, tab, whitespace). - PyMca-Aware: Optimized for ESRF PyMca data. It correctly parses files with
Channel,Counts, andEnergycolumns. - Axis Selection: Toggle the plot's x-axis between Channel and Energy (if energy data is present).
- Interactive Plotting: Powered by Matplotlib, including navigation, zoom, and pan controls.
- Automatic Fitting:
- Uses
scipy.signal.find_peaksfor automatic peak detection. - Adjust peak-finding sensitivity with a "Auto Peak Height" slider.
- Performs a composite Gaussian fit for up to 10 peaks using
scipy.optimize.curve_fit.
- Uses
- Manual Fitting:
- A "Manual" mode opens a new window with sliders for all fit parameters (center, width, height, baseline).
- Visually see your manual adjustments update on the main plot in real-time.
- "Reset to Auto-Fit" button to discard manual changes.
- Export Results:
- "Show Results" button displays all fit parameters in a clean pop-up window.
- "Save Results" button exports the complete fit parameters and function definitions to a
.txtfile.
This tool is designed to be flexible but is optimized for data from PyMca.
- File Types:
.txtor.csv. - Delimiters: Automatically detects comma (
,), semicolon (;), tab, or whitespace. - Headers: Automatically attempts to skip a single header line if it contains non-numeric text.
- Column Structure: The script expects data in the following order:
Column 1: ChannelColumn 2: CountsLast Column: Energy
PS: While PyMca may label last column as Energy (eV), this tool can process data where this column is actually Wavelength (nm).
This script requires Python 3 and the libraries listed below.
-
Clone the repository:
git clone [https://github.com/ocakiroglu/XRay-Spectrum-Data-Fitting.git](https://github.com/ocakiroglu/XRay-Spectrum-Data-Fitting.git) cd XRay-Spectrum-Data-Fitting -
Create a
requirements.txtfile in the same directory with the following content:customtkinter numpy scipy matplotlib -
Set up your Python environment:
You can use either a standard
venvorconda.
# Create a virtual environment python -m venv venv # Activate it # On Windows: .\venv\Scripts\activate # On macOS/Linux: source venv/bin/activate # Install libraries pip install -r requirements.txt
# Create a new conda environment (e.g., named 'spectrum_fitter') # We recommend Python 3.13.5 or newer conda create --name spectrum_fitter python=3.13.5 # Activate the environment conda activate spectrum_fitter # Install libraries using pip (easiest way) pip install -r requirements.txt
(Note: Using
pip install -r requirements.txtwithin the active conda environment is the simplest way, ascustomtkinteris not on the default conda channels.)
Activate the environment and then follow this steps:
-
Run the application:
python gaussian_fit_on_spectrum_GUI_modern_v3.py
-
Basic Workflow:
- Click "Open File" to load your
.txtor.csvspectrum file. - Select your desired x-axis ("Energy" or "Channel") using the radio buttons.
- Define the area you want to fit by typing in the "Fit Range" boxes and pressing
Enteror clicking "Apply Range". - Adjust the "Auto Peak Height" slider until the "Number of Peaks" label shows the correct number of peaks for your selected range. The plot will update with an automatic fit.
- (Optional) Click the "Manual" button to open the slider window. Adjust the parameters as needed. Your changes will appear on the main plot.
- Click "Show Results" to view the final parameters or "Save Results" to export them to a text file.
- Click "Open File" to load your
This project is licensed under the MIT License. See the LICENSE file for details.
This code was developed with coding assistance from Google's Gemini and Github Copilot.
