-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
executable file
·59 lines (37 loc) · 1.79 KB
/
metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 14:32:29 2019
@author: georgiabaltsou
"""
import os
import csv
import numpy as np
def metrics(myFile, GTC, trueComm):
with open('./communities/' + myFile + '_communities.csv', 'r') as in_file:
reader = csv.reader(in_file, delimiter=';')
#skip 1st line as its the header line
next(reader, None)
with open('./communities/metrics_'+str(myFile) + '.csv', 'a') as out_file:
writer = csv.writer(out_file, delimiter=';')
if os.stat('./communities/metrics_'+str(myFile) + '.csv').st_size == 0:
writer.writerow(["ALGORITHM","Seed node","Method", "PRECISION", "RECALL", "F1"])
for row in reader:
alg = str(row[0])
seed = str(row[1])
weight = str(row[2])
inComm = 0
obtainedComm = np.unique(row[3:])
for i in obtainedComm:
resultComm = len(obtainedComm)
if i in GTC:
inComm += 1
precision = inComm/resultComm
recall = inComm/trueComm
if (precision+recall)==0:
F1=0
else:
F1 = 2 * (precision * recall) / (precision + recall)
# JI = len(np.intersect1d(GTC, obtainedComm))/len(np.union1d(GTC, obtainedComm))
# writer.writerow([alg, node1, node2, seed, weight, precision, recall, F1, JI])
writer.writerow([alg, seed, weight, precision, recall, F1])