46 lines
3.1 KiB
Python
46 lines
3.1 KiB
Python
"""S3-A reporting using unchanged S2 metrics, aggregation and bootstrap."""
|
|
import numpy as np
|
|
from s2_custody_statistics import component_values, contrast, part_metrics
|
|
from s3a_data import ARMS
|
|
|
|
|
|
def reports(rows, roster, frozen_members):
|
|
keys = [(r['case'], r['arm'], r['pool'], r['seed']) for r in rows]
|
|
if len(keys) != len(set(keys)):
|
|
raise ValueError('Duplicate part/arm/pool score')
|
|
if any(r['arm'] not in ('R2', *ARMS) or r['pool'] not in (25, 50, 100) or r['seed'] is not None for r in rows):
|
|
raise ValueError('Unexpected arm/pool/seed')
|
|
tables, contrasts, component_rows = [], [], []
|
|
for subset in ('all', 'not_known_exposed'):
|
|
for pool in (25, 50, 100):
|
|
primary = {}
|
|
for metric in ('relative_l2', 'near_wall', 'kendall_tau_b', 'last_location'):
|
|
for arm in ('R2', *ARMS):
|
|
values, excluded, details = component_values(rows, roster, arm, pool, metric,
|
|
subset != 'all', frozen_members)
|
|
worst = max(values, key=values.get) if values and metric == 'relative_l2' else None
|
|
tables.append(dict(subset=subset, pool=pool, arm=arm, metric=metric,
|
|
mean=float(np.mean(list(values.values()))) if values else None,
|
|
components=len(values), included_parts=sum(details[k]['parts'] for k in values),
|
|
included_nodes=sum(details[k]['nodes'] for k in values), excluded_components=excluded,
|
|
fold_means={str(f): float(np.mean(v)) if v else None for f in range(5)
|
|
for v in [[value for k, value in values.items() if details[k]['fold'] == f]]},
|
|
worst_component=dict(component_id=worst, value=values[worst], **details[worst]) if worst else None))
|
|
component_rows.extend(dict(subset=subset, pool=pool, arm=arm, metric=metric,
|
|
component_id=k, value=v, **details[k]) for k, v in values.items())
|
|
if metric == 'relative_l2':
|
|
primary[arm] = (values, details)
|
|
reference, details = primary['R2']
|
|
for arm in ARMS:
|
|
candidate, other_details = primary[arm]
|
|
pair = set(reference) & set(candidate)
|
|
excluded = sorted((set(details) | set(other_details)) - pair)
|
|
contrasts.append(dict(subset=subset, pool=pool, reference='R2', candidate=arm,
|
|
primary=subset == 'all' and pool == 100 and arm == 'R2_E4',
|
|
interpretation='post-S2 adaptive follow-up; interval conditional on fixed models/folds',
|
|
included_folds=sorted({details[k]['fold'] for k in pair}),
|
|
excluded_components=excluded, excluded_parts=sum(details[k]['parts'] for k in excluded),
|
|
excluded_nodes=sum(details[k]['nodes'] for k in excluded),
|
|
**contrast(reference, candidate, replicates=2000, seed=2026111408)))
|
|
return tables, contrasts, component_rows
|