/
graph_bouncer.py
executable file
·217 lines (186 loc) · 5.95 KB
/
graph_bouncer.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
######################################################################################################
# Project: The Deep Purple Network. #
# (CC) 2020 Made by Gregorio Tedde just for analysis purpose. #
# Work License: Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License #
# Libraries licenses in environment.yml #
# Git Repo: https://github.com/greggtdd/DeepPurpleNetwork #
# PyVis docs used for this module: https://pyvis.readthedocs.io/en/latest/ #
# Info: greggtedde@gmail.com #
######################################################################################################
import pandas as pd
from pyvis.network import Network
pd.set_option('display.width', 1000)
pd.set_option('display.max_rows', 2000)
pd.set_option('display.max_columns', 500)
class DataFramer():
"""
Python class for filtering the
original union edges dataframe.
Parameters
----------
self.dir: string
Path of the initial dataframe
"""
def __init__(self, path):
self.path = path
def upload_df(self):
"""
Reads the dataframe located
at the specified directory.
Returns
-------
self.data_
"""
self.data_ = pd.read_csv(self.path)
def density_sources(self):
"""
Adds a new column based on
each source density inside
the network.
Returns
-------
self.data_.Density
"""
self.data_['Density'] = \
self.data_.groupby('Source')['Source'].\
transform('count')
def artist_research(self):
"""
Used for artists research.
It creates a subset based on
the original dataframe sources.
Returns
-------
self.sour_
"""
self.sour_ = []
for source in self.data_['Source']:
if source not in self.sour_:
self.sour_.append(source)
def sub_framer(self, keyw):
"""
Creates the final subset from which
the graph is built and defines the
input lists for sources, targets
and edges weights.
Parameters
----------
keyw: string
Name of artist, band or album.
Returns
-------
self
"""
if keyw in self.sour_:
sub_df = self.data_[(self.data_['Source'] == keyw)]
sub_univ = []
for univ in sub_df['Universe']:
if univ not in sub_univ:
sub_univ.append(univ)
sub_df = self.data_[self.data_.Universe.isin(sub_univ)]
self.sources_ = sub_df['Source']
self.targets_ = sub_df['Target']
self.weights_ = sub_df['Density']
return self
@staticmethod
def get_edge_data(sources, targets, weights):
"""
Creates a collection of tuples
for linking sources and targerts
inside the graph.
Parameters
----------
sources: pandas.core.series.Series
Sources data.
targets: pandas.core.series.Series
Targets data.
weights: pandas.core.series.Series
Weights data.
Returns
-------
zip
"""
edge_data = zip(sources, targets, weights)
return edge_data
class Networker:
"""
Python class for creating
a network graph from the
original dataframe.
Parameters
----------
data: pandas.core.frame.DataFrame
Original dataframe.
"""
def __init__(self, data):
self.edges = data
def init_graph(self):
"""
Initializes a new graph and
sets its basic parameters.
Returns
-------
pyvis.network.Network
"""
self.nw_ = Network(height="1080px",
width="100%",
bgcolor="#272651",
font_color="white",
notebook=True)
self.nw_.barnes_hut()
self.nw_.set_edge_smooth(smooth_type="horizontal")
self.nw_.toggle_hide_edges_on_drag(status=True)
return self.nw_
def add_elements(self):
"""
Adds nodes and weighted edges
to initial object.
Returns
-------
self
"""
for edge in self.edges:
source = edge[0]
dist = edge[1]
weight= edge[2]
self.nw_.add_node(source, source, title=source)
self.nw_.add_node(dist, dist, title=dist)
self.nw_.add_edge(source, dist, value=weight)
return self
def get_neighbors(self):
"""
Finds neighbors for each node
in the original graph.
Returns
-------
pyvis.network.Network
"""
neighbor_map = self.nw_.get_adj_list()
for node in self.nw_.nodes:
node['title'] += ":<br>" + "<br>".\
join(neighbor_map[node['id']])
node['value'] = len(neighbor_map[node['id']])
return self
def get_info(self):
"""
Prints basic information
about the original graph.
"""
print("\n\U0001F310 Total nodes:",
len(self.nw_.nodes))
print("\n\U0001F310 Total edges:",
len(self.nw_.edges))
def show_graph(self):
"""
Saves an HTML static webpage
containing the interactive
plot of the obtained graph.
Parameters
----------
name: string
Name for the output HTML file.
Returns
-------
pyvis.network.Network
"""
return self.nw_.show("dp_last_graph.html")