Note
Go to the end to download the full example code.
Decoupling Analysis with Sorting Bins¶
Stratified binning to reveal how radiation response changes across temperature ranges.
How does ecosystem radiation response depend on temperature? This example bins data by vapor pressure deficit (VPD) within separate temperature classes, showing whether the relationship stays stable or changes as temperature increases.
Best for: Understanding how environmental controls interact
Load and prepare data¶
import diive as dv
# Load the dataset
data_df = dv.load_exampledata_parquet()
# Keep summer months (June-September is typical for vegetation growth)
data_df = data_df.loc[(data_df.index.month >= 6) & (data_df.index.month <= 9)].copy()
# Select variables
vpd_col = 'VPD_f'
tair_col = 'Tair_f'
radiation_col = 'Rg_f'
df = data_df[[vpd_col, tair_col, radiation_col]].copy()
# Keep daytime only: radiation > 20 W/m^2 and temperature > 0 C
# (These thresholds define when the ecosystem is actually functioning)
daytime_locs = (df[radiation_col] > 20) & (df[tair_col] > 0)
df = df[daytime_locs].copy()
print(f"Loaded {len(df)} daytime records from summer")
> 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.050
seconds).
Loaded 31182 daytime records from summer
Bin data by VPD within temperature strata¶
# The idea: group data into temperature bins, then within each bin,
# bin by VPD. This shows if the radiation-VPD relationship changes
# as temperature increases (decoupling).
analysis = dv.analysis.StratifiedAnalysis(
df=df,
xvar=vpd_col, # Bin by VPD on x-axis
yvar=radiation_col, # Show radiation on y-axis
zvar=tair_col, # Stratify by temperature
n_bins_z=5, # 5 temperature bins
n_bins_x=10, # 10 VPD bins within each temperature class
conversion=None, # No z-score conversion
agg='median' # Use median for aggregation
)
analysis.calcbins()
print("Binning complete")
Binning complete
Access and examine results¶
# Results are stored as a dictionary with temperature medians as keys
bin_results = analysis.get_binaggs()
# List all temperature classes (as strings of temperature medians)
temp_classes = list(bin_results.keys())
print(f"\nTemperature bins (medians in C): {temp_classes}")
# Look at the warmest bin
warmest = bin_results[temp_classes[-1]]
print(f"\nResults for warmest class ({temp_classes[-1]} C):")
print(warmest)
Temperature bins (medians in C): ['6.9', '10.51', '13.37', '16.6', '20.56']
Results for warmest class (20.56 C):
group_VPD_f Tair_f ... yerror_neg yerror_pos
0 (3.4009999989999997, 10.52] 18.9670 ... 322.36160 323.67116
1 (10.52, 11.8754] 19.0765 ... 358.34296 317.46056
2 (11.8754, 12.7918] 19.4330 ... 347.95576 246.26000
3 (12.7918, 13.7384] 19.7670 ... 363.01704 236.52872
4 (13.7384, 14.678] 20.3050 ... 381.96670 193.45638
5 (14.678, 15.6772] 20.8000 ... 363.78960 212.87688
6 (15.6772, 16.837] 21.2740 ... 373.53696 190.56032
7 (16.837, 18.2688] 22.0000 ... 314.78056 189.71008
8 (18.2688, 20.461] 22.9285 ... 341.41844 192.90128
9 (20.461, 32.118] 24.7000 ... 323.26100 144.45328
[10 rows x 13 columns]
Visualization¶
# Plot the stratified relationship
analysis.showplot_decoupling_sbm(marker='o', emphasize_lines=True)

Total running time of the script: (0 minutes 0.366 seconds)