Fit

Interactive curve fitting for Linux, Windows and macOS

User guide

Fitting

Fitting a model

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

A fit adjusts the parameters of the model's curves until the calculated profile matches the measured one as closely as it can.

A fit needs a profile to fit and a curve type to fit it with. Everything else - where the curves start, which stretches of the profile are scored, the background - can be prepared by hand or left to the program, which fills in whatever is missing before it starts.

There are three ways to start a fit, all in the Fit menu:

While a fit runs, the chart area shows its progress and every command that would change the model is greyed. Fit > Stop, or the Stop button of the Tools tab, ends it.

The fit itself runs in the compute server the window is connected to (Fit > Compute Server), not in the window. What it makes as small as possible is chosen under Fit > Loss Function, and the algorithm that does it under Fit > Minimizer.

Limitations

See also: Automatic decomposition · Minimize Difference · Why a fit command is greyed · The difference (R-factor) · The Tools tab · Fit progress

Automatic decomposition

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

Fit > Automatically decides how many curves the profile needs: it starts with far more than any profile needs and removes every one the fit can do without.

Fit > Automatically - also the last button of the toolbar above the chart - runs five steps in the compute server:

Every curve is of the curve type selected on the Tools tab. When the run ends, the chart, the tables and the Model panel show the curves that remain. The removed curves are gone together with the positions they were placed from, so a later edit or fit does not bring them back.

The one setting that decides the answer is the maximum acceptable difference, 0.01 % by default. Ask for less accuracy and you get fewer, broader curves; ask for more and the program keeps the curves it needs to deliver it.

It is offered as soon as a profile is open, including before the background has been dealt with - which is exactly what its first step does.

Limitations

See also: How many curves: the difference ceiling · Max Acceptable Difference · Minimize Number of Curves · Fit progress

How many curves: the difference ceiling

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

Fit decides how many curves a profile needs with a ceiling on the difference that you choose, rather than with an information criterion.

With enough curves any profile can be fitted perfectly, noise included, so a least-squares fit alone cannot say how many curves there are. The usual answer weighs how well a model fits against how many parameters it has, with an information criterion such as AIC, AICc or BIC, and keeps whichever model scores best.

Fit answers differently. You state how good the fit has to be - the maximum acceptable difference, set with Fit > Set Max Acceptable Difference - and the program removes every curve it can remove without breaking that promise. The ceiling is not a penalty to calibrate; it is the accuracy your measurement deserves: its noise level, its counting statistics, the precision you are prepared to claim.

Each fit interval is decomposed on its own, in a loop of attempts that are each allowed to fail:

The loop runs twice. The first pass removes the curve standing where the measured profile is steepest: on a flank, a curve is often doing the work of a shoulder that its neighbours can take over. The second pass removes the curve with the smallest amplitude, the classic candidate for being noise. A pass also ends when one curve is left. A final fit then polishes what survived.

A re-fit inside the loop only has to show that the ceiling can be met, so with the built-in engine it stops as soon as the difference is back under it, with a looser tolerance and no restarts. Only the final fit runs to full precision. That is what keeps the method quick although it fits once for every curve it removes: on the two-peak sample Data/2.dat, with two-branch Pseudo-Voigt curves and a ceiling of 0.01 %, it ends with a handful of curves in well under a minute.

Removing weak components, re-fitting and rolling back when the result is worse is not new in itself; GaussPy+ does so and keeps the smaller model only if its AICc improves. What is uncommon here is that the test is a fixed ceiling you choose rather than an improvement in an information criterion, and that one pass picks the curve on the steepest slope of the measured profile.

Limitations

Sources

See also: Automatic decomposition · Max Acceptable Difference · The difference (R-factor)

Minimize Number of Curves

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

Removes every curve the fit can do without, starting from the curves and fit intervals you already have.

Fit > Minimize Number of Curves (F4) runs steps 4 and 5 of Fit > Automatically - the removal passes and the final fit - on the model as it stands. It does not touch the background, and it keeps the positions and fit intervals you placed.

What it needs is filled in only where it is missing: with fewer than two interval bounds it marks a fit interval around each peak, and with no curve positions it places a curve on every point of every peak, as the automatic run does.

