This section provides technical documentation for RegressionLab's Python API. It's intended for developers who want to understand the codebase, contribute to the project, or integrate RegressionLab into their own applications.
RegressionLab follows a modular architecture with clear separation of concerns:
RegressionLab/
βββ src/
β βββ config/ # Configuration package
β β βββ color_utils.py # Hex color utilities (lighten, muted)
β β βββ env.py # Environment and .env
β β βββ theme.py # UI theme and plot style
β β βββ paths.py # Paths and output
β β βββ constants.py # Version, signals, EQUATIONS (from YAML)
β β βββ equations.yaml # Equation definitions (function, formula, format, param_names)
β βββ i18n.py # Internationalization
β βββ main_program.py # Entry point for Tkinter app
β β
β βββ fitting/ # Curve fitting core
β β βββ functions/ # Mathematical and fit_* functions
β β β βββ _base.py
β β β βββ polynomials.py
β β β βββ trigonometric.py
β β β βββ inverse.py
β β β βββ special.py
β β βββ fitting_functions/ # Re-exports (fitting.fitting_functions)
β β βββ fitting_utils.py
β β βββ estimators.py
β β βββ workflow_controller.py
β β βββ custom_function_evaluator.py
β β
β βββ frontend/ # User interface (Tkinter)
β β βββ ui_main_menu.py
β β βββ image_utils.py
β β βββ keyboard_nav.py # Keyboard navigation
β β βββ window_utils.py # Window positioning (centering)
β β βββ ui_dialogs/ # Dialog package
β β βββ data_selection.py
β β βββ equation.py
β β βββ help.py
β β βββ config_dialog.py
β β βββ result.py
β β βββ tooltip.py
β β
β βββ data_analysis/ # Transforms and cleaning
β β βββ _utils.py # Shared get_numeric_columns
β β βββ transforms.py
β β βββ cleaning.py
β β
β βββ loaders/ # Data loading
β β βββ data_loader.py
β β βββ loading_utils.py
β β βββ saving_utils.py
β β
β βββ plotting/ # Visualization
β β βββ plot_utils.py
β β
β βββ streamlit_app/ # Web interface
β β βββ app.py # Entry point
β β βββ theme.py # Streamlit theme config
β β βββ sections/ # UI sections
β β βββ sidebar.py
β β βββ data.py
β β βββ fitting.py
β β βββ results.py
β β βββ help_section.py
β β βββ modes.py
β β
β βββ locales/ # Translation JSON (en, es, de)
β β
β βββ utils/ # Utilities
β βββ exceptions.py
β βββ logger.py
β βββ validators.py
- config - Configuration management and application constants
- i18n - Internationalization and translation system
- fitting.fitting_functions - Mathematical functions and curve fitting implementations
- fitting.fitting_utils - Generic fitting utilities and helpers
- fitting.workflow_controller - Orchestrates fitting workflows and modes
- fitting.custom_function_evaluator - Evaluates user-defined custom formulas
- fitting.estimators - Parameter estimation functions for initial guesses
- loaders.data_loader - High-level data loading interface
- loaders.loading_utils - CSV and Excel file readers
- loaders.saving_utils - Save DataFrame to CSV, TXT, XLSX
- data_analysis - Transforms (FFT, DCT, Hilbert, Laplace, cepstrum, Hadamard, log, exp, etc.) and cleaning (drop NaN, outliers, etc.). Used by the View Data window in Tkinter and Streamlit.
- plotting.plot_utils - Plot generation and styling (2D fit, pair, residual, 3D)
- frontend.ui_main_menu - Main menu and navigation (Tkinter)
- frontend.ui_dialogs - Dialog windows and user input (Tkinter)
- frontend.image_utils - Image loading and scaling utilities
- frontend.keyboard_nav - Keyboard navigation utilities
- frontend.window_utils - Window positioning and centering
- streamlit_app.app - Web interface (Streamlit)
- utils.exceptions - Custom exception classes
- utils.logger - Logging configuration and utilities
- utils.validators - Data validation functions
# Clone repository
git clone https://github.com/your-repo/RegressionLab.git
cd RegressionLab
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies including dev tools
pip install -r requirements-dev.txt # or: pip install -e ".[dev]"
# Run tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=src --cov-report=html# From project root (activate .venv first if using setup scripts)
# Bin launchers require .venv (run setup.bat / setup.sh first).
# Tkinter (desktop)
python src/main_program.py
# Or: bin\run.bat (Windows) / ./bin/run.sh (Linux/macOS)
# Streamlit (web)
streamlit run src/streamlit_app/app.py
# Or: bin\run_streamlit.bat (Windows) / ./bin/run_streamlit.sh (Linux/macOS)RegressionLab follows PEP 8 with these conventions:
- Line length: 100 characters max
- Docstrings: Google style (use "Examples:" for example blocks)
- Type hints: Required for function signatures
- Imports: Sorted alphabetically, grouped by standard/third-party/local. Cross-package imports use the package root (e.g.
from config import X,from loaders import Y). Imports within the same package use full module paths (e.g.from config.constants import Z,from loaders.loading_utils import csv_reader) to avoid circular references
Example:
from typing import Optional, Tuple
import numpy as np
from numpy.typing import NDArray
def example_function(data: NDArray, threshold: float = 0.5) -> Tuple[NDArray, float]:
"""
Short description of what the function does.
Longer explanation if needed, including mathematical
formulation or algorithmic details.
Args:
data: Input data array
threshold: Cutoff value for filtering
Returns:
Tuple of (filtered_data, metric_value)
Raises:
ValueError: If data is empty or threshold is negative
"""
if len(data) == 0:
raise ValueError("Data cannot be empty")
filtered = data[data > threshold]
metric = np.mean(filtered)
return filtered, metricSee Extending RegressionLab for detailed instructions.
Quick summary:
- Add mathematical function in
fitting/functions/(e.g.polynomials.py,special.py) - Create fitting wrapper function
- Export the fit function from
src/fitting/functions/__init__.py, then register inconfig/equations.yaml(add entry withfunction,formula,format,param_names) and add translations insrc/locales/ - Test thoroughly
Tkinter:
- Main menu: Edit
frontend/ui_main_menu.py - Keyboard navigation: Edit
frontend/keyboard_nav.py(see keyboard_nav) - Image utilities: Edit
frontend/image_utils.py(see image_utils) - Window positioning: Edit
frontend/window_utils.py(see window_utils) - Dialogs: Edit modules in
frontend/ui_dialogs/(e.g.data_selection.py,equation.py) - Styling: Configure in
.envfile
Streamlit:
- Entry point:
streamlit_app/app.py; UI logic instreamlit_app/sections/ - Theme:
streamlit_app/theme.py - CSS in
SIDEBAR_CSSinsections/sidebar.py - Add new modes in
sections/modes.py
- Create reader function in
loaders/loading_utils.py - Register it in the
FILE_TYPE_READERSdict inloaders/data_loader.py(key = file type, value = reader function) - Add file type filter to
open_load_dialog()infrontend/ui_dialogs/load_data_dialog.py - Test with sample data
- Per-plot: Pass parameters to
create_plot()inplotting/plot_utils.py - Globally: Configure in
.envfile - Programmatically: Modify
PLOT_CONFIGand related settings inconfig/theme.py
tests/
βββ __init__.py
βββ conftest.py # Pytest fixtures
βββ run_tests.py # Test runner (invokes pytest)
βββ test_config.py # Configuration
βββ test_custom_function_evaluator.py
βββ test_data_loader.py # Data loading
βββ test_exceptions.py
βββ test_fitting_functions.py # Curve fitting
βββ test_fitting_utils.py
βββ test_i18n.py
βββ test_loading_utils.py
βββ test_logger.py
βββ test_validators.py # Validation
βββ test_workflow_controller.py
Run tests via pytest tests/ or python tests/run_tests.py, or use bin/run_tests.bat (Windows) / bin/run_tests.sh (Linux/macOS). The bin test launchers require .venv (run setup first).
# All tests
pytest tests/
# Specific test file
pytest tests/test_fitting_functions.py
# Specific test
pytest tests/test_fitting_functions.py::test_linear_fit
# With coverage
pytest tests/ --cov=src
# Verbose output
pytest tests/ -v
# Stop on first failure
pytest tests/ -xExample test:
import numpy as np
import pandas as pd
import pytest
from fitting.fitting_functions import func_lineal, ajlineal
def test_func_lineal_scalar():
"""Test linear function with scalar input."""
result = func_lineal(5.0, 2.0)
assert result == 10.0
def test_func_lineal_array():
"""Test linear function with array input."""
t = np.array([1, 2, 3])
result = func_lineal(t, 2.0)
expected = np.array([2, 4, 6])
np.testing.assert_array_equal(result, expected)
def test_ajlineal_perfect_fit():
"""Test linear fitting with perfect data."""
# Generate perfect linear data
x = np.linspace(0, 10, 50)
y = 3.0 * x # y = 3*x
data = pd.DataFrame({'x': x, 'y': y})
text, y_fitted, equation, *_ = ajlineal(data, 'x', 'y')
# Check RΒ² is nearly perfect (RΒ² is included in text output)
assert 'RΒ²' in text or 'R^2' in text
# Check fitted values match data
np.testing.assert_array_almost_equal(y_fitted, y, decimal=10)
@pytest.mark.parametrize("slope,n_points", [
(1.0, 10),
(2.5, 50),
(-1.5, 100),
])
def test_ajlineal_various_slopes(slope, n_points):
"""Test linear fitting with various slopes and data sizes."""
x = np.linspace(0, 10, n_points)
y = slope * x + np.random.normal(0, 0.1, n_points)
data = pd.DataFrame({'x': x, 'y': y})
text, _, _ = ajlineal(data, 'x', 'y')
# Even with noise, should be good fit (RΒ² is included in text output)
assert 'RΒ²' in text or 'R^2' in textFitting functions return a 4-tuple from generic_fit (and from get_fitting_function / custom evaluator):
(text: str, y_fitted: NDArray, equation: str, fit_info: Optional[dict])Callers that only need the main result use the first three; fit_info holds fit metadata. text contains formatted parameters, uncertainties, RΒ², and statistics.
Data loaders return pandas DataFrame
Plot functions return path to saved plot (str or Path)
- Use
FittingErrorfor curve fitting failures - Use
DataLoadErrorfor data loading problems - Use standard Python exceptions for other errors
- Always log errors before raising
Example:
from utils.exceptions import FittingError
from utils.logger import get_logger
logger = get_logger(__name__)
def risky_operation(data):
try:
result = perform_fit(data)
return result
except RuntimeError as e:
logger.error(f"Fitting failed: {e}", exc_info=True)
raise FittingError(f"Could not fit data: {str(e)}")Use the logger from utils.logger:
from utils.logger import get_logger
logger = get_logger(__name__)
logger.debug("Detailed diagnostic information")
logger.info("General informational message")
logger.warning("Warning message")
logger.error("Error message", exc_info=True) # Include traceback
logger.critical("Critical error")Always include type hints:
from typing import Optional, List, Tuple, Callable
from numpy.typing import NDArray
import numpy as np
def process_data(
data: NDArray[np.floating],
threshold: float,
callback: Optional[Callable[[float], None]] = None
) -> Tuple[NDArray[np.floating], List[int]]:
"""Process data and return results."""
...- Use vectorized operations instead of loops
- Pre-allocate arrays when possible
- Use in-place operations to reduce memory
# Bad - loop
result = []
for x_val in x:
result.append(a * x_val + b)
result = np.array(result)
# Good - vectorized
result = a * x + b
# Bad - creates new array
data = data + 1
# Good - in-place
data += 1Use functools.lru_cache for expensive computations:
from functools import lru_cache
@lru_cache(maxsize=128)
def expensive_computation(param1: float, param2: float) -> float:
"""Cache results of expensive computation."""
...Import heavy libraries only when needed:
def function_using_scipy():
"""Function that uses SciPy."""
# Import here, not at module level
from scipy.optimize import curve_fit
result = curve_fit(...)
return resultUse Google-style docstrings:
def example_function(param1: int, param2: str, param3: Optional[float] = None) -> bool:
"""
Short one-line summary.
Longer description with more details about what the function does,
including any important algorithms or mathematical formulations.
Args:
param1: Description of first parameter
param2: Description of second parameter
param3: Optional parameter with default value
Returns:
Description of return value
Raises:
ValueError: When param1 is negative
KeyError: When param2 not found in database
Examples:
>>> example_function(5, "test")
True
>>> example_function(0, "hello", 3.14)
False
Note:
Additional notes or warnings for users.
"""
...For detailed documentation of each module, see the individual module pages:
- Core: config, i18n
- Fitting: fitting_functions, fitting_utils, workflow_controller, custom_function_evaluator, estimators
- Loaders: data_loader, loading_utils, saving_utils
- Data Analysis: data_analysis
- Plotting: plot_utils
- Frontend: ui_main_menu, ui_dialogs, image_utils, keyboard_nav, window_utils, streamlit_app
- Utils: exceptions, logger, validators
See Contributing Guide for:
- Code style guidelines
- Pull request process
- Development workflow
- Testing requirements
Last updated: February 2026.