|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# This is a simple script that takes in an scurve file produced by |
| 4 | +# csvcolumn_to_scurve and produces a png graph of the scurve. |
| 5 | + |
| 6 | +import argparse |
| 7 | +import csv |
| 8 | + |
| 9 | +import matplotlib.pyplot as plt |
| 10 | + |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +FIELDS = ['N/total', 'New/Old'] |
| 14 | + |
| 15 | + |
| 16 | +def get_data(input_file): |
| 17 | + global FIELDS |
| 18 | + for row in csv.DictReader(input_file): |
| 19 | + yield (float(row[FIELDS[0]]), float(row[FIELDS[1]])) |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + p = argparse.ArgumentParser() |
| 24 | + p.add_argument('input_csv_file', type=argparse.FileType('r')) |
| 25 | + p.add_argument('output_file', type=str) |
| 26 | + p.add_argument('-y-axis-num-tick-marks', type=int, |
| 27 | + help='The number of y tick marks to use above/below zero.') |
| 28 | + p.add_argument('-y-axis-min', type=float, |
| 29 | + help='Override the min y axis that we use') |
| 30 | + p.add_argument('-y-axis-max', type=float, |
| 31 | + help='Override the min y axis that we use') |
| 32 | + p.add_argument('-title', type=str, |
| 33 | + help='Title of the graph') |
| 34 | + p.add_argument('-x-axis-title', type=str, |
| 35 | + help='The title to use on the x-axis of the graph') |
| 36 | + p.add_argument('-y-axis-title', type=str, |
| 37 | + help='The title to use on the x-axis of the graph') |
| 38 | + |
| 39 | + args = p.parse_args() |
| 40 | + |
| 41 | + data = np.array(list(get_data(args.input_csv_file))) |
| 42 | + assert np.all(data >= 0) |
| 43 | + |
| 44 | + x = data[:, 0] |
| 45 | + y = data[:, 1] |
| 46 | + |
| 47 | + x_axis_title = args.x_axis_title or FIELDS[0] |
| 48 | + y_axis_title = args.y_axis_title or FIELDS[1] |
| 49 | + title = args.title or "{} vs {}".format(x_axis_title, y_axis_title) |
| 50 | + |
| 51 | + fig, ax = plt.subplots() |
| 52 | + fig.set_size_inches(18.5, 18.5) |
| 53 | + |
| 54 | + fig.suptitle(title, fontsize=20) |
| 55 | + ax.set_xlabel(x_axis_title, fontsize=20) |
| 56 | + ax.set_ylabel(y_axis_title, fontsize=20) |
| 57 | + ax.plot(x, y) |
| 58 | + ax.scatter(x, y) |
| 59 | + |
| 60 | + # To get good bounds, we: |
| 61 | + # |
| 62 | + # 1. Re-center our data at 0 by subtracting 1. This will give us the % |
| 63 | + # difference in between new and old (i.e. (new - old)/old) |
| 64 | + # |
| 65 | + # 2. Then we take the maximum absolute delta from zero and round to a |
| 66 | + # multiple of 5 away from zero. Lets call this value limit. |
| 67 | + # |
| 68 | + # 3. We set [min_y, max_y] = [1.0 - limit, 1.0 + limit] |
| 69 | + recentered_data = y - 1.0 |
| 70 | + max_magnitude = int(np.max(np.abs(recentered_data)) * 100.0) |
| 71 | + y_limit = float(((max_magnitude // 5) + 1) * 5) * 0.01 |
| 72 | + |
| 73 | + ax.set_xlim(0.0, 1.0) |
| 74 | + y_min = args.y_axis_min or 1.0 - y_limit |
| 75 | + y_max = args.y_axis_max or 1.0 + y_limit |
| 76 | + assert(y_min <= y_max) |
| 77 | + ax.set_ylim(y_min, y_max) |
| 78 | + ax.grid(True) |
| 79 | + ax.xaxis.set_ticks(np.arange(0.0, 1.0, 0.05)) |
| 80 | + if args.y_axis_num_tick_marks: |
| 81 | + y_delta = y_max - y_min |
| 82 | + y_tickmark_frequency = y_delta / float(args.y_axis_num_tick_marks) |
| 83 | + ax.yaxis.set_ticks(np.arange(y_min, y_max, y_tickmark_frequency)) |
| 84 | + plt.savefig(args.output_file) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments