Virtual flow metering¶
Author: Olaf Yunus Laitinen Imanov (ORCID 0009-0006-5184-0810)
Affiliation: Department of Mathematics and Statistics, University of Helsinki
License: Mozilla Public License 2.0 (MPL-2.0), Copyright (c) 2026 Olaf Yunus Laitinen Imanov
Predict bottomhole pressure from surface measurements with machine learning, validate the model honestly and explain what drives it.
# Install wellcast when it is not available yet (for example on Google Colab).
import importlib.util
from IPython import get_ipython
if importlib.util.find_spec("wellcast") is None:
get_ipython().run_line_magic(
"pip",
'install -q "wellcast[all]==0.1.0"',
)
Features¶
build_features adds physics features (GOR, water cut, drawdown and so on) and lag and rolling features. Lag and rolling features only use earlier rows of the same well, so they cannot leak the future. Same-row features computed from the target are removed automatically by the Predictor.
import numpy as np
import wellcast
from wellcast.features import build_features, features_using
from wellcast.ml import Predictor
from wellcast.plotting import predicted_vs_actual, prediction_interval_plot
production = wellcast.make_synthetic_field(n_wells=5, years=4, n_injectors=0).production
data = build_features(production)
print("features derived from bhp on the same row:", features_using("bhp"))
data.shape
features derived from bhp on the same row: ['dp_tubing', 'drawdown', 'static_pressure']
(6876, 47)
Honest validation¶
Two questions need different tests: how well does the model extrapolate in time (time-ordered splits on calendar dates), and how well does it transfer to a well it has never seen (leave one well out).
model = Predictor("bhp")
cv_time = model.cross_validate(data, "timeseries", n_splits=4)
cv_time[cv_time["well"] == "all"].round(3)
| fold | well | n | mae | rmse | mape | r2 | |
|---|---|---|---|---|---|---|---|
| 5 | 0 | all | 1422 | 182.627 | 232.993 | 6.269 | 0.478 |
| 11 | 1 | all | 1395 | 83.889 | 114.139 | 3.259 | 0.832 |
| 17 | 2 | all | 1419 | 39.420 | 53.454 | 1.675 | 0.949 |
| 23 | 3 | all | 1412 | 30.147 | 42.135 | 1.394 | 0.944 |
cv_well = model.cross_validate(data, "lowo")
cv_well[cv_well["well"] != "all"].round(3)
| fold | well | n | mae | rmse | mape | r2 | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | P-01 | 1394 | 49.153 | 70.332 | 2.238 | 0.972 |
| 2 | 1 | P-02 | 1401 | 31.642 | 46.112 | 1.292 | 0.985 |
| 4 | 2 | P-03 | 1310 | 15.633 | 23.795 | 0.609 | 0.996 |
| 6 | 3 | P-04 | 1275 | 108.537 | 200.868 | 2.927 | 0.846 |
| 8 | 4 | P-05 | 1354 | 20.506 | 30.757 | 0.696 | 0.992 |
Hold-out period with prediction intervals¶
Train on the first 80 percent of dates and predict the rest. Two quantile models give an 80 percent interval.
dates = np.sort(data.index.unique())
cutoff = dates[int(0.8 * len(dates))]
train, test = data[data.index < cutoff], data[data.index >= cutoff]
test = test[test["on_stream_hours"] > 0]
model.fit(train)
interval = model.predict_interval(test)
inside = (test["bhp"] >= interval["lower"]) & (test["bhp"] <= interval["upper"])
print(f"interval coverage on the hold-out period: {inside.mean():.1%}")
model.score(test).round(3)
interval coverage on the hold-out period: 35.8%
| n | mae | rmse | mape | r2 | |
|---|---|---|---|---|---|
| well | |||||
| P-01 | 279 | 64.141 | 75.039 | 3.176 | -2.153 |
| P-02 | 289 | 26.461 | 31.784 | 1.191 | 0.337 |
| P-03 | 280 | 25.710 | 34.931 | 1.204 | 0.597 |
| P-04 | 275 | 18.057 | 24.044 | 0.724 | 0.930 |
| P-05 | 289 | 16.817 | 22.258 | 0.698 | 0.758 |
| all | 1412 | 30.147 | 42.135 | 1.394 | 0.944 |
predicted_vs_actual(test["bhp"], interval["prediction"], test["well"])
well = test["well"] == "P-02"
prediction_interval_plot(
test.loc[well, "bhp"], interval[well], title="P-02 bottomhole pressure"
)
What drives the prediction¶
SHAP values split each prediction into feature contributions (exact for tree models).
model.explain(test, max_samples=300, top=12)
Saving the model¶
Predictors are saved with joblib. Joblib files are pickles: only load files you trust.
import tempfile
from pathlib import Path
path = Path(tempfile.mkdtemp()) / "vfm_bhp.joblib"
model.save(path)
Predictor.load(path)
Predictor(target='bhp', estimator='lightgbm', fitted)