Use it when you have placed more curves than you think are needed and want the data to decide which of them stay, within the maximum acceptable difference (Fit > Set Max Acceptable Difference).

Curves it removes are gone together with their positions.

Limitations

See also: How many curves: the difference ceiling · Automatic decomposition · Why a fit command is greyed

Minimize Difference

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

Fits the model as it stands: every curve is kept and only their parameters change.

Fit > Minimize Difference (F5), and the Fit button in the Fit group of the Tools tab, adjust the parameters of every curve until the selected loss function can be made no smaller. No curve is added or removed, and the maximum acceptable difference plays no part.

Anything missing is filled in first, as for the other fit commands: a fit interval around each peak when fewer than two interval bounds are marked, and a curve on every point of every peak when there are no curve positions.

Each fit interval is fitted as a problem of its own. The difference reported afterwards pools all of them into one figure.

Use it after placing curves by hand, after changing a setting such as the loss function or the engine, or to fit again from where the last fit left off.

Limitations

See also: Fitting a model · Loss Function · Minimizer · When a fit ends

Stop

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

Ends the fit or other long computation that is running.

Fit > Stop, and the Stop button in the Fit group of the Tools tab, are available only while a computation runs; the rest of the time they are greyed.

The engine is told to end, and does so at its next step. The window then shows what the fit had reached, exactly as it does when a fit finishes by itself: the curves, the tables and the statistics in the status bar are refreshed.

Stopping an automatic decomposition ends its passes where they are: curves already removed stay removed.

Limitations

See also: When a fit ends · Fit progress

Why a fit command is greyed

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

The fit commands are offered only when there is something to fit, the background has been dealt with, and nothing else is running.

The window decides which commands it offers from the state of the compute server. The rules, in the order they apply:

Within the settings, single entries can be greyed with a reason in their hint, shown in the status bar:

Fit > Set Max Acceptable Difference, Fit > Enable Curve Scaling and Fit > Compute Server are never greyed: they are settings, and a changed setting applies to the next fit.

Limitations

See also: Fitting a model · Automatic decomposition · What the fit will actually do

The difference (R-factor)

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

The number Fit reports for how well the model matches the data is an R-factor: the squared differences divided by the square of the summed data.

Throughout the program "difference" and "R-factor" name the same number. Over the points inside the fit intervals it is

R = sum( (s * calc - obs)^2 ) / ( sum obs )^2

where obs is a measured value, calc the model's value at the same point, and s the curve-scaling factor of the interval (1 when Fit > Enable Curve Scaling is off). With several fit intervals, the sums are pooled over all of them before dividing, so the figure means the same however the profile is divided up.

Dividing by the data makes it a relative measure: the same sample measured for ten times as long gives the same R-factor. A perfect fit gives 0. It is shown as a plain number, such as 7.7E-6 - not in percent - while Fit > Set Max Acceptable Difference asks for its ceiling in percent.

It is the default loss function, so it is usually also what the fit minimises. Whatever loss function is selected, the number reported and the number compared with the maximum acceptable difference is this R-factor.

While a fit runs, it is shown in the status bar and above the progress chart. When the fit ends, the status bar shows the reduced chi-squared and R-squared of the result in its place.

In diffraction the name R-factor usually means the profile R-factors of Rietveld refinement: Rp, the summed absolute differences over the summed data, and Rwp, the square root of the weighted squared differences over the weighted squared data. Fit's number shares their intent - a difference relative to the data - but is neither: it is unweighted, squared, and has no square root, so it is not directly comparable with an Rp or Rwp quoted elsewhere.

Limitations

Sources

See also: Max Acceptable Difference · Loss Function · Curve scaling

Max Acceptable Difference

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

The ceiling on the R-factor that decides how many curves the automatic decomposition keeps.

Fit > Set Max Acceptable Difference opens the Max Acceptable Difference dialog, which has one field: "Max acceptable difference (percent):". Type the ceiling in percent, with a full stop as the decimal separator, and press OK. The default is 0.01, meaning an R-factor of 0.0001.

Anything that is not a number is refused with "Please enter a valid number (for example, 1.5)." and the dialog stays open. Cancel leaves the ceiling as it was.

