API: lsqfitgui

run_server

run_server(fit=None, name='Lsqfit GUI', fit_setup_function=None, fit_setup_kwargs=None, meta_config=None, use_default_content=True, get_additional_content=None, additional_plots=None, run_app=True, debug=True, host='localhost', port=8000)

Initialize the GUI and start the dash app.

Requires either a fit object or a fit_setup_function.

Parameters
  • fit (Optional[lsqfit.nonlinear_fit]) – Non-linear fit object.

  • name (str) – Name of the app displayed as title and browser tab title.

  • fit_setup_function (Optional[Callable[[Any], lsqfit.nonlinear_fit]]) – Function which returns a non-linear fit object. Its keywords are provided by fit_setup_kwargs.

  • fit_setup_kwargs (Optional[Dict]) – Initial kwargs which are passed to the fit_setup_function for creating the first fit object.

  • meta_config (Optional[List[Dict]]) – Configuration for the fit_setup_kwargs represented in the GUI. These must match dcc.Input arguments.

  • use_default_content (Optional[bool]) – Add default elements like the function documentation and plot tabs to the GUI.

  • get_additional_content (Optional[Callable[[lsqfit.nonlinear_fit], dash.html.Base.Base]]) – Function used to determine dynamic content depending on fit results.

  • additional_plots (Optional[Dict[str, Callable]]) – List of dictionaries specifying plots rendered in the tab element. Must contain at least the name: str and fcn:Callable[[nonlinear_fit], Figure] items. This populates FitGUI.plots. See also the lsqfitgui.frontend.content.DEFAULT_PLOTS.

  • run_app (bool) – Call run server on the dash app.

  • debug (bool) – Run the dash app in debug mode. Only used if run_app=True.

  • host (str) – The hosting address of the dash app. Only used if run_app=True.

  • port (int) – The port of the dash app. Only used if run_app=True.

Return type

lsqfitgui.lsqfitgui.FitGUI

Example

The most basic example just requires a nonlinear_fit object:

fit = lsqfit.nonlinear_fit(data, fcn=fcn, prior=prior)
app = run_server(fit)

More sophisticated examples, where also meta arguments are used, are:

def generate_fit(n_exp=3):
    ...
    return lsqfit.nonlinear_fit(data, fcn=fcn, prior=prior)

fit_setup_kwargs = {"n_exp": 3}
meta_config = [{"name": "n_exp", "type": "number", "min": 1, "max": 10, "step": 1}]

fit_gui = run_server(
    fit_setup_function=generate_fit,
    fit_setup_kwargs=fit_setup_kwargs,
    meta_config=meta_config
)

plot_gvar

plot_gvar(x, y, fig=None, kind='band', add_log_menu=False, scatter_kwargs=None)

Plot gvars as go.Figures including their uncertainties.

Parameters
  • x (Union[numpy.ndarray, Dict[str, numpy.ndarray]]) – The independent variable. Can be either an array or a dictionary of arrays where keys must match the keys of the dependent variable. If kind="band", tries to interpolate the values.

  • y (gvar._bufferdict.BufferDict) – The dependent variable. If it is a dictionary of gvar arrays, the figure will contain several subfigures.

  • fig (Optional[plotly.graph_objs._figure.Figure]) – Figure to add traces to. If not specified, creates a new figure.

  • kind (str) – Either "band" or "errorbars".

  • add_log_menu (bool) – Add a menu to switch from a linear to a log scale. Only available if y is not a dictionary.

  • scatter_kwargs (Optional[Dict]) – Keyword arguments passed to go.Scatter.

Return type

plotly.graph_objs._figure.Figure

wrap_plot_gvar

wrap_plot_gvar(kind='band', add_log_menu=False, scatter_kwargs=None)

Wraps functions taking x and p arguments such that they can be used by the lsqfitgui.FitGUI.plots to generate plots of gvars.

Parameters
  • kind (str) – Allowed values: "band", "errorbar". Returned figure will contain error bars or error bands.

  • add_log_menu (bool) – Should the returned figure have a menu allowing to change from regular to log y-axis?

  • scatter_kwargs (Optional[Dict]) – Keyword arguments passed to go.Scatter().

Return type

