<commit_before>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()<commit_msg>save histogram to pdf<commit_after>import numpy
from matplotlib import pyplot
from matplotlib.backends.backend_pdf import PdfPages
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, fig_file):
	bins = numpy.linspace(0.0, 2.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()
	pp = PdfPages(fig_file+".pdf")
	pyplot.savefig(pp, format='pdf')
	pp.close()

def main():

	parser = argparse.ArgumentParser()
	parser.add_argument("-nc", "--nocluster", required=True, type=str, help="path to the result file without relabeling")
	parser.add_argument("-c", "--cluster", required=True, type=str, help="path to the result file with relabeling")
	parser.add_argument("-f", "--file", required=True, type=str, help="name of the pdf figure (without the .pdf extension")
	args = parser.parse_args()

	score1 = parse_result_file(args.nocluster)
	score2 = parse_result_file(args.cluster)

	plot_hist(score1, "no cluster", score2, "cluster", args.file)

if __name__ == "__main__":
	main()
{'fraction_non_alphanumeric': 0.15169412496114393, 'fraction_numerical': 0.020516008703761267, 'mean_word_length': 4.463497453310696, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2791803', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1204, 'n_words': 287}