-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·139 lines (101 loc) · 4.7 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 4 13:07:16 2020
@author: georgiabaltsou
"""
import networkx as nx
import csv
import sys
import copy
from metrics import metrics
from collections import defaultdict
from fileWeightAdjacement import FilesAdjAll
from ReWeighting import reWeighting
from tceForAdjlist import tce
from lteForAdjlist import lte
from newLCDForAdjlist import newLCD
from simRank import simRank
from loopEdge import addLoopEdge
from CNR import cnr
from WERWKpath import KpathAlg
from propinquity import propinquityD
def call_method(method, seedSetFile, file, G, newGraph, l, d, a, b, hops, rewire):
if method == 'plain':
lte(seedsetFile, file, 1, copy.deepcopy(G), copy.deepcopy(newGraph), method,l)
lte(seedsetFile, file, 3, copy.deepcopy(G), copy.deepcopy(newGraph), method,l)
newLCD(seedsetFile, file, copy.deepcopy(G), copy.deepcopy(newGraph), method,l)
tce(copy.deepcopy(G), seedsetFile, file, copy.deepcopy(newGraph), myFile, copy.deepcopy(newGraph), method,l)
else:
if method == 'propinquity':
tmpGraph, tmpDict = propinquityD(seeds, copy.deepcopy(G), copy.deepcopy(newGraph), copy.deepcopy(newGraph), d, a, b)
elif method == 'k-path':
tmpGraph, tmpDict = KpathAlg(seeds, copy.deepcopy(G), copy.deepcopy(newGraph))
elif method == 'CNR':
tmpGraph, tmpDict = cnr(seeds, copy.deepcopy(G), copy.deepcopy(newGraph))
elif method == 'SimRank':
tmpGraph, tmpDict = simRank(seeds, copy.deepcopy(G), copy.deepcopy(newGraph), hops, rewire)
elif method == 'MultiplyWeight':
tmpGraph, tmpDict = FilesAdjAll(seeds, copy.deepcopy(G), copy.deepcopy(newGraph))
elif method == 'Triangles':
tmpGraph, tmpDict = reWeighting(seeds, copy.deepcopy(G), copy.deepcopy(newGraph))
elif method == 'Loop edge':
tmpGraph, tmpDict = addLoopEdge(seeds, copy.deepcopy(G), copy.deepcopy(newGraph))
lte(seedsetFile, file, 1, copy.deepcopy(tmpGraph), copy.deepcopy(tmpDict), method,l)
tce(copy.deepcopy(tmpGraph), seedsetFile, file, copy.deepcopy(tmpDict), myFile, copy.deepcopy(tmpDict), method,l)
newLCD(seedsetFile, file, copy.deepcopy(tmpGraph), copy.deepcopy(tmpDict), method,l)
del tmpGraph
del tmpDict
print("{} method completed.".format(method))
print("------------------------------")
dataset = sys.argv[1]
arr = dataset.split('/')
myFile = dataset
#file is the input file with the LFR parameters in its name, in string format(without .csv)
file = arr[len(arr) - 1][:-4]
#community file
communityFile = sys.argv[2]
#seeds file
seedsetFile = sys.argv[3]
#seeds = read seed nodes from seedFile
seedFile = open(seedsetFile, 'r')
seeds = seedFile.readline().rstrip('\n').split(" ")
#keep as seed the 1st seed of seedFile
seed = seeds[0]
#Creation of the graph with the input file
G = nx.read_weighted_edgelist(myFile, create_using=nx.Graph(), delimiter=";", encoding='utf-8-sig')
Graph = defaultdict(dict)
with open(myFile, 'r') as read_file:
reader = csv.reader(read_file, delimiter=';')
for row in reader:
Graph[row[0]][row[1]] = row[2]
Graph[row[1]][row[0]] = row[2]
newGraph = {k: {kk: float(vv) for kk, vv in v.items()}
for k, v in Graph.items()}
G = nx.Graph(newGraph)
for source, target in G.edges():
G[source][target]['weight'] = newGraph[source][target]
print("------------------------------")
d = 2 #distance in propinquity
a = 30 #threshold for popinquity
b = 400 #threshold for popinquity
rewire = False #if True SimRank will rewire the G. Otherwise it will reweight it.
hops = 2 #hops for rewiring distance for SimRank
l = 102 #desired community size
if rewire: methods = ['SimRank']
else: methods = ['plain', 'propinquity', 'k-path', 'CNR', 'SimRank', 'MultiplyWeight', 'Triangles', 'Loop edge' ]
for method in methods:
call_method(method, seedsetFile, file, G, newGraph, l, d, a, b, hops, rewire)
#Give ground truth community details for metrics computation
#seedstr = seed+ ' ' # without spaces eg if seed = 10 if is sees 101 firstly it will stop and take as community the one that 101 belongs to.
seedstr = ' ' +seed+ ' '
## !!!!!! if seed is the first item of a line it won't be read properly. SHOULD ADD SPACE IN FRONT OF EACH LINE OF COMMUNITYFILE!
with open(communityFile, 'r') as comm:
for line in comm:
if seedstr in line:
GTC = [n for n in line.strip().split(' ')]
trueComm = len(GTC)
#create seperated metrics file for each algorithm in the communities dir
metrics(file, GTC, trueComm)
print("Computation of metrics completed.")
print("------------------------------")