auto-sync 2026-07-02T17:27:17Z workspace (part 4)
Browse files
workspace/tests/test_causal_action_metrics.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
from dovla_cil.eval.metrics import (
|
| 6 |
+
branch_causal_action_regret,
|
| 7 |
+
candidate_prefix_causal_metrics,
|
| 8 |
+
causal_action_decomposition,
|
| 9 |
+
normalized_causal_action_regret,
|
| 10 |
+
positive_tangent_recall_at_k,
|
| 11 |
+
selector_regret_at_k,
|
| 12 |
+
support_gap,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_causal_action_regret_metrics() -> None:
|
| 17 |
+
assert branch_causal_action_regret(2.0, 1.25) == 0.75
|
| 18 |
+
assert selector_regret_at_k([1.25, 2.0, 0.5], selected_index=0) == 0.75
|
| 19 |
+
assert support_gap(3.0, 2.0) == 1.0
|
| 20 |
+
assert positive_tangent_recall_at_k([0.4, 0.7], base_utility=0.6) == 1.0
|
| 21 |
+
assert positive_tangent_recall_at_k([0.4, 0.6], base_utility=0.6) == 0.0
|
| 22 |
+
assert math.isclose(
|
| 23 |
+
normalized_causal_action_regret(2.0, 1.25, 1.0),
|
| 24 |
+
0.75,
|
| 25 |
+
rel_tol=1.0e-6,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_candidate_prefix_metrics_use_policy_residual_as_base() -> None:
|
| 30 |
+
metrics = candidate_prefix_causal_metrics(
|
| 31 |
+
branch_scores=[1.0, 1.4, 0.2],
|
| 32 |
+
branch_types=[
|
| 33 |
+
"retrieval_residual_policy_residual",
|
| 34 |
+
"retrieval_residual_residual_near_miss",
|
| 35 |
+
"retrieval_residual_residual_wrong_gripper",
|
| 36 |
+
],
|
| 37 |
+
valid_mask=[True, True, True],
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
assert math.isclose(metrics["car_to_proposal_oracle"], 0.4)
|
| 41 |
+
assert math.isclose(metrics["selector_regret_at_k"], 0.4)
|
| 42 |
+
assert metrics["ptr_at_k"] == 1.0
|
| 43 |
+
assert metrics["base_utility"] == 1.0
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_causal_action_decomposition_matches_support_selector_story() -> None:
|
| 47 |
+
decomposition = causal_action_decomposition(
|
| 48 |
+
base=0.2974,
|
| 49 |
+
selected=0.3890,
|
| 50 |
+
proposal_oracle=0.4435,
|
| 51 |
+
same_state_oracle=0.5699,
|
| 52 |
+
full_oracle=0.6933,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
assert math.isclose(decomposition.clean_gain, 0.0916)
|
| 56 |
+
assert math.isclose(decomposition.selector_gap, 0.0545)
|
| 57 |
+
assert math.isclose(decomposition.support_gap, 0.1264)
|
| 58 |
+
assert math.isclose(decomposition.target_for_gap_closure(0.65), 0.474525)
|
workspace/tests/test_cil_schema.py
CHANGED
|
@@ -5,9 +5,12 @@ import pytest
|
|
| 5 |
from dovla_cil.data.schema import (
|
| 6 |
CIL_VERSION,
|
| 7 |
ActionChunk,
|
|
|
|
|
|
|
| 8 |
CILGroup,
|
| 9 |
CILRecord,
|
| 10 |
FailureInfo,
|
|
|
|
| 11 |
RewardInfo,
|
| 12 |
StructuredEffect,
|
| 13 |
compute_regret_and_ranks,
|
|
@@ -96,3 +99,62 @@ def test_deterministic_record_ids_and_state_hashes() -> None:
|
|
| 96 |
assert make_record_id("g", "a", 1) != make_record_id("g", "a", 2)
|
| 97 |
assert compute_state_hash(b"state") == compute_state_hash(b"state")
|
| 98 |
assert compute_state_hash(b"state") != compute_state_hash(b"other")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from dovla_cil.data.schema import (
|
| 6 |
CIL_VERSION,
|
| 7 |
ActionChunk,
|
| 8 |
+
CILBenchBranch,
|
| 9 |
+
CILBenchGroup,
|
| 10 |
CILGroup,
|
| 11 |
CILRecord,
|
| 12 |
FailureInfo,
|
| 13 |
+
OutcomeVector,
|
| 14 |
RewardInfo,
|
| 15 |
StructuredEffect,
|
| 16 |
compute_regret_and_ranks,
|
|
|
|
| 99 |
assert make_record_id("g", "a", 1) != make_record_id("g", "a", 2)
|
| 100 |
assert compute_state_hash(b"state") == compute_state_hash(b"state")
|
| 101 |
assert compute_state_hash(b"state") != compute_state_hash(b"other")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def test_outcome_vector_from_reward_and_utility() -> None:
|
| 105 |
+
reward = RewardInfo(
|
| 106 |
+
progress=0.6,
|
| 107 |
+
success=True,
|
| 108 |
+
terminal_success=True,
|
| 109 |
+
dense_components={
|
| 110 |
+
"contact_quality": 0.5,
|
| 111 |
+
"task_stage_quality": 0.25,
|
| 112 |
+
"smoothness": 0.8,
|
| 113 |
+
"recovery": 1.0,
|
| 114 |
+
},
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
outcome = OutcomeVector.from_reward(reward)
|
| 118 |
+
|
| 119 |
+
assert outcome.success == 1.0
|
| 120 |
+
assert outcome.progress == 0.6
|
| 121 |
+
assert outcome.contact_quality == 0.5
|
| 122 |
+
assert outcome.safety_violation == 0.0
|
| 123 |
+
assert outcome.lexicographic_utility() > reward.score
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def test_cilbench_group_roundtrip_and_same_state_contrast() -> None:
|
| 127 |
+
base_action = ActionChunk(action_id="base", horizon=1, values=[[0.0, 0.0]])
|
| 128 |
+
repair_action = ActionChunk(action_id="repair", horizon=1, values=[[0.1, 0.0]])
|
| 129 |
+
group = CILBenchGroup(
|
| 130 |
+
group_id="chart-0",
|
| 131 |
+
task_id="PickCube-v1",
|
| 132 |
+
split_id="train",
|
| 133 |
+
simulator_state_hash="hash",
|
| 134 |
+
instruction="pick the cube",
|
| 135 |
+
observation_ref=None,
|
| 136 |
+
observation_inline={"rgb": "obs/000.png"},
|
| 137 |
+
scene_metadata={"target_object": "cube"},
|
| 138 |
+
anchor_policy="h16_bc",
|
| 139 |
+
branches=[
|
| 140 |
+
CILBenchBranch(
|
| 141 |
+
branch_id="base",
|
| 142 |
+
action=base_action,
|
| 143 |
+
branch_family="anchor",
|
| 144 |
+
outcome=OutcomeVector(success=0.0, progress=0.2),
|
| 145 |
+
),
|
| 146 |
+
CILBenchBranch(
|
| 147 |
+
branch_id="repair",
|
| 148 |
+
action=repair_action,
|
| 149 |
+
branch_family="recovery_tangent",
|
| 150 |
+
outcome=OutcomeVector(success=1.0, progress=0.8, recovery=1.0),
|
| 151 |
+
),
|
| 152 |
+
],
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
restored = CILBenchGroup.from_dict(group.to_dict())
|
| 156 |
+
|
| 157 |
+
assert restored == group
|
| 158 |
+
assert restored.same_state_causal_contrast("repair", "base") > 0.0
|
| 159 |
+
with pytest.raises(KeyError):
|
| 160 |
+
restored.same_state_causal_contrast("repair", "missing")
|
workspace/tests/test_maniskill_policy_rollout.py
CHANGED
|
@@ -135,6 +135,10 @@ def test_policy_rollout_summary_includes_candidate_oracle_when_present() -> None
|
|
| 135 |
"candidate_oracle_restore_error": 3e-7,
|
| 136 |
"candidate_oracle_candidate_type": "retrieval_residual_residual_no_op",
|
| 137 |
"candidate_oracle_valid_mask": [True, True],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
"candidate_oracle_branch_successes": [False, True],
|
| 139 |
"candidate_oracle_branch_progress": [0.1, 0.8],
|
| 140 |
"candidate_oracle_branch_scores": [0.1, 1.8],
|
|
@@ -162,6 +166,10 @@ def test_policy_rollout_summary_includes_candidate_oracle_when_present() -> None
|
|
| 162 |
"candidate_oracle_restore_error": 2e-7,
|
| 163 |
"candidate_oracle_candidate_type": "retrieval_residual_policy_residual",
|
| 164 |
"candidate_oracle_valid_mask": [True, True],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
"candidate_oracle_branch_successes": [True, True],
|
| 166 |
"candidate_oracle_branch_progress": [0.9, 0.8],
|
| 167 |
"candidate_oracle_branch_scores": [1.9, 1.8],
|
|
@@ -188,6 +196,10 @@ def test_policy_rollout_summary_includes_candidate_oracle_when_present() -> None
|
|
| 188 |
summary["candidate_oracle_branch_score_gains_over_selected"],
|
| 189 |
[0.0, 0.8],
|
| 190 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
|
| 193 |
def test_policy_rollout_loads_state_archive(tmp_path: Path) -> None:
|
|
|
|
| 135 |
"candidate_oracle_restore_error": 3e-7,
|
| 136 |
"candidate_oracle_candidate_type": "retrieval_residual_residual_no_op",
|
| 137 |
"candidate_oracle_valid_mask": [True, True],
|
| 138 |
+
"candidate_oracle_types": [
|
| 139 |
+
"retrieval_residual_policy_residual",
|
| 140 |
+
"retrieval_residual_residual_no_op",
|
| 141 |
+
],
|
| 142 |
"candidate_oracle_branch_successes": [False, True],
|
| 143 |
"candidate_oracle_branch_progress": [0.1, 0.8],
|
| 144 |
"candidate_oracle_branch_scores": [0.1, 1.8],
|
|
|
|
| 166 |
"candidate_oracle_restore_error": 2e-7,
|
| 167 |
"candidate_oracle_candidate_type": "retrieval_residual_policy_residual",
|
| 168 |
"candidate_oracle_valid_mask": [True, True],
|
| 169 |
+
"candidate_oracle_types": [
|
| 170 |
+
"retrieval_residual_policy_residual",
|
| 171 |
+
"retrieval_residual_residual_near_miss",
|
| 172 |
+
],
|
| 173 |
"candidate_oracle_branch_successes": [True, True],
|
| 174 |
"candidate_oracle_branch_progress": [0.9, 0.8],
|
| 175 |
"candidate_oracle_branch_scores": [1.9, 1.8],
|
|
|
|
| 196 |
summary["candidate_oracle_branch_score_gains_over_selected"],
|
| 197 |
[0.0, 0.8],
|
| 198 |
)
|
| 199 |
+
assert np.isclose(summary["candidate_oracle_car_to_proposal_oracle"], 0.85)
|
| 200 |
+
assert np.isclose(summary["candidate_oracle_selector_regret_at_k"], 0.85)
|
| 201 |
+
assert summary["candidate_oracle_ptr_at_k"] == 0.5
|
| 202 |
+
assert summary["candidate_oracle_base_trace_coverage"] == 1.0
|
| 203 |
|
| 204 |
|
| 205 |
def test_policy_rollout_loads_state_archive(tmp_path: Path) -> None:
|