#!/usr/bin/env python import csv from pathlib import Path import numpy as np out = [] def emit(metric, *values): line = ' '.join([metric, *map(str, values)]) print(line) out.append((metric, values)) stats = Path('/workspace/mp-20/xrd_stats/mp20_xrd_material_stats.csv') peaks = Path('/workspace/mp-20/xrd_stats/mp20_xrd_peaks.csv') mins = [] lasts = [] retry = [] with stats.open() as f: for row in csv.DictReader(f): if row['status'] == 'ok': mins.append(float(row['min_peak_gap'])) lasts.append(float(row['last_peak_2theta'])) if row['error']: retry.append(row) mins = np.array(mins) lasts = np.array(lasts) emit('materials', len(lasts)) emit('boundary_retry', len(retry), retry[0]['material_id'] if retry else '') for t in [0.001, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2]: emit(f'per_material_min_gap_le_{t:g}', int((mins <= t).sum()), float((mins <= t).mean())) for t in [120, 130, 140, 150, 160, 170, 175, 178, 179, 180]: emit(f'last_peak_le_{t:g}', int((lasts <= t).sum()), float((lasts <= t).mean())) thresholds = [0.001, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2] counts = dict.fromkeys(thresholds, 0) total = 0 prev_key = None prev_theta = None smallest = [] with peaks.open() as f: reader = csv.DictReader(f) for row in reader: key = (row['split'], row['index'], row['material_id']) theta = float(row['two_theta']) if key == prev_key: gap = theta - prev_theta total += 1 for t in thresholds: if gap <= t: counts[t] += 1 if len(smallest) < 10 or gap < smallest[-1][0]: smallest.append((gap, key, prev_theta, theta)) smallest.sort(key=lambda x: x[0]) smallest = smallest[:10] prev_key = key prev_theta = theta emit('all_adjacent_total', total) for t in thresholds: emit(f'all_adjacent_gap_le_{t:g}', counts[t], counts[t] / total) emit('smallest_gaps', '') for rank, item in enumerate(smallest, start=1): gap, key, left, right = item emit(f'smallest_gap_{rank}', gap, '|'.join(key), left, right) with open('/workspace/mp-20/xrd_stats/mp20_xrd_threshold_summary.csv', 'w', encoding='utf-8') as f: f.write('metric,value1,value2,value3,value4\n') for metric, values in out: vals = list(values)[:4] vals += [''] * (4 - len(vals)) f.write(metric + ',' + ','.join(map(str, vals)) + '\n')