Workflow controller for fitting operations.
The workflow_controller.py module contains coordination functions and workflow patterns for the fitting application. It orchestrates complex operations like data loading, equation selection, and fitting loops.
Reload data from a file based on its type.
This function is used in loop mode to reload updated data from the same file. Useful when the user is modifying data in real-time and wants to see the updated fit after each modification.
Parameters:
file_path: Path to the data filefile_type: Type of file ('csv', 'xlsx', 'txt')
Returns:
- Loaded data as DataFrame
Raises:
DataLoadError: If file_type is not supported or loading fails
Example:
from fitting.workflow_controller import reload_data_by_type
# Reload CSV data
data = reload_data_by_type('input/data.csv', 'csv')single_fit_with_loop(fitter_function, data, x_name, y_name, plot_name, data_file_path, data_file_type) -> None
Execute a single fitting operation with optional loop mode.
This function performs an initial fit and then optionally loops, reloading data and refitting each iteration. This is useful for iterative data analysis where the user modifies the data file between fits to explore different scenarios.
Workflow:
- Perform initial fit with current data
- Show results and ask if user wants to continue
- If yes: reload data from file and repeat
- If no: exit
Parameters:
fitter_function: Fitting function to call (must accept data, x_name, y_name, plot_name)data: Initial dataset (pandas DataFrame)x_name: X variable column namey_name: Y variable column nameplot_name: Plot name for window titles and filenamedata_file_path: Path to data file for reloadingdata_file_type: File type ('csv', 'xlsx', 'txt')
Example:
from fitting.workflow_controller import single_fit_with_loop
from fitting.fitting_utils import get_fitting_function
fit_func = get_fitting_function('linear_function')
single_fit_with_loop(
fit_func, data, 'x', 'y', 'my_plot',
'input/data.csv', 'csv'
)Execute multiple fitting operations with optional loop mode.
Performs fitting on multiple datasets sequentially, with the option to reload and refit each dataset in a loop. Each dataset can be independently continued or stopped.
Workflow:
- Fit all datasets once
- Ask user for each dataset if they want to continue
- For datasets marked to continue: reload and refit
- Repeat until no datasets are marked to continue
Parameters:
fitter_function: Fitting function to calldatasets: List of dictionaries, each containing:'data': dataset (pandas DataFrame)'x_name': X variable column name'y_name': Y variable column name'plot_name': plot name for display and filename'file_path': path to data file for reloading'file_type': file type ('csv', 'xlsx', 'txt')
Example:
datasets = [
{
'data': df1, 'x_name': 'x', 'y_name': 'y',
'plot_name': 'dataset1', 'file_path': 'data1.csv', 'file_type': 'csv'
},
{
'data': df2, 'x_name': 'time', 'y_name': 'distance',
'plot_name': 'dataset2', 'file_path': 'data2.csv', 'file_type': 'csv'
}
]
multiple_fit_with_loop(fit_func, datasets)apply_all_equations(equation_setter, get_fitter, equation_types, data, x_name, y_name, plot_name=None) -> None
Apply all available equation types to a dataset.
This function automatically tests all predefined equation types on a single dataset, displaying results for each. Useful for exploratory data analysis to determine which mathematical model best fits the data.
Parameters:
equation_setter: Function to set the current equation type (e.g., 'linear_function')get_fitter: Function to retrieve the fitter for the currently set equation typeequation_types: List of equation type identifiers to testdata: Dataset to fit (pandas DataFrame)x_name: Independent variable column namey_name: Dependent variable column nameplot_name: Plot name for display and filename (optional)
Example:
from config import AVAILABLE_EQUATION_TYPES
from fitting.workflow_controller import apply_all_equations
apply_all_equations(
equation_setter=my_setter,
get_fitter=my_getter,
equation_types=AVAILABLE_EQUATION_TYPES,
data=df,
x_name='x',
y_name='y',
plot_name='comparison'
)Coordinate the complete data loading workflow.
This function orchestrates the entire data loading process:
- Open native file dialog to select a data file (CSV, TXT, XLSX)
- Load the data
- Ask user for variables to use
Parameters:
parent_window: Parent Tkinter windowopen_load_func: Function that opens native file dialog, returns(path, file_type)or(None, None)on cancel (e.g.open_load_dialog)ask_variables_func: Function to ask for variables
Returns:
- Tuple:
(data, x_name, y_name, plot_name, file_path, file_type) - Returns empty tuple if user cancels
Example:
from frontend.ui_dialogs import open_load_dialog, ask_variables
from fitting.workflow_controller import coordinate_data_loading
result = coordinate_data_loading(
parent_window=root,
open_load_func=open_load_dialog,
ask_variables_func=ask_variables
)
if result:
data, x_name, y_name, plot_name, file_path, file_type = resultCoordinate the data viewing workflow.
This function orchestrates the process of selecting and displaying data from files without performing any fitting operations.
Parameters:
parent_window: Parent Tkinter windowopen_load_func: Function that opens native file dialog, returns(path, file_type)or(None, None)on cancelshow_data_func: Function to display data
coordinate_equation_selection(parent_window, ask_equation_type_func, ask_num_parameters_func, ask_parameter_names_func, ask_custom_formula_func, get_fitting_function_func) -> Tuple[str, Optional[Callable]]
Coordinate the equation selection workflow.
Handles the complete process of equation selection, including both predefined equations and custom user-defined equations.
Parameters:
parent_window: Parent Tkinter windowask_equation_type_func: Function to ask for equation typeask_num_parameters_func: Function to ask for number of parametersask_parameter_names_func: Function to ask for parameter namesask_custom_formula_func: Function to ask for custom formulaget_fitting_function_func: Function to retrieve fitting function by name
Returns:
- Tuple of
(equation_name, fitter_function)
Example:
from frontend.ui_dialogs import (
ask_equation_type, ask_num_parameters, ask_parameter_names,
ask_custom_formula
)
from fitting.fitting_utils import get_fitting_function
from fitting.workflow_controller import coordinate_equation_selection
eq_name, fit_func = coordinate_equation_selection(
parent_window=root,
ask_equation_type_func=ask_equation_type,
ask_num_parameters_func=ask_num_parameters,
ask_parameter_names_func=ask_parameter_names,
ask_custom_formula_func=ask_custom_formula,
get_fitting_function_func=get_fitting_function
)coordinate_custom_equation(parent_window, ask_num_parameters_func, ask_parameter_names_func, ask_custom_formula_func) -> Tuple[str, Optional[Callable]]
Coordinate the custom equation creation workflow.
Handles the process of creating a user-defined custom fitting equation by collecting parameter information and the formula.
Parameters:
parent_window: Parent Tkinter windowask_num_parameters_func: Function to ask for number of parametersask_parameter_names_func: Function to ask for parameter namesask_custom_formula_func: Function to ask for custom formula
Returns:
- Tuple of
('custom: <formula>', backend_fit_function)or(EXIT_SIGNAL, None)if cancelled
from fitting.workflow_controller import single_fit_with_loop
from fitting.fitting_utils import get_fitting_function
# Get fitting function
fit_func = get_fitting_function('linear_function')
# Perform fit with loop capability
single_fit_with_loop(
fitter_function=fit_func,
data=dataframe,
x_name='x',
y_name='y',
plot_name='my_fit',
data_file_path='input/data.csv',
data_file_type='csv'
)from fitting.workflow_controller import multiple_fit_with_loop
datasets = [
{
'data': df1, 'x_name': 'x', 'y_name': 'y',
'plot_name': 'dataset1', 'file_path': 'data1.csv', 'file_type': 'csv'
},
{
'data': df2, 'x_name': 'time', 'y_name': 'distance',
'plot_name': 'dataset2', 'file_path': 'data2.csv', 'file_type': 'csv'
}
]
multiple_fit_with_loop(fit_func, datasets)from fitting.workflow_controller import (
coordinate_data_loading,
coordinate_equation_selection
)
from frontend.ui_dialogs import (
open_load_dialog, ask_variables,
ask_equation_type, ask_num_parameters, ask_parameter_names, ask_custom_formula
)
from fitting.fitting_utils import get_fitting_function
# Load data
data_result = coordinate_data_loading(
parent_window=root,
open_load_func=open_load_dialog,
ask_variables_func=ask_variables
)
if data_result:
data, x_name, y_name, plot_name, file_path, file_type = data_result
# Select equation
eq_name, fit_func = coordinate_equation_selection(
parent_window=root,
ask_equation_type_func=ask_equation_type,
ask_num_parameters_func=ask_num_parameters,
ask_parameter_names_func=ask_parameter_names,
ask_custom_formula_func=ask_custom_formula,
get_fitting_function_func=get_fitting_function
)
if fit_func:
# Perform fit
fit_func(data, x_name, y_name, plot_name)- Error Handling: Always check return values for empty tuples (user cancellation)
- Logging: The module logs all operations for debugging
- User Feedback: Functions show appropriate dialogs and messages
- File Management: Reload functions handle file errors gracefully
For more information about fitting workflows, see Usage Guide.