Create carb/pr_plot.py
Browse files
evaluation_data/carb/pr_plot.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Usage:
|
| 2 |
+
pr_plot --in=DIR_NAME --out=OUTPUT_FILENAME
|
| 3 |
+
|
| 4 |
+
Options:
|
| 5 |
+
--in=DIR_NAME Folder in which to search for *.dat files, all of which should be in a P/R column format (outputs from benchmark.py)
|
| 6 |
+
--out=OUTPUT_FILENAME Output filename, filetype will determine the format. Possible formats: pdf, pgf, png
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
import ntpath
|
| 13 |
+
import numpy as np
|
| 14 |
+
from glob import glob
|
| 15 |
+
from docopt import docopt
|
| 16 |
+
import matplotlib.pyplot as plt
|
| 17 |
+
import logging
|
| 18 |
+
import ipdb
|
| 19 |
+
logging.basicConfig(level = logging.INFO)
|
| 20 |
+
|
| 21 |
+
plt.rcParams.update({'font.size': 14})
|
| 22 |
+
|
| 23 |
+
def trend_name(path):
|
| 24 |
+
''' return a system trend name from dat file path '''
|
| 25 |
+
head, tail = ntpath.split(path)
|
| 26 |
+
ret = tail or ntpath.basename(head)
|
| 27 |
+
return ret.split('.')[0]
|
| 28 |
+
|
| 29 |
+
def get_pr(path):
|
| 30 |
+
''' get PR curve from file '''
|
| 31 |
+
with open(path) as fin:
|
| 32 |
+
# remove header line
|
| 33 |
+
fin.readline()
|
| 34 |
+
prc = list(zip(*[[float(x) for x in line.strip().split('\t')] for line in fin]))
|
| 35 |
+
p = prc[0]
|
| 36 |
+
r = prc[1]
|
| 37 |
+
return p, r
|
| 38 |
+
|
| 39 |
+
if __name__ == '__main__':
|
| 40 |
+
args = docopt(__doc__)
|
| 41 |
+
input_folder = args['--in']
|
| 42 |
+
output_file = args['--out']
|
| 43 |
+
|
| 44 |
+
# plot graphs for all *.dat files in input path
|
| 45 |
+
files = glob(os.path.join(input_folder, '*.dat'))
|
| 46 |
+
for _file in files:
|
| 47 |
+
p, r = get_pr(_file)
|
| 48 |
+
name = trend_name(_file)
|
| 49 |
+
plt.plot(r, p, label = name)
|
| 50 |
+
|
| 51 |
+
# Set figure properties and save
|
| 52 |
+
logging.info("Plotting P/R graph to {}".format(output_file))
|
| 53 |
+
plt.ylim([0.0, 1.05])
|
| 54 |
+
plt.xlim([0.0, 0.8])
|
| 55 |
+
plt.xlabel('Recall')
|
| 56 |
+
plt.ylabel('Precision')
|
| 57 |
+
plt.legend(loc="lower right")
|
| 58 |
+
plt.savefig(output_file)
|