68 lines
3.3 KiB
Python
68 lines
3.3 KiB
Python
"""R4(a) shared-cell-speed graph and descriptive grouped statistics."""
|
|
from itertools import combinations
|
|
import numpy as np
|
|
from scipy.sparse import csr_matrix
|
|
from scipy.sparse.csgraph import dijkstra
|
|
from s2_custody_statistics import contrast
|
|
|
|
|
|
def graph_field(g, speed, sources):
|
|
speed = np.asarray(speed, dtype=np.float64)
|
|
if speed.shape != (len(g['t']),) or not np.isfinite(speed).all() or np.any(speed <= 0):
|
|
raise ValueError('Invalid shared cell speed')
|
|
edges = np.sort(g['t'][:, list(combinations(range(4), 2))].reshape(-1, 2), axis=1)
|
|
incident_speed = np.repeat(speed, 6)
|
|
unique, inverse = np.unique(edges, axis=0, return_inverse=True)
|
|
fastest = np.zeros(len(unique), dtype=np.float64)
|
|
np.maximum.at(fastest, inverse, incident_speed)
|
|
weights = np.linalg.norm(g['x'][unique[:, 0]]-g['x'][unique[:, 1]], axis=1)/fastest
|
|
if not np.isfinite(weights).all() or np.any(weights <= 0):
|
|
raise ValueError('Nonpositive edge length/cost; no repair')
|
|
graph = csr_matrix((np.r_[weights, weights],
|
|
(np.r_[unique[:, 0], unique[:, 1]], np.r_[unique[:, 1], unique[:, 0]])),
|
|
shape=(len(g['x']), len(g['x'])))
|
|
field = dijkstra(graph, directed=False, indices=np.asarray(sources), min_only=True)
|
|
if not np.isfinite(field).all() or np.any(field < 0) or np.any(field[sources] != 0):
|
|
raise ValueError('Invalid/unreachable graph field')
|
|
return field, dict(edges=len(unique), minimum_cost=float(weights.min()), maximum_cost=float(weights.max()))
|
|
|
|
|
|
def paired_summary(rows, roster, metric):
|
|
by = {r['case']: r for r in rows}
|
|
groups = {}
|
|
for r in roster:
|
|
groups.setdefault(r['component_id'], []).append(r['case'])
|
|
values = {'F': {}, 'G': {}}
|
|
excluded = {}
|
|
for component, cases in sorted(groups.items()):
|
|
reasons = []
|
|
for case in cases:
|
|
for arm in values:
|
|
value = by.get(case, {}).get('metrics', {}).get(arm, {}).get(metric, {}).get('value')
|
|
if value is None or not np.isfinite(value):
|
|
reasons.append(dict(case=case, arm=arm, reason='missing or undefined metric'))
|
|
if reasons:
|
|
excluded[component] = reasons
|
|
continue
|
|
for arm in values:
|
|
values[arm][component] = float(np.mean([by[case]['metrics'][arm][metric]['value'] for case in cases]))
|
|
result = dict(component_values=values, excluded_components=excluded,
|
|
paired_components=len(values['F']), requested_components=len(groups),
|
|
paired_parts=sum(len(groups[k]) for k in values['F']))
|
|
if metric == 'relative_l2':
|
|
delta = contrast(values['G'], values['F'], replicates=2000, seed=2026111408)
|
|
delta.pop('region', None)
|
|
delta['interpretation'] = 'descriptive only; no CI gate or effect-size margin'
|
|
result['delta'] = delta
|
|
else:
|
|
result['means'] = {a: float(np.mean(list(v.values()))) if v else None for a, v in values.items()}
|
|
return result
|
|
|
|
|
|
def may_dispatch(spent, active, cap=28800., per_part=1800.):
|
|
return spent+(active+1)*(per_part+2.) <= cap
|
|
|
|
|
|
def scoring_allowed(records, requested, stop_reason):
|
|
return stop_reason is None and len(records) == requested and all(r['worker']['status'] == 'PASS' for r in records)
|