Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
72 lines
3.2 KiB
Python
72 lines
3.2 KiB
Python
"""Counterfactual plans and exact feature comparisons, without model dependencies.
|
|
|
|
An unchanged map is evidence of preservation on that input, never recall or
|
|
generalization. Every retained stage after the first omission must be rerun.
|
|
"""
|
|
import hashlib
|
|
import json
|
|
|
|
|
|
def digest(value):
|
|
return hashlib.sha256(json.dumps(value, sort_keys=True, allow_nan=False,
|
|
separators=(',', ':')).encode()).hexdigest()
|
|
|
|
|
|
def features(document):
|
|
result = {f['id']: f for f in document['features']}
|
|
if len(result) != len(document['features']):
|
|
raise ValueError('Duplicate feature identity')
|
|
digest(result) # Reject NaN, including unchanged invalid input.
|
|
return result
|
|
|
|
|
|
def compare(reference, candidate):
|
|
a, b = features(reference), features(candidate)
|
|
shared = a.keys() & b.keys()
|
|
changed = sorted(k for k in shared if a[k] != b[k])
|
|
nonmarking = lambda fs: {k: v for k, v in fs.items()
|
|
if v['properties']['class'] != 'road_marking'}
|
|
return dict(features_equal=a == b, reference_features=len(a),
|
|
candidate_features=len(b), added_ids=sorted(b.keys() - a.keys()),
|
|
removed_ids=sorted(a.keys() - b.keys()), changed_ids=changed,
|
|
non_marking_equal=nonmarking(a) == nonmarking(b),
|
|
reference_sha256=digest(a), candidate_sha256=digest(b),
|
|
recall_measured=False, accuracy_measured=False)
|
|
|
|
|
|
def tail_plan(definition, start, omitted):
|
|
groups = definition['groups']
|
|
names = [g['group'] for g in groups]
|
|
if len(names) != len(set(names)) or start not in names:
|
|
raise ValueError('Invalid group definition/start')
|
|
omitted = set(omitted)
|
|
tail = groups[names.index(start):]
|
|
if not omitted <= {g['group'] for g in tail}:
|
|
raise ValueError('Omission outside replay tail')
|
|
# Explicit named inputs cannot silently fall back to frozen artifacts of a
|
|
# removed producer. Implicit alias consumers are additionally reviewed by
|
|
# the frozen input manifest and the read-only mount census.
|
|
for group in tail:
|
|
if group['group'] in omitted:
|
|
continue
|
|
dependencies = set(group.get('input_files', {}).values())
|
|
if group.get('extra_coverage'):
|
|
dependencies.add(group['extra_coverage'])
|
|
if dependencies & omitted:
|
|
raise ValueError('Removed named producer: ' + group['group'])
|
|
return dict(start=start, omitted=sorted(omitted),
|
|
groups=[g['group'] for g in tail if g['group'] not in omitted],
|
|
stages=sum(len(g['stages']) for g in tail if g['group'] not in omitted),
|
|
skipped_stages=sum(len(g['stages']) for g in tail if g['group'] in omitted),
|
|
downstream_cache_reuse=False)
|
|
|
|
|
|
def validate_split(development, holdout):
|
|
"""Reject exact-source and same-location leakage, not just duplicate IDs."""
|
|
for key in ('source_sha256', 'location_group'):
|
|
if any(not x.get(key) for x in development + holdout):
|
|
raise ValueError('Missing split provenance: ' + key)
|
|
if {x[key] for x in development} & {x[key] for x in holdout}:
|
|
raise ValueError('Evaluation leakage: ' + key)
|
|
return True
|