-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_cluster.py
217 lines (170 loc) · 6.36 KB
/
get_cluster.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# author : Johann-Mattis List
# email : mattis.list@uni-marburg.de
# created : 2014-02-07 14:37
# modified : 2014-02-07 14:37
"""
Load the networkx-graph and retrieve a specific cluster.
"""
__author__="Johann-Mattis List"
__date__="2014-02-07"
from clics_lib.csv import *
import networkx as nx
import igraph as ig
from sys import argv
gml = ig.read('output/clics_c.gml')
comms = dict([(k[0],k[3]) for k in csv2list('output/nodes2communities.csv')])
g = nx.Graph()
for edge in gml.es:
source = gml.vs[edge.source]['concept']
target = gml.vs[edge.target]['concept']
weight = edge['weight']
if source not in g:
tmp = gml.vs[edge.source].attributes()
tmp['out_edge'] = []
del tmp['id']
g.add_node(source,**tmp)
if target not in g:
tmp = gml.vs[edge.target].attributes()
del tmp['id']
tmp['out_edge'] = []
g.add_node(target,**tmp)
g.add_edge(source,target,**edge.attributes())
if argv[1] == 'ALL':
nodes = [n for n in g.nodes() if n.strip()]
else:
nodes = [argv[1]]
dset = {}
os.system('git rm website/clics.de/data/cuts/*.json')
os.system('rm website/clics.de/data/cuts/*.json')
nodeD = dict()
blacklist = []
for this_node in nodes:
if this_node not in blacklist:
direct_neighbor = []
subg = nx.Graph()
queue = [(this_node, g.edge[this_node],0)]
weight = 4
weightB = 4
while queue:
source,neighbors,generation = queue.pop(0)
for n,d in neighbors.items():
if n.strip():
if d['families'] > weight:
subg.add_node(n,**g.node[n])
subg.add_edge(source,n,**d)
queue += [(n,g.edge[n],generation+1)]
elif d['families'] > weightB:
subg.add_node(n,**g.node[n])
subg.add_edge(source,n,**d)
# check for common nodes
#if generation == 0 and d['families'] > weightB:
# if n in dset:
# direct_neighbor = n
if generation > 2:
break
# repeat if no good results are found
if len(subg) < 5:
weight = 3
weightB = 3
subg = nx.Graph()
queue = [(this_node, g.edge[this_node],0)]
while queue:
source,neighbors,generation = queue.pop(0)
for n,d in neighbors.items():
if n.strip():
if d['families'] > weight:
subg.add_node(n,**g.node[n])
subg.add_edge(source,n,**d)
queue += [(n,g.edge[n],generation+1)]
elif d['families'] > weightB:
subg.add_node(n,**g.node[n])
subg.add_edge(source,n,**d)
# check for common nodes
#if generation == 0 and d['families'] > weightB:
# if n in dset:
# direct_neighbor = n
if generation > 2:
break
for n,d in subg.nodes(data=True):
links = g.edge[n]
for l in links:
if l not in subg:
if g.edge[n][l]['families'] > weightB:
if 'out_edge' not in d:
d['out_edge'] = []
d['out_edge'] += [
(
comms[g.node[l]['key']],
l,
g.edge[n][l]['families'],
g.edge[n][l]['weight'],
'x'
)
]
d['out_edge'] = sorted(set(d['out_edge']), key=lambda
x:x[2])
from clics_lib.gml2json import *
if '/' in this_node:
nodename = this_node.replace('/','_')
else:
nodename = this_node
nodeD[this_node] = [n for n in subg.nodes() if n.strip()]
dset[this_node] = [g.node[this_node]['key'], len(nodeD[this_node]), 'network_'+nodename+'_'+str(len(nodeD[this_node]))]
labels = {}
for n in nodeD:
subg = g.subgraph(nodeD[n]+[n])
label = sorted(subg.degree().items(),key=lambda x:x[1],reverse=True)[0][0]
labels[n] = label
for node,data in subg.nodes(data=True):
dels = []
data['out_edge'] = sorted(set([tuple(t) for t in data['out_edge']]), key=lambda x:x[2],
reverse=True)
for i,(a,b,c,d,e) in enumerate(data['out_edge']):
if b in nodeD[n]+[n]:
#pass
dels += [i]
else:
clabel = a.split('_')[-1]
print(clabel,a)
try:
data['out_edge'][i] = [a,b,c,d,clabel]
print(n)
except:
dels += [i]
for i in dels[::-1]:
del data['out_edge'][i]
#for node,data in subg.nodes(data=True):
# for i,(a,b,c,d,e) in enumerate(data['out_edge']):
# data[i] = [a,b,c,d,e.split('_')[-1]]
if '/' in n:
nodename = n.replace('/','_')
else:
nodename = n
if len(subg) > 1:
graph2json(subg,'website/clics.de/data/cuts/network_'+nodename+'_'+str(len(subg.nodes())))
print(len(subg))
with open('output/nodes2cuts.csv', 'w') as f:
for key in dset:
line = [key] + [k for k in dset[key]]
f.write('\t'.join([str(x) for x in line])+'\n')
#os.system('git add cuts/*.json')
maxvals = []
import sqlite3
conn = sqlite3.connect('website/clics.de/data/clips.sqlite3')
cursor = conn.cursor()
try:
cursor.execute('drop table cuts;')
except:
pass
cursor.execute('create table cuts(id,gloss,path,size,label);')
for a in dset:
b,c,d = dset[a]
if a.strip():
cursor.execute(
'insert into cuts values(?,?,?,?,?);',
(b,a,d,c,labels[a])
)
maxvals += [c]
conn.commit()
print(max(maxvals))
os.system('git add website/clics.de/data/cuts/*.json')