QuickFill: Rapid Prototyping

Rapid gap-filling using RandomForestTS with minimal features.

QuickFill enables exploratory and preliminary gap-filling with speed. Its only features are a single lag of one record and the timestamp features, and it uses small trees for very fast execution (~3 seconds for typical dataset).

Quality: Low (exploratory only - not for production) Speed: Very fast (~3 seconds) Use case: Quick prototyping, testing, parameter exploration

QuickFill example

For exploratory analysis, use QuickFillRFTS for rapid gap-filling with minimal features and fast execution. Demonstrates complete workflow from data loading through results reporting.

import pandas as pd
import importlib.metadata
from datetime import datetime

import diive as dv

Session information

Display diive version and current timestamp.

dt_string = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Session timestamp: {dt_string}")
version_diive = ("diive")
print(f"diive version: v{version_diive}")
Session timestamp: 2026-10-21 00:21:14
diive version: v0.92.0

QuickFillRFTS overview

Show the QuickFillRFTS class docstring explaining the rapid prototyping approach.

QuickFillRFTS

    Quick gap-filling using RandomForestTS with pre-defined minimal parameters

    The purpose of this class is preliminary/exploratory gap-filling for quick tests
    and rapid prototyping. It is NOT meant for production/final gap-filling.

    Uses minimal feature engineering and model complexity for speed:

    - FeatureEngineer with a single lag of one record: [-1, -1]
    - Vectorized timestamp features, but no rolling statistics, differencing,
      EMA, polynomial or STL features
    - RandomForestTS with n_estimators=9 (fast, low quality)
    - min_samples_split=10 and min_samples_leaf=5 to keep the trees small

    Methods:
        fill(): Train the model and fill the gaps
        report(): Print the gap-filling report
        gapfilling_df(): DataFrame with gap-filled target and flags
        get_gapfilled_target(): Series with gap-filled values
        get_flag(): Series with gap-filling flags (0=observed, 1=filled, 2=fallback)

Load and prepare data

Load example ecosystem flux data and filter to high-quality observations.

TARGET_COL = 'NEE_CUT_REF_orig'
subsetcols = [TARGET_COL, 'Tair_f', 'VPD_f', 'Rg_f']

df = dv.load_exampledata_parquet()
keep = (df.index.year >= 2020) & (df.index.year <= 2020)
df = df[keep].copy()

# Only High-quality (QCF=0) measured NEE used for model training in this example
lowquality = df["QCF_NEE"] > 0
df.loc[lowquality, TARGET_COL] = pd.NA
df = df[subsetcols].copy()
  > 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).

Data summary statistics

print("\nData summary:")
df.describe()
statsdf = dv.sstats(df[TARGET_COL])
print(statsdf)

dv.plotting.TimeSeries(series=df[TARGET_COL]).plot()
NEE_CUT_REF_orig
Data summary:
                  NEE_CUT_REF_orig
STARTDATE         2020-01-01 00:15
ENDDATE           2020-12-31 23:45
PERIOD           365 days 23:30:00
NOV                           4389
MISSING                      13179
MISSING_PERC             75.017077
MEAN                     -3.421411
MEDIAN                      -3.479
SD                        5.613558
VAR                      31.512037
SD/MEAN                  -1.640714
SUM                     -15016.574
MIN                        -24.975
MAX                         20.974
CV                        1.640714
IQR                          7.774
SKEWNESS                 -0.042092
KURTOSIS                   0.48986
MEAN_ABS_CHANGE           2.932959
TREND_SLOPE               0.000178
ACF_LAG1                  0.703503
OUTLIER_COUNT                   27
OUTLIER_PERC              0.615174
CUMSUM                  -15016.574
P01                      -17.47712
P05                       -12.6554
P25                         -7.082
P75                          0.692
P95                         5.2938
P99                       10.41896

<Axes: title={'left': 'NEE_CUT_REF_orig'}, ylabel='NEE_CUT_REF_orig'>

Run QuickFill

Execute rapid gap-filling with minimal configuration.

  > 20 feature(s), 4,389 complete record(s) of 17,568 (2020-01-01 00:15:00 to
2020-12-31 23:45:00)
  > Train/test split: 3,291 train / 1,098 test (25% held out)
  > Test set is a RANDOM subset of the complete records, not a temporal block.
