Add protocol v5: T5 similar-interval retrieval and neighbour forecast
Protocol v5 extends v4 and was locked before any retrieval score. It fixes the T5 items (own-hole T3 intervals with a known section and formation), the rule-based graded relevance, leakage-free representations, nDCG@10, MRR, P@10, the whole-well bootstrap and the k-nearest-neighbour forecast. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
107
tests/test_m5_t5.py
Normal file
107
tests/test_m5_t5.py
Normal file
@@ -0,0 +1,107 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
from volve_dia import m5_t5 as t5
|
||||
|
||||
|
||||
@pytest.mark.parametrize("unit,expected", [
|
||||
("Hugin Fm. VOLVE", "hugin"), ("Heather Fm. Sand VOLVE", "heather"), ("NORDLAND GP.", "nordland"),
|
||||
("Smith Bank Fm.", "smith bank"), ("Seabed", "seabed"), ("Heather Fm.", "heather")])
|
||||
def test_normalise_unit(unit, expected):
|
||||
assert t5.normalise_unit(unit) == expected
|
||||
|
||||
|
||||
def test_formation_and_eligibility():
|
||||
picks = pd.DataFrame({"wellbore": ["W1", "W1", "W1"], "unit": ["NORDLAND GP.", "Utsira Fm.", "Hugin Fm. VOLVE"],
|
||||
"md": [100.0, 500.0, 3000.0]})
|
||||
assert t5.formation_at(picks, "W1", 50) is None
|
||||
assert t5.formation_at(picks, "W1", 500) == "utsira"
|
||||
assert t5.formation_at(picks, "W1", 3500) == "hugin"
|
||||
intervals = pd.DataFrame({
|
||||
"interval_id": ["W1:b:I1", "W1:b:I2", "W1:b:I3", "W1:a:I1", "W2:b:I1"], "definition": ["b", "b", "b", "a", "b"],
|
||||
"wellbore": ["W1", "W1", "W1", "W1", "W2"], "well": ["A"] * 5, "md_top": [80.0, 600.0, 3100.0, 600.0, 700.0],
|
||||
"causes": ["equipment", "equipment|weather", None, "other", "other"],
|
||||
"act_ids": [["W1_2008_01_02:001"], ["W1_2008_01_03:004", "W1_2008_01_03:002"], ["W1_2008_01_09:001"],
|
||||
["W1_2008_01_03:002"], ["W2_2008_02_01:001"]]})
|
||||
cells = pd.DataFrame({"wellbore": ["W1", "W2"], "own_hole_top_md": [50.0, 0.0]})
|
||||
sections = pd.Series({"W1_2008_01_02": "26", "W1_2008_01_03": "12.25"})
|
||||
items = t5.eligible_intervals(intervals, cells, sections, picks, ["W1"], "b")
|
||||
# I1 lies above the first pick, I3 has no section, W2 is not a T3 wellbore.
|
||||
assert list(items["interval_id"]) == ["W1:b:I2"]
|
||||
row = items.iloc[0]
|
||||
assert row["report_id"] == "W1_2008_01_03" and row["formation"] == "utsira"
|
||||
assert row["cause_set"] == frozenset({"equipment", "weather"})
|
||||
|
||||
|
||||
def test_gains_and_metrics():
|
||||
query = pd.Series({"section": "8.5", "formation": "hugin", "cause_set": frozenset({"equipment"})})
|
||||
pool = pd.DataFrame({"section": ["8.5", "8.5", "12.25", "8.5"], "formation": ["hugin", "ty", "hugin", "hugin"],
|
||||
"cause_set": [frozenset({"equipment", "weather"}), frozenset(), frozenset({"equipment"}),
|
||||
frozenset({"weather"})]})
|
||||
gain, parts = t5.gains(query, pool)
|
||||
assert list(gain) == [3, 1, 2, 2]
|
||||
best = t5.ranking_metrics(np.array([0, 2, 3, 1]), gain, parts, k=2)
|
||||
assert best["ndcg10"] == pytest.approx(1.0) and best["mrr"] == 1.0 and best["p10"] == 0.5
|
||||
worst = t5.ranking_metrics(np.array([1, 2, 3, 0]), gain, parts, k=2)
|
||||
idcg = 3 + 2 / np.log2(3)
|
||||
assert worst["ndcg10"] == pytest.approx((1 + 2 / np.log2(3)) / idcg) and worst["mrr"] == 0.25
|
||||
assert worst["section10"] == 0.5 and worst["formation10"] == 0.5
|
||||
none = t5.ranking_metrics(np.array([1, 0]), np.array([0, 2]), {a: np.zeros(2, bool) for a in t5.ATTRIBUTES}, k=2)
|
||||
assert np.isnan(none["mrr"])
|
||||
|
||||
|
||||
def test_order_ties_and_rrf():
|
||||
assert list(t5.order([1.0, 2.0, 1.0], np.array([2, 0, 1]))) == [1, 2, 0]
|
||||
fused = t5.rrf([np.array([3.0, 2.0, 1.0]), np.array([1.0, 2.0, 3.0])], np.arange(3))
|
||||
assert fused[1] == pytest.approx(2 / 62) and fused[0] == pytest.approx(fused[2])
|
||||
|
||||
|
||||
def synthetic_items(n_wells=4, per_well=12, seed=0):
|
||||
rng = np.random.default_rng(seed)
|
||||
rows = []
|
||||
for w in range(n_wells):
|
||||
for i in range(per_well):
|
||||
deep = i % 2 == 0
|
||||
rows.append({"interval_id": f"W{w}:b:I{i:02d}", "well": f"W{w}", "wellbore": f"W{w}",
|
||||
"md_top": (3000.0 if deep else 500.0) + rng.normal(0, 50),
|
||||
"section": "8.5" if deep else "17.5", "formation": "hugin" if deep else "utsira",
|
||||
"cause_set": frozenset({"equipment" if deep else "weather"}),
|
||||
"text_pre": "reaming tight hole" if deep else "waiting on weather",
|
||||
"text_event": "packed off" if deep else "crane stopped wind",
|
||||
**{c: rng.normal(5 if deep else 0, 1) for c in t5.WITSML_COLUMNS}})
|
||||
return pd.DataFrame(rows)
|
||||
|
||||
|
||||
def test_evaluate_and_summarise():
|
||||
items = synthetic_items()
|
||||
per_query = t5.evaluate(items, "well", seed=1, orders=20)
|
||||
assert set(per_query["representation"]) == set(t5.REPRESENTATIONS)
|
||||
assert per_query.groupby("representation").size().eq(len(items)).all()
|
||||
assert (per_query["n_candidates"] == 36).all() and (per_query["n_fully_relevant"] == 18).all()
|
||||
mean = per_query.groupby("representation")["ndcg10"].mean()
|
||||
for rep in ("depth", "witsml_pre", "text_pre", "text_event", "rrf_pre", "rrf_all"):
|
||||
assert mean[rep] == pytest.approx(1.0)
|
||||
assert mean["random"] < 0.9
|
||||
summary = t5.summarise(per_query, resamples=200, seed=1)
|
||||
depth = summary["depth"]
|
||||
assert depth["ndcg10"]["mean"] == pytest.approx(1.0)
|
||||
assert depth["ndcg10_minus_random"]["ci95"][0] > 0
|
||||
assert "ndcg10_minus_depth" not in depth and "ndcg10_minus_random" not in summary["random"]
|
||||
|
||||
|
||||
def test_cluster_ci_weights_queries():
|
||||
out = t5.cluster_ci([1.0, 1.0, 1.0, 0.0, np.nan], ["A", "A", "A", "B", "B"], 500, 0)
|
||||
assert out["n"] == 4 and out["mean"] == pytest.approx(0.75)
|
||||
assert out["ci95"][0] <= 0.75 <= out["ci95"][1]
|
||||
|
||||
|
||||
def test_knn_scores_leave_group_out():
|
||||
features = pd.DataFrame({"well": ["A"] * 4 + ["B"] * 4, "md_top": [10.0, 20, 30, 40] * 2,
|
||||
"label_b": [1, 0, 0, 0, 1, 0, 0, 0]})
|
||||
scores = t5.knn_scores(features, "label_b", ["md_top"], k=1)
|
||||
assert list(scores) == [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
|
||||
result = t5.ap_ci(features["label_b"], scores, features["well"], 100, 0)
|
||||
assert result["ap"] == pytest.approx(1.0) and result["prevalence"] == 0.25
|
||||
Reference in New Issue
Block a user