The ceiling is used by Fit > Automatically and Fit > Minimize Number of Curves: a curve is removed only if the fit interval it belongs to still meets the ceiling without it. Fit > Minimize Difference does not use it.

Choose it near the level the noise of your data allows. Too low, and noise is fitted with extra curves; too high, and real components are merged away.

It is saved in the project file, so a project reopens with the ceiling it was fitted with.

Limitations

See also: How many curves: the difference ceiling · The difference (R-factor) · Automatic decomposition

Curve scaling

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

With curve scaling on, the model is multiplied by one factor per fit interval so that its area matches the data before the two are compared.

Fit > Enable Curve Scaling is a tick, and it is on by default. While it applies, the built-in engine compares s times the model with the data, where s is the sum of the measured values in the fit interval divided by the sum of the model's. The curves then only have to get the shape right, and the factor takes care of the overall height, which helps the fit converge.

It does not apply in two cases, whatever the tick says:

The setting is saved in the project file.

Limitations

See also: The difference (R-factor) · Loss Function · What the fit will actually do

Minimizer

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

Fit > Minimizer chooses the algorithm that searches for the best parameters; the first one, the default, needs nothing extra.

The entries of Fit > Minimizer are the engines this build offers, in order. The first is the default, and is also what a saved choice falls back to when it names an engine this build does not have. The choice is remembered between sessions.

Downhill Simplex (native) is the Nelder-Mead simplex method. It needs only the value of the loss function, not its derivatives, so it fits any curve type, including those that compute their points without a formula. It gives no uncertainties for the parameters.

Levenberg-Marquardt (Python/lmfit) uses lmfit's least_squares method - despite the name, scipy's Trust Region Reflective solver. It reports an uncertainty for each fitted parameter, shown after the value in the Curve Attributes table as "value ± error".

An engine that fits by evaluating a formula is greyed while the selected curve type has none, and if it was selected, the default engine is selected instead. An engine that can only minimise a sum of squares leaves a loss function that is not one to the built-in engine; a dialog and the status bar say so.

Without Python: the Python engine needs Python and three libraries set up on the machine that runs the compute server (Help > Compute Backends says how). If they are missing, a fit with that engine does not start: the server answers that the Python backend is not available. Nothing switches engines behind your back; choose Downhill Simplex (native), which needs no Python, to fit.

Limitations

Sources

See also: Compute backends · Weighting · What the fit will actually do · The Curve Attributes table

Loss Function

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

Fit > Loss Function chooses the number a fit makes as small as possible; the default, R-factor, suits almost every fit.

Fit > Loss Function is the last entry of the Fit menu. Its entries are the objectives this build offers, the default first; the choice is remembered between sessions. Each entry's hint says what it measures:

R-factor and Sum of squares differ only by a constant factor, so they find the same parameters; only the number differs. Relative deviation sums absolute rather than squared differences, so a few large misfits count for less. R-factor (legacy) divides by the model instead of the data: with curve scaling on, a fit can lower it by inflating the model without matching the data any better, which is why it is greyed for a curve type whose amplitude is free, and replaced by R-factor if it was selected.

Whatever is chosen, the difference the program reports, and compares with the maximum acceptable difference, is the R-factor. The statistics shown after a fit do not depend on the choice either; only the fitted parameters do.

Limitations

See also: The difference (R-factor) · What the fit will actually do · Curve scaling

Weighting

ConventionCommon practice or a guideline in the field, not a rule its sources require.

Weighting decides how much each point counts in a fit by the Python engine; counting data are conventionally weighted by one over the square root of the count.

The Fit > Weighting submenu is shown only while an engine that can be weighted is selected under Fit > Minimizer - the Python one. The built-in engine always fits unweighted, so the menu is hidden under it. It has two entries:

The choice is remembered between sessions. The reduced chi-squared shown in the status bar after any fit uses the Poisson weights, whichever engine ran.

Limitations

Sources

See also: Minimizer · Compute backends

What the fit will actually do

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

When a choice cannot be honoured, the fit changes it and says so: always in the status bar, and in a dialog when you make the choice.

The right-hand end of the status bar always says what the next fit will do - for example "Minimising R-factor." or "Fitting with the built-in engine, minimising Relative deviation." When something was changed, hovering over the status bar shows why.

