-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp_grand_average_curves.py
105 lines (83 loc) · 3.1 KB
/
cp_grand_average_curves.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
# -*- coding: utf-8 -*-
"""
CP-projektin analyysiskripti
vertaile keskiarvoja norm vs kognitiivinen
TODO:
nopeusdata mukaan
@author: Jussi (jnu@iki.fi)
"""
from __future__ import print_function
import gaitutils
from gaitutils import cfg
import matplotlib.pyplot as plt
import glob
import numpy as np
import sys
import os
import os.path as op
import logging
from cp_common import get_files
def _do_average(subjects, side):
if not isinstance(subjects, list):
subjects = [subjects]
max_dist = 25 # deg, for outlier detection
# special layout
lout = [['HipAnglesX', 'KneeAnglesX', 'AnkleAnglesX'],
['PelvisAnglesX', 'PelvisAnglesY', 'PelvisAnglesZ'],
['ThoraxAnglesX', 'ThoraxAnglesY', 'ThoraxAnglesZ'],
['ShoulderAnglesX', 'ShoulderAnglesY', 'ShoulderAnglesZ']]
# add side to layout
for i, row in enumerate(lout):
for j, item in enumerate(row):
lout[i][j] = side+item
# flatten into list
lout_ = [item for row in lout for item in row]
Nfiles_all = list()
for subject in subjects:
Nfiles = get_files(subject, 'normal')
if not Nfiles:
raise Exception('No trials for %s' % subject)
Nfiles_all.extend(Nfiles)
# average over trials
Ntr = gaitutils.stats.AvgTrial(Nfiles_all, max_dist=max_dist)
# plot all
pl = gaitutils.Plotter()
pl.layout = lout
pl.trial = Ntr
cfg['plot']['model_stddev_alpha'] = '0.2'
cfg['plot']['model_stddev_colors'] = "{'R': 'blue', 'L': 'blue'}"
cfg['plot']['model_tracecolors'] = "{'R': 'blue', 'L': 'blue'}"
pl.plot_trial(plot_model_normaldata=True, model_stddev=Ntr.stddev_data)
#pl.plot_trial()
"""
# create custom legend outside axes
from matplotlib.patches import mlines
l_norm = mlines.Line2D([], [], color='blue')
l_cogn = mlines.Line2D([], [], color='red')
plt.legend([l_norm, l_cogn], ['normal', 'cognitive'], bbox_to_anchor=(.98, .98),
bbox_transform=plt.gcf().transFigure, fontsize=8)
# create pdf and png figs
figname = '%s_%s' % (subject, side)
figname = op.join(plotdir, figname)
plt.savefig(figname+'.pdf')
plt.savefig(figname+'.png')
logname = figname+'.log'
# report N of cycles per var
print('\n%s: %s' % (subject, side))
print('N of normal cycles per variable:')
print({key: var for key, var in N_ok.items() if key in lout_})
print('N of cogn. cycles per variable:')
print({key: var for key, var in C_ok.items() if key in lout_})
# ...also into logfile
with open(logname, 'w') as f:
print('\n%s: %s' % (subject, side), file=f)
print('N of normal cycles per variable:', file=f)
print({key: var for key, var in N_ok.items() if key in lout_}, file=f)
print('N of cogn. cycles per variable:', file=f)
print({key: var for key, var in C_ok.items() if key in lout_}, file=f)
"""
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
subjects = ['DP03', 'DP05', 'DP07', 'DP09', 'DP10']
side = 'R'
_do_average(subjects, side)