-
Notifications
You must be signed in to change notification settings - Fork 2
/
fig_updater.py
229 lines (192 loc) · 7.27 KB
/
fig_updater.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
218
219
220
221
222
223
224
225
226
227
228
229
"""
Copyright (C) 2020 David Ollodart
GNU General Public License <https://www.gnu.org/licenses/>.
"""
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from smooth import whittaker_smooth
from data import df, gbl, numeric_dtypes
from dash.dependencies import Input, Output
#from scipy.interpolate import UnivariateSpline
# for spline interpolation
#from numpy import ones, convolve
# for moving average (series method used instead)
color_cycle = px.colors.qualitative.Plotly
ncolors = len(color_cycle)
def cartesian_product(xs, ys):
"""
Cartesian product in which xs "run fastest".
"""
xys = []
for cy, y in enumerate(ys):
for cx, x in enumerate(xs):
xys.append( ((cx, cy), (x, y)) )
return xys
def fig_updater(df, xs, ys, size=None, color=None, symbol=None,
hover_data = None, smoother = None, smoother_parameter=None,
max_size=35, cartesian_prod = False):
"""Evaluate inputs and updates the figure correctly based on inputs
x:
sequence of labels (column names)
y:
sequence of labels (column names)
size:
string, label (column name)
color:
string, label (column name)
symbol:
string, label (column name)
cartesian_prod:
bool, whether the list should be expanded
Relevant variables:
Number of x variables
Number of y variables
Data type(s) of x variable(s)
Data type(s) of y variable(s)
Data types of size (must be numeric or will be "factorized"), color, symbol (must be categorical or will be histogramed)
"""
dtypes = df.dtypes.to_dict()
shared_xaxes = shared_yaxes = False
if cartesian_prod:
shared_xaxes = shared_yaxes = True
xys = cartesian_product(xs, ys)
rows = len(ys)
cols = len(xs)
elif len(xs) > 1 and len(ys) == 1:
xys = tuple( ((i, 0),(xs[i], ys[0])) for i in range(len(xs)) )
shared_yaxes = True
rows = 1
cols = len(xs)
elif len(xs) == 1 and len(ys) > 1:
xys = tuple( ((0, i),(xs[0], ys[i])) for i in range(len(ys)) )
shared_xaxes = True
rows = len(ys)
cols = 1
elif len(xs) == 1 and len(ys) == 1:
rows = cols = 1
xys = ((0, 0),(xs[0], ys[0])),
else:
n = max(len(xs), len(ys))
m = min(len(xs), len(ys))
cols = 2
rows = 1 + (n - 1) // 2
if len(xs) < len(ys):
xys = tuple( ((i % 2, i // 2), (xs[i % m], ys[i])) for i in range(n))
else:
xys = tuple( ((i % 2, i // 2), (xs[i], ys[i % m])) for i in range(n))
fig = make_subplots(rows=rows,
cols=cols,
shared_yaxes=shared_yaxes,
shared_xaxes=shared_xaxes)
if size is not None:
if not dtypes[size] in numeric_dtypes:
dfsize = disc2cont(df[size]) * max_size
else:
dfsize = df[size] * max_size / df[size].max()
if color is not None: # color is used for legending, not quantitative heat maps
if dtypes[color] in numeric_dtypes:
dfcolor = cont2disc(df[color])
else:
dfcolor = df[color]
coloriter = dfcolor.unique()
else:
dfcolor = df['dummy']
coloriter = [True]
if symbol is not None:
if dtypes[symbol] in numeric_dtypes:
dfsymbol = cont2disc(df[symbol]) # interval type
else:
dfsymbol = df[symbol]
symboliter = dfsymbol.unique()
else:
dfsymbol = df['dummy']
symboliter = [True]
for ccounter, c in enumerate(coloriter):
for scounter, s in enumerate(symboliter):
bl = (dfcolor == c) & (dfsymbol == s)
if bl.sum() < 2:
continue
gr = df[bl]
name = (str(c) if c is not None and c is not True else '') +\
('-' + str(s) if s is not None and s is not True else '')
if size is not None:
size_array = dfsize[bl]
else:
size_array = None
marker_array = dict(color=color_cycle[ccounter % ncolors],
symbol=scounter,
size=size_array)
# hovertext must be sequence
if hover_data is not None:
hovertext_array = []
for hii, hi in gr[list(hover_data)].iterrows(): # allow passing tuples
hr = []
for hvt in hover_data:
hr.append(f'{hvt}: {hi[hvt]}')
hovertext_array.append('\n\n'.join(hr))
else:
hovertext_array = None
for (xcounter, ycounter), (xinst, yinst) in xys:
fig.update_yaxes(title_text=yinst,
row=1+ycounter,
col=1+xcounter)
fig.update_xaxes(title_text=xinst,
row=1+ycounter,
col=1+xcounter)
trace = go.Scattergl(x=gr[xinst],
y=gr[yinst],
mode='markers',
name=name + '-' + xinst + '-' + yinst,
hovertext=hovertext_array,
marker=marker_array)
fig.add_trace(trace, row=ycounter+1, col=xcounter+1)
gr = gr.sort_values(by=xinst)
if smoother == 'whittaker':
y2 = whittaker_smooth(gr[yinst].values, 10**smoother_parameter) # input is a linear range of 0 to 5
elif smoother == 'moving-average':
y2 = gr[yinst].rolling(window=smoother_parameter, center=False).mean()
else:
continue
trace = go.Scattergl(x=gr[xinst],
y=y2,
mode='lines',
name=name + '-' + xinst + '-' + yinst + '-smooth',
hovertext=None,
marker=dict(color=color_cycle[ccounter % ncolors]))
fig.add_trace(trace, row=ycounter+1, col=xcounter+1)
return fig
def cont2disc(series, ncategories=5):
"""
Converts continuous into intervals to be treated as discrete (intervals represented as strings).
"""
q = series.quantile(np.linspace(0, 1, ncategories))
return pd.cut(series, pd.unique(q)).astype(str)
def disc2cont(series):
"""
Converts discrete (str or other) to continuous numeric in interval [0, 1].
"""
return series.map(dict(zip(series.unique(), np.linspace(0, 1, series.nunique()))))
if __name__ == '__main__':
x1 = 'dateRep'
df = pd.read_csv('data.csv')
df = df[df['geoId'].isin(['US', 'BR', 'IN'])]
df[x1] = pd.to_datetime(df[x1], dayfirst=True)
x2 = 'cases'
y1 = 'cases'
y2 = 'deaths'
shape = 'continentExp'
color = 'geoId'
size = 'popData2019'
hover_data = ('countriesAndTerritories',
'Cumulative_number_for_14_days_of_COVID-19_cases_per_100000')
# don't throw errors, but may not be correct
#color, size = size, color
#shape, size = size, shape
color = shape = None
xs = (x1, x2)
ys = (y1, y2)
fig = fig_updater(df, xs, ys, size=size, color=color, symbol=shape,hover_data=hover_data,smoother='whittaker',cartesian_prod=False)
fig.show()