SW_IN Gap-Filling (Physics + XGBoost)

Gap-fill shortwave incoming radiation with physics-aware partitioning combined with XGBoost.

Nighttime gaps are set to zero (no solar radiation after sunset). Daytime gaps are filled with XGBoost. The two parts are assembled into one complete, physically consistent time series.

Three configurations are run in turn, each differing from the first by exactly one setting, and each reporting its own features as it goes:

  1. the defaults, with no context driver – the climatology ceiling;

  2. the same, plus reduce_features=True;

  3. the same, plus a second radiation sensor via context_df.

They are compared at the end on withheld daytime-gap RMSE computed at runtime.

Part of the diive library: https://github.com/holukas/diive

The climatology ceiling

SW_IN is exactly zero at night. SWINGapFillerXGBoost uses potential radiation (SW_IN_POT, from lat/lon) to split the record into daytime and nighttime, sets nighttime gaps to zero, and fills daytime gaps with XGBoost.

SW_IN has its own class rather than a plain XGBoostTS call because it also needs corrections, and one of them doubles as a gap-fill: the nighttime zero-offset correction (on by default) sets every nighttime record to exactly zero, which fills the nighttime gaps in passing. Correcting and filling are one operation here, so they live in one place and run in a fixed order.

With no context drivers, every feature the model sees is a deterministic function of the timestamp: SW_IN_POT, the timestamp features and the record number are all fixed once the time is known. The model can therefore only reproduce a climatology – the expected SW_IN for that time of day and year. It cannot know whether a gap was overcast or clear, because nothing in its inputs carries the sky state. More timestamp-derived features (lags, rolling means, EMAs of SW_IN_POT) do not help: they are the same function again.

What breaks the ceiling is a second radiation measurement passed through context_df. In preference order: a co-located sensor (pyranometer or PPFD) seeing the same sky, a nearby station’s radiation, or reanalysis SW_IN such as ERA5-Land. All carry synoptic sky state that the timestamp cannot.

import textwrap

import matplotlib.pyplot as plt
import numpy as np

import diive as dv

Setup

The dataset carries Rg_f (the target) and PPFD, a second independent radiation sensor at the same site, nearly gap-free in 2020.

Beyond lat/lon/utc_offset the gap-filler needs nothing: it ships SW_IN defaults for the XGBoost hyperparameters and the feature windows.

One default is worth watching. n_estimators is an upper budget, not a tree count: early_stopping_rounds halts boosting once the held-out error stops improving. Whether it gets the chance depends on the data, so each run reports the trees it actually built. The SHAP pass costs time linear in the tree count, so a config that runs to the budget is markedly slower.

SITE_LAT = 46.815  # CH-DAV Davos, Switzerland
SITE_LON = 9.855
SITE_UTC_OFFSET = 1
TARGET_COL = 'Rg_f'      # Shortwave incoming radiation (W/m2)
CONTEXT_COL = 'PPFD'     # Second radiation sensor, carries the sky state

# The class carries the SW_IN defaults for the tree budget, depth, early stopping
# and the seed, so only n_jobs is left to set here: a machine choice, not a
# modelling one.
XGB_KWARGS = dict(n_jobs=-1)

df = dv.load_exampledata_parquet()
df = df[df.index.year == 2020].copy()

# Keep an untouched copy of the target to score the fill against.
truth = df[TARGET_COL].copy()

# Introduce artificial gaps: randomly remove 15% of observed values.
rng = np.random.default_rng(seed=42)
observed_idx = df[TARGET_COL].dropna().index
gap_idx = rng.choice(observed_idx, size=int(0.15 * len(observed_idx)), replace=False)
df.loc[gap_idx, TARGET_COL] = np.nan

print(f"Records: {len(df)}")
print(f"Gaps in {TARGET_COL}: {df[TARGET_COL].isnull().sum()} "
      f"({100 * df[TARGET_COL].isnull().mean():.1f}%)")
print(f"Gaps in {CONTEXT_COL}: {df[CONTEXT_COL].isnull().sum()}")

print("\nXGBoost parameters from the class (no need to pass these):")
for key, value in dv.gapfilling.SWINGapFillerXGBoost._XGB_DEFAULTS.items():
    print(f"  {key:22s} {value}")
print("set by this example:")
for key, value in XGB_KWARGS.items():
    print(f"  {key:22s} {value}")
  > 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).
Records: 17568
Gaps in Rg_f: 2635 (15.0%)
Gaps in PPFD: 18

XGBoost parameters from the class (no need to pass these):
  n_estimators           3000
  max_depth              6
  early_stopping_rounds  20
  random_state           42
