yang
This project has not described itself yet.
Brought by drakestapleton
yang / scripts / summarize_vertex_shell.py
| 1 | """Summarize recorded --vertex-shell TSVs without treating correlated rows as IID.""" |
| 2 | import csv |
| 3 | import sys |
| 4 | from itertools import groupby |
| 5 | |
| 6 | import numpy as np |
| 7 | |
| 8 | |
| 9 | def summarize(rows): |
| 10 | def col(name): |
| 11 | return np.array([float(r[name]) for r in rows]) |
| 12 | |
| 13 | inner, outer, witness, lower = map(col, ('inner_j', 'outer_j', 'witness_j', 'dual_lower_j')) |
| 14 | fixed = np.stack([outer, col('checker_j'), col('pattern_j'), col('pattern_checker_j')]) |
| 15 | weight, cancel, logloss = map(col, ('inner_abs', 'weighted_cancellation', 'log_weighted_loss')) |
| 16 | # Decision buffer applies to signs, not to the measure of a neighborhood. |
| 17 | eps = 1e-8 |
| 18 | assert np.all(lower <= witness+eps) and np.all(witness <= outer+eps) |
| 19 | positive = fixed.max(axis=0) > eps |
| 20 | negative = np.minimum(witness, fixed.min(axis=0)) < -eps |
| 21 | crosses = positive & negative |
| 22 | excludes = lower > eps |
| 23 | assert not np.any(negative & excludes) |
| 24 | np.testing.assert_allclose(np.abs(np.tanh(inner)), weight, atol=1e-10, rtol=0) |
| 25 | np.testing.assert_allclose(col('weighted_mean_abs')-col('weighted_abs_mean'), cancel, atol=1e-10, rtol=0) |
| 26 | assert not np.isnan(logloss).any() and np.all(logloss <= 1e-9) |
| 27 | if weight.sum(): |
| 28 | log_fraction = (np.logaddexp.reduce(logloss)-np.log(weight.sum()))/np.log(10.) |
| 29 | gain = cancel.sum()/weight.sum() |
| 30 | else: |
| 31 | log_fraction = gain = float('nan') # no remaining product to normalize |
| 32 | return (len(rows), crosses.sum(), excludes.sum(), (~(crosses|excludes)).sum(), |
| 33 | (positive & (fixed.min(axis=0)<-eps)).sum(), gain, log_fraction) |
| 34 | |
| 35 | |
| 36 | if __name__ == '__main__': |
| 37 | print('group beta start n witness_cross dual_positive unresolved fixed_cross cancellation_fraction log10_loss_fraction') |
| 38 | for path in sys.argv[1:]: |
| 39 | with open(path) as f: |
| 40 | for key, group in groupby(csv.DictReader(f, delimiter='\t'), lambda r: (r['group'], r['beta'], r['start'])): |
| 41 | print(*key, *(f'{x:.9g}' for x in summarize(list(group)))) |