Karlsruhe Fit Environment 2 (kafe2)
kafe2 (Karlsruhe Fit Environment 2) is an open-source Python package (current version 2.11.0) for likelihood-based parameter estimation and elementary data analysis. Primarily used for fitting models to measured data and visualizing results, it provides a user-friendly interface for state-of-the-art statistical methods, relying on established numerical and optimization libraries like NumPy and SciPy. The library aims to offer an easy-to-use and performance-optimized pipeline for data analysis, including parameter confidence intervals and publication-quality plots. It typically sees a few releases per year.
Warnings
- breaking Argument order for all wrapper functions (e.g., `xy_fit`, `hist_fit`) changed in v2.8.0. The model function is now accepted as the first argument, followed by the data.
- breaking Python version support has changed across minor releases. Python 3.6 was dropped in v2.8.1, and Python 3.8 was dropped in v2.11.0. Python 3.13 was added in v2.11.0.
- gotcha The optional `iminuit` minimizer, if used, requires a C++ compiler to be available on your system during installation. Installation may fail or default to SciPy's minimizers if not present.
- gotcha As of v2.11.0, the model function code consistently uses function signatures instead of bytecode. While this enables overriding `__signature__`, custom model functions that relied on specific bytecode introspection might behave differently.
Install
-
pip install kafe2
Imports
- kafe2
import kafe2
- XYFit, Plot, XYContainer, ContoursProfiler
from kafe2 import XYFit, Plot, XYContainer, ContoursProfiler
Quickstart
import kafe2
import numpy as np
import matplotlib.pyplot as plt
# 1. Define your data
x_data = np.array([1.0, 2.0, 3.0, 4.0])
y_data = np.array([2.3, 4.2, 7.5, 9.4])
# 2. Perform a fit using the simplified functional interface
# A 'line' model is used by default if not specified explicitly.
fit_object = kafe2.xy_fit(
"line",
x_data,
y_data,
x_error=0.1,
y_error=[0.40, 0.45, 0.40, 0.25] # Point-wise y errors
)
# 3. Plot the results
plot_object = kafe2.plot(fit_object, x_label="$t$ [s]", y_label="$h$ [m]")
plt.show()