yang
This project has not described itself yet.
Brought by drakestapleton
yang / scripts / compare_local_neighborhoods.py
| 1 | """Compare total incoming weight lost over both shells, on matched replica pairs.""" |
| 2 | import argparse |
| 3 | import csv |
| 4 | from itertools import groupby |
| 5 | |
| 6 | import numpy as np |
| 7 | |
| 8 | CHAIN = ('group', 'side', 'warmup', 'samples', 'stride', 'beta', 'start', 'seed') |
| 9 | SHARED = (*CHAIN, 'sample', 'source_faces', 'target_faces', 'source_j', 'target_j') |
| 10 | |
| 11 | |
| 12 | def compare(local, interior): |
| 13 | pairs = zip(local, interior, strict=True) |
| 14 | for key, chain in groupby(pairs, lambda pair: tuple(pair[0][k] for k in CHAIN)): |
| 15 | rows = list(chain) |
| 16 | if [int(a['sample']) for a, _ in rows] != list(range(int(key[3]))): |
| 17 | raise ValueError('incomplete or reordered local sample series') |
| 18 | if any(a[k] != b[k] for a, b in rows for k in SHARED): |
| 19 | raise ValueError('neighborhood and whole-slice pairs do not match') |
| 20 | def field(side, name): |
| 21 | return np.array([float(pair[side][name+'_j']) for pair in rows]) |
| 22 | a = abs(np.tanh(field(0, 'source'))) |
| 23 | live = a > 0 |
| 24 | values = [] |
| 25 | for side, first, second in ((0,'narrow1','narrow2'), (0,'wide1','wide2'), |
| 26 | (1,'radius1','radius2')): |
| 27 | if not live.any(): |
| 28 | values.append(float('nan')) |
| 29 | continue |
| 30 | b, c = (abs(field(side, k)[live]) for k in (first, second)) |
| 31 | # 1 - BC = (1 - B) + B(1 - C). Keep both terms even if B rounds to one. |
| 32 | log_first = np.log(2.)-np.logaddexp(0., 2*b) |
| 33 | with np.errstate(divide='ignore'): |
| 34 | log_second = np.log(np.tanh(b))+np.log(2.)-np.logaddexp(0., 2*c) |
| 35 | loss = np.log(a[live])+np.logaddexp(log_first, log_second) |
| 36 | values.append((np.logaddexp.reduce(loss)-np.log(a.sum()))/np.log(10.)) |
| 37 | yield key, values |
| 38 | |
| 39 | |
| 40 | if __name__ == '__main__': |
| 41 | parser = argparse.ArgumentParser(description=__doc__) |
| 42 | parser.add_argument('local', type=argparse.FileType('r')) |
| 43 | parser.add_argument('interior', type=argparse.FileType('r')) |
| 44 | args = parser.parse_args() |
| 45 | print('group beta start pairs log10_joint_narrow log10_joint_wide log10_joint_slices') |
| 46 | for key, values in compare(*(csv.DictReader(f, delimiter='\t') for f in (args.local, args.interior))): |
| 47 | print(key[0], key[5], key[6], key[3], *(f'{x:.9g}' for x in values)) |