-
Notifications
You must be signed in to change notification settings - Fork 0
/
try_plot_extracted.py
390 lines (304 loc) · 11.4 KB
/
try_plot_extracted.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
# -*- coding: utf-8 -*-
"""
plot histograms from extracted values
TODO:
titles are not centered
add JP defined vars
-obtain both peaks for GRFZ?
add ylabels as in gait curve plots
extract num. values into pdf report (table format)
plt.table
https://towardsdatascience.com/simple-little-tables-with-matplotlib-9780ef5d0bc4
looks bad buggy
create html table, use weasyprint -> png, then use matplotlib image functions
-weasyprint is hard to get working :(
create plotly table, save into png, insert into matplotlib
halutut muuttujat:
nilkan asento (koukistusarvo) alkukontaktissa (IC)
polven asento (ojennusarvo) alkukontaktissa IC
lonkan koukistuskulma alkukontaktissa
nilkan koukistuskulman arvo tukivaiheen lopulla (stance, esim. kohta 40 tai 45% ) tai nilkan maksimi koukistusarvo tukivaiheessa
polven ojennus tukivaiheen lopulla (stance, esim. kohta 40 tai 45% ) tai polven maksimi ojennusarvo tukivaiheessa
Lonkan maksimi ojennusarvo tukivaiheessa
polven maksimi koukistus heilahdusvaiheessa.
• (polven ojennus heilahdusvaiheen lopulla - KYSEENALAINEN, SAMA KUIN ALKUKONTAKTISSA?)
lonkan maksimi koukistusarvo heilahdusvaiheessa
+ muuttujat JP:n paperista:
peak hip abductor moment
@author: Jussi ([email protected])
"""
# %% init
from collections import defaultdict
from collect_gait import utils
import datetime
import gaitutils
import itertools
import matplotlib.pyplot as plt
import numpy as np
import os.path as op
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from gaitutils.viz.plot_plotly import _get_plotly_axis_labels, plot_extracted_box
from gaitutils import cfg
import logging
# logging.basicConfig(level=logging.DEBUG)
# %% read the data
# let's get all sessions under this dir...
rootdir = r"Z:\Userdata_Vicon_Server"
rootdir = r"C:\Temp\D0063_RR"
rootdir = r"Y:\Userdata_Vicon_Server\1_Diplegia\D0063_RR"
# ...newer than this date
date = datetime.datetime(2018, 1, 1)
# tags for dynamic trials
tags = ['E1', 'E2', 'E3', 'T1', 'T2', 'T3']
substrings = None
sessions = list(utils.get_sessiondirs(rootdir, newer_than=date, substrings=substrings))
session_trials = {
session: gaitutils.sessionutils._get_tagged_dynamic_c3ds_from_sessions(
session, tags=cfg.eclipse.tags
)
for session in sessions
}
allvars = [vardef[0] for vardefs in cfg.report.vardefs.values() for vardef in vardefs]
from_models = set(gaitutils.models.model_from_var(var) for var in allvars)
curve_vals = {
session: gaitutils.stats._trials_extract_values(trials, from_models=from_models)
for session, trials in session_trials.items()
}
# %% box plot
for title, vardefs in cfg.web_report.vardefs.items():
fig = plot_extracted_box(curve_vals, vardefs)
fig['layout']['title'] = title
gaitutils.viz.plot_misc.show_fig(fig)
# %%
import numpy as np
import matplotlib.pyplot as plt
title_text = 'Loss by Disaster'
footer_text = 'June 24, 2020'
fig_background_color = 'skyblue'
fig_border = 'steelblue'
table = [
['Freeze', 'Wind', 'Flood', 'Quake', 'Hail'],
['5 year', 66386, 174296, 75131, 577908, 32015],
['10 year', 58230, 381139, 78045, 99308, 160454],
['20 year', 89135, 80552, 152558, 497981, 603535],
['30 year', 78415, 81858, 150656, 193263, 69638],
['40 year', 139361, 331509, 343164, 781380, 52269],
]
# Pop the headers from the data array
column_headers = table.pop(0)
row_headers = [x.pop(0) for x in table]
# Table data needs to be non-numeric text. Format the data while I'm at it.
cell_text = []
for row in table:
cell_text.append([f'{x/1000:1.1f}' for x in row])
# Get some lists of color specs for row and column headers
rcolors = plt.cm.BuPu(np.full(len(row_headers), 0.1))
ccolors = plt.cm.BuPu(np.full(len(column_headers), 0.1))
# Create the figure. Setting a small pad on tight_layout
# seems to better regulate white space. Sometimes experimenting
# with an explicit figsize here can produce better outcome.
plt.figure(
linewidth=2,
edgecolor=fig_border,
facecolor=fig_background_color,
tight_layout={'pad': 1},
# figsize=(5,3)
)
# Add a table at the bottom of the axes
the_table = plt.table(
cellText=cell_text,
rowLabels=row_headers,
rowColours=rcolors,
rowLoc='right',
colColours=ccolors,
colLabels=column_headers,
loc='center',
)
# Scaling is the only influence we have over top and bottom cell padding.
# Make the rows taller (i.e., make cell y scale larger).
the_table.scale(1, 1.5) # Hide axes
ax = plt.gca()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# Hide axes border
plt.box(on=None) # Add title
plt.suptitle(title_text) # Add footer
plt.figtext(
0.95, 0.05, footer_text, horizontalalignment='right', size=6, weight='light'
)
# Force the figure to update, so backends center objects correctly within the figure.
# Without plt.draw() here, the title will center on the axes and not the figure.
plt.draw()
# Create image. plt.savefig ignores figure edge and face colors, so map them.
fig = plt.gcf()
plt.savefig(
'pyplot-table-demo.png',
# bbox='tight',
edgecolor=fig.get_edgecolor(),
facecolor=fig.get_facecolor(),
dpi=150,
)
# %%
import numpy as np
import matplotlib.pyplot as plt
title_text = 'Loss by Disaster'
footer_text = 'June 24, 2020'
table = [
['Freeze', 'Wind', 'Flood', 'Quake', 'Hail'],
['5 year', 66386, 174296, 75131, 577908, 32015],
['10 year', 58230, 381139, 78045, 99308, 160454],
['20 year', 89135, 80552, 152558, 497981, 603535],
['30 year', 78415, 81858, 150656, 193263, 69638],
['40 year', 139361, 331509, 343164, 781380, 52269],
]
# Pop the headers from the data array
column_headers = table.pop(0)
row_headers = [x.pop(0) for x in table]
# Table data needs to be non-numeric text. Format the data while I'm at it.
cell_text = []
for row in table:
cell_text.append([f'{x/1000:1.1f}' for x in row])
# Create the figure. Setting a small pad on tight_layout
# seems to better regulate white space. Sometimes experimenting
# with an explicit figsize here can produce better outcome.
plt.figure(
linewidth=2,
tight_layout={'pad': 1},
# figsize=(5,3)
)
# %% fun
from gaitutils.viz.plot_matplotlib import _plot_tabular_data
from gaitutils import models
table = [
['Freeze', 'Wind', 'Flood', 'Quake', 'Hail'],
['5 year', 66386, 174296, 75131, 577908, 32015],
['10 year', 58230, 381139, 78045, 99308, 160454],
['20 year', 89135, 80552, 152558, 497981, 603535],
['30 year', 78415, 81858, 150656, 193263, 69638],
['40 year', 139361, 331509, 343164, 781380, 52269],
]
# Pop the headers from the data array
column_headers = table.pop(0)
row_headers = [x.pop(0) for x in table]
# Table data needs to be non-numeric text. Format the data while I'm at it.
data = []
for row in table:
data.append([f'{x/1000:1.1f}' for x in row])
def _nested_get(di, keys):
"""Get a value from a nested dict, using a list of keys"""
for key in keys:
di = di[key] # iterate until we exhaust the nested keys
return di
def _compose_varname(vardef):
"""Compose a variable name for extracted variable.
E.g. ['HipAnglesX', 'peaks', 'swing', 'max']
-> 'Hip flexion maximum during swing phase'
"""
varname = vardef[0]
# get variable description from gaitutils.models
themodel = models.model_from_var(varname)
name = themodel.varlabels_noside[varname]
if vardef[1] == 'contact':
name += ' at initial contact'
elif vardef[1] in ['peaks', 'extrema']:
phase = vardef[2] # swing, stance etc.
valtype = vardef[3] # min, max etc.
val_trans = {'max': 'maximum', 'min': 'minimum'}
if phase == 'overall':
name += ', %s %s' % (phase, val_trans[valtype])
else:
name += ', %s phase %s' % (phase, val_trans[valtype])
if vardef[1] == 'peaks':
name += ' peak'
return name
# nvars = len(vardefs)
# subtitles = [_compose_varname(nested_keys) for nested_keys in vardefs]
# _plot_tabular_data(data, row_headers, column_headers)
# %% box plot
for title, vardefs in cfg.report.vardefs.items():
row_labels = [_compose_varname(vardef) for vardef in vardefs]
col_labels = [
session + '_%s' % ctxt for session in curve_vals.keys() for ctxt in 'LR'
]
_plot_tabular_data(data, row_labels, col_labels)
# %% massage curve_vals data into rows
from matplotlib.backends.backend_pdf import PdfPages
page_size = (11.69, 8.27) # report page size = landscape A4
col_labels = [
op.split(session)[-1] + ' / %s' % ctxt
for session in curve_vals.keys()
for ctxt in 'LR'
]
pdfpath = 'C:\\Temp\\foo.pdf'
def _var_unit(vardef):
"""Return unit for a vardef"""
varname = vardef[0]
themodel = models.model_from_var(varname)
return themodel.units[varname]
if unit == 'deg':
unit = u'\u00B0' # Unicode degree sign
return unit
with PdfPages(pdfpath) as pdf:
for title, vardefs in cfg.report.vardefs.items():
row_labels = [_compose_varname(vardef) for vardef in vardefs]
table = list()
for vardef in vardefs:
row = list()
for session, session_vals in curve_vals.items():
for ctxt in 'LR':
vardef_ctxt = [ctxt + vardef[0]] + vardef[1:]
this_vals = _nested_get(
session_vals, vardef_ctxt
) # returns list of values for given session and context = column
mean, std = np.mean(this_vals), np.std(this_vals)
unit = _var_unit(vardef_ctxt)
if unit == 'deg':
unit = u'\u00B0' # Unicode degree sign
else:
unit = ' ' + unit
row.append('%.2f±%.2f%s' % (mean, std, unit))
table.append(row)
fig = _plot_tabular_data(table, row_labels, col_labels)
fig.set_size_inches(*page_size)
pdf.savefig(fig)
# %% massage curve_vals data into rows
from matplotlib.backends.backend_pdf import PdfPages
page_size = (11.69, 8.27) # report page size = landscape A4
fn = op.split(gaitutils.envutils._named_tempfile(suffix='.pdf'))[-1]
pdfpath = 'C:\\Temp\\%s' % fn
with PdfPages(pdfpath) as pdf:
for title, vardefs in cfg.report.vardefs.items():
fig = gaitutils.viz.plot_matplotlib._plot_extracted_table2(curve_vals, vardefs)
fig.set_size_inches(*page_size)
fig.suptitle('Curve extracted values: %s' % title)
pdf.savefig(fig)
# %% plotly -> png bytestream -> matplotlib -> pdf
# requires
import plotly.graph_objects as go
import os.path as op
import gaitutils
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import io
page_size = (11.69, 8.27) # report page size = landscape A4
fig = go.Figure(data=[go.Table(header=dict(values=['A Scores', 'B Scores']),
cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]))
])
fig.show()
fn = op.split(gaitutils.envutils._named_tempfile(suffix='.png'))[-1]
figpath = 'C:\\Temp\\%s' % fn
bytes = fig.to_image(format='png', engine='kaleido', width=1600, height=1200)
bio = io.BytesIO(bytes)
im = plt.imread(bio, format='png')
#mfig = plt.figure()
#mfig.figimage(im)
mfig, axes = plt.subplots()
mfig.set_size_inches(*page_size)
mfig.set_dpi(200)
axes.imshow(im)
plt.show()
fn = op.split(gaitutils.envutils._named_tempfile(suffix='.pdf'))[-1]
pdfpath = 'C:\\Temp\\%s' % fn
with PdfPages(pdfpath) as pdf:
pdf.savefig(mfig)