.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/fits/fit_fitter.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_fits_fit_fitter.py: ============================== Curve Fitting with BinFitterCP ============================== Demonstrates binned curve fitting with polynomial functions and confidence intervals. BinFitterCP is useful for analyzing driver-response relationships in ecosystem data with uncertainty quantification. Best for: Understanding nonlinear relationships between flux drivers and response variables. .. GENERATED FROM PYTHON SOURCE LINES 14-23 NEE Response to Vapor Pressure Deficit ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Fit a quadratic relationship between net ecosystem exchange (NEE) and vapor pressure deficit (VPD) using summer daytime data. This example shows: 1. Data filtering for specific conditions 2. Binning and aggregation 3. Curve fitting with confidence intervals 4. Visualization with prediction bands .. GENERATED FROM PYTHON SOURCE LINES 23-115 .. code-block:: Python import pandas as pd import diive as dv # Load example data df_orig = dv.load_exampledata_parquet() # Variables vpd_col = 'VPD_f' ta_col = 'Tair_f' nee_col = 'NEE_CUT_REF_f' xcol = vpd_col ycol = nee_col print("BinFitterCP - Driver-Response Analysis") print("=" * 50) # Filter for summer daytime with adequate radiation subset = df_orig.loc[(df_orig.index.month >= 6) & (df_orig.index.month <= 9)].copy() subset = subset.loc[subset['Rg_f'] > 200] print(f"\nData Filtering:") print(f" Original records: {len(df_orig)}") print(f" Summer daytime (Jun-Sep, Rg>200): {len(subset)} records") # Convert units subset[vpd_col] = subset[vpd_col].multiply(0.1) # hPa --> kPa subset[nee_col] = subset[nee_col].multiply(0.0792171) # umol CO2 m-2 s-1 --> g CO2 m-2 30min-1 print(f"\nUnit Conversions:") print(f" VPD: hPa -> kPa") print(f" NEE: umol CO2 m-2 s-1 -> g CO2 m-2 30min-1") print(f"\nVariable Statistics:") print(f" {vpd_col}: {subset[vpd_col].min():.2f} to {subset[vpd_col].max():.2f} kPa") print(f" {nee_col}: {subset[nee_col].min():.2f} to {subset[nee_col].max():.2f} g CO2 m-2 30min-1") # Format labels y_units = r"gCO_{2}\ m^{-2}\ 30min^{-1}" ylabel = f"NEE (${y_units}$)" x_units = "kPa" xlabel = f"Vapor Pressure Deficit (${x_units}$)" print(f"\nCurve Fitting") print("=" * 50) # Create fitter and run bf = dv.analysis.BinFitterCP( df=subset, xcol=xcol, ycol=ycol, n_predictions=1000, n_bins_x=10, # Bin into 10 equal-width bins bins_y_agg='mean', fit_type='quadratic_offset' # y = ax² + bx + c ) bf.run() fit_results = bf.fit_results print(f"\nFitting Configuration:") print(f" Fit type: {fit_results['fit_type']}") print(f" Number of bins: 10") print(f" Aggregation: {fit_results['yvar']} mean per bin") print(f" Fit equation: {fit_results['fit_equation_str']}") print(f" R²: {fit_results['fit_r2']:.4f}") n_vals_min = int(fit_results['n_vals_per_bin']['min']) n_vals_max = int(fit_results['n_vals_per_bin']['max']) print(f"\nBin Statistics:") print(f" Values per bin: {n_vals_min} to {n_vals_max}") print(f" Total bins: {len(fit_results['bin_df'])}") # Display plot print(f"\nVisualization") print("=" * 50) try: bf.showplot( show_unbinned_data=True, show_bin_variation=True, showfit=True, show_prediction_interval=True, title='NEE Response to Vapor Pressure Deficit (Summer Daytime)', xlabel=xlabel, ylabel=ylabel, unbinned_alpha=0.5 ) print("[OK] Fit plot generated successfully") except Exception as e: print(f"Plot display info: {type(e).__name__}") .. image-sg:: /auto_examples/fits/images/sphx_glr_fit_fitter_001.png :alt: NEE Response to Vapor Pressure Deficit (Summer Daytime) :srcset: /auto_examples/fits/images/sphx_glr_fit_fitter_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none > Loaded .parquet file /home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.049 seconds). BinFitterCP - Driver-Response Analysis ================================================== Data Filtering: Original records: 175296 Summer daytime (Jun-Sep, Rg>200): 19674 records Unit Conversions: VPD: hPa -> kPa NEE: umol CO2 m-2 s-1 -> g CO2 m-2 30min-1 Variable Statistics: VPD_f: 0.00 to 3.21 kPa NEE_CUT_REF_f: -3.06 to 1.40 g CO2 m-2 30min-1 Curve Fitting ================================================== Fitting Configuration: Fit type: quadratic_offset Number of bins: 10 Aggregation: NEE_CUT_REF_f mean per bin Fit equation: y = 0.0123x^2+0.1598x-0.8172 R²: 0.9761 Bin Statistics: Values per bin: 1966 to 1968 Total bins: 10 Visualization ================================================== [OK] Fit plot generated successfully .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.491 seconds) .. _sphx_glr_download_auto_examples_fits_fit_fitter.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: fit_fitter.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: fit_fitter.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: fit_fitter.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_