import numpy
from matplotlib import pyplot
import os, sys
import argparse

"""Read the program similarity result files and plot histograms"""

def parse_result_file(result_file):
	"""
	file format: 
	path_to_dotA: 
	path_to_similar_dot1 , score
	...
	path_to_similar_dot5 , score

	path_to_dotB:
	...
	"""
	avg_score_vector = []

	count = 0
	score = 0.0
	with open(result_file, "r") as fi:
		for line in fi:
			line = line.rstrip('\n')
			linarr = line.split(" , ")
			if linarr[0][-3:]=="dot":
				count += 1
				score += float(linarr[1])
				if count==5:
					avg_score_vector.append(score/count)
					count = 0
					score = 0.0
	return avg_score_vector

def plot_hist(x, xlabel, y, ylabel):
	bins = numpy.linspace(0.0, 1.0, 100)
	#pyplot.hist(x, bins, alpha=0.5, label=xlabel)
	#pyplot.hist(y, bins, alpha=0.5, label=ylabel)
	data = numpy.vstack([x, y]).T
	pyplot.hist(data, bins, alpha=0.7, label=[xlabel, ylabel])
	pyplot.legend(loc="upper right")
	pyplot.show()

def main():

	parser = argparse.ArgumentParser()
	parser.add_argument("-r1", "--result1", required=True, type=str, help="path to the first result file")
	parser.add_argument("-r2", "--result2", required=True, type=str, help="path to the second result file")
	args = parser.parse_args()

	score1 = parse_result_file(args.result1)
	score2 = parse_result_file(args.result2)

	plot_hist(score1, "no_cluster", score2, "cluster")

if __name__ == "__main__":
	main()
@@ -36,16 +36,69 @@
  pyplot%0A
+from matplotlib.backends.backend_pdf import PdfPages%0A
 import o
@@ -737,16 +737,26 @@
 , ylabel
+, fig_file
 ):%0A%09bins
@@ -778,17 +778,17 @@
 ce(0.0, 
-1
+2
 .0, 100)
@@ -1010,16 +1010,17 @@
 ight%22)%0A%09
+#
 pyplot.s
@@ -1024,16 +1024,94 @@
 t.show()
+%0A%09pp = PdfPages(fig_file+%22.pdf%22)%0A%09pyplot.savefig(pp, format='pdf')%0A%09pp.close()
 %0A%0Adef ma
@@ -1180,23 +1180,25 @@
 t(%22-
-r1
+nc
 %22, %22--
-result1
+nocluster
 %22, r
@@ -1243,14 +1243,8 @@
 the 
-first 
 resu
@@ -1246,24 +1246,43 @@
  result file
+ without relabeling
 %22)%0A%09parser.a
@@ -1299,23 +1299,22 @@
 t(%22-
-r2
+c
 %22, %22--
-result2
+cluster
 %22, r
@@ -1359,15 +1359,8 @@
 the 
-second 
 resu
@@ -1366,16 +1366,153 @@
 ult file
+ with relabeling%22)%0A%09parser.add_argument(%22-f%22, %22--file%22, required=True, type=str, help=%22name of the pdf figure (without the .pdf extension
 %22)%0A%09args
@@ -1572,15 +1572,17 @@
 rgs.
-result1
+nocluster
 )%0A%09s
@@ -1616,15 +1616,15 @@
 rgs.
-result2
+cluster
 )%0A%0A%09
@@ -1644,17 +1644,17 @@
 re1, %22no
-_
+ 
 cluster%22
@@ -1672,16 +1672,27 @@
 cluster%22
+, args.file
 )%0A%0Aif __
@@ -1716,12 +1716,13 @@
 __%22:%0A%09main()
+%0A

save histogram to pdf