-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
64 lines (46 loc) · 1.45 KB
/
graph.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
from graph_tool.all import *
import gc
def create_graph(network, data, with_properties=False):
print("Creating graph...")
g = Graph(directed=False)
if(with_properties):
RATING = 'Rating'
GROUP = 'Group'
CATEGORIES = 'Categories'
#Adding properties
vprop_rating = g.new_vertex_property("int")
g.vp.rating = vprop_rating
vprop_categories = g.new_vertex_property("int")
g.vp.categories = vprop_categories
vprop_group = g.new_vertex_property("object")
g.vp.group = vprop_group
g.add_vertex(548552)
for line in network:
try:
from_node = line[0]
to_node = line[1]
v1 = g.vertex(from_node)
v2 = g.vertex(to_node)
if(with_properties):
#Set properties
g.vp.rating[v1] = data[from_node][RATING]
g.vp.group[v1] = data[from_node][GROUP]
g.vp.categories[v1] = data[from_node][CATEGORIES]
g.vp.rating[v2] = data[to_node][RATING]
g.vp.group[v2] = data[to_node][GROUP]
g.vp.categories[v2] = data[to_node][CATEGORIES]
e = g.add_edge(v1,v2)
except:
print("Couldn't create vertices ", from_node, to_node)
del data
del network
gc.collect()
print("Graph successfully created.")
print(" It contains %d vertices and %d edges." % (g.num_vertices(), g.num_edges()))
return g
def save_graph(g):
g.save('resources/amazon-ungraph.gml')
print("Graph saved in 'resources' folder in file 'amazon-ungraph.gml'.")
def load_graph_from_file(filename):
print("Loading graph from file..")
return load_graph(filename)