-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisegnaGrafoGT.py
More file actions
140 lines (121 loc) · 4.11 KB
/
Copy pathDisegnaGrafoGT.py
File metadata and controls
140 lines (121 loc) · 4.11 KB
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
#!python2
from graph_tool.all import *
from random import randint
from numpy import sqrt, log
def disegnaGrafo(pfGT, pfOut, pfClassi, isgc=False):
# seed_rng(135)
seed_rng(246)
g = Graph(directed=False)
v_id = g.new_vertex_property('string')
v_nome = g.new_vertex_property('string')
v_label = g.new_vertex_property('string')
# e_weight = g.new_edge_property('int')
e_weight = g.new_edge_property('float')
e_text = g.new_edge_property('string')
e_color = g.new_edge_property('int')
# carico il grafo
with open(pfGT, 'rb') as fPaj:
line = fPaj.readline().rstrip()
N = int(line.split(' ')[1])
# print(N)
for i in range(N):
line = fPaj.readline().rstrip()
# print(line)
num = int(line.split('\t')[0]) # graphtool parte da 0
nome = line.split('\t')[1]
idaut = line.split('\t')[2]
# print(num, nome)
v = g.add_vertex()
v_nome[v] = nome
v_id[v] = idaut
lineadiintestazioneedge = fPaj.readline()
for line in fPaj:
pz = line.rstrip().split()
src = int(pz[0])
dst = int(pz[1])
wei = int(pz[2])
e = g.add_edge(g.vertex(src), g.vertex(dst) )
e_weight[e] = wei
if wei > 5:
e_text[e] = wei
# partiziono il grafo secondo blockmodel
state = minimize_blockmodel_dl(g)
com = state.get_blocks()
print('Numero di comunita minimize_blockmodel_dl {}'.format(max(com.a)))
with open(pfClassi, 'wb') as fClassi:
for v in g.vertices():
fClassi.write('{}\t{}\t{}\r\n'.format(v_id[v], v_nome[v], com[v]) )
# disegno il grafo
deg = g.degree_property_map('total', e_weight)
# deg = g.degree_property_map("in")
# deg.a = 4 * (sqrt(deg.a) * 0.5 + 0.4)
deg.a = 2 * (sqrt(deg.a) * 0.3 + 0.4)
# deg.a = 1 * (sqrt(sqrt(deg.a)) * 0.2 + 0.4)
# deg.a = 1 * (log(deg.a) * 0.2 + 0.4)
degoriginale = g.degree_property_map('total', e_weight)
e_norm = g.new_edge_property('float')
e_norm.a = 2 * (sqrt(e_weight.a) * 0.2 + 0.1)
# estetica grafo
for e in g.edges():
if deg[e.source()] > deg[e.target()]:
e_color[e] = com[e.source()]
else:
e_color[e] = com[e.target()]
for v in g.vertices():
if degoriginale[v] > -20:
v_label[v] = v_nome[v].title()
pos = sfdp_layout(g, eweight=e_weight,
# p=8,
K = 10000,
C = 1.6,
)
graph_draw(g, pos=pos,
vertex_size=deg,
vertex_text=v_label,
vertex_text_position = 1,
vertex_font_size=5,
# vertex_color=com,
vertex_fill_color=com,
edge_pen_width=e_norm,
edge_text=e_text,
edge_font_size=5,
edge_color=e_color,
output_size=(1500, 1500), output=pfOut.format(''), # tutto il grafo
)
if not isgc: # se NON e' una componente centrale, la disegno
lc = label_largest_component(g)
g.set_vertex_filter(lc)
pos = sfdp_layout(g, eweight=e_weight,
# p=8,
K = 10000,
C = 1.6,
)
graph_draw(g, pos=pos,
vertex_size=deg,
vertex_text=v_label,
vertex_text_position = 1,
vertex_font_size=5,
# vertex_color=com,
vertex_fill_color=com,
edge_pen_width=e_norm,
edge_text=e_text,
edge_font_size=5,
edge_color=e_color,
output_size=(1500, 1500), output=pfOut.format('_gc'), # solo componente centrale
)
# nodes properties:
# font_size, size, fill_color, color
# edges
# color, pen_width, text, font_size
# for v in list(g.vertices()):#[0:5]:
# print('comunita {}'.format(com[v]))
# for e in list(g.edges())[0:50]:
# print('peso {}'.format(e_weight[e]) )
if __name__ == '__main__':
# print('disegnaGrafo eseguito da solo')
# pfEdge = 'EdgeUnificatiGephi_padovani_distanza_DEI.tsv'
# pfAutori = 'AutoriUnificatiGephi_padovani_distanza_DEI.tsv'
pfGT = 'AutoriEdgeCollab_padovani_DEI.tsv'
pfOut = 'padovani.pdf'
pfClassi = 'AutoriComunitaGT.tsv'
disegnaGrafo(pfGT, pfOut, pfClassi)