"""
This script is to take the intersection of :
1. the test instances (400 instances) of API usage which we will annotate
2. the identified misuse instances of our tool

Mainly useful for finding an estimate of the progress of our tool
"""


test_cases = []
test_cases_misuses = []


is_identified_by_specific_method = False
with open('all_probability_for_othercombing_instances.txt') as infile:
	for line in infile:
		if '__' in line :
			is_identified_by_specific_method = True
			break
		else:
			is_identified_by_specific_method = False
			break


with open('annotation_prep/to_annotate.csv') as infile:
	infile.readline()
	for line in infile:
		API = line.split(',')[0]
		if not is_identified_by_specific_method:
			API = API.split("__")[0]

		method = line.split(',')[1]
		github_url = line.split(',')[2]
		
		path = github_url.split('/')[4] + '/' + '/'.join(github_url.split('/')[7:])
		path = path.split('#')[0]

		is_misuse = line.split(',')[3].strip() == 'M'

		
		location = path + ' - ' + method + "," + API
		# print(location, is_misuse)

		test_cases.append(location)
		if is_misuse:
			test_cases_misuses.append(location)

identified_misuses = set()
with open('all_probability_for_othercombing_instances.txt') as infile:
	for line in infile:
		location_and_api = line[:line.rindex(',')]
		identified_misuses.add(location_and_api.split('/Users/author/repos_for_misuses/')[1])


print('==== from output_and_test_intersection =====')
print('total guesses by output', len(set(test_cases) & identified_misuses))
print('total correct misuse guesses by output', len(set(test_cases_misuses) & identified_misuses))
print('total misuses in test', len(test_cases_misuses))

prec = len(set(test_cases_misuses) & identified_misuses) / len(set(test_cases) & identified_misuses)
print('precision = ', prec)
recall = len(set(test_cases_misuses) & identified_misuses) / len(test_cases_misuses)
print('recall = ', recall)

print('F1 = ', (2 * prec * recall / (prec + recall)))

# for item in set(test_cases) & identified_misuses:
	# print(item)

# misuses that are missed


from IPython import embed
embed()
