FluxMDS reproducibility / determinism

Show that MDS gap-filling is deterministic: running FluxMDS twice on the same data produces bit-identical gap-filled values and quality flags.

Unlike the machine-learning gap-fillers (Random Forest / XGBoost), MDS does not train a model and uses no random sampling - it fills each gap from the average of meteorologically similar measured records (ONEFlux’s marginal-distribution- sampling cascade). So repeated runs always return exactly the same result, which makes MDS reproducible without a random seed. This holds for all three modes: the default mode='diive' (Vekuri et al. 2023 symmetric mean), mode='oneflux' (plain mean, bit for bit as ONEFlux) and mode='reddyproc' (as REddyProc). The example uses the default.

Imports

import numpy as np
import matplotlib.pyplot as plt

import diive as dv
from diive.gapfilling.mds import FluxMDS

Load one year of example data

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

flux = 'NEE_CUT_REF_orig'
swin, ta, vpd = 'Rg_f', 'Tair_f', 'VPD_f'

print(f"Records: {len(df):,d}")
print(f"Missing {flux}: {df[flux].isnull().sum():,d} "
      f"({100.0 * df[flux].isnull().sum() / len(df):.1f}%)")
  > 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: 17,520
Missing NEE_CUT_REF_orig: 11,412 (65.1%)

Run FluxMDS twice

runs = []
for _ in range(2):
    # VPD_f is in hPa in the example data, so vpd_in_kpa=False
    model = FluxMDS(df=df, flux=flux, swin=swin, ta=ta, vpd=vpd, vpd_in_kpa=False, verbose=0)
    model.run()
    runs.append(model)

gapfilled_a = runs[0].get_gapfilled_target().to_numpy()
gapfilled_b = runs[1].get_gapfilled_target().to_numpy()
flag_a = runs[0].get_flag().to_numpy()
flag_b = runs[1].get_flag().to_numpy()

Verify the two runs are identical

values_identical = np.array_equal(gapfilled_a, gapfilled_b, equal_nan=True)
flags_identical = np.array_equal(flag_a, flag_b, equal_nan=True)

print(f"\nGap-filled values identical: {values_identical}")
print(f"Quality flags identical:     {flags_identical}")
assert values_identical and flags_identical, "MDS gap-filling is not deterministic"
print("OK - MDS gap-filling is deterministic (no random seed needed).")
Gap-filled values identical: True
Quality flags identical:     True
OK - MDS gap-filling is deterministic (no random seed needed).

Plot the gap-filled series

model = runs[0]
fig, ax = plt.subplots(figsize=(14, 4), dpi=100, layout='constrained')
flag = model.get_flag()
measured = flag == 0
ax.plot(df.index[measured], gapfilled_a[measured], ls='none', marker='o',
        ms=2, color='#455A64', label='measured')
ax.plot(df.index[~measured], gapfilled_a[~measured], ls='none', marker='o',
        ms=2, color='#F44336', label='gap-filled (MDS)')
ax.set_ylabel(flux)
ax.legend(loc='upper right')
ax.set_title('FluxMDS gap-filling (2022)')
# fig.show()  # disabled for the example gallery
FluxMDS gap-filling (2022)
Text(0.5, 1.0, 'FluxMDS gap-filling (2022)')

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

Gallery generated by Sphinx-Gallery