Three things can be changed. The compute server decides with the same rule the status bar reports, so the two cannot disagree:

When you change the engine, the loss function or the curve type and the result would be changed, a dialog titled "About this fit" explains it. It is not repeated while you stay with that selection, and not shown at start-up for a setting chosen earlier. Curve scaling being switched off never opens the dialog; hovering over the status bar explains it.

See also: Loss Function · Minimizer · Curve scaling · The status bar

When a fit ends

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

The chart, the tables and the Model panel are refreshed from the result, and the status bar shows how long the fit took and how good it is.

While a fit runs, the chart area shows its progress - or, with View > Animation Mode ticked, the model moving - and the tables keep the last finished result.

When it ends, whether it finished or was stopped, the window:

The elapsed time counts only the time the computer was awake. A fit left running while the machine slept or hibernated does not count the sleep, and neither does the time axis of the progress chart.

The result is part of the project from then on, and is kept on disk when the project is saved.

Limitations

See also: Fit progress · Animation Mode · The status bar · The difference (R-factor)

Compute Server

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

The window does no fitting itself: every computation runs in a compute server, fit_server, and Fit > Compute Server says where it is.

Fit > Compute Server opens a box titled Compute Server that asks for the "Server URL (fit_server must be running there):". It offers the address in use, http://127.0.0.1:8787 - a server on this computer - unless another has been set. Leaving it empty also means that default.

On Windows, the launcher the installed shortcut starts makes sure a server is answering at the default address before it opens the window.

Enter the address of a fit_server running elsewhere - a machine with more cores, say - to fit there. After OK the window checks at once whether anything answers. If nothing does, it says "No server answered at" the address, and "Start fit_server there - the application cannot fit without it." If a server answers and a profile is open, the profile is sent to it.

The address is remembered between sessions. A running fit_server describes its own interface: open the address followed by /docs in a web browser.

Limitations

See also: Compute backends · Minimizer

Compute backends

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

Help > Compute Backends summarises the engines a fit can run on and how to set up the optional Python one.

Fitting runs in the compute server the window is connected to (Fit > Compute Server). Behind it there are two engines, chosen under Fit > Minimizer:

fit_server looks for that environment in a folder named sidecar under %LOCALAPPDATA%\Fit\py on Windows and ~/.local/share/fit/py elsewhere ($XDG_DATA_HOME replaces ~/.local/share when it is set), or under the folder named by the environment variable FIT_PY_HOME when that is set. The libraries and their versions are listed in Worker/py/requirements.txt.

You never start the Python engine yourself: fit_server starts it the first time a fit asks for it and stops it when it exits. The window only ever talks to fit_server.

A third arrangement needs no engine choice at all: the same fit_server on another machine, reached through Fit > Compute Server.

Limitations

See also: Setting up the Python engine · Minimizer · Compute Server · Weighting

Setting up the Python engine

This software's choiceA decision made by this software rather than by the field's sources; the explanation says why it was made.

The Python engine needs a one-time setup: a private Python environment with numpy, scipy and lmfit at the tested versions.

You need Python 3.10 to 3.13 and the list of pinned libraries, Worker/py/requirements.txt, from the source repository (https://github.com/dvmorozov/fit). The libraries go into a separate virtual environment at exactly those versions, so your own Python is not touched and fitted numbers reproduce.

On Linux and macOS, from the folder holding the repository:

On Windows, in PowerShell:

--only-binary=:all: stops pip from compiling numpy or scipy from source when your Python is newer than the pinned versions have ready-made packages for; it fails instead, and a Python in the range above is the fix.

The environment belongs to the machine, not to a copy of the program: every installation and every checkout on the machine shares it. Set the environment variable FIT_PY_HOME to keep it elsewhere; it then goes in the sidecar folder under that path.

To check it, start the Python engine by hand with sidecar/bin/python Worker/py/fit_backend.py --port 8788 (on Windows, sidecar\Scripts\python.exe) and open http://127.0.0.1:8788/health in a browser: it answers {"ok": true, ...}. Stop it with Ctrl+C - fit_server starts its own when a fit needs it.

Limitations

See also: Compute backends · Minimizer