Callable[[lsqfit.nonlinear_fit], plotly.graph_objs._figure.Figure]

Example

The code below presents how to use the wrapper to add new plots to the GUI:

def fcn(x, p):
    yy = ...
    return yy

def plot_fcn(fit):
    yy = fcn(fit.x, fit.p)
    return plot_gvar(fit.x, yy, kind="band")

@wrap_plot_gvar(kind="band")
def wrapped_fcn(x, p):
    return fcn(x, p)

gui.plots.append({"name": "Fcn results", "fcn": wrapped_fcn})

Both functions, wrapped_fcn and plot_fcn will produce the same plot when added to the gui.

FitGUI

class FitGUI(fit=None, fit_setup_function=None, fit_setup_kwargs=None, meta_config=None, use_default_content=True)

Class which initializes the dashboard.

Initialize the fit gui.

You must either provide a fit object or a fit_setup_function function to initialize this class. Note that this dose not create a Dash app; the app is created by calling FitGUI.setup_app() (which is implicitly called by FitGUI.run_server()).

Parameters
  • fit (Optional[lsqfit.nonlinear_fit]) – Non-linear fit object.

  • fit_setup_function (Optional[Callable]) – Function which returns a non-linear fit object. Its keywords are provided by fit_setup_kwargs.

  • fit_setup_kwargs (Optional[Dict]) – Initial kwargs which are passed to the fit_setup_function for creating the first fit object.

  • meta_config (Optional[List[Dict]]) –

    Configuration for the fit_setup_kwargs represented in the GUI. These must match dcc.Input arguments.

  • use_default_content (bool) – Add default elements like the function documentation and plot tabs to the GUI.

Example

The most basic example just requires a nonlinear_fit object:

fit = lsqfit.nonlinear_fit(data, fcn=fcn, prior=prior)
gui = FitGUI(fit)

More sophisticated examples, where also meta arguments are used, are:

from dash import Dash

def generate_fit(n_exp=3):
    ...
    return lsqfit.nonlinear_fit(data, fcn=fcn, prior=prior)

fit_setup_kwargs = {"n_exp": 3}
meta_config = [{"name": "n_exp", "type": "number", "min": 1, "max": 10, "step": 1}]

gui = FitGUI(
    fit_setup_function=generate_fit,
    fit_setup_kwargs=fit_setup_kwargs,
    meta_config=meta_config
)
fit_gui.run_server(host=host, debug=debug, port=port)
property app: dash.dash.Dash

Return plotly dash app.

property fit: lsqfit.nonlinear_fit

Return current fit object.

get_additional_content: Callable[[lsqfit.nonlinear_fit], dash.html.Base.Base]

Function used to determine dynamic content depending on fit results.

property initial_fit: lsqfit.nonlinear_fit

Return fit object used to initialize the app.

property layout: dash.html.Base.Base

Return the current layout.

name: str

Name of the app displayed as title and browser tab title.

plots: List[Dict[str, Any]]

List of dictionaries specifying plots rendered in the tab element. Must contain at least the name: str and fcn:Callable[[nonlinear_fit], Figure] items.

Example

Plot the fit results:

def plot_fcn(fit):
    yy = fit.fcn(fit.x, fit.p)
    return plot_gvar(fit.x, yy, kind="band")

gui.plots.append({"name": "Fit results", "fcn": plot_fcn})

Allowed keywords are

  • name (str): The name presented in the tabs.

  • fcn (Callable[[nonlinear_fit], Figure]): The function used to generate the plot. Must take a plot and kwargs as an input.

  • description (str): Text displayed below figure (can contain latex using).

  • kwargs (Dict[str, Any]): A dictionary passed to the above function.

  • static_plot_gvar (Dict[str, Any]): Static data passed to plot_gvar() added to the same figure (i.e., to also plot data as an comparison).

See also the lsqfitgui.frontend.content.DEFAULT_PLOTS.

run_server(*args, **kwargs)

Wrapper to self.app.run_server.

setup_app(app=None)

Initialize the dash app.

Sets up layout and callbacks and create a Dash instance if not provided.

Parameters

app (Optional[dash.dash.Dash]) – The dash app which runs the server. If provided, requires to manually set up style sheets, scripts and assets.

Raises RuntimeError if app already set up.