-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjazz_graph.py
105 lines (84 loc) · 4.01 KB
/
jazz_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
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
from unipath import Path
import json
import networkx as nx
from termcolor import cprint
GRAPH_JSON_FILE = Path(__file__).parent.child('data', 'jazz_musicians_graph.json')
def flatten_edges(master_graph):
for source, targets in graph.adjacency():
for target, rels in targets.items():
for rel_data in rels.values():
yield (source, target, rel_data)
def extract_recorded_graph(master_graph):
graph = nx.Graph()
graph.add_nodes_from(master_graph.nodes())
flatten = flatten_edges(master_graph)
for source, target, rel_data in flatten:
if rel_data['type'] != 'RECORDED':
continue
graph.add_edge(source, target, **rel_data)
return graph
def extract_played_with_graph(master_graph):
graph = nx.Graph()
for n, data in master_graph.nodes(data=True):
if data['type'] == 'MUSICIAN':
graph.add_node(n, **data)
flatten = flatten_edges(master_graph)
for source, target, data in flatten:
if not data['type'] == 'PLAYED_WITH':
continue
if not graph.has_edge(source, target):
graph.add_edge(source, target, weight=0, **data)
graph.edges[(source, target)]['weight'] += 1
return graph
def top_10_recorders(recorded_graph, master_graph):
centralities = nx.degree_centrality(recorded_graph)
is_musician = lambda n: master_graph.node[n]['type'] == 'MUSICIAN'
sorted_centralities = sorted(centralities, key=centralities.get, reverse=True)
sorted_nodes = [n for n in sorted_centralities if is_musician(n)]
return sorted_nodes[:10], centralities
def top_10_prolifc_musicians(played_with_graph, master_graph):
centralities = nx.eigenvector_centrality(played_with_graph, weight='weight')
sorted_centralities = sorted(centralities, key=centralities.get, reverse=True)
return sorted_centralities[:10], centralities
def top_10_albuns(recorded_graph, master_graph):
centralities = nx.eigenvector_centrality(recorded_graph)
is_album = lambda n: master_graph.node[n]['type'] == 'RELEASE'
sorted_centralities = sorted(centralities, key=centralities.get, reverse=True)
sorted_nodes = [n for n in sorted_centralities if is_album(n)]
return sorted_nodes[:10], centralities
if __name__ == '__main__':
import networkx as nx
graph = nx.MultiDiGraph()
with open(GRAPH_JSON_FILE) as fd:
graph_data = json.load(fd)
graph.add_nodes_from(graph_data['nodes'].items())
graph.add_edges_from(graph_data['edges'])
recorded_graph = extract_recorded_graph(graph)
played_with_graph = extract_played_with_graph(graph)
cprint('##### About the Graphs', 'yellow')
cprint('RECORDED Graph', 'red')
cprint("\t {} nodes".format(recorded_graph.number_of_nodes()))
cprint('\t {} edges'.format(recorded_graph.number_of_edges()))
cprint('PLAYED_WITH Graph', 'red')
cprint('\t {} nodes'.format(played_with_graph.number_of_nodes()))
cprint('\t {} edges'.format(played_with_graph.number_of_edges()))
print()
cprint("##### Top 10 Recorders", 'yellow')
top_10, degrees = top_10_recorders(recorded_graph, graph)
for i, node in enumerate(top_10[:10]):
data = graph.node[node]
instruments = '/'.join(data['roles'])
cprint("{}) {} / score: {}".format(i + 1, '{} - {}'.format(data['name'], instruments), degrees[node]), 'white')
print()
cprint("##### Top 10 Albuns", 'yellow')
top_10, eigenvector = top_10_albuns(recorded_graph, graph)
for i, node in enumerate(top_10[:10]):
data = graph.node[node]
cprint("{}) {} / score: {}".format(i + 1, '{} - {}'.format(data['title'], data['year']), eigenvector[node]), 'white')
top_10, side = top_10_prolifc_musicians(played_with_graph, graph)
print()
cprint("##### Top 10 Prolific Musicians", 'yellow')
for i, node in enumerate(top_10[:10]):
data = graph.node[node]
instruments = '/'.join(data['roles'])
cprint("{}) {} / score: {}".format(i + 1, '{} - {}'.format(data['name'], instruments), side[node]), 'white')