forked from hekimgil/imdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model1a.py
159 lines (144 loc) · 5.9 KB
/
model1a.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
# -*- coding: utf-8 -*-
"""
Python code to read .tsv İMDB data and store it in a sqlite database
This model assumes actors/actresses as nodes and common movies as weighted
edges. Different 20-year periods are used to set up separate networks and basic
data such as number of nodes, number of edges, and distribution of sizes
of connected components are examined to get a general idea on various 20-year
windows.
THIS VERSION PROCESSES THE WHOLE MOVIE DATABASE INSTEAD OF 20-YEAR WINDOWS.
@author: Hakan Hekimgil
"""
import numpy as np
import pandas as pd
import sqlite3
import networkx as nx
# initialize graph
G = nx.Graph()
# connect to database
conn = sqlite3.connect("data/imdblite.db")
db = conn.cursor()
# start reading and moving to db
# transfer titles
print("Reading titles...")
numtitles = 0
titleset = set()
peopleset = set()
# some helper functions
def titleinfo(n):
conntemp = sqlite3.connect("data/imdblite.db")
dbtemp = conntemp.cursor()
dbtemp.execute("SELECT * FROM titles WHERE tconst = ?;", (int(n),))
infotemp = dbtemp.fetchone()
conntemp.close()
return infotemp
def findtitle(txt):
conntemp = sqlite3.connect("data/imdblite.db")
dbtemp = conntemp.cursor()
dbtemp.execute("SELECT * FROM titles WHERE primaryTitle = ?;", (txt,))
infotemp = dbtemp.fetchone()
conntemp.close()
return infotemp
def peopleinfo(n):
conntemp = sqlite3.connect("data/imdblite.db")
dbtemp = conntemp.cursor()
dbtemp.execute("SELECT * FROM people WHERE nconst = ?;", (int(n),))
infotemp = dbtemp.fetchone()
conntemp.close()
return infotemp
def findperson(txt):
conntemp = sqlite3.connect("data/imdblite.db")
dbtemp = conntemp.cursor()
dbtemp.execute("SELECT * FROM people WHERE primaryName = ?;", (txt,))
infotemp = dbtemp.fetchone()
conntemp.close()
return infotemp
infodf = pd.DataFrame(columns=["period", "# vertices", "# edges", "density", "max degree", "largest CC"])
ccdist = []
top50df = pd.DataFrame(columns=["degree", "name", "birth", "death"])
# read and add actors/actresses
db.execute("SELECT id FROM categories WHERE category = ? OR category = ?;", ("actor","actress"))
catids = db.fetchall()
assert len(catids) == 2
for year1 in range(1930,2010,100):
year2 = year1 + 99
G.clear()
# read people and titles for a 20-year window
db.execute(
"SELECT DISTINCT t.tconst, nconst " +
"FROM (SELECT nconst, tconst FROM principals WHERE catid = ? OR catid = ?) AS p " +
"INNER JOIN (SELECT tconst FROM titles WHERE startYear >= ? AND startYear <= ?) AS t " +
"ON t.tconst = p.tconst " +
"ORDER BY t.tconst;", (catids[0][0], catids[1][0], year1, year2))
edgeinfo = pd.DataFrame(db.fetchall(), columns=["tconst", "nconst"])
peopleset = set(edgeinfo.nconst.unique())
G.add_nodes_from(edgeinfo.nconst.unique())
prevedge = None
tempset = set()
for row in edgeinfo.itertuples(index=False):
titleid = row.tconst
actid = row.nconst
#print(titleid, actid)
if titleid != prevedge:
prevedge = titleid
tempset = set([actid])
else:
for node in tempset:
if G.has_edge(node, actid):
G[node][actid]["weight"] += 1
else:
G.add_edge(node, actid, weight=1)
tempset.add(actid)
#print("Number of nodes: ", G.number_of_nodes())
#print("Number of edges: ", G.number_of_edges())
print(nx.info(G))
if G.number_of_nodes() <= 20:
print("Nodes: ", G.nodes())
for node in G.nodes():
print(G.node[node])
if G.number_of_edges() <= 20:
print("Edges: ", G.edges())
print("Network density:", nx.density(G))
maxd = max([d for n,d in G.degree()])
d50th = sorted([d for n,d in G.degree()])[-50]
print("Maximum degree:", maxd)
top50dact = sorted([(d,n) for n,d in G.degree() if d >= d50th], reverse=True)
top50dactfull = [(d,peopleinfo(n)) for d,n in top50dact]
ind=0
for t in top50dactfull:
ind += 1
death = np.NAN
if t[1][3] != "NULL":
death = int(t[1][3])
top50dfrow = pd.Series({"degree":int(t[0]),
"name":t[1][1],
"birth":int(t[1][2]),
"death":death}, name=ind)
top50df = top50df.append(top50dfrow)
#print("Actors/actresses with maximum degrees:", [peopleinfo(m) for m in top20dact])
print("Maximum degree:", max([d for n,d in G.degree()]))
connecteds = list(nx.connected_components(G))
maxconnectedsize = max([len(c) for c in connecteds])
print("Size of largest connected component:", maxconnectedsize)
infodfrow = pd.Series({"period":str(year1)+"-"+str(year2),
"# vertices":G.number_of_nodes(),
"# edges":G.number_of_edges(),
"density":nx.density(G),
"max degree":max([d for n,d in G.degree()]),
"largest CC":maxconnectedsize},
name=str(year1)+"-"+str(year2))
infodf = infodf.append(infodfrow)
temp = set([len(c) for c in connecteds])
temp2 = [len(c) for c in connecteds]
connectedsizes = {k:temp2.count(k) for k in temp}
ccdist.append((str(year1)+"-"+str(year2),connectedsizes))
print("Number of connected components according to size:", connectedsizes)
#G.clear()
conn.close()
print("\n\nActors/actresses as vertices and movies as weighted (by number) edges:")
print("----------------------------------------------------------------------")
print(infodf)
print("\n\nActors/actresses with maximum degrees:")
print("--------------------------------------")
print(top50df)
#print(ccdist)