-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsankey_automated.py
159 lines (136 loc) · 4.3 KB
/
sankey_automated.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 11 12:39:30 2023
This program produces a sankey chart for publication record by discipline
Note: link this to Google Scholar to autogenerate
@author: tjards
"""
# import stuff
# ------------
import numpy as np
import plotly.io as pio
import plotly.graph_objects as go
pio.renderers.default='browser'
import my_scholar as schol
# pull data from Google Scholar
# ----------------------------
my_name = 'Peter Travis Jardine'
pubs, dates, dates_norm, titles, discs, connects = schol.build_lists(my_name)
print('processing...')
# manually add degree information
# ------------------------------
degree = ['Phd (Queen\'s)'] # only accepts one degree right now
degree_start = 2015 # start year (to count pubs)
degree_stop = 2018.5 # end year (to count pubs)
# labels
# ------
labels = discs + pubs
# draw the connections
# -------------------
connects_target = []
connects_source = []
xs = [x for x in dates_norm]
ys = [0.9*x for x in xs]
color_links = []
for i in range(0,len(connects)):
if type(connects[i]) == int:
connects_target += [i+len(discs)]
connects_source += [connects[i]]
if connects[i] == 0:
color_links += ['rgba(255, 192, 192, 0.5)']
elif connects[i] == 1:
color_links += ['rgba(192, 192, 255 0.5)']
elif connects[i] == 2:
color_links += ['rgba(192, 255, 192, 0.5)']
else:
color_links += ['rgba(0, 0, 0, 0.2)']
else:
for j in connects[i]:
connects_target += [i+len(discs)]
connects_source += [j]
if j == 0:
color_links += ['rgba(255, 192, 192 0.5)']
elif j == 1:
color_links += ['rgba(192, 192, 255 0.5)']
elif j == 2:
color_links += ['rgba(192, 255, 192 0.5)']
else:
color_links += ['rgba(0, 0, 0, 0.2)']
# align by publcation dates
# ------------------------
discsx = []
discsy = []
left_edge = 0
for s in range(0,len(discs)):
discsy = discsy + [0.1*s + 0.1]
discsx = discsx + [0]
ys = discsy + ys
xs = discsx + xs
# add degrees
# ------------
degrees_source = []
degrees_target = []
degrees_year_norm = [(x - min(dates) + 1)/(max(dates)-min(dates) + 1) for x in [degree_stop]]
# look through pubs
for d in range(0,len(pubs)):
# if publication occured during degree
if dates[d] > degree_start and dates[d] < degree_stop:
degrees_source = degrees_source + [len(discs) + d]
degrees_target = degrees_target + [len(discs) + len(pubs)]
# add colors
degrees_color = ['rgba(255, 255, 255, 0.2)']
degrees_color_links = []
for i in degrees_source:
degrees_color_links += ['rgba(180, 128, 200, 0.5)']
# draw the nodes
# --------------
color_node = []
# make the pubs white
for k in pubs:
color_node += ['rgba(255, 255, 255, 0.2)']
color_node = ['rgb(255, 192, 192, 0.8)', 'rgb(192, 192, 255,0.8) ', 'rgb(192, 255, 192,0.8)'] + ['rgb(192, 255, 192,0.8)'] + color_node
# truncate labels
# ---------------
trunc_size = 10 # max length
# remove numbers
translation_table = str.maketrans("", "", "0123456789 ")
labels = [label.translate(translation_table) for label in labels]
# truncate
for i in range(len(discs),len(labels)):
labels[i] = labels[i][0:trunc_size]
# produce the chart
# -----------------
fig = go.Figure(go.Sankey(
arrangement = "snap",
node = {
"label": labels + degree,
"x": xs + degrees_year_norm,
"y": ys + [0.2],
'pad':0.1,
'color': color_node + degrees_color,
}, # 10 Pixels
link = {
"source": connects_source + degrees_source,
"target": connects_target + degrees_target,
'color': color_links + degrees_color_links,
"value": list(np.ones(len(connects_source + degrees_source)))
}))
fig.update_layout(
font_size=14
)
for x_coordinate, column_name in enumerate(dates):
fig.add_annotation(
x=xs[x_coordinate+len(discs)],
y = 1.05,
xref="paper",
yref="paper",
text=column_name,
showarrow=False,
font=dict(
size=16,
color="black"
),
align="center"
)
fig.show()