This reproduces the gap-filling task: gaps lie between observed records and are
filled from the drivers at the same timestamp.
  v Training complete.
  > Predicted 17568 records where all features available.
  > Filling 13179 missing records in NEE_CUT_REF_orig_gfRF ...
  > Fallback: filling 0 remaining gaps in NEE_CUT_REF_orig_gfRF ...
───────────────────────────── GAP-FILLING RESULTS ──────────────────────────────

Scores and importances from predicted (13179) vs observed (4389) targets.

## TARGET
- period:  2020-01-01 00:15:00 to 2020-12-31 23:45:00
- potential values: 17568
- observed (NEE_CUT_REF_orig):  missing 13179
- gap-filled (NEE_CUT_REF_orig_gfRF):  17568 values, missing 0
- flag FLAG_NEE_CUT_REF_orig_gfRF_ISFILLED:  0=observed (4389)  1=gap-filled
(13179)  2=fallback (0)

## FEATURE IMPORTANCES  (20 features, SHAP TreeExplainer)

             SHAP_IMPORTANCE   SHAP_SD
Rg_f                3.542771  1.862305
.Tair_f-1           0.552122  0.451243
.YEARWEEK           0.465787  0.573889
.HOUR_SIN           0.383879  0.372793
.Rg_f-1             0.339601  0.299638
.DOY_COS            0.314785  0.337933
.YEARDOY            0.307933  0.384649
Tair_f              0.208131  0.215863
.VPD_f-1            0.162046  0.122386
.DOY_SIN            0.138333  0.182473
.WEEK_COS           0.135725  0.162491
VPD_f               0.124664  0.121220
.HOUR_COS           0.077160  0.079832
.SEASON_COS         0.048714  0.086412
.WEEK_SIN           0.043980  0.057114
.MONTH_COS          0.020603  0.032050
.YEARMONTH          0.009503  0.018891
.MONTH_SIN          0.006368  0.015906
.SEASON_SIN         0.002135  0.005090
.YEAR               0.000000  0.000000

## MODEL  (test size 25.0%)
- estimator:  RandomForestRegressor(min_samples_leaf=5, min_samples_split=10,
n_estimators=9,
                      n_jobs=-1, random_state=42)
- parameters:  {'bootstrap': True, 'ccp_alpha': 0.0, 'criterion':
'squared_error', 'max_depth': None, 'max_features': 1.0, 'max_leaf_nodes': None,
'max_samples': None, 'min_impurity_decrease': 0.0, 'min_samples_leaf': 5,
'min_samples_split': 10, 'min_weight_fraction_leaf': 0.0, 'monotonic_cst': None,
'n_estimators': 9, 'n_jobs': -1, 'oob_score': False, 'random_state': 42,
'verbose': 0, 'warm_start': False}

## MODEL SCORES (in-sample: predicted on ALL data incl. training rows;
optimistically biased)
- MAE:   1.5147595224387582 (mean absolute error)
- MedAE: 1.047268191721134 (median absolute error)
- MSE:   4.679550047412643 (mean squared error)
- RMSE:  2.163226767450108 (root mean squared error)
- MAXE:  14.048067832670903 (max error)
- MAPE:  0.930 (mean absolute percentage error)
- R2:    0.8514657583897378

## MODEL SCORES (out-of-sample: held-out test set, 25.0%; generalization
estimate)
- MAE:   1.9441698895630055 (mean absolute error)
- MedAE: 1.4067984067493873 (median absolute error)
- MSE:   7.19842876255679 (mean squared error)
- RMSE:  2.682988774213711 (root mean squared error)
- MAXE:  14.048067832670903 (max error)
- MAPE:  1.063 (mean absolute percentage error)
- R2:    0.769718204967714

Visualize results

Display heatmap comparisons of observed vs. gap-filled data.

dv.plotting.HeatmapDateTime(series=df[TARGET_COL]).show()
dv.plotting.HeatmapDateTime(series=gapfilled).show()

print("[OK] QuickFill gap-filling complete.")
  • NEE_CUT_REF_orig (30min)
  • NEE_CUT_REF_orig_gfRF (30min)
[OK] QuickFill gap-filling complete.

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

Gallery generated by Sphinx-Gallery