set by this example:
  n_jobs                 -1

Scoring, and a helper to run one configuration

The true SW_IN at every artificial gap is known, so the fill can be measured directly. Nighttime gaps are trivially zero, so only daytime gaps are scored – that is where the model has to work.

All three configurations go through run_config, so they differ only in the keywords each one passes.

WIDTH = 80


def section(title):
    """Print a banner so each configuration's output is easy to find."""
    print("\n" + "=" * WIDTH)
    print(title)
    print("=" * WIDTH)


def print_features(title, names):
    """Print a wrapped, comma-separated feature list under a counted heading."""
    print(f"\n{title} ({len(names)}):")
    print(textwrap.fill(", ".join(names), width=78,
                        initial_indent="    ", subsequent_indent="    "))


def daytime_gap_rmse(result):
    """RMSE of the fill against truth, at artificial daytime gap records."""
    swinpot = result.gapfilling_df['SW_IN_POT']
    daytime = swinpot >= 0.001
    idx = df.index.isin(gap_idx) & daytime.values
    err = result.gapfilled[idx] - truth[idx]
    return float(np.sqrt((err ** 2).mean())), int(idx.sum())


def run_config(label, **kwargs):
    """Build, run and score one SWINGapFillerXGBoost configuration."""
    gf = dv.gapfilling.SWINGapFillerXGBoost(
        series=df[TARGET_COL],
        lat=SITE_LAT,
        lon=SITE_LON,
        utc_offset=SITE_UTC_OFFSET,
        verbose=1,
        **kwargs,
        **XGB_KWARGS,
    )
    gf.run()
    r = gf.results
    rmse, n_gaps = daytime_gap_rmse(r)
    r2 = r.scores_traintest.get('r2', float('nan')) if r.scores_traintest else float('nan')
    n_features = int(len(r.feature_importances)) if r.feature_importances is not None else 0
    # Trees actually built, versus the n_estimators budget: early stopping at work.
    # gf.kwargs is the effective config -- class defaults merged with any override.
    n_trees = r.model.get_booster().num_boosted_rounds()
    budget = gf.kwargs['n_estimators']
    print(f"\n{label}: RMSE {rmse:.1f} W/m2 | held-out R2 {r2:.3f} | "
          f"{n_features} features | {n_trees} of {budget} trees "
          f"| scored over {n_gaps} records")
    return dict(label=label, rmse=rmse, r2=r2, n_features=n_features,
                n_trees=n_trees, budget=budget, interp=gf.interpolate_short_gaps,
                results=r)

Configuration 1: the defaults

Nothing is passed but the series and the site coordinates. This is the baseline the other two are measured against, and the climatology ceiling in practice: the model sees SW_IN_POT plus timestamp features and nothing else, so every input is a deterministic function of the timestamp.

One default is worth naming here, because it differs in configuration 3: interpolate_short_gaps='auto' resolves to on (a 2-record limit), since there is no context_df to resolve short gaps better.

section("Configuration 1: defaults, no context driver")
ceiling = run_config("1 no context (ceiling)")

print(f"\ninterpolate_short_gaps='auto' resolved to: {ceiling['interp']} records")
print_features("features in the final model",
               list(ceiling['results'].feature_importances.index))
================================================================================
Configuration 1: defaults, no context driver
================================================================================
─────────────────────────── SW_IN Gap-Filling (Rg_f) ───────────────────────────
  > Step 1: Correcting nighttime sensor offset (0.1s)
  > Step 2: Computing potential radiation and day/night split (0.0s, 9159
daytime / 8409 nighttime records)
  > Gaps to fill: 1373 daytime, 1262 nighttime
  > Step 3: Filling nighttime gaps with 0 W/m2 (no incoming solar radiation)
(0.0s, 1262 records set to 0)
  > Step 4: Interpolating short daytime gaps (<= 2 records) (0.1s, 1059 records
interpolated)
  > Step 5: Engineering features (0.0s, 16 features over 17568 rows)
  > Step 6: Training XGBoost on 7786 daytime records (incl. SHAP importances)
(0.7s)
  > Step 7: Filling daytime gaps (SHAP importances over up to 10000 records)
(1.9s)
  v Done in 2.8s - 2635 records filled (2635 gaps total)

1 no context (ceiling): RMSE 76.8 W/m2 | held-out R2 0.884 | 16 features | 167 of 3000 trees | scored over 1373 records

interpolate_short_gaps='auto' resolved to: 2 records

