mcetl.fitting.fitting_gui

Provides a GUI to fit data using lmfit Models and save the results to Excel.

@author: Donald Erb Created on May 24, 2020

Notes

openpyxl is imported within fit_to_excel to reduce the import time of the module, and is only imported if saving fit results to Excel.

Module Contents

Classes

ResultsPlot

Shows the results of a fit to allow user to decide if fit was acceptable.

SimpleEmbeddedFigure

A window containing just an embedded figure and a close button.

Functions

fit_dataframe

Creates a GUI to select data from a dataframe for fitting.

fit_to_excel

Outputs the relevant data from peak fitting to an Excel file.

launch_fitting_gui

Convenience function to fit dataframe(s) and write their results to Excel.

class mcetl.fitting.fitting_gui.ResultsPlot(fit_result)

Bases: mcetl.plot_utils.EmbeddedFigure

Shows the results of a fit to allow user to decide if fit was acceptable.

Parameters

fit_result (lmfit.ModelResult) -- The fit result to display.

Initialize self. See help(type(self)) for accurate signature.

event_loop(self)

Handles the event loop for the GUI.

Returns

Returns True if the Continue button was pressed, otherwise False.

Return type

bool

class mcetl.fitting.fitting_gui.SimpleEmbeddedFigure(dataframe, gui_values)

Bases: mcetl.plot_utils.EmbeddedFigure

A window containing just an embedded figure and a close button.

Parameters
  • dataframe (pd.DataFrame) -- The dataframe that contains the x and y data.

  • gui_values (dict) -- A dictionary of values needed for plotting.

Initialize self. See help(type(self)) for accurate signature.

event_loop(self)

Handles the event loop for the GUI.

Notes

This function should typically be overwritten by a subclass, and should typically return any desired values from the embedded figure.

This simple implementation makes the window visible, and closes the window as soon as anything in the window is clicked.

mcetl.fitting.fitting_gui.fit_dataframe(dataframe, user_inputs=None)

Creates a GUI to select data from a dataframe for fitting.

Parameters
  • dataframe (pd.DataFrame) -- A pandas dataframe containing the data to be fit.

  • user_inputs (dict, optional) -- Values to use as the default inputs in the GUI.

Returns

  • fit_result (lmfit.model.ModelResult or None) -- A lmfit.ModelResult object, which gives information for the fit done on the dataset. Is None if fitting was skipped.

  • values_df (pd.DataFrame or None) -- The dataframe containing the x and y data, the y data for every individual model, the summed y data of all models, and the background, if present. Is None if fitting was skipped.

  • params_df (pd.DataFrame or None) -- The dataframe containing the value and standard error associated with all of the parameters in the fitting (eg. coefficients for the baseline, areas and sigmas for each peak). Is None if fitting was skipped.

  • descriptors_df (pd.DataFrame or None) -- The dataframe which contains some additional information about the fitting. Currently has the adjusted r squared, reduced chi squared, the Akaike information criteria, the Bayesian information criteria, and the minimization method used for fitting. Is None if fitting was skipped.

  • gui_values (dict or None) -- The values selected in the GUI for all of the various fields, which can be used to reuse the values from a past interation. Is None if fitting was skipped.

mcetl.fitting.fitting_gui.fit_to_excel(values_dataframe, params_dataframe, descriptors_dataframe, excel_writer_handler, sheet_name=None, plot_excel=False)

Outputs the relevant data from peak fitting to an Excel file.

Parameters
  • values_dataframe (pd.DataFrame) -- The dataframe containing the x and y data, the y data for every individual model, the summed y data of all models, and the background, if present.

  • params_dataframe (pd.DataFrame) -- The dataframe containing the value and standard error associated with all of the parameters in the fitting (eg. coefficients for the baseline, areas and sigmas for each peak).

  • descriptors_dataframe (pd.DataFrame) -- The dataframe which contains some additional information about the fitting. Currently has the adjusted r squared, reduced chi squared, the Akaike information criteria, the Bayesian information criteria, and the minimization method used for fitting.

  • excel_writer_handler (mcetl.excel_writer.ExcelWriterHandler) -- The ExcelWriterHandler that contains the pandas ExcelWriter object for writing to Excel, and the styles for styling the cells in Excel.

  • sheet_name (str, optional) -- The Excel sheet name.

  • plot_excel (bool, optional) -- If True, will create a simple plot in Excel that plots the raw x and y data, the data for each peak, the total of all peaks, and the background if it is present.

mcetl.fitting.fitting_gui.launch_fitting_gui(dataframe=None, gui_values=None, excel_writer=None, save_excel=True, plot_excel=True, mpl_changes=None, save_when_done=True, excel_formats=None)

Convenience function to fit dataframe(s) and write their results to Excel.

Parameters
  • dataframe (pd.DataFrame or list/tuple, optional) -- The dataframe or list/tuple of dataframes to fit.

  • gui_values (dict, optional) -- A dictionary containing the default gui values to pass to fit_dataframe.

  • excel_writer (pd.ExcelWriter, optional) -- The Excel writer used to save the results to Excel. If input, the engine must be "openpyxl".

  • save_excel (bool, optional) -- If True (default), then the fit results will be saved to an Excel file.

  • plot_excel (bool, optional) -- If True (default), then the fit results will be plotted in the Excel file (if saving).

  • mpl_changes (dict, optional) -- A dictionary of changes to apply to matplotlib's rcParams file, which affects how plots look.

  • save_when_done (bool, optional) -- If True (default), then the Excel file will be saved once all dataframes are fit.

  • excel_formats (dict, optional) --

    A dictionary of formats to use when writing to Excel. The dictionary must have one of the following keys:

    'fitting_header_even', 'fitting_header_odd', 'fitting_subheader_even', 'fitting_subheader_odd', 'fitting_columns_even', 'fitting_columns_odd', 'fitting_descriptors_even', 'fitting_descriptors_odd'

    The values for each key must be a dictionary, with keys in this internal dictionary representing keyword arguments for openpyxl's NamedStyle or openpyxl.styles.NamedStyle objects. See mcetl.excel_writer.ExcelWriterHandler.styles as an example for the dictionary.

Returns

  • fit_results (list(lmfit.models.ModelResult)) -- A list of lmfit.ModelResult objects, which give information for each of the fits done on the dataframes.

  • gui_values (dict, optional) -- A dictionary containing the default gui values to pass to fit_dataframe.

  • proceed (bool) -- True if the fitting gui was not exited from prematurely, otherwise, the value is False. Useful when calling this function from an outside function that needs to know whether to continue doing peak fitting.