-
Notifications
You must be signed in to change notification settings - Fork 3
/
index_trope_smallviz.py
executable file
·127 lines (112 loc) · 4.44 KB
/
index_trope_smallviz.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 26 2020
@author: jzimmer1, philnguyen
"""
import os
import json
from networkx import algorithms
from networkx import community
from networkx import centrality
import networkx as nx
import itertools
import matplotlib.pyplot as plt
import collections
class IndexGraph():
def __init__(self):
self.G = nx.Graph()
self.masterlist = self.get_json('index-list/index-list.json')
self.indices = [x for x in self.masterlist.keys()]
#truncated list for testing things quickly or on a subset of indices
self.masterlist = {self.indices[i]:self.masterlist[self.indices[i]] for i in range(len(self.masterlist)) if self.indices[i] in ("MediaTropes","NarrativeTropes","TopicalTropes","GenreTropes")}
self.centraltropes = self.get_most_central_tropes_by_all_4_metrics("top_10000_central.json")
self.masterlisttropes = self.get_json('all-tropes-with-links.json')
self.add_trope_nodes()
self.go_thru_indices_sets()
def write_gml(self,G,name):
nx.write_gml(G, name+".gml")
return None
def get_json(self, filename):
with open(filename) as f:
jsonobj = json.load(f)
#print(jsonobj)
return jsonobj
def write_json(self, data, filename):
with open(filename,"w") as f:
json.dump(data,f)
return None
def show_graph(self):
pos = nx.spring_layout(self.G)
nx.draw(self.G, pos)
node_labels = nx.get_node_attributes(self.G,'label')
#print(node_labels)
nx.draw_networkx_labels(self.G, pos, labels=node_labels)
return None
def get_most_central_tropes(self, filename):
centraltropes = self.get_json(filename)
#print(centraltropes)
tropelist = []
for x in centraltropes.values():
for trope in x:
tropelist.append(trope)
#print(len(tropelist))
return set(tropelist)
def get_most_central_tropes_by_all_4_metrics(self, filename):
centraltropes = self.get_json(filename)
#print(centraltropes)
tropelist = []
for x in centraltropes.values():
for trope in x:
tropelist.append(trope)
trope_centrality_counts = collections.Counter(tropelist)
tropelist = [x for x in trope_centrality_counts if trope_centrality_counts[x] == 4]
print(len(tropelist))
return set(tropelist)
def add_sets(self):
for index in self.masterlist: #add all linked tropes as a set
indexlinks = set(self.masterlist[index])
self.G.add_node(index,tropes=indexlinks)
return None
def add_trope_nodes(self):
supercat = "BigFour_centralbyall4_top50_top50"
self.G.add_node(supercat,label=supercat)
#print(len(self.centraltropes))
for index in self.masterlist: #add all linked tropes as nodes
indexlinks = []
for x in self.masterlist[index]:
#if x not in indexlinks:
if x in self.centraltropes and x not in indexlinks:
indexlinks.append(x)
if len(indexlinks) > 50:
break
self.G.add_node(index,label=index)
self.G.add_edge(supercat,index)
for trope in indexlinks:
self.G.add_node(trope,label=trope)
self.G.add_edge(index,trope)
tropetropelinks = self.masterlisttropes[trope]
print(len(tropetropelinks))
tropetropenodes = []
for tropelink in tropetropelinks:
if tropelink in self.centraltropes and tropelink not in tropetropenodes:
tropetropenodes.append(tropelink)
if len(tropetropenodes) > 50:
break
for tr in tropetropenodes:
self.G.add_node(tr,label=tr)
self.G.add_edge(trope, tr)
self.write_gml(self.G, supercat)
return None
def histogram_plot(self, datax, datay):
fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True)
axs[0].hist(datax, bins=100)
axs[1].hist(datay, bins=100)
plt.show()
return None
def go_thru_indices_sets(self):
for idx, node in enumerate(self.G.nodes()):
pass
return None
i = IndexGraph()
i.show_graph()