features in the final model (16):
    SW_IN_POT, .DOY_SIN, .DOY_COS, .RECORDNUMBER, .HOUR_SIN, .HOUR_COS,
    .WEEK_COS, .WEEK_SIN, .MONTH_COS, .YEARDOY, .SEASON_SIN, .MONTH_SIN,
    .SEASON_COS, .YEARWEEK, .YEARMONTH, .YEAR

Configuration 2: the defaults plus SHAP feature reduction

Identical to configuration 1 except for reduce_features=True. This trims the feature list rather than adding information, so it cannot lift the climatology ceiling – what it buys is a smaller, cheaper model.

Reduction adds a .RANDOM column of pure noise, scores every feature by SHAP importance, and keeps only those clearing the noise by a margin. The cut-off is not the random column’s importance itself but

threshold = random_importance + shap_threshold_factor * random_SD

with shap_threshold_factor defaulting to 0.5. That is a stricter bar than it sounds: several features below score above .RANDOM and are still dropped. Lower the factor to keep more of them.

section("Configuration 2: defaults + reduce_features=True")
reduced = run_config("2 no context + reduce_features", reduce_features=True)

r_reduced = reduced['results']
fi_reduction = r_reduced.feature_importances_reduction

SHAP_THRESHOLD_FACTOR = 0.5  # the SWINGapFillerXGBoost default
random_importance = fi_reduction.loc['.RANDOM', 'SHAP_IMPORTANCE']
random_sd = fi_reduction.loc['.RANDOM', 'SHAP_SD']
threshold = random_importance + SHAP_THRESHOLD_FACTOR * random_sd

print_features("scored before reduction (incl. the .RANDOM benchmark)",
               list(fi_reduction.index))
print_features("kept", r_reduced.accepted_features)
print_features("dropped", r_reduced.rejected_features)

print(f"\n.RANDOM importance {random_importance:.2f}, SD {random_sd:.2f}"
      f"  ->  keep threshold = {random_importance:.2f} + "
      f"{SHAP_THRESHOLD_FACTOR} * {random_sd:.2f} = {threshold:.2f}")
print("\nSHAP importances at reduction time, strongest first:")
verdict = {name: ('.RANDOM (benchmark)' if name == '.RANDOM'
                  else 'kept' if name in r_reduced.accepted_features else 'dropped')
           for name in fi_reduction.index}
print(fi_reduction.assign(VERDICT=fi_reduction.index.map(verdict)).to_string())
================================================================================
Configuration 2: defaults + reduce_features=True
================================================================================
─────────────────────────── SW_IN Gap-Filling (Rg_f) ───────────────────────────
  > Step 1: Correcting nighttime sensor offset (0.0s)
  > Step 2: Computing potential radiation and day/night split (0.0s, 9159
daytime / 8409 nighttime records)
  > Gaps to fill: 1373 daytime, 1262 nighttime
  > Step 3: Filling nighttime gaps with 0 W/m2 (no incoming solar radiation)
(0.0s, 1262 records set to 0)
  > Step 4: Interpolating short daytime gaps (<= 2 records) (0.1s, 1059 records
interpolated)
  > Step 5: Engineering features (0.0s, 16 features over 17568 rows)
  > Step 6: Training XGBoost on 7786 daytime records (incl. SHAP importances)
(0.7s)
  > Step 7: Reducing features (SHAP vs random benchmark) and retraining (2.1s)
  > Step 8: Filling daytime gaps (SHAP importances over up to 10000 records)
(1.5s)
  v Done in 4.5s - 2635 records filled (2635 gaps total)

2 no context + reduce_features: RMSE 76.6 W/m2 | held-out R2 0.854 | 4 features | 142 of 3000 trees | scored over 1373 records

scored before reduction (incl. the .RANDOM benchmark) (17):
    SW_IN_POT, .DOY_SIN, .DOY_COS, .RECORDNUMBER, .HOUR_SIN, .YEARDOY,
    .WEEK_SIN, .RANDOM, .HOUR_COS, .WEEK_COS, .MONTH_SIN, .MONTH_COS,
    .SEASON_SIN, .SEASON_COS, .YEARWEEK, .YEARMONTH, .YEAR

kept (4):
    SW_IN_POT, .DOY_SIN, .DOY_COS, .RECORDNUMBER

dropped (12):
    .YEAR, .SEASON_SIN, .SEASON_COS, .MONTH_SIN, .MONTH_COS, .WEEK_SIN,
    .WEEK_COS, .HOUR_SIN, .HOUR_COS, .YEARMONTH, .YEARDOY, .YEARWEEK

