-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtriplots.py
429 lines (371 loc) · 12.5 KB
/
triplots.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
"""
Plotting functions used by the TriScale module
"""
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.io as pio
pio.templates.default = "none"
from helpers import acorr
import colors
def autocorr_plot( x,
layout=None,
out_name=None,
show=True,
verbose=False):
"""
Plot the autocorellation function of x.
If x is a time series, the function assumes that the series is equally
spaced (as required for simple autocorellation).
The plot also displays the 95% confidence interval for the sample
autocorellation coefficient values, that is:
+/- 1.96*sqrt( len(x) )
If the sample autocorellation coefficients are within these bounds,
the series is i.i.d. with 95% probability.
"""
todo = ''
todo += '# ---------------------------------------------------------------- \n'
todo += '# TODO autocorr_plot\n'
todo += '# ---------------------------------------------------------------- \n'
todo += '- check the custom layout input\n'
todo += '- finish the docstring (input/output, support to save the plot)\n'
todo += '- uniformize the plot colors (use the same as TriScale logo)\n'
todo += '# ---------------------------------------------------------------- \n'
if verbose:
print('%s' % todo)
## Initialize the figure
figure = go.Figure()
# IID bounds
bounds = go.Scatter(
x=[0,len(x),len(x),0],
y= np.array([1,1,-1,-1])*(1.96)/np.sqrt(len(x)),
hoverinfo='skip',
mode='lines',
fill='toself',
fillcolor=colors.light_orange,
line=dict(color=colors.light_orange, width=0),
showlegend=True,
name='95% CI on i.i.d. test')
figure.add_trace(bounds)
# Autocorellation coefficients
trace = go.Scatter(
x=list(range(0,len(x))),
y=acorr(x),
mode='markers, lines',
line={'color':colors.orange},
marker={'color':colors.orange},
showlegend=True,
name='Sample Autocor. Coefficients')
figure.add_trace(trace)
# Default Layout
default_layout = go.Layout(
title='Autocorrelation',
xaxis={'title':'Lag'})
# Custom Layout
if layout is not None:
default_layout.update(layout)
figure.update_layout(default_layout)
# Output
if out_name is not None:
figure.write_image(out_name)
if show:
figure.show()
return figure
def theil_plot( y,
x=None,
metric_data=None,
convergence_data=None,
layout=None,
raw_opacity=0.3,
out_name=None,
verbose=False):
todo = ''
todo += '# ---------------------------------------------------------------- \n'
todo += '# TODO theil_plot\n'
todo += '# ---------------------------------------------------------------- \n'
todo += '- check the custom layout input\n'
todo += '- write the docstring (input/output, support to save the plot)\n'
todo += '- uniformize the plot colors (use the same as TriScale logo)\n'
todo += '- Add the computed metric value (somehow...)\n'
todo += '# ---------------------------------------------------------------- \n'
if verbose:
print('%s' % todo)
## Parse the inputs
if type(y) != np.ndarray:
y = np.array(y)
if x is not None and x.size != 0:
if x.shape[0] != y.shape[0]:
raise ValueError('x and y must be the same shape.')
else:
x = np.arange(y.size)
if metric_data is None:
convergence_data_x = np.array(x)
convergence_data_y = np.array(y)
else:
convergence_data_x = np.array(metric_data[0])
convergence_data_y = np.array(metric_data[1])
## Initialize the figure
figure = go.Figure()
## Create the traces to plot
if len(x) > 1000:
step = int(len(x)/1000)
x_subsampled = x[::step]
y_subsampled = y[::step]
else:
x_subsampled = x
y_subsampled = y
trace = go.Scatter(
name='Data',
x=x_subsampled,
y=y_subsampled,
mode='markers',
marker={
'color':colors.blue,
'opacity':raw_opacity
}
)
figure.add_trace(trace)
if metric_data is not None:
metric_x = np.array(metric_data[0])
metric_y = np.array(metric_data[1])
metric = go.Scatter(
name='Metric',
x=metric_x,
y=metric_y,
marker={
'symbol':'circle-open',
'color':colors.orange},
mode='markers+lines'
)
figure.add_trace(metric)
if convergence_data is not None:
# Plot the Theil slope and its bounds
trend_data = convergence_data[1]
bounds_slope = go.Scatter(
name='CI ( Slope )',
x=[ convergence_data_x.min(), convergence_data_x.max(),
convergence_data_x.max(), convergence_data_x.min()],
y=[
trend_data[2],
trend_data[3],
trend_data[5],
trend_data[4]
],
hoverinfo='skip',
fill='toself',
fillcolor=colors.light_blue,
line=dict(color='rgba(0,0,0,0)')
)
figure.add_trace(bounds_slope)
med_slope = go.Scatter(
name='Slope',
x=[convergence_data_x.min(), convergence_data_x.max()],
y=[trend_data[0],trend_data[1]],
hoverinfo='skip',
mode='lines',
line={'color':colors.darker_orange}
)
figure.add_trace(med_slope)
# Plot tolerance bounds
tolerance_data = convergence_data[2]
tol_slope_up = go.Scatter(
name='Tolerance',
x=[convergence_data_x.min(), convergence_data_x.max()],
y=[
tolerance_data[0],
tolerance_data[1],
],
hoverinfo='skip',
line=dict(color='rgba(20,20,20,100)', dash='dash'),
mode='lines',
showlegend=False
)
figure.add_trace(tol_slope_up)
tol_slope_lo = go.Scatter(
name='Tolerance',
x=[convergence_data_x.min(), convergence_data_x.max()],
y=[
tolerance_data[2],
tolerance_data[3],
],
hoverinfo='skip',
line=dict(color='rgba(20,20,20,100)', dash='dash'),
mode='lines',
showlegend=True
)
figure.add_trace(tol_slope_lo)
## Layout
if layout is not None:
figure.update_layout(go.Layout(layout))
## Output
if out_name is not None:
figure.write_image(out_name)
return figure
def ThompsonCI_plot( data,
CI,
CI_bound,
to_plot,
layout=None,
out_name=None,
verbose=False ):
todo = ''
todo += '# ---------------------------------------------------------------- \n'
todo += '# TODO ThompsonCI_plot \n'
todo += '# ---------------------------------------------------------------- \n'
todo += '- write the doctring\n'
todo += '- check input types\n'
todo += '- add cumulative mass function\n'
todo += '- add a text note on the bounds\n'
todo += '- uniformize the plot colors (use the same as TriScale logo)\n'
todo += '# ---------------------------------------------------------------- \n'
if verbose:
print('%s' % todo)
# Check inputs
valid_plots = ['vertical', 'horizontal']
if to_plot is None:
return
if to_plot not in valid_plots:
raise ValueError("Wrong plot type. Valid types: 'vertical', 'horizontal'")
# Make sure data is sorted
sorted_data = np.sort(data)
# Initialize the CI shape
interval_shape = {
'type': 'rect',
'layer': 'above',
'xref': 'x',
'yref': 'paper',
'x0': 0,
'y0': 0,
'x1': 1,
'y1': 1,
'fillcolor': colors.light_orange,
'line': {'width': 0,},
'layer':'below'
}
## Initialize the figure
figure = go.Figure()
##
# Horizontal CI plot
##
if to_plot == 'horizontal':
# Default layout
default_layout = go.Layout(
yaxis={'visible':False, 'range':[0,2]},
height=250)
figure.update_layout(default_layout)
# Serie data
samples = go.Scatter(
x=data,
y=np.ones((len(data),), dtype=int),
mode='markers',
marker={'symbol':'circle-open', 'size':8},
line={'color':'black'},
name='Data',
)
# CI bounds
if not(np.isnan(CI[1])):
up_bound = go.Scatter(
x=[sorted_data[CI[1]], sorted_data[CI[1]]],
y=[0, 2],
mode='lines+text',
# text=sorted_data[CI[1]],
line={'color':colors.orange, 'width':4},
hoverinfo='skip',
name='CI',
# textposition="top center",
# textfont={'size':18 },
)
if CI_bound == 'upper':
figure.add_trace(up_bound)
interval_shape['x0'] = min(data)
interval_shape['x1'] = sorted_data[CI[1]]
if not(np.isnan(CI[0])):
lo_bound = go.Scatter(
x=[sorted_data[CI[0]], sorted_data[CI[0]]],
y=[0,2],
mode='lines+text',
line={'color':colors.orange, 'width':4},
hoverinfo='skip',
name='CI',
)
if CI_bound == 'lower':
figure.add_trace(lo_bound)
interval_shape['x0'] = sorted_data[CI[0]]
interval_shape['x1'] = max(data)
if ((not np.isnan(CI[0])) and
(not np.isnan(CI[1])) and
(CI_bound == 'two-sided')):
up_bound.showlegend = False
figure.add_trace(up_bound)
figure.add_trace(lo_bound)
interval_shape['x0'] = sorted_data[CI[0]]
interval_shape['x1'] = sorted_data[CI[1]]
##
# Vertical CI plot
##
if to_plot == 'vertical':
# Default layout
default_layout = go.Layout(
xaxis={'visible':False},
# height=250
)
figure.update_layout(default_layout)
# Serie data
samples = go.Scatter(
x=np.arange(len(data))+1,
y=data,
mode='markers',
marker={'symbol':'circle-open', 'size':8},
line={'color':'black'},
name='Data',
)
# CI bounds
if not(np.isnan(CI[1])):
up_bound = go.Scatter(
x=[0, len(data)+1],
y=[sorted_data[CI[1]], sorted_data[CI[1]]],
mode='lines',
line={'color':colors.orange, 'width':4},
hoverinfo='skip',
name='CI',
)
if CI_bound == 'upper':
figure.add_trace(up_bound)
interval_shape['y0'] = min(data)
interval_shape['y1'] = sorted_data[CI[1]]
if not(np.isnan(CI[0])):
lo_bound = go.Scatter(
x=[0, len(data)+1],
y=[sorted_data[CI[0]], sorted_data[CI[0]]],
mode='lines',
line={'color':colors.orange, 'width':4},
hoverinfo='skip',
name='CI',
)
if CI_bound == 'lower':
figure.add_trace(lo_bound)
interval_shape['y0'] = sorted_data[CI[0]]
interval_shape['y1'] = max(data)
if ((not np.isnan(CI[0])) and
(not np.isnan(CI[1])) and
(CI_bound == 'two-sided')):
up_bound.showlegend = False
figure.add_trace(up_bound)
figure.add_trace(lo_bound)
interval_shape['y0'] = sorted_data[CI[0]]
interval_shape['y1'] = sorted_data[CI[1]]
interval_shape['xref'] = 'paper'
interval_shape['yref'] = 'y'
##
# Customization and output
##
# Custom layout
if layout is not None:
figure.update_layout(layout)
# Output
figure.add_trace(samples)
figure.update_layout(shapes=[interval_shape])
# figure.show()
if out_name is not None:
figure.write_image(out_name)
return figure