-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
178 lines (147 loc) · 6.06 KB
/
demo.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
#!/usr/bin/env python3
import os
import json
import numpy as np
import matplotlib.pyplot as plt
script_dir = os.path.dirname(os.path.realpath(__file__))
FOLDER = os.path.join(script_dir, 'GaitData')
CODE_LIST = [filename.replace(".csv", "") for filename in os.listdir(
FOLDER) if filename.endswith(".csv")]
COLUMN_NAMES = {'LAV': 0, 'LAX': 1, 'LAY': 2, 'LAZ': 3, 'LRV': 4, 'LRX': 5, 'LRY': 6, 'LRZ': 7,
'RAV': 8, 'RAX': 9, 'RAY': 10, 'RAZ': 11, 'RRV': 12, 'RRX': 13, 'RRY': 14, 'RRZ': 15}
def load_metadata(subject, trial):
"""Return the metadata dict for the given subject-trial.
Arguments:
subject {int} -- Subject number
trial {int} -- Trial number
Returns
-------
dict
Metadata
"""
code = str(subject) + "-" + str(trial)
fname = os.path.join(FOLDER, code)
with open(fname + ".json") as metadata_file:
metadata_dict = json.load(metadata_file)
return metadata_dict
def load_signal(subject, trial):
"""Return the signal associated with the subject-trial pair.
Parameters
----------
subject : int
Subject number
trial : int
Trial number
Returns
-------
numpy array
Signal
"""
code = str(subject) + "-" + str(trial)
fname = os.path.join(FOLDER, code)
signal = np.loadtxt(fname+".csv", delimiter=",", skiprows=1)
return signal
def print_trial_info(metadata_dict, signal):
"""Dump the trial information in a text file (trial_info.txt)
Parameters
----------
metadata_dict : dict
Metadata of the trial.
signal : numpy array
Time series of the trial.
"""
n_samples, _ = signal.shape
display_dict = {'Subject': "Subject: {Subject}".format(**metadata_dict),
'Trial': "Trial: {Trial}".format(**metadata_dict),
'Age': "Age (year): {Age}".format(**metadata_dict),
'Gender': "Gender: {Gender}".format(**metadata_dict),
'Height': "Height (m): {Height}".format(**metadata_dict),
'Weight': "Weight (kg): {Weight}".format(**metadata_dict),
'PathologyGroup': "Pathology group: {PathologyGroup}".format(**metadata_dict),
'WalkingSpeed': "Walking speed (km/h): {WalkingSpeed}".format(**metadata_dict),
'Duration': 'Duration (s): {:.1f}'.format(n_samples/100),
'LeftGaitCycles': ' - Left foot: {}'.format(len(metadata_dict['LeftFootActivity'])),
'RightGaitCycles': ' - Right foot: {}'.format(len(metadata_dict['RightFootActivity'])),
'Laterality': "Laterality : {Laterality}".format(**metadata_dict),
}
info_msg = """
{Subject:^30}|{Trial:^30}
------------------------------+------------------------------
{Age:<30}| {WalkingSpeed:<30}
{Height:<30}| {Duration:<30}
{Weight:<30}| Number of footsteps:
{PathologyGroup:<30}| {LeftGaitCycles:<30}
{Laterality:<30}| {RightGaitCycles:<30}
"""
# Dump information
with open("trial_info.txt", "w") as f:
print(info_msg.format(**display_dict), file=f)
def dump_plot(signal, metadata_dict, to_plot=["RAV", "RAZ", "RRY"]):
n_samples, _ = signal.shape
tt = np.arange(n_samples) / 100
# get limits
acc = np.take(signal, indices=[COLUMN_NAMES[dim_name]
for dim_name in to_plot if dim_name[1] == "A"], axis=1)
if acc.size > 0:
acc_ylim = [acc.min()-0.1, acc.max()+0.1]
rot = np.take(signal, indices=[COLUMN_NAMES[dim_name]
for dim_name in to_plot if dim_name[1] == "R"], axis=1)
if rot.size > 0:
rot_ylim = [rot.min()-20, rot.max()+20]
for dim_name in to_plot:
fig, ax = plt.subplots(figsize=(10, 4))
# xlim
ax.set_xlim(0, n_samples/100)
# plot
dim = COLUMN_NAMES[dim_name]
ax.plot(tt, signal[:, dim])
# ylim
if dim_name[1] == "A":
ax.set_ylim(acc_ylim)
elif dim_name[1] == "R":
ax.set_ylim(rot_ylim)
# number of yticks
plt.locator_params(axis='y', nbins=6)
# ylabel
ylabel = "m/s²" if dim_name[1] == "A" else "deg/s"
ax.set_ylabel(ylabel, fontdict={"size": 20})
for z in ax.get_yticklabels() + ax.get_xticklabels():
z.set_fontsize(15)
# step annotations
if dim_name[0] == "R":
steps = metadata_dict["RightFootActivity"]
elif dim_name[0] == "L":
steps = metadata_dict["LeftFootActivity"]
ymin, ymax = ax.get_ylim()
for start, end in steps:
ax.vlines([start/100, end/100], ymin, ymax, linestyles="--", lw=1)
ax.fill_between([start/100, end/100], ymin, ymax,
facecolor="green", alpha=0.3)
fig.tight_layout()
plt.savefig(dim_name + ".svg", dpi=300,
transparent=True, bbox_inches='tight')
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description='Display information and time series for a given trial.')
parser.add_argument('--subject', metavar='subject', type=int,
help='The subject identifier')
parser.add_argument('--trial', metavar='trial', type=int,
help='The trial identifier')
args = parser.parse_args()
subject, trial = args.subject, args.trial
to_plot = ["RAV", "RAZ", "RRY", "LAV", "LAZ", "LRY"]
# check if the code exists.
code = str(subject) + "-" + str(trial)
assert code in CODE_LIST, "The following code does not exist: {}".format(
code)
# Check if the signal to display follow the naming convention.
assert all(
dim_name in COLUMN_NAMES for dim_name in to_plot), "Check the names of the dimensions to plot."
# load metadata and signal
metadata = load_metadata(subject, trial)
signal = load_signal(subject, trial)
# dump trial info
print_trial_info(metadata, signal)
# dump plots
dump_plot(signal, metadata, to_plot=to_plot)