Skip to content

wellcast.features

features

Feature engineering for production data.

Two kinds of features are built:

  • Physics features combine measurements of the same row (GOR, water cut, drawdown and so on). FEATURE_SOURCES records which raw columns each one uses, so a model can drop features computed from its own target.
  • Lag and rolling features summarize the past of a column within each well. They are leakage-safe by construction: the value at time t only uses rows strictly before t of the same well.

All functions expect the canonical schema of wellcast.io, sorted by date within each well, and return a new DataFrame.

features_using

features_using(column: str) -> list[str]

Return physics features computed from column on the same row.

Parameters:

Name Type Description Default
column str

Raw column, typically a model target such as "bhp".

required

Returns:

Type Description
list of str

Feature names that must be excluded when predicting column.

Examples:

>>> features_using("bhp")
['dp_tubing', 'drawdown', 'static_pressure']

add_physics_features

add_physics_features(df: DataFrame, *, reservoir_pressure: float | Mapping[str, float] | None = None, by: str | None = 'well') -> pd.DataFrame

Add physics-informed features computed from each row.

Features are added only when their source columns are present.

Parameters:

Name Type Description Default
df DataFrame

Production table with a DatetimeIndex, sorted within each well.

required
reservoir_pressure float or mapping of str to float

Average reservoir pressure (psi), one value for all wells or one per well. When omitted, the static pressure is estimated as the last bottomhole pressure recorded during a shut-in (end of build-up), carried forward in time; rows before the first shut-in get NaN.

None
by str or None

Well column. Default "well".

'well'

Returns:

Type Description
DataFrame

Copy of df with the new columns:

liquid_rate (bbl/d), gor (scf/STB), water_cut (fraction), glr (scf/STB), dp_tubing (bhp - whp, psi), static_pressure and drawdown (psi), choke_normalized_rate (bbl/d per square inch of bean area), cum_oil/cum_water (bbl), cum_gas (Mscf) and days_on_production.

Raises:

Type Description
DataValidationError

If the index is not a DatetimeIndex.

Notes
\[ \mathrm{GOR} = 1000 \frac{q_g}{q_o}, \quad f_w = \frac{q_w}{q_o + q_w}, \quad \Delta p = \bar{p}_r - p_{wf} \]

Gas rates are in Mscf/d, hence the factor 1000. Cumulative volumes integrate daily-average rates over the time step to the previous row.

Examples:

>>> import pandas as pd
>>> idx = pd.date_range("2024-01-01", periods=2, freq="D")
>>> df = pd.DataFrame(
...     {
...         "oil_rate": [800.0, 0.0],
...         "gas_rate": [400.0, 0.0],
...         "water_rate": [200.0, 0.0],
...     },
...     index=idx,
... )
>>> out = add_physics_features(df)
>>> out[["gor", "water_cut", "cum_oil"]]
              gor  water_cut  cum_oil
2024-01-01  500.0        0.2    800.0
2024-01-02    NaN        NaN    800.0

add_lag_features

add_lag_features(df: DataFrame, columns: Sequence[str], lags: Sequence[int] = (1, 7), *, by: str | None = 'well') -> pd.DataFrame

Add lagged copies of columns, shifted within each well.

Parameters:

Name Type Description Default
df DataFrame

Table sorted by date within each well.

required
columns sequence of str

Columns to lag.

required
lags sequence of int

Positive lags in rows. Default (1, 7).

(1, 7)
by str or None

Well column. Default "well".

'well'

Returns:

Type Description
DataFrame

Copy with columns named <column>_lag<k>.

Raises:

Type Description
DataValidationError

If a column is missing or a lag is not a positive integer.

Examples:

>>> import pandas as pd
>>> df = pd.DataFrame({"well": ["A", "A", "B", "B"], "q": [1.0, 2.0, 3.0, 4.0]})
>>> add_lag_features(df, ["q"], [1])["q_lag1"].tolist()
[nan, 1.0, nan, 3.0]

add_rolling_features

add_rolling_features(df: DataFrame, columns: Sequence[str], windows: Sequence[int] = (7, 30), stats: Sequence[str] = ('mean', 'std'), *, by: str | None = 'well', min_periods: int = 1) -> pd.DataFrame

Add rolling statistics over strictly past rows of each well.

The window ending at row t covers rows t - w to t - 1, so the current value never enters its own feature.

Parameters:

Name Type Description Default
df DataFrame

Table sorted by date within each well.

required
columns sequence of str

Columns to summarize.

required
windows sequence of int

Window lengths in rows. Default (7, 30).

(7, 30)
stats sequence of str

Any of "mean", "std", "min", "max", "median". Default ("mean", "std").

('mean', 'std')
by str or None

Well column. Default "well".

'well'
min_periods int

Minimum past observations required. Default 1.

1

Returns:

Type Description
DataFrame

Copy with columns named <column>_roll<w>_<stat>.

Raises:

Type Description
DataValidationError

If a column is missing, a window is < 1 or a statistic is unknown.

Examples:

>>> import pandas as pd
>>> df = pd.DataFrame({"q": [1.0, 2.0, 3.0, 4.0]})
>>> add_rolling_features(df, ["q"], [2], ["mean"], by=None)["q_roll2_mean"].tolist()
[nan, 1.0, 1.5, 2.5]

build_features

build_features(df: DataFrame, *, lag_columns: Sequence[str] | None = None, lags: Sequence[int] = (1, 7), windows: Sequence[int] = (7, 30), reservoir_pressure: float | Mapping[str, float] | None = None, by: str | None = 'well') -> pd.DataFrame

Build physics, lag and rolling features in one call.

Parameters:

Name Type Description Default
df DataFrame

Production table in the canonical schema.

required
lag_columns sequence of str

Columns for lag and rolling-mean features. Default: the canonical rate and pressure columns present in df.

None
lags sequence of int

Lags in rows. Default (1, 7).

(1, 7)
windows sequence of int

Rolling-mean windows in rows. Default (7, 30).

(7, 30)
reservoir_pressure float or mapping

Passed to add_physics_features.

None
by str or None

Well column. Default "well".

'well'

Returns:

Type Description
DataFrame

Copy of df with all features.

Examples:

>>> from wellcast.datasets import make_synthetic_field
>>> field = make_synthetic_field(n_wells=1, years=1, n_injectors=0)
>>> features = build_features(field.production)
>>> {"gor", "bhp_lag1", "oil_rate_roll7_mean"} <= set(features.columns)
True