.RANDOM importance 9.50, SD 12.15  ->  keep threshold = 9.50 + 0.5 * 12.15 = 15.58

SHAP importances at reduction time, strongest first:
               SHAP_IMPORTANCE     SHAP_SD              VERDICT
SW_IN_POT           198.174026  109.978737                 kept
.DOY_SIN             38.745514   44.027554                 kept
.DOY_COS             31.246647   34.445782                 kept
.RECORDNUMBER        16.435020   16.151417                 kept
.HOUR_SIN            13.736460   12.842910              dropped
.YEARDOY             13.032132   12.908169              dropped
.WEEK_SIN            11.492268   13.157096              dropped
.RANDOM               9.499084   12.154028  .RANDOM (benchmark)
.HOUR_COS             8.961270    7.917135              dropped
.WEEK_COS             7.314944    9.913866              dropped
.MONTH_SIN            5.794357    8.370724              dropped
.MONTH_COS            5.550896    7.872417              dropped
.SEASON_SIN           3.798099    3.851175              dropped
.SEASON_COS           3.076316    3.788469              dropped
.YEARWEEK             1.785332    3.594590              dropped
.YEARMONTH            0.048285    0.349328              dropped
.YEAR                 0.000000    0.000000              dropped

Configuration 3: the defaults plus a second radiation sensor

Identical to configuration 1 except for context_df=df[['PPFD']]. PPFD is a second, independent radiation sensor at the same site: unlike any timestamp feature it measures the sky state directly, which is what breaks the ceiling. The feature engineer derives rolling, EMA and SD variants of it, so the feature count roughly doubles.

Note the knock-on effect: interpolate_short_gaps='auto' now resolves to off, because a strong context sensor resolves short gaps better than clearness-index interpolation, which would overwrite those fills.

section("Configuration 3: defaults + PPFD context sensor")
context = run_config("3 PPFD context", context_df=df[[CONTEXT_COL]])

print(f"\ninterpolate_short_gaps='auto' resolved to: {context['interp']}")
print_features("features in the final model",
               list(context['results'].feature_importances.index))

print(f"\nResult columns: {list(context['results'].gapfilling_df.columns)}")
print("\nTop 10 features by SHAP importance:")
print(context['results'].feature_importances.head(10).to_string())
================================================================================
Configuration 3: defaults + PPFD context sensor
================================================================================
─────────────────────────── SW_IN Gap-Filling (Rg_f) ───────────────────────────
  > Step 1: Correcting nighttime sensor offset (0.0s)
  > Step 2: Computing potential radiation and day/night split (0.0s, 9159
daytime / 8409 nighttime records)
  > Gaps to fill: 1373 daytime, 1262 nighttime
  > Step 3: Filling nighttime gaps with 0 W/m2 (no incoming solar radiation)
(0.0s, 1262 records set to 0)
  > Step 4: Engineering features (0.2s, 31 features over 17568 rows)
  > Step 5: Training XGBoost on 7786 daytime records (incl. SHAP importances)
(0.9s)
  > Step 6: Filling daytime gaps (SHAP importances over up to 10000 records)
(1.9s)
  v Done in 3.0s - 2635 records filled (2635 gaps total)

3 PPFD context: RMSE 12.6 W/m2 | held-out R2 0.997 | 31 features | 159 of 3000 trees | scored over 1373 records

interpolate_short_gaps='auto' resolved to: None

features in the final model (31):
    PPFD, .PPFD_ROLLMEDIAN24, SW_IN_POT, .PPFD_MEAN48, .PPFD_ROLLMEDIAN4,
    .HOUR_COS, .PPFD_ROLLMEDIAN48, .PPFD_SD8, .HOUR_SIN, .PPFD_MEAN4,
    .PPFD_EMA24, .DOY_SIN, .PPFD_SD48, .PPFD_MEAN24, .PPFD_ROLLMEDIAN8,
    .YEARDOY, .PPFD_SD4, .DOY_COS, .YEARMONTH, .RECORDNUMBER, .PPFD_SD24,
    .PPFD_MEAN8, .WEEK_SIN, .MONTH_COS, .PPFD_EMA6, .WEEK_COS, .SEASON_SIN,
    .MONTH_SIN, .SEASON_COS, .YEARWEEK, .YEAR

Result columns: ['Rg_f', 'Rg_f_offset_corrected', 'Rg_f_gapfilled', 'SW_IN_POT', 'flag']

Top 10 features by SHAP importance:
                    SHAP_IMPORTANCE     SHAP_SD
