Curve fitting

Use these guides to fit curves and inspect saved fits in the GUI. ERLabPy uses lmfit for models, parameters, and optimization. Use the lmfit documentation for general curve-fitting concepts.

For measured-reference fitting and Fermi edge correction with goldtool, see Fermi edge correction.

Fitting one curve

  1. Open the curve in ftool. See ftool for the available entry points.

  2. Select the model and model options in Setup.

  3. Set the fit window with X range or the dashed bounds in the plot.

  4. Open Fit and choose Guess when the model provides suitable initial estimates.

  5. Inspect and adjust parameter values, bounds, and expressions.

  6. Choose Fit.

  7. Inspect the data, best fit, components, residual behavior, parameter uncertainties, and fit statistics.

If the fit reaches Max nfev, increase the limit only after checking the model and initial parameters. Use Fit ×20 when repeated optimization from the previous result is appropriate for the model. Do not treat convergence alone as a physically valid fit.

Use Copy code to reproduce the fit in Python. Use Save fit to store the result with xarray_lmfit.save_fit().

Use Fitting a stack of curves for a stack of curves and Reopening a saved fit to continue work from a saved result.

Fitting with standard uncertainties

  1. Open the measured values and their absolute standard uncertainties as two ImageTool rows in Manager.

  2. Select both rows.

  3. Right-click either selected row and choose Open in ftool….

  4. Confirm the Data and Standard uncertainty assignments. Use Swap if they are reversed.

  5. Select OK.

  6. Continue with the fitting procedure in Fitting one curve.

The uncertainty coordinates must align with the data coordinates. Its dimensions must be a subset of the data dimensions so that xarray can broadcast it to the data shape. Uncertainty values must be finite and positive where the data is finite.

Fitting a stack of curves

  1. Open the two-dimensional data in ftool.

  2. Confirm that the fitted coordinate is horizontal. Choose Transpose if the image has the wrong orientation.

  3. Set the curve fit window with X range.

  4. Set the sequence range with Y range.

  5. Select a representative curve with Index or the yellow cursor.

  6. Fit that curve and confirm that its model and parameters are suitable.

  7. Select a Fill mode:

    • Use Previous to initialize from the last good fit.

    • Use Extrapolate to project parameters from the previous two fits.

    • Use None when each curve already has suitable initial values.

  8. Choose Fit ⤒ or Fit ⤓ for the required sequence direction.

  9. Inspect the parameter plot and fitted curve at each suspicious or failed index.

  10. Correct failed fits before choosing Save fit or Copy code.

To fit selected parameter values with their standard errors, right-click the parameter plot and choose Open parameter values in ftool. This action omits points without a finite, positive standard error. If no valid points remain, ftool does not open.

When ftool is managed, parameter maps opened in ImageTool appear below the ftool row. Use Refit after update only when compatible input changes should repeat the same fit sequence.

Reopening a saved fit

Load the saved fit dataset and pass it directly to ftool:

import erlab.interactive as eri
from xarray_lmfit import load_fit

fit_result = load_fit("fit-result.h5")
eri.ftool(fit_result)

Confirm that the saved model and fitted parameter values are present. For a two-dimensional fit, every saved curve must use the same model definition.

The saved data is limited to the fit range stored in the result. Use a Manager workspace instead when the full source data, ftool state, fit result, and child ImageTools must be restored together.