-
Notifications
You must be signed in to change notification settings - Fork 1
/
phase3_users_graph.py
49 lines (45 loc) · 1.99 KB
/
phase3_users_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
from igraph import *
import pandas as pd
g = Graph(directed = True)
users_df= pd.read_pickle("users_pickles/p3_users_df_with_polar")
g.add_vertex("pal")
pal_index = g.vs.select(name_eq = "pal")[0].index
g.add_vertex("neu")
pal_index = g.vs.select(name_eq = "neu")[0].index
g.add_vertex("isr")
pal_index = g.vs.select(name_eq = "isr")[0].index
def addVertices(df):
edgelist = []
for index,row in df.head(len(df)).iterrows():
if(len(g.vs) == 0):
g.add_vertex(row["username1"])
g.add_vertex(row["username1"])
index = g.vs.select(name_eq = row["username1"])[0].index
g.vs[index]["polar"] = row["p1"]
polar_index = g.vs.select(name_eq = row["p1"])[0].index
edgelist.append((index, polar_index))
if(len(g.vs.select(name_eq = row["username1"])) == 0): #no vertex for same username exists
g.add_vertex(row["username1"])
index = g.vs.select(name_eq = row["username1"])[0].index
g.vs[index]["polar"] = row["p1"]
polar_index = g.vs.select(name_eq = row["p1"])[0].index
edgelist.append((index, polar_index))
if(len(g.vs.select(name_eq = row["username2"])) == 0): #no vertex for same username exists
g.add_vertex(row["username2"])
index = g.vs.select(name_eq = row["username2"])[0].index
g.vs[index]["polar"] = row["p2"]
polar_index = g.vs.select(name_eq = row["p2"])[0].index
edgelist.append((index, polar_index))
g.add_edges(edgelist)
def addEdges(df):
edgelist = []
for index,row in df.head(len(df)).iterrows():
repliedto_index = g.vs.select(name_eq = row["username1"])[0].index
reply_index = g.vs.select(name_eq = row["username2"])[0].index
edgelist.append((reply_index, repliedto_index))
g.add_edges(edgelist)
addVertices(users_df)
addEdges(users_df)
# layout = g.layout("kk")
# plot(g, layout=layout, vertex_size = 20)
g.save("users_graphs/p3_users_graph_with_polar.graphml")