PPFD                     236.436768  151.623505
.PPFD_ROLLMEDIAN24         3.511211    3.438063
SW_IN_POT                  2.519942    2.584952
.PPFD_MEAN48               1.789075    2.178339
.PPFD_ROLLMEDIAN4          1.537773    1.680531
.HOUR_COS                  1.477440    1.821355
.PPFD_ROLLMEDIAN48         1.442425    1.262650
.PPFD_SD8                  1.388407    1.794861
.HOUR_SIN                  1.362729    1.552065
.PPFD_MEAN4                1.234567    0.898992

Comparison

All numbers come from this run: withheld daytime-gap RMSE (lower is better), the daytime model’s held-out R2, the feature count, and the trees early stopping settled on.

runs = [ceiling, reduced, context]

section("SWINGapFillerXGBoost: feature reduction, and a second radiation sensor")
print(f"{'config':<30}{'RMSE W/m2':>11}{'held-R2':>9}{'n_feat':>8}{'trees':>8}")
print("-" * WIDTH)
for run in runs:
    print(f"{run['label']:<30}{run['rmse']:>11.1f}{run['r2']:>9.3f}"
          f"{run['n_features']:>8d}{run['n_trees']:>8d}")
print("-" * WIDTH)


def vs_ceiling(run):
    """Change in RMSE relative to the ceiling run, stated with its direction."""
    change = 100 * (run['rmse'] / ceiling['rmse'] - 1)
    return (f"RMSE {ceiling['rmse']:.1f} -> {run['rmse']:.1f} W/m2 "
            f"({abs(change):.0f}% {'higher' if change > 0 else 'lower'})")


print(f"Feature reduction: {vs_ceiling(reduced)}, "
      f"{ceiling['n_features']} -> {reduced['n_features']} features")
print(f"Second sensor:     {vs_ceiling(context)}")

maxed = [r['label'] for r in runs if r['n_trees'] >= r['budget']]
print(f"Early stopping: settled below the {ceiling['budget']}-tree budget"
      if not maxed else
      f"Early stopping never triggered for {maxed} -- n_estimators was the "
      f"binding limit (raise it, or lower early_stopping_rounds)")
================================================================================
SWINGapFillerXGBoost: feature reduction, and a second radiation sensor
================================================================================
config                          RMSE W/m2  held-R2  n_feat   trees
--------------------------------------------------------------------------------
1 no context (ceiling)               76.8    0.884      16     167
2 no context + reduce_features       76.6    0.854       4     142
3 PPFD context                       12.6    0.997      31     159
--------------------------------------------------------------------------------
Feature reduction: RMSE 76.8 -> 76.6 W/m2 (0% lower), 16 -> 4 features
Second sensor:     RMSE 76.8 -> 12.6 W/m2 (84% lower)
Early stopping: settled below the 3000-tree budget

Observed versus gap-filled

Three panels: the observed record with gaps, the no-context fill (still on the ceiling), and the PPFD-context fill (ceiling broken).

fig, axes = plt.subplots(1, 3, figsize=(20, 5),
                         gridspec_kw={'wspace': 0.2},
                         constrained_layout=True)

dv.plotting.HeatmapDateTime(series=df[TARGET_COL]).plot(
    ax=axes[0], zlabel=r'$\mathrm{W\ m^{-2}}$')
axes[0].set_title('Observed SW_IN\n(with gaps)', fontsize=11, fontweight='bold')

dv.plotting.HeatmapDateTime(series=ceiling['results'].gapfilled).plot(
    ax=axes[1], zlabel=r'$\mathrm{W\ m^{-2}}$')
axes[1].set_title(f"Gap-filled, no context\nRMSE {ceiling['rmse']:.0f} W/m2",
                  fontsize=11, fontweight='bold')

dv.plotting.HeatmapDateTime(series=context['results'].gapfilled).plot(
    ax=axes[2], zlabel=r'$\mathrm{W\ m^{-2}}$')
axes[2].set_title(f"Gap-filled, PPFD context\nRMSE {context['rmse']:.0f} W/m2",
                  fontsize=11, fontweight='bold')

fig.suptitle('SW_IN Gap-Filling: the second radiation sensor breaks the '
             'climatology ceiling', fontsize=13, fontweight='bold')
plt.show()

print("\nSW_IN gap-filling example complete.")
SW_IN Gap-Filling: the second radiation sensor breaks the climatology ceiling, Observed SW_IN (with gaps), Gap-filled, no context RMSE 77 W/m2, Gap-filled, PPFD context RMSE 13 W/m2
SW_IN gap-filling example complete.

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

Gallery generated by Sphinx-Gallery