|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Plot the histogram of measurements. All the input files |
| 5 | +are specified in the path json file. |
| 6 | +
|
| 7 | +:copyright: |
| 8 | + Wenjie Lei (lei@princeton.edu), 2016 |
| 9 | +:license: |
| 10 | + GNU Lesser General Public License, version 3 (LGPLv3) |
| 11 | + (http://www.gnu.org/licenses/lgpl-3.0.en.html) |
| 12 | +""" |
| 13 | +from __future__ import print_function, division |
| 14 | +import os |
| 15 | +import json |
| 16 | +import argparse |
| 17 | +import numpy as np |
| 18 | +import matplotlib |
| 19 | +matplotlib.use('Agg') |
| 20 | +import matplotlib.pyplot as plt |
| 21 | + |
| 22 | + |
| 23 | +def load_txt(txtfile): |
| 24 | + with open(txtfile, 'r') as fh: |
| 25 | + return [line.rstrip() for line in fh] |
| 26 | + |
| 27 | + |
| 28 | +def load_json(fn): |
| 29 | + with open(fn) as fh: |
| 30 | + return json.load(fh) |
| 31 | + |
| 32 | + |
| 33 | +def dump_json(content, fn): |
| 34 | + with open(fn, 'w') as fh: |
| 35 | + json.dump(content, fh, indent=2, sort_keys=True) |
| 36 | + |
| 37 | + |
| 38 | +def check_file_exists(filename): |
| 39 | + if not os.path.exists(filename): |
| 40 | + raise ValueError("Missing file: %s" % filename) |
| 41 | + |
| 42 | + |
| 43 | +def load_one_measurefile(measure_file): |
| 44 | + measure = load_json(measure_file) |
| 45 | + |
| 46 | + dt = {} |
| 47 | + dlna = {} |
| 48 | + for sta, stainfo in measure.iteritems(): |
| 49 | + for chan, chaninfo in stainfo.iteritems(): |
| 50 | + comp = chan.split(".")[-1] |
| 51 | + if comp not in dt: |
| 52 | + dt[comp] = [] |
| 53 | + dt[comp].extend([m["dt"] for m in chaninfo]) |
| 54 | + if comp not in dlna: |
| 55 | + dlna[comp] = [] |
| 56 | + dlna[comp].extend([m["dlna"] for m in chaninfo]) |
| 57 | + |
| 58 | + return dt, dlna |
| 59 | + |
| 60 | + |
| 61 | +def update_overall(dict_one, dict_all, pb): |
| 62 | + if pb not in dict_all: |
| 63 | + dict_all[pb] = {} |
| 64 | + for comp in dict_one: |
| 65 | + if comp not in dict_all[pb]: |
| 66 | + dict_all[pb][comp] = [] |
| 67 | + dict_all[pb][comp].extend(dict_one[comp]) |
| 68 | + |
| 69 | + |
| 70 | +def get_mean_and_std(dictv): |
| 71 | + mean = {} |
| 72 | + std = {} |
| 73 | + for pb, pbinfo in dictv.iteritems(): |
| 74 | + mean[pb] = {} |
| 75 | + std[pb] = {} |
| 76 | + for comp, compinfo in pbinfo.iteritems(): |
| 77 | + mean[pb][comp] = np.mean(compinfo) |
| 78 | + std[pb][comp] = np.std(compinfo) |
| 79 | + |
| 80 | + return mean, std |
| 81 | + |
| 82 | + |
| 83 | +def stats_analysis(dts, dlnas, outputdir): |
| 84 | + dt_mean, dt_std = get_mean_and_std(dts) |
| 85 | + dlna_mean, dlna_std = get_mean_and_std(dlnas) |
| 86 | + |
| 87 | + log_content = {"dt": {"mean": dt_mean, "std": dt_std}, |
| 88 | + "dlna": {"mean": dlna_mean, "std": dlna_std}} |
| 89 | + |
| 90 | + outputfn = os.path.join(outputdir, "measure.log.json") |
| 91 | + print("log file: %s" % outputfn) |
| 92 | + dump_json(log_content, outputfn) |
| 93 | + |
| 94 | + |
| 95 | +def load_measurements(inputs): |
| 96 | + dts = {} |
| 97 | + dlnas = {} |
| 98 | + for ev, evinfo in inputs.iteritems(): |
| 99 | + for pb, pbinfo in evinfo["period_info"].iteritems(): |
| 100 | + _dt, _dlna = load_one_measurefile(pbinfo["measure_file"]) |
| 101 | + update_overall(_dt, dts, pb) |
| 102 | + update_overall(_dlna, dlnas, pb) |
| 103 | + |
| 104 | + return dts, dlnas |
| 105 | + |
| 106 | + |
| 107 | +def plot_hist(data, figname=None): |
| 108 | + period_bands = ["17_40", "40_100", "90_250"] |
| 109 | + components = ["BHR", "BHT", "BHZ"] |
| 110 | + |
| 111 | + fig = plt.figure(figsize=(20, 20)) |
| 112 | + |
| 113 | + irow = 0 |
| 114 | + for pb in period_bands: |
| 115 | + icol = 0 |
| 116 | + for comp in components: |
| 117 | + idx = irow * 3 + icol + 1 |
| 118 | + plt.subplot(3, 3, idx) |
| 119 | + plt.hist(data[pb][comp], bins=30) |
| 120 | + mean = np.mean(data[pb][comp]) |
| 121 | + std = np.std(data[pb][comp]) |
| 122 | + xloc = plt.xlim()[0] + 0.05 * (plt.xlim()[1] - plt.xlim()[0]) |
| 123 | + plt.text(xloc, plt.ylim()[1] * 0.9, "mean: %.4f" % |
| 124 | + (mean)) |
| 125 | + plt.text(xloc, plt.ylim()[1] * 0.85, "std: %.4f" % |
| 126 | + (std)) |
| 127 | + if icol == 0: |
| 128 | + plt.ylabel(pb) |
| 129 | + if irow == 2: |
| 130 | + plt.xlabel(comp) |
| 131 | + icol += 1 |
| 132 | + irow += 1 |
| 133 | + |
| 134 | + print("Save figure to: %s" % figname) |
| 135 | + plt.tight_layout() |
| 136 | + plt.savefig(figname) |
| 137 | + plt.close(fig) |
| 138 | + |
| 139 | + |
| 140 | +def plot_measures(dts, dlnas, outputdir): |
| 141 | + |
| 142 | + figname = os.path.join(outputdir, "dt.histogram.pdf") |
| 143 | + plot_hist(dts, figname=figname) |
| 144 | + |
| 145 | + figname = os.path.join(outputdir, "dlna.histogram.pdf") |
| 146 | + plot_hist(dlnas, figname=figname) |
| 147 | + |
| 148 | + |
| 149 | +def main(path): |
| 150 | + inputs = path["input"] |
| 151 | + outputdir = path["outputdir"] |
| 152 | + print("Number of events: %d" % len(inputs)) |
| 153 | + if not os.path.exists(outputdir): |
| 154 | + os.makedirs(outputdir) |
| 155 | + |
| 156 | + dts, dlnas = load_measurements(inputs) |
| 157 | + |
| 158 | + stats_analysis(dts, dlnas, outputdir) |
| 159 | + plot_measures(dts, dlnas, outputdir) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + parser = argparse.ArgumentParser() |
| 164 | + parser.add_argument('-f', action='store', dest='path', |
| 165 | + required=True) |
| 166 | + args = parser.parse_args() |
| 167 | + |
| 168 | + path = load_json(args.path) |
| 169 | + main(path) |
0 commit comments