### Get kafe2go help Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide_kafe2go.rst This command displays the help information for kafe2go, providing details on available command-line arguments for customizing output. ```bash kafe2go --help ``` -------------------------------- ### Create Development Environment for kafe2 Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/developer_guide.rst This command creates a virtual environment for kafe2 development, installs all necessary dependencies, and sets up the kafe2 package in editable mode. ```bash make devenv ``` -------------------------------- ### Python Object-Oriented Programming for Fits Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Demonstrates the basic object-oriented approach in kafe2 for performing fits, equivalent to initial examples. It sets up data and a model for a straight-line fit, considering x-errors. ```python from kafe2 import XYFit from kafe2.utils import XYContainer # Load data data = XYContainer.from_file("my_data.txt") # Define model def straight_line(x, a, b): return a * x + b # Create fit object fit = XYFit(data, straight_line) # Perform fit fit.do_fit() # Print fit results print(fit.summary()) ``` -------------------------------- ### Install kafe2 using setuptools Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs kafe2 directly from source using setuptools after downloading the package. This is an alternative to installing from PyPI. ```bash python -m build ``` -------------------------------- ### Run kafe2go on Linux/MacOS Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide_kafe2go.rst This command executes the kafe2go program on Linux or MacOS systems after kafe2 has been installed. It requires a YAML file specifying the fit configuration. ```bash kafe2go path/to/fit.yml ``` -------------------------------- ### Applying Parameter Constraints in Kafe2go Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Shows how to apply parameter constraints within the kafe2go command-line tool. This is useful for fixing parameters or defining their expected ranges during a fit, illustrated with a damped harmonic oscillator example. ```yaml fit: name: "damped_oscillator" input_file: "constraints.yml" model: name: "DampedOscillator" parameters: g: { value: 9.81, uncertainty: 0.01, constraint: "gaussian(9.81, 0.01)" } # other parameters... ``` -------------------------------- ### Install pip using easy_install Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the pip package installer using the easy_install command, which is included with setuptools. ```bash easy_install pip ``` -------------------------------- ### kafe2go Configuration Examples (YAML) Source: https://github.com/philfitters/kafe2/blob/master/examples/README.rst Provides examples of YAML files intended for use with kafe2go, a command-line interface for kafe2. These files define fitting configurations. ```YAML # Example YAML configuration for kafe2go # model: # name: line # data: # file: data.csv # fit: # parameters: # slope: 1.0 # intercept: 0.0 ``` -------------------------------- ### YAML Configuration for Linear Fit in Kafe2 Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst An example YAML configuration file for defining a linear model function in Kafe2. This snippet shows the basic structure for defining a linear model with parameters. ```yaml model_function: name: "linear" function: "m*x + c" parameters: m: { value: 1.0 } c: { value: 0.0 } ``` -------------------------------- ### Run kafe2go Fit Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This command-line snippet demonstrates how to initiate a fitting process using the kafe2go tool by specifying the path to a YAML configuration file. ```bash kafe2go path/to/fit.yml ``` -------------------------------- ### Python Poisson Cost Function Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This Python code snippet is part of an example demonstrating a Poisson cost function. It is extracted from a larger script, focusing on lines 35 onwards. ```python # Example of a Poisson cost function # This section is part of a larger script focusing on cost functions. ``` -------------------------------- ### Kafe2go Command for Multiple Fits Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Illustrates how to use the kafe2go command-line tool to process multiple YAML fit files. ```bash kafe2go path/to/fit1.yml path/to/fit2.yml ``` -------------------------------- ### Example: Straight Line Fitting with kafe2 Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb A placeholder comment indicating a simple example for fitting a straight line using kafe2. This comment is intended to be followed by executable code. ```Python # einfaches Beispiel: Geradenanpassung mit kafe2 ``` -------------------------------- ### Build Documentation for kafe2 Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/developer_guide.rst Commands to build the documentation for kafe2. It includes options for generating HTML or PDF output and cleaning generated files. ```bash make docs ``` ```bash make html ``` ```bash make latex ``` ```bash make clean ``` -------------------------------- ### Install kafe2 using pip Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the 'kafe2' package from PyPI using pip in the WinPython command prompt. ```bash pip install kafe2 ``` -------------------------------- ### Python Double Slit Experiment Fit Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Implements a fit for the double-slit experiment, a real-world scenario requiring handling of asymmetric parameter uncertainties due to the highly non-linear nature of the model. This example showcases advanced fitting capabilities. ```python from kafe2 import XYFit from kafe2.utils import XYContainer import numpy as np # Define the double slit model function def double_slit(x, I0, d, lambda_val, offset): # ... (implementation of double slit function) pass # Load or generate double slit data data = XYContainer.from_file("double_slit_data.txt") # Create and perform the fit fit = XYFit(data, double_slit) fit.set_parameters({ "I0": 1.0, "d": 1e-4, "lambda_val": 500e-9, "offset": 0.0 }) fit.do_fit() # Display results and potentially contours print(fit.summary()) # fit.plot_contours("double_slit_contours.png") # Uncomment to save contours ``` -------------------------------- ### YAML Configuration for Exponential Fit in Kafe2 Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst An example YAML configuration file for defining an exponential model function in Kafe2. This snippet highlights the definition of the model function, which is crucial for fitting exponential data. ```yaml model_function: name: "exponential" function: "A0 * exp(x / x0)" parameters: A0: { value: 1.0, min: 0.0, max: 10.0 } x0: { value: 1.0, min: -5.0, max: 5.0 } ``` -------------------------------- ### Run kafe2go Line Fit Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This command-line snippet executes a line fit using the kafe2go tool, referencing a specific YAML configuration file named 'line_fit.yml'. ```bash kafe2go line_fit.yml ``` -------------------------------- ### Install iminuit Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the iminuit package, a Python wrapper for the Minuit minimizer, using pip. The --upgrade option can be used to update an existing installation. ```bash pip install iminuit ``` -------------------------------- ### Install kafe2 using pip Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the kafe2 package and its minimal dependencies using the pip package installer. This command can be run system-wide or for the current user. ```bash pip install kafe2 ``` -------------------------------- ### Heating Resistor Data Example in kafe2 Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_en.ipynb Provides the experimental data (Voltage, Current, Temperature) and their uncertainties for the heating resistor example, used to demonstrate the multi-fit capabilities of kafe2. ```Python # the data U = [ 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0 ] I = [ 0.5, 0.89, 1.41, 1.67, 2.3, 2.59, 2.77, 3.57, 3.94, 4.24, 4.73, 4.87, 5.35, 5.74, 5.77, 6.17, 6.32, 6.83, 6.87, 7.17 ] T = [ 20.35, 20.65, 22.25, 23.65, 26.25, 27.85, 29.85, 34.25, 37.75, 41.95, 44.85, 50.05, 54.25, 60.55, 65.05, 69.95, 76.85, 81.55, 85.45, 94.75 ] sigU, sigI, sigT = 0.2, 0.1, 0.5 # uncertainties ``` -------------------------------- ### Install iminuit using pip Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the 'iminuit' package, a dependency for kafe2, using pip within the WinPython command prompt. ```bash pip install 'iminuit' ``` -------------------------------- ### Run kafe2go on Windows Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide_kafe2go.rst This command executes the kafe2go program on Windows systems. It requires a YAML file specifying the fit configuration and uses a .py extension for the executable. ```bash kafe2go.py path/to/fit.yml ``` -------------------------------- ### Install pip on Ubuntu/Debian Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the pip package installer for Python 3 on Ubuntu, Mint, and Debian-based systems using the apt-get package manager. ```bash apt-get install python3-pip ``` -------------------------------- ### Kafe2go Command for Contour Plot Generation Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Demonstrates how to use the kafe2go command-line tool to generate contour plots for all specified fits. ```bash kafe2go path/to/fit1.yml path/to/fit2.yml --separate -c ``` -------------------------------- ### Install Python headers for iminuit on Ubuntu/Debian Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the Python development headers for Python 3 on Ubuntu, Mint, and Debian-based systems, which may be needed for iminuit to compile. ```bash apt-get install libpython3-dev ``` -------------------------------- ### Kafe2go Command for Multiple Fits with Separate Plots Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Shows how to use the kafe2go command-line tool to process multiple YAML fit files and display them in separate figures. ```bash kafe2go path/to/fit1.yml path/to/fit2.yml --separate ``` -------------------------------- ### Accessing Kafe2 Fit Results as Python Variables Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This example illustrates how to retrieve the results of a Kafe2 fit directly as Python variables, rather than relying solely on human-readable reports or plots. This allows for programmatic access to fitted parameters and uncertainties. ```python from kafe.fit import Fit # Assuming 'fit' is a performed Fit object # Access fitted parameters fitted_params = fit.get_params() print(f"Fitted parameters: {fitted_params}") # Access uncertainties uncertainties = fit.get_uncertainties() print(f"Uncertainties: {uncertainties}") # Access chi-squared value chi_squared = fit.get_chi_squared() print(f"Chi-squared: {chi_squared}") ``` -------------------------------- ### Install Python headers for iminuit on Fedora/RHEL/CentOS Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the Python development headers for Python 3 on Fedora, RHEL, and CentOS systems, which may be needed for iminuit to compile. ```bash yum install python3-devel ``` -------------------------------- ### Install ROOT on Fedora/RHEL/CentOS Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs ROOT and its Python bindings on Fedora, RHEL, and CentOS systems. ```bash yum install root root-python ``` -------------------------------- ### Configure ROOT environment variables Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Sets environment variables required for Python to recognize ROOT and PyROOT bindings when ROOT is built from source. ```bash export ROOTSYS= export LD_LIBRARY_PATH=$ROOTSYS/lib:$PYTHONDIR/lib:$LD_LIBRARY_PATH export PYTHONPATH=$ROOTSYS/lib:$PYTHONPATH ``` -------------------------------- ### Install kafe2 using pip Source: https://github.com/philfitters/kafe2/blob/master/README.rst This command installs the latest stable version of the kafe2 Python package using pip. Ensure you have a C++ compiler available for the iminuit dependency, which is recommended for optimal performance. ```bash pip install kafe2 ``` -------------------------------- ### Install ROOT on Ubuntu/Debian Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs ROOT system and its Python bindings for older versions of Ubuntu and related Linux distributions. ```bash apt-get install root-system libroot-bindings-python5.34 libroot-bindings-python-dev ``` -------------------------------- ### YAML Configuration for Line Fit Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This YAML snippet defines the configuration for a line fit, including data points and model parameters. It is intended to be used with the kafe2go command-line tool. ```yaml data: x: [1, 2, 3, 4, 5] y: [2.1, 3.9, 6.2, 8.1, 9.8] model: name: line parameters: a: 2.0 b: 0.0 ``` -------------------------------- ### Myon Lifetime Data Example Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_en.ipynb Provides a dataset representing measured values for the myon lifetime example, likely used for fitting or analysis within the kafe2 library. ```Python ''' the data for the myon life time example''' # real data from measurement with a Water Cherenkov detector ("Kamiokanne") dT = [7.42, 3.773, 5.968, 4.924, 1.468, 4.664, 1.745, 2.144, 3.836, 3.132, 1.568, 2.352, 2.132, 9.381, 1.484, 1.181, 5.004, 3.06, 4.582, 2.076, 1.88, 1.337, 3.092, 2.265, 1.208, 2.753, 4.457, 3.499, 8.192, 5.101, 1.572, 5.152, 4.181, 3.52, 1.344, 10.29, 1.152, 2.348, 2.228, 2.172, 7.448, 1.108, 4.344, 2.042, 5.088, 1.02, 1.051, 1.987, 1.935, 3.773, 4.092, 1.628, 1.688, 4.502, 4.687, 6.755, 2.56, 1.208, 2.649, 1.012, 1.73, 2.164, 1.728, 4.646, 2.916, 1.101, 2.54, 1.02, 1.176, 4.716, 9.671, 1.692, 9.292, 10.72, 2.164, 2.084, 2.616, 1.584, 5.236, 3.663, 3.624, 1.051, 1.544, 1.496, 1.883, 1.92, 5.968, 5.89, 2.896, 2.76, 1.475, 2.644, 3.6, 5.324, 8.361, 3.052, 7.703, 3.83, 1.444, 1.343, 4.736, 8.7, 6.192, 5.796, 1.4, 3.392, 7.808, 6.344, 1.884, 2.332, 1.76, 4.344, 2.988, 7.44, 5.804, 9.5, 9.904, 3.196, 3.012, 6.056, 6.328, 9.064, 3.068, 9.352, 1.936, 1.08, 1.984, 1.792, 9.384, 10.15, 4.756, 1.52, 3.912, 1.712, 10.57, 5.304, 2.968, 9.632, 7.116, 1.212, 8.532, 3.000, 4.792, 2.512, 1.352, 2.168, 4.344, 1.316, 1.468, 1.152, 6.024, 3.272, 4.96, 10.16, 2.14, 2.856, 10.01, 1.232, 2.668, 9.176 ] ``` -------------------------------- ### Fit Line to XY Data with Uncertainties (Python) Source: https://github.com/philfitters/kafe2/blob/master/examples/README.rst Illustrates fitting a line to data points with uncertainties in both the x and y directions using kafe2. This example is suitable for understanding basic fitting procedures. ```Python import kafe2 # Example usage for 001_line_fit (assuming data and model are defined) # fit_result = kafe2.fit(data, model) # print(fit_result) ``` -------------------------------- ### Fit Probability Distribution to Binned Data (Python) Source: https://github.com/philfitters/kafe2/blob/master/examples/README.rst Demonstrates how to bin one-dimensional data and fit a probability distribution to it using kafe2. This is a common technique in statistical analysis. ```Python import kafe2 # Example usage for 009_histogram_fit (assuming binned_data and distribution_model) # fit_result = kafe2.fit(binned_data, distribution_model) # print(fit_result) ``` -------------------------------- ### Fit Model to Indexed Data (Python) Source: https://github.com/philfitters/kafe2/blob/master/examples/README.rst Shows how to perform a fit using a model on indexed data with kafe2. This is useful for datasets with inherent ordering or grouping. ```Python import kafe2 # Example usage for 008_indexed_fit (assuming indexed_data and model) # fit_result = kafe2.fit(indexed_data, model) # print(fit_result) ``` -------------------------------- ### Handle Multiple Fits with Shared Parameters (Python) Source: https://github.com/philfitters/kafe2/blob/master/examples/README.rst Illustrates how to manage several fits that share the same parameters in kafe2, either by using constraints or a MultiFit object. This is useful for comparative analysis. ```Python import kafe2 # Example usage for 011_multifit (assuming multiple datasets and a shared model) # multi_fit = kafe2.MultiFit(datasets, model) # fit_results = multi_fit.fit() # print(fit_results) ``` -------------------------------- ### Object-Oriented XY Fitting with Kafe2 Source: https://github.com/philfitters/kafe2/blob/master/examples/001_line_fit/README.rst Presents a more general and flexible approach to fitting XY data using the object-oriented interface of the Kafe2 library. This example addresses the same problem as the line fitting example but leverages the power of Python classes for greater control and extensibility. ```Python # This snippet is a placeholder for the actual code found in: # /003_profiling/00_object_oriented_programming.py # The actual implementation would involve defining model classes, # data handling, and fitting procedures using Kafe2's OO interface. ``` -------------------------------- ### Kafe2: Ensemble Test (Not Production Ready) Source: https://github.com/philfitters/kafe2/blob/master/case_studies/README.rst Provides an example of how to perform an ensemble test using kafe2. Note that this functionality is explicitly marked as not production-ready. ```Python import kafe2 # Example usage for ensemble_test.py (conceptual) def perform_ensemble_test(): # Define models and datasets for ensemble # Run ensemble test using kafe2 print("Performing ensemble test with kafe2 (not production ready)...") pass perform_ensemble_test() ``` -------------------------------- ### YAML Unbinned Fit Configuration Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This YAML snippet configures an unbinned fit for kafe2go. It specifies the fit type and demonstrates how to fix single parameters and limit background values. ```yaml type: unbinned # Example of fixing parameters and limiting background fbg: [0, 100] ``` -------------------------------- ### Specify equidistant binning in YAML Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide_kafe2go.rst This YAML configuration defines equidistant binning for histogram fits by specifying the number of bins and the range. For example, 10 bins between -5 and 5. ```yaml n_bins: 10 bin_range: [-5, 5] ``` -------------------------------- ### Fill HistContainer with data in Python Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide.rst Shows two methods for filling a HistContainer with data in kafe2: directly during initialization using `fill_data` or afterwards using the `fill` method. ```python from kafe2 import HistContainer histogram = HistContainer(n_bins=10, bin_range=(-5, 5), fill_data=[-7.5, 1.23, 5.74, 1.9, -0.2, 3.1, -2.75, ...]) # Alternative way histogram = HistContainer(n_bins=10, bin_range=(-5, 5)) histogram.fill([-7.5, 1.23, 5.74, 1.9, -0.2, 3.1, -2.75, ...]) ``` -------------------------------- ### Breit-Wigner Example Data Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Provides sample data for a Breit-Wigner analysis, including center-of-mass energies (E), their errors, hadronic cross sections (sig), and their errors. It also includes correlated errors. ```Python ''' the data for the Breit-Wigner example''' # Center-of-mass energies E (GeV): ECM = [ 88.387, 89.437, 90.223, 91.238, 92.059, 93.004, 93.916 ] E_errors = [ 0.005, 0.0015, 0.005, 0.003, 0.005, 0.0015, 0.005 ] ECor_abs = 0.0017 # Correlated absolute errors. # Hadronic cross sections with photonic corrections applied (nb): sig = [6.803, 13.965, 26.113, 41.364, 27.535, 13.362, 7.302 ] sig_errors = [ 0.036, 0.013, 0.075, 0.010, 0.088, 0.015, 0.045 ] sigCor_rel = 0.0007 ``` -------------------------------- ### Specify Constraints for Model Parameters (Python) Source: https://github.com/philfitters/kafe2/blob/master/examples/README.rst Shows how to define constraints for model parameters in kafe2, which can be an alternative to error propagation. It also covers limiting parameters to specific intervals. ```Python import kafe2 # Example usage for 004_constraints (assuming data, model, and constraints) # constraints = {'param1': {'min': 0, 'max': 10}} # fit_result = kafe2.fit(data, model, constraints=constraints) # print(fit_result) ``` -------------------------------- ### Get Confidence Contours as Arrays Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide.rst Retrieves the data for confidence contours between two specified parameters as numpy arrays. This is useful for detailed analysis or custom visualization of parameter correlations. ```python vals1, vals2 = get_contours('', 'n') ``` -------------------------------- ### Get Profile Data as Arrays Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide.rst Retrieves the data for a parameter profile as numpy arrays. This function allows for programmatic access to the profile likelihood data, enabling further custom analysis or plotting. ```python xp, cost_function = cpf.get_profile('', low=lower_bound, high=upper_bound) ``` -------------------------------- ### Fit Probability Distribution to Unbinned Data (Python) Source: https://github.com/philfitters/kafe2/blob/master/examples/README.rst Explains how to fit a probability distribution directly to data without binning it using kafe2. This method can preserve more information from the original data. ```Python import kafe2 # Example usage for 010_unbinned_fit (assuming data and distribution_model) # fit_result = kafe2.fit(data, distribution_model) # print(fit_result) ``` -------------------------------- ### Python Data and Fit Serialization Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Demonstrates how to save kafe2 fit objects and datasets to files in a kafe2go-compatible YAML format, and how to load them back using corresponding classes. This facilitates data persistence and sharing. ```python from kafe2 import XYFit from kafe2.utils import XYContainer # Assuming 'fit' is an existing XYFit object and 'data' is an XYContainer # Save data to file data.to_file("my_data.yml") # Save fit to file fit.to_file("my_fit.yml") # Load data from file loaded_data = XYContainer.from_file("my_data.yml") # Load fit from file loaded_fit = XYFit.from_file("my_fit.yml") print("Data and fit saved and loaded successfully.") ``` -------------------------------- ### Prepare Data and Fit Objects for MultiFit Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Illustrates the process of preparing data containers (XYContainer) and Fit objects for two different datasets (Temperature vs. Voltage and Current vs. Voltage). These are then used to create a MultiFit object. ```Python # Step 1: construct the singular data containers and fit objects TU_data = XYContainer(U,T) TU_data.label = 'Temperaturen' TU_data.axis_labels = ['Spannung (V)','Temperatur (°C)'] fit_1 = Fit(TU_data, model_function=empirical_T_U_model) fit_1.model_label = 'Parametrisierung' IU_data = XYContainer(U,I) IU_data.label = 'Ströme' IU_data.axis_labels = ['Spannung (V)','Strom (A)'] fit_2 = Fit(IU_data, model_function=I_U_model) fit_2.model_label = 'Temperaturabhängiger Leitwert' ``` -------------------------------- ### Install pip on Fedora/RHEL/CentOS Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the pip package installer for Python 3 on Fedora, RHEL, and CentOS systems using the yum package manager. ```bash yum install python3-pip ``` -------------------------------- ### Python Histogram Fit Implementation Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This Python code demonstrates how to perform a histogram fit using the kafe2.hist_fit wrapper or by using kafe2.HistContainer and kafe2.HistFit objects. It covers setting histogram limits and bins. ```python # To use a histogram fit in a Python script you can use the wrapper :python:`kafe2.hist_fit`. # Alternatively you can use objects via :python:`from kafe2 import HistContainer, HistFit`. # The creation of a histogram requires the user to set the limits of the histogram and the amount of # bins. Alternatively the bin edges for each bin can be set manually. ``` -------------------------------- ### Kafe2go Command for Multiple Fits with Separate Plots and Contours Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Demonstrates how to use the kafe2go command-line tool to process multiple YAML fit files, display them in separate figures, and generate contour plots for each fit. ```bash kafe2go path/to/fit1.yml path/to/fit2.yml --separate -c ``` -------------------------------- ### Install specific iminuit version Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs a specific version of the iminuit package, such as version 1.5.4, using pip. ```bash pip install iminuit==1.5.4 ``` -------------------------------- ### Run Unit Tests for kafe2 Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/developer_guide.rst Executes Python 3 unit tests for kafe2 and generates code coverage reports. It allows for general reports or specific file coverage analysis. ```bash make test ``` ```bash coverage report ``` ```bash coverage report -m path/to/file ``` ```bash coverage html ``` -------------------------------- ### Install Tkinter on Fedora/RHEL/CentOS Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the Tkinter library for Python 3 on Fedora, RHEL, and CentOS systems, which is required by matplotlib. ```bash yum install python3-tkinter ``` -------------------------------- ### YAML Configuration for Non-linear Fit Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This YAML file configures a non-linear fit, likely for use with kafe2go. It specifies parameters and settings for the fitting process, including details relevant to profiling and uncertainty estimation. ```yaml fit: name: "non_linear_fit" # ... other fit configurations ``` -------------------------------- ### Install Tkinter on Ubuntu/Debian Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/installation.rst Installs the Tkinter library for Python 3 on Ubuntu, Mint, and Debian-based systems, which is required by matplotlib. ```bash apt-get install python3-tk ``` -------------------------------- ### Saving Kafe2 Objects to YAML Files Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Demonstrates the process of converting Kafe2 objects, such as fit configurations or results, into the human-readable YAML format and saving them to files. These files can be used for later loading into Python or as input for kafe2go. ```python from kafe.fit import Fit import yaml # Assuming 'fit' is a configured or performed Fit object # Convert fit object to a dictionary representation fit_dict = fit.to_dict() # Save to a YAML file with open("fit_config.yml", 'w') as f: yaml.dump(fit_dict, f) ``` -------------------------------- ### Object-Oriented Python Fit Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This Python snippet illustrates the object-oriented approach to using the kafe2 library for fitting. It shows how to import specific classes like XYFit and Plot for more granular control over the fitting process. ```python from kafe2 import XYFit, Plot ``` -------------------------------- ### Python Script for Line Fit Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst This Python script performs a line fit using the kafe2 library. It likely involves defining data, specifying a line model, executing the fit, and potentially plotting the results. ```python import kafe2 # Define data x_data = [1, 2, 3, 4, 5] y_data = [2.1, 3.9, 6.2, 8.1, 9.8] y_error = [0.1, 0.2, 0.1, 0.3, 0.2] # Perform the line fit fit_result = kafe2.xy_fit("line", x_data, y_data, y_error=y_error) # Plot the results kafe2.plot(fit_result) ``` -------------------------------- ### Configure Logging for Minimizer Output (Python) Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide.rst Illustrates how to enable and configure logging for minimizer output before calling `do_fit`. This allows viewing the progress and details of the fitting process, particularly for `scipy` and `iminuit` minimizers. Increasing the logging level to `DEBUG` provides more verbose output. ```python import logging logger = logging.getLogger() logger.setLevel(logging.INFO) ``` -------------------------------- ### Perform Fit with Fit Object in Python Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Shows how to create a Fit object using a data container and then perform the fit using the do_fit method. It also mentions the possibility of setting parameter constraints before fitting. ```Python from kafe2 import Fit # Assuming xy_data is a defined XYContainer object line_fit = Fit(data=xy_data) line_fit.do_fit() ``` -------------------------------- ### Fit PDF to 1D Histogram Data (Python) Source: https://github.com/philfitters/kafe2/blob/master/examples/009_histogram_fit/README.rst Demonstrates how to fit a probability density function (PDF) to binned one-dimensional data using Kafe2. This is a fundamental example for statistical analysis and model fitting. ```Python import sys import os # Add Kafe2 to the Python path sys.path.append(os.path.abspath('../..')) from kafe2 import FitManager, FitConfiguration from kafe2.utils import plot_fit_results # Example usage (assuming you have data and a model defined elsewhere) # config = FitConfiguration(data_file='your_data.txt', model_name='your_model') # fm = FitManager(config) # fm.do_fit() # plot_fit_results(fm) print('Example: 01_histogram_fit.py') print('This script shows how to fit a probability density function to binned one-dimensional data.') ``` -------------------------------- ### Simple Function Fitting with kafe2 Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Demonstrates a basic usage of kafe2 for fitting a line to data points. It shows how to define x and y data, specify uncertainties for both, and then use `kafe2.xy_fit` to perform the fit. The example also includes how to generate a plot of the fitted data using `kafe2.plot`. ```Python # Define or read in the data for your fit: x_data = [1.0, 2.0, 3.0, 4.0] y_data = [2.3, 4.2, 7.5, 9.4] # x_data and y_data are combined depending on their order. # The above translates to the points (1.0, 2.3), (2.0, 4.2), (3.0, 7.5), and (4.0, 9.4). # Important: Specify uncertainties for the data! x_error = 0.1 y_error = 0.4 # Pass the information to kafe2: kafe2.xy_fit("line", x_data, y_data, x_error=x_error, y_error=y_error) # The string "line" gets mapped to a first degree polynomial for the model function. # Call another function to create a plot: kafe2.plot( x_label="x", # x axis label y_label="y", # y axis label data_label="Data", # label of data in legend ) ``` -------------------------------- ### Use kafe2 Convenience Features (Python) Source: https://github.com/philfitters/kafe2/blob/master/examples/README.rst Demonstrates using kafe2 functionalities beyond direct fitting, such as changing plot colors or saving fit results to files. This enhances the usability of the fitting process. ```Python import kafe2 # Example usage for 005_convenience (assuming fit_result is obtained) # kafe2.save_fit_results(fit_result, 'fit_output.txt') # kafe2.set_plot_color('blue') ``` -------------------------------- ### Myon Life Time Data Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb A placeholder comment indicating that the following code or data relates to the myon life time example. ```Python ''' the data for the myon life time example''' ``` -------------------------------- ### Save fit object to YAML Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide_kafe2go.rst This Python method saves a fit object to a YAML file, allowing the configuration to be stored and reused. ```python fit_object.to_file("path/to/fit.yml") ``` -------------------------------- ### Constrain Model Parameters to Measurements Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Adds constraints to model parameters based on measured values and their uncertainties. This is useful for guiding the fitting process with known data. ```python fit.add_parameter_constraint(name='l', value=lm, uncertainty=delta_lm) fit.add_parameter_constraint(name='r', value=rm, uncertainty=delta_rm) fit.add_parameter_constraint(name='a0', value=a0m, uncertainty=delta_a0m, relative=True) ``` -------------------------------- ### Perform Fit and Access Results Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Executes the fitting process and demonstrates how to access various fit results, including the cost function value, degrees of freedom, chi-squared probability, parameter names, values, uncertainties, and the correlation matrix. ```python # Perform the fit fit.do_fit() # Optional: Print out a report on the fit results on the console. #fit.report(show_data=False, show_model=False, show_fit_results=True) # Custom printout of results: print("cost function at minimum: %.4g " % fit.cost_function_value, " number of degrees of freedom:", fit.ndf) print(" --> probability: %.1f%%" % (fit.chi2_probability * 100)) print("parameter names:\n", fit.parameter_names) np.set_printoptions(precision=5, suppress=False) print("prameter values:\n", fit.parameter_values) print("parameter uncertainties:\n",fit.parameter_errors) np.set_printoptions(precision=3, suppress=True) print("correlation matrix:\n", fit.parameter_cor_mat ) ``` -------------------------------- ### Set underflow and overflow bin heights in YAML Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide_kafe2go.rst This YAML configuration allows setting the heights for underflow and overflow bins in histogram fits. ```yaml underflow: 5 overflow: 3 ``` -------------------------------- ### Construct MultiFit Object Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Initializes a MultiFit object with a list of fit objects and specifies the minimizer to be used. This is the first step in setting up a multi-fit analysis. ```python multi_fit = MultiFit(fit_list=[fit_1, fit_2], minimizer='iminuit') ``` -------------------------------- ### Specify bin edges in YAML Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide_kafe2go.rst This YAML configuration allows direct specification of bin edges for histogram fits, enabling arbitrary distances between bins. ```yaml bin_edges: [-5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0] ``` -------------------------------- ### Definieren einer Gaußschen Dichtefunktion (PDF) Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Definiert eine Python-Funktion, die die Wahrscheinlichkeitsdichtefunktion (PDF) für eine Gaußsche Verteilung berechnet. Diese Funktion benötigt die Eingabewerte x, Mittelwert (mu) und Standardabweichung (sigma). ```Python def normal_distribution_pdf(x, mu, sigma): return np.exp(-0.5 * ((x - mu) / sigma) ** 2) / np.sqrt(2.0 * np.pi * sigma** 2) ``` -------------------------------- ### Import MultiFit for Simultaneous Fitting Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Demonstrates how to import the MultiFit class from the kafe2 library, which is necessary for performing simultaneous fits on multiple datasets with shared parameters. ```Python from kafe2 import MultiFit ``` -------------------------------- ### Create IndexedContainer and UnbinnedContainer in Python Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide.rst Shows how to create IndexedContainer and UnbinnedContainer for one-dimensional data using kafe2. These containers are used for datasets with single values. ```python from kafe2 import IndexedContainer, UnbinnedContainer idx_data = IndexedContainer([5.3, 5.2, 4.7, 4.8]) unbinned_data = UnbinnedContainer([5.3, 5.2, 4.7, 4.8]) ``` -------------------------------- ### Set Initial Parameter Values Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_en.ipynb Provides initial guesses for the fit parameters, including gravitational acceleration, initial amplitude, length, and radius, to aid convergence. ```python g_initial = 9.81 # Initial guess for g. fit.set_parameter_values(g=g_initial, a0=a0m, l=lm, r=rm) ``` -------------------------------- ### Plotting with X-Errors using Kafe2go Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/beginners_guide.rst Demonstrates how to use kafe2go to perform a line fit with asymmetric errors on the x-axis and add a grid to the contours. This involves specifying an input YAML file and using command-line flags. ```yaml fit: name: "line" input_file: "03_x_errors.yml" plot: contour: true profile: true grid: all ``` -------------------------------- ### Load fit object from YAML Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/user_guide_kafe2go.rst This Python method loads a fit configuration from a YAML file into a fit object, enabling the restoration of previous fit settings. ```python FitBase.from_file("path/to/fit.yml") ``` -------------------------------- ### Get Keywords for Plot Customization Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_en.ipynb Retrieves the available keywords for customizing a specific plot element, such as 'model_error_band'. This helps in understanding what parameters can be modified. ```Python p.get_keywords('model_error_band') ``` -------------------------------- ### Limit Parameter Based on Physical Constraints Source: https://github.com/philfitters/kafe2/blob/master/examples/jupyter_tutorial_de.ipynb Applies parameter limits derived from the physical behavior of the system. This example limits the parameter 'c' based on a condition derived from the model function. ```python c_max = 0.9 * g_initial / (lm + rm) # A little lower than our best guess for the limit. fit.limit_parameter("c", lower=1e-6, upper=c_max) ``` -------------------------------- ### Watch Uncertainty Example Source: https://github.com/philfitters/kafe2/blob/master/doc/src/parts/mathematical_foundations.rst Demonstrates the calculation of standard deviation and covariance matrix for watch uncertainty, which is correlated across all data points and proportional to the measured values. ```mathematics \bm{\sigma}_\mathrm{watch} = 0.02 \cdot \bm{d} = 0.02 \cdot \begin{pmatrix} d_1 \\ d_2 \\ d_3 \end{pmatrix}, \quad \bm{\rho}_\mathrm{watch} = \begin{pmatrix} 1 & 1 & 1 \\ 1 & 1 & 1\\ 1 & 1 & 1\end{pmatrix}, \quad \bm{V}_\mathrm{watch} = 0.0004 \cdot ```