Python scripts for quickly analyzing numerous spectra with Fityk


Iterative Spectral Fitting with Fityk and Python

These are a series of python 2.7 scripts that make use of the wonderful fitting software, Fityk, which has an available 0.9.8 version for free, written by Marcin Wojdyr. These scripts are compatible with Fityk 0.9.8.

The scripts will assist in fitting and analyzing large amounts of spectra acquired from the same sample or something that where the expected Spectrum changes gradually, such as a linescane or a temperature-dependent experiment.

This python script will generate a text file that can be executed as a script by Fityk which will open up, fit, and output parameters of a series of space delimited spectra. This generated text file will be of the form

The first spectrum in the series needs to be manually fit initially using Fityk, to create the desired peaks, and to determine the appropriate background, if any, that should be subtracted from each spectrum.

The generated Fityk script will create a space delimited text file that contains all the peak parameters and uncertainties for each spectrum. This output txt file can be parsed using the script.

This python script will parse the peak data output by Fityk when running the generated Fityk script. It will output individual space delimited txt files for each peak. It will also create graphs for the center, FWHM, intensity, and calibrated intensity.

Version 2 does not include the graphic capabilities, and thus does not require some of the additional python packages necessary for plotting.

Detailed Instructions

These instructions will detail, in rough order, the steps needed to complete the iterative spectral fitting with Fityk and these Python scripts.

File naming convention

Data files should be space- or comma-delimted txt files in the following format:

spectra_name 000001.txt

There should be a space between the filename and the numeric index as Python uses the space for parsing the filenames, though this can obviously be changed to another character by editing the python code. Also, there should probably not be any spaces elsewhere in the filename, try to use underscores instead of spaces.

Set up the inital fit using Fityk

Note: If the spectral series is similar to a previous dataset that has already been analyzed using this technique, much of the steps can be replaced by simply opening the generated file in a text editor and copy/pasting lines 2-5 of that script into a previous saved .fit session from the previous series, after loading in the initial spectrum. Lines 2-5 will read similar to

A = (x > 450 and x < 600)
%bg0 = Spline(argmin(y if x > 450 and x < 600), min(y if x > 450 and x < 600)
Y = y - %bg0(x)
plot [400:650] []

Open and load data

Open up the Fityk program and load in the first spectrum from your data set (Data > Load File).

Set active region of dataset

Determine the active region of the spectrum that needs to be fit. Click on the Data-Range Mode icon and then right click and drag to unselect regions of the spectrum. Left clicking and dragging will re-select data points. The exact data range can be set by entering in the following command into Fityk's command line (located at the bottom of the main Fityk UI).

A = (x > 200 and x < 600)


If a new background needs to be created, click on the Baseline Mode icon in the toolbar. Single points that will define the baseline can now be added by left clciking on the plotted data. Right clicking on an already added point will remove the point. The polynomial order of the baseline will be defined by the amount of points added to the background. One point defines a constant background, two points define a linear background, etc.

Once satisfied with the background, subtract it by selected GUI > Baseline Handling > Subtract Baseline from the menu, or by entering the following command:

Y = y - %bg0(x)

For future use, the background will need to be input into the script generator Python script, so write down the x coordinates of all points used in the background. The coordinates of where the mouse cursor is located are displayed in the lower right of Fityk UI

Add peaks

The next step is to add peaks to the fit. Currently, these scripts are compatible with Lorentzian, Gaussian, Voigt, and SplitVoigt. Be careful not to select LorentzianA, GaussianA, varieties of these peaks.

Fit and save session

After becoming reasonably satisfied with the background and peaks, run the fit. If the fit goes well, the peaks will all fit to your expectations. However, it is possible that something goes haywire, and a peak or two will fit to ridiculous parameters. If this occurs, it may be possible to simply delete the offending peak(s) and try something else. Occasionally it may be easier to start the initial fit over from scratch.

Assuming everything fits properly, save this initial fit, so that i can be opened later if needed.

Fityk script generator

Edit the script generator

Open in your favorite text/code editor. There are a few things that need to be edited within the script generator's Python code before running the script to generate the Fityk script.


Edit the filename prefix, i.e. sample_10X_50mW


EDit the number for the first file in the data series. Ideally this should be 1, but if it isn't enter it here.


Edit the total number of spectra in the data series. For large data series where the spectra changes, it may be a good idea to increment through small portions of the series, just to test that your initial fit is working well, and to make changes if needed.


This determines the output .txt file that Fityk creates and writes all the peak parameters of every spectra to.


Set the active range to what was decided upon during the initial fit of the spectrum.


Edit the zoome range to slightly above and below the active range. This only determines how Fityk displays the spectrum during the fitting process, and is for aesthetic purposes only.


This is the most difficult term within the code. For a constant background, this term will look like

%bg0 = Spline(argmin(y if x > 450 and x < 600),
                min(y if x > 450 and x < 600))

The numbers in this expression are small regions of the spectrum where Fityk looks for the minimum y-value. the () function tells Fityk the x coordinates of the minimum value, and the min() function tells Fityk the actual minimum y value in that same region. We cannot tell Fityk to use the exact (x, y) coordiantes from the background of the initial fit, as these will vary slightly for each spectrum. Instead, we tell Fityk to find the minimum value in a certain region of the spectrum.

If a higher order, such as linear or another polynomial, backgrounds are needed, we must add additional (argmin(), min()) coordinates to the background. For example, a linear baseline could be

%bg0 = Spline(argmin(y if x > 150 and x < 200),
                min(y if x > 150 and x < 200),
             argmin(y if x > 450 and x < 600),
                min(y if x > 450 and x < 600))

and a second order polynomial could be

%bg0 = Spline(argmin(y if x > 150 and x < 200),
                min(y if x > 150 and x < 200),
             argmin(y if x > 450 and x < 600),
                min(y if x > 450 and x < 600),
             argmin(y if x > 700 and x < 750),
                min(y if x > 700 and x < 750))

Be very careful with the parenthesis and commas here. The form of Spline() command has arguments of x, y coordinate pairs, all separated by commas

Spline(x1, y1, x2, y2, x3, y3)

Execute script

After all the above fields within fityk_script_generator have been edited, run the Python script. This will create a .fit file named in the same directory. This is the file that needs to be executed from within Fityk by clicking on the Execute Script icon in the toolbar. Fityk will now proceed to open, fit, and write parameters to the file for all spectra within the data series. This process may take a few minutes, and Fityk will be unresponsive during this time. Just be patient while the script runs.

It is possible that a peak or two will misbehave and fit to ridiculous parameters during the process, sometimes resulting in Fityk aborting the fitting process. This will require the user to tweak the initial fit.

Execute script

After Fityk finish with the fitting process and has generated the sample_10X_50mW.txt data file, run the Python script to parse the data.

This script will prompt the user for the filename to be parsed, which be the aforementioned .txt file. Type that name in and press enter. The script will then prompt the user for which peak to normalize the integrated intensity of all other peaks to. Just enter the peak number of the individual peak; do not enter any underscores or percentile symbols. The peak numbers can be found in the Fityk main UI screen on the right, under the functions tab.

This script will then go through the file and create new space-delimited .txt files, _output.txt being a summary of all peaks in every spectrum. There will also be _peakXX.txt files created for each individual peak and its parameters throughout the entire data series. These files can easily be imported in a graphing software of the user's choice.