-
Notifications
You must be signed in to change notification settings - Fork 0
/
building_output.py
214 lines (130 loc) · 5.76 KB
/
building_output.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
import networkx as nx
import json
import collections
from collections import Counter
import itertools
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import pickle
import numpy as np
from sklearn.cluster import KMeans
import seaborn as sns
from load_save import load_json, save_obj_pickle,save_graph,load_obj_pickle
from Part_1 import parsejson_graph
from part_2_A import Part_2_a
from part_2_B import Part_2_b
import part_2_A
import part_2_B
import part_3_A
import part_3_B
import pandas as pd
def Part_1(json):
print('loading data...')
data= load_json(json)
P=parsejson_graph(data) #parse json and building graph
# If you want to save the objects, remove the code comment
#print('Saving dictionary...')
#save_obj_pickle(P.dictionary,'dict_'+json)
Author_Graph=P.graph
#print('Saving Graph...')
#save_graph(Author_Graph,'Graph_'+json)
print('info Graph... ')
print( nx.info(Author_Graph))
return P.dictionary,Author_Graph
def Part_2_A(Dict,Graph):
conf_list={v for k in tqdm(Dict.keys()) for v in Dict[k]['id_conference_int']}
control=False
while control==False:
print("Insert conference id:")
conf_input=int(input())
if conf_input not in conf_list:
print("ERROR\nConference ID not found")
print("Try another time")
print("\n")
else:
control=True
print('building conference subgraph...')
c=Part_2_a(Dict,conf_input,Graph)
sub_graph=c.conference_subgraph
print('subgraph info...')
print( nx.info(sub_graph))
print('check number of nodes...type yes if you want the plot ')
if str(input())=='yes':
print('drawing graph...')
c.draw_subgraph()
else:
pass
print('computing degree_centrality...')
degree_centrality=c.degree()
print('computing closeness...very slow full_data')
closeness=c.closeness()
print('computing betweeness...very slow full_data...')
betweeness=c.betweeness()
print('...end')
data_frame=pd.DataFrame({'closeness':list(closeness.values()),
'betweeness':list(betweeness.values()),
'degree_centrality':list(degree_centrality.values())})
return degree_centrality,closeness,betweeness,sub_graph, data_frame
def Part_2_B(Dict,Graph):
control=False
while control==False:
print("Insert author id:")
author_id=int(input())
if author_id not in Dict.keys():
print("ERROR\n author_id not found")
print("Try another time")
print("\n")
else:
control=True
P= Part_2_b()
print('insert hop_distance d:')
d= int(input())
print('computing Hop Distance...')
Hop_Distance_subgraph=P.hop_distance(Graph,author_id,d)
print('subgraph info...')
print( nx.info(Hop_Distance_subgraph))
print('check number of nodes... plot the graph type yes ')
if str(input())=='yes':
print('drawing graph...')
P.draw_author_subgraph()
return Hop_Distance_subgraph
def Part_3_A(starting_graph):
return part_3_A.weight_dict(starting_graph)
def Part_3_A_solution(def_dict,starting_graph):
control=False
while control==False:
print("Type author id:")
aut_input=int(input())
if aut_input in starting_graph.nodes():
control=True
else:
print("Invalid input:\nTry another time\n")
aris=256176
aux = part_3_A.dijkstra_heap2(def_dict,aris)
if aut_input in aux.keys():
print(aux[aut_input])
else:
print("There is no possible path")
def Part_3_B(D_dict,graph):
subset=[]
controll=False
while controll==False:
sub_set_n=int(input("Insert the number of nodes that you want to insert in the subset:"))
if sub_set_n>21:
print("Cardinality has to be smaller that 21!")
else:
controll=True
for i in range(sub_set_n):
cont=False
while cont==False:
print("Insert element number",i+1)
element=int(input())
if element not in graph.nodes():
print("This node is not in the graph!\n Insert another time the",i+1,"node")
elif element in subset:
print("This element has already been selected")
else:
subset.append(element)
cont=True
return part_3_B.group_number(subset,D_dict,graph)