|
a |
|
b/sisfall_clean.py |
|
|
1 |
import pandas as pd |
|
|
2 |
import numpy as np |
|
|
3 |
import warnings |
|
|
4 |
import time |
|
|
5 |
warnings.filterwarnings('ignore') |
|
|
6 |
import matplotlib |
|
|
7 |
matplotlib.use('TkAgg') |
|
|
8 |
import matplotlib.pyplot as plt |
|
|
9 |
|
|
|
10 |
path = '/Users/mattjohnson/Desktop/Python2018/sisfall/SubjectDataFrames/acm_SA' |
|
|
11 |
|
|
|
12 |
subjectList = [] |
|
|
13 |
|
|
|
14 |
firstIndex = 1 |
|
|
15 |
lastIndex = 4 |
|
|
16 |
|
|
|
17 |
print('*',firstIndex, 'to', lastIndex-1, '...') |
|
|
18 |
|
|
|
19 |
for i in range(firstIndex, lastIndex): |
|
|
20 |
data = pd.read_csv(path + str(i).zfill(2) + '.csv') |
|
|
21 |
data = data.drop('Unnamed: 0', axis=1) |
|
|
22 |
df = data[['x1', 'y1', 'z1', 'x2', 'y2', 'z2', 'activity', 'subject', 'trial']] |
|
|
23 |
subjectList.append(df) |
|
|
24 |
|
|
|
25 |
# Codes for ADLs |
|
|
26 |
dailies = ['D01', 'D02', 'D03', 'D04', 'D05', 'D06', 'D07', 'D08', 'D09', 'D10', 'D11', 'D12', 'D13', 'D14', 'D15', |
|
|
27 |
'D16', 'D17', 'D18', 'D19'] |
|
|
28 |
# Codes for Falls |
|
|
29 |
falls = ['F01', 'F02', 'F03', 'F04', 'F05', 'F06', 'F07', 'F08', 'F09', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15'] |
|
|
30 |
|
|
|
31 |
# Lists to hold dataframes sorted by activity (ADLs and Falls) |
|
|
32 |
adl_list = [] |
|
|
33 |
fall_list = [] |
|
|
34 |
# Iterate through subject data and sort into ADLs and Falls |
|
|
35 |
for s in subjectList: |
|
|
36 |
for d in dailies: |
|
|
37 |
tempdf = s[s['activity'] == d] |
|
|
38 |
adl_list.append(tempdf) |
|
|
39 |
|
|
|
40 |
for f in falls: |
|
|
41 |
tempdf = s[s['activity'] == f] |
|
|
42 |
fall_list.append(tempdf) |
|
|
43 |
|
|
|
44 |
|
|
|
45 |
# Titles for fall activities |
|
|
46 |
fall_titles = ['Fall forward while walking caused by a slip', 'Fall backward while walking caused by a slip', |
|
|
47 |
'Lateral fall while walking caused by a slip', 'Fall forward while walking caused by a trip', |
|
|
48 |
'Fall forward while jogging caused by a trip', 'Vertical fall while walking caused by fainting', |
|
|
49 |
'Fall while walking, with use of hands in a table to dampen fall, caused by fainting', |
|
|
50 |
'Fall forward when trying to get up', 'Lateral fall when trying to get up', |
|
|
51 |
'Fall forward when trying to sit down', 'Fall backward when trying to sit down', 'Lateral fall when trying to sit down', |
|
|
52 |
'Fall forward while sitting, caused by fainting or falling asleep', |
|
|
53 |
'Fall backward while sitting, caused by fainting or falling asleep', |
|
|
54 |
'Lateral fall while sitting, caused by fainting or falling asleep'] |
|
|
55 |
# Titles for ADLs |
|
|
56 |
adl_titles = ['Walking slowly', 'Walking quickly', 'Jogging slowly', 'Jogging quickly', 'Walking upstairs and downstairs slowly', |
|
|
57 |
'Walking upstairs and downstairs quickly','Slowly sit in a half height chair, wait a moment, and up slowly', |
|
|
58 |
'Quickly sit in a half height chair, wait a moment, and up quickly', |
|
|
59 |
'Slowly sit in a low height chair, wait a moment, and up slowly','Quickly sit in a low height chair, wait a moment, and up quickly', |
|
|
60 |
'Sitting a moment, trying to get up, and collapse into a chair', |
|
|
61 |
'Sitting a moment, lying slowly, wait a moment, and sit again','Sitting a moment, lying quickly, wait a moment, and sit again', |
|
|
62 |
'Being on oneís back change to lateral position, wait a moment, and change to oneís back', |
|
|
63 |
'Standing, slowly bending at knees, and getting up', 'Standing, slowly bending without bending knees, and getting up', |
|
|
64 |
'Standing, get into a car, remain seated and get out of the car','Stumble while walking', |
|
|
65 |
'Gently jump without falling (trying to reach a high object)'] |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
from scipy.signal import butter, lfilter, freqz |
|
|
69 |
|
|
|
70 |
# Filter requirements. |
|
|
71 |
order = 4 |
|
|
72 |
fs = 200.0 # sample rate, Hz |
|
|
73 |
cutoff = 5.0 # desired cutoff frequency of the filter, Hz |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
# From?????? |
|
|
77 |
def butter_lowpass(cutoff, fs, order=5): |
|
|
78 |
nyq = 0.5 * fs |
|
|
79 |
normal_cutoff = cutoff / nyq |
|
|
80 |
b, a = butter(order, normal_cutoff, btype='low', analog=False) |
|
|
81 |
return b, a |
|
|
82 |
|
|
|
83 |
|
|
|
84 |
def butter_lowpass_filter(data, cutoff, fs, order=5): |
|
|
85 |
b, a = butter_lowpass(cutoff, fs, order=order) |
|
|
86 |
y = lfilter(b, a, data) |
|
|
87 |
return y |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
# Codes for trials |
|
|
91 |
trials = ['R01', 'R02', 'R03', 'R04', 'R05'] |
|
|
92 |
horiz_std_mag_THRESHOLD = 170 # Set threshold for Horizontal Standard Deviation Magnitude |
|
|
93 |
horiz_vec_mag_THRESHOLD = 400 # Set threshold for Horizontal Sum Vector Magnitude |
|
|
94 |
vector_mag_THRESHOLD = 900 # Set threshold for Sum Vector Magnitude |
|
|
95 |
gyro_horiz_std_THRESHOLD = 170 |
|
|
96 |
|
|
|
97 |
# Lists to hold prepared fall and ADL dataframes |
|
|
98 |
fall_df_list = [] |
|
|
99 |
adl_df_list = [] |
|
|
100 |
# Lists to hold sliding windows of 1.25s with a 50% overlap |
|
|
101 |
windowList_adl = [] |
|
|
102 |
windowList_fall = [] |
|
|
103 |
|
|
|
104 |
windowHighlights_adl = [] |
|
|
105 |
falseAlarms = [] |
|
|
106 |
|
|
|
107 |
|
|
|
108 |
# Method that takes in a dataframe and kind=['f', 'a'] and stores sliding windows of 1.25s |
|
|
109 |
# with a 50% overlap |
|
|
110 |
def sliding_window(dataframe, kind): |
|
|
111 |
k = 0 |
|
|
112 |
for i in range(0, len(dataframe) - 256, 128): |
|
|
113 |
w = 256 # Size of sliding window (256 points at 200Hz = 1.25 seconds) |
|
|
114 |
w1 = dataframe.iloc[i:i + w][:] |
|
|
115 |
|
|
|
116 |
if ((w1['horiz_std_mag9'].max() >= horiz_std_mag_THRESHOLD) * 1 + ( |
|
|
117 |
w1['horiz_vector_mag9'].max() >= horiz_vec_mag_THRESHOLD) * 1 |
|
|
118 |
+ (w1['vm'].max() >= vector_mag_THRESHOLD) * 1 + ( |
|
|
119 |
w1['gyro_horiz_std_mag'].max() >= gyro_horiz_std_THRESHOLD) * 1 >= 3): |
|
|
120 |
w1['Fall'] = 1 |
|
|
121 |
|
|
|
122 |
if kind == 'a': |
|
|
123 |
falseAlarms.append(k) |
|
|
124 |
print('k:', k, 'i:', i, '\n') |
|
|
125 |
print('False Alarm: hstd9:', w1['horiz_std_mag9'].max(), 'hvm9:', w1['horiz_vector_mag9'].max()) |
|
|
126 |
print('\tvm:', w1['vm'].max(), 'hvm9:', 'ghstd:', w1['gyro_horiz_std_mag'].max()) |
|
|
127 |
windowHighlights_adl.append(1) |
|
|
128 |
else: |
|
|
129 |
w1['Fall'] = 0 |
|
|
130 |
if kind == 'a': |
|
|
131 |
windowHighlights_adl.append(0) |
|
|
132 |
|
|
|
133 |
if kind == 'f': # If fall |
|
|
134 |
windowList_fall.append(w1) |
|
|
135 |
else: # If ADL |
|
|
136 |
windowList_adl.append(w1) |
|
|
137 |
k += 1 |
|
|
138 |
|
|
|
139 |
|
|
|
140 |
# Method that takes in a kind=['f', 'a'] and prepares respective data |
|
|
141 |
def prepare_data(kind): |
|
|
142 |
if kind == 'f': # If fall |
|
|
143 |
putList, takeList = fall_df_list, fall_list |
|
|
144 |
else: # If ADL |
|
|
145 |
putList, takeList = adl_df_list, adl_list |
|
|
146 |
|
|
|
147 |
start = time.time() # Timer for testing |
|
|
148 |
|
|
|
149 |
for i in range(0, len(takeList)): # Iterate through the takeList (fall_list or adl_list) |
|
|
150 |
|
|
|
151 |
my_df = takeList[i].copy() # copy dataframe from list |
|
|
152 |
new_df = pd.DataFrame() # placeholder |
|
|
153 |
|
|
|
154 |
for trial in trials: # Iterate through trials (1-5) |
|
|
155 |
|
|
|
156 |
# Get relevant trial data |
|
|
157 |
trial_df = my_df[my_df['trial'] == trial] |
|
|
158 |
|
|
|
159 |
tempdf = pd.DataFrame() # dataframe for putting into filter |
|
|
160 |
# Low Pass Buttersworth Filter and remove bias |
|
|
161 |
tempdf['ax'], tempdf['ay'], tempdf['az'] = trial_df['x1'], trial_df['y1'], trial_df['z1'] |
|
|
162 |
tempdf = tempdf.reset_index(drop=True) |
|
|
163 |
tempdf['fx'] = pd.Series(butter_lowpass_filter(trial_df['x1'], cutoff, fs, order)) |
|
|
164 |
tempdf['fy'] = pd.Series(butter_lowpass_filter(trial_df['y1'], cutoff, fs, order)) |
|
|
165 |
tempdf['fz'] = pd.Series(butter_lowpass_filter(trial_df['z1'], cutoff, fs, order)) |
|
|
166 |
tempdf['bx'] = tempdf['fx'].diff() |
|
|
167 |
tempdf['by'] = tempdf['fy'].diff() |
|
|
168 |
tempdf['bz'] = tempdf['fz'].diff() |
|
|
169 |
|
|
|
170 |
tempdf = tempdf.reset_index(drop=True) |
|
|
171 |
trial_df = trial_df.reset_index(drop=True) |
|
|
172 |
tempdf['gx'], tempdf['gy'], tempdf['gz'] = trial_df['x2'], trial_df['y2'], trial_df['z2'] |
|
|
173 |
|
|
|
174 |
# Rolling averages |
|
|
175 |
tempdf['y_roll'] = pd.Series(tempdf['by'].rolling(200).mean()) |
|
|
176 |
tempdf['fy_roll'] = pd.Series(tempdf['fy'].rolling(200).mean()) |
|
|
177 |
tempdf['gy_roll'] = pd.Series(tempdf['by'].rolling(200).mean()) |
|
|
178 |
|
|
|
179 |
# Rolling standard deviations |
|
|
180 |
tempdf['bx_std'] = tempdf['bx'].rolling(200).std() |
|
|
181 |
tempdf['by_std'] = tempdf['by'].rolling(200).std() |
|
|
182 |
tempdf['bz_std'] = tempdf['bz'].rolling(200).std() |
|
|
183 |
tempdf['fx_std'] = tempdf['fx'].rolling(200).std() |
|
|
184 |
tempdf['fy_std'] = tempdf['fy'].rolling(200).std() |
|
|
185 |
tempdf['fz_std'] = tempdf['fz'].rolling(200).std() |
|
|
186 |
tempdf['gx_std'] = tempdf['fx'].rolling(200).std() |
|
|
187 |
tempdf['gy_std'] = tempdf['fy'].rolling(200).std() |
|
|
188 |
tempdf['gz_std'] = tempdf['fz'].rolling(200).std() |
|
|
189 |
|
|
|
190 |
|
|
|
191 |
# Integral stuff |
|
|
192 |
tempdf['xsum'] = pd.expanding_sum(((abs(tempdf['ax']).rolling(2).sum() / 2) * (1 / 200)).fillna(0)) |
|
|
193 |
tempdf['ysum'] = pd.expanding_sum(((abs(tempdf['ay']).rolling(2).sum() / 2) * (1 / 200)).fillna(0)) |
|
|
194 |
tempdf['zsum'] = pd.expanding_sum(((abs(tempdf['az']).rolling(2).sum() / 2) * (1 / 200)).fillna(0)) |
|
|
195 |
tempdf['time'] = 1 / 200 |
|
|
196 |
tempdf['time'] = pd.expanding_sum(tempdf['time']) |
|
|
197 |
# C10 Signal Magnitude Area |
|
|
198 |
tempdf['SigMagArea'] = (tempdf['xsum'] + tempdf['ysum'] + tempdf['zsum']) / tempdf['time'] |
|
|
199 |
# C11 |
|
|
200 |
tempdf['HorizSigMagArea'] = (tempdf['xsum'] + tempdf['zsum']) / tempdf['time'] |
|
|
201 |
# Sum vector magnitude |
|
|
202 |
tempdf['vm'] = np.sqrt(tempdf['fx'] ** 2 + tempdf['fy'] ** 2 + tempdf['fz'] ** 2) |
|
|
203 |
# Maximum peak to peak acceleration amplitude |
|
|
204 |
tempdf['Amax'] = (tempdf['vm'].rolling(200).max()) |
|
|
205 |
tempdf['Amin'] = (tempdf['vm'].rolling(200).min()) |
|
|
206 |
# C3 |
|
|
207 |
tempdf['peak_diff'] = tempdf['Amax'] - tempdf['Amin'] |
|
|
208 |
# Angle from horizontal to z-axis |
|
|
209 |
tempdf['angle_from_horiz'] = np.arctan2(np.sqrt(tempdf['fx'] ** 2 + tempdf['fz'] ** 2), -tempdf['fy']) * 180 / np.pi |
|
|
210 |
tempdf['angle_std'] = pd.rolling_std(tempdf['angle_from_horiz'], 200) |
|
|
211 |
|
|
|
212 |
# had to make versions of this to put into sliding window, will change once I |
|
|
213 |
# confirm they're the same as the others below |
|
|
214 |
tempdf['horiz_std_mag9'] = np.sqrt(tempdf['fx_std'] ** 2 + tempdf['fz_std'] ** 2) |
|
|
215 |
tempdf['horiz_vector_mag9'] = np.sqrt(tempdf['fx'] ** 2 + tempdf['fz'] ** 2) |
|
|
216 |
tempdf['std_mag9'] = np.sqrt(tempdf['fx_std'] ** 2 + tempdf['fy_std'] ** 2 + tempdf['fz_std'] ** 2) |
|
|
217 |
tempdf['diff_std_mag9'] = np.sqrt(tempdf['bx_std'] ** 2 + tempdf['by_std'] ** 2 + tempdf['bz_std'] ** 2) |
|
|
218 |
tempdf['horiz_mag2'] = np.sqrt(tempdf['bx'] ** 2 + tempdf['bz'] ** 2) |
|
|
219 |
tempdf['horiz_std_mag2'] = np.sqrt(tempdf['bx_std'] ** 2 + tempdf['bz_std'] ** 2) |
|
|
220 |
tempdf['vector_mag2'] = np.sqrt(tempdf['bx'] ** 2 + tempdf['by'] ** 2 + tempdf['bz'] ** 2) |
|
|
221 |
|
|
|
222 |
tempdf['gyro_horiz_std_mag'] = np.sqrt(tempdf['gx_std'] ** 2 + tempdf['gz_std'] ** 2) |
|
|
223 |
tempdf['gyro_vector_mag'] = np.sqrt(tempdf['gx'] ** 2 + tempdf['gy'] ** 2 + tempdf['gz'] ** 2) |
|
|
224 |
tempdf['gyro_horiz_mag'] = np.sqrt(tempdf['gx'] ** 2 + tempdf['gz'] ** 2) |
|
|
225 |
tempdf['gyro_std_mag'] = np.sqrt(tempdf['gx_std'] ** 2 + tempdf['gy_std'] ** 2 + tempdf['gz_std'] ** 2) |
|
|
226 |
|
|
|
227 |
tempdf = pd.concat( |
|
|
228 |
[tempdf.reset_index(drop=True), trial_df[['activity', 'subject', 'trial']].reset_index(drop=True)], |
|
|
229 |
axis=1) |
|
|
230 |
new_df = pd.concat([new_df.reset_index(drop=True), tempdf]) |
|
|
231 |
|
|
|
232 |
sliding_window(tempdf, kind) |
|
|
233 |
|
|
|
234 |
# differential vector mag |
|
|
235 |
new_df['vector_mag'] = np.sqrt(new_df['fx'] ** 2 + new_df['fy'] ** 2 + new_df['fz'] ** 2) |
|
|
236 |
# C2 |
|
|
237 |
new_df['horiz_mag'] = np.sqrt(new_df['fx'] ** 2 + new_df['fz'] ** 2) |
|
|
238 |
# |
|
|
239 |
new_df['vert'] = new_df['by'] - new_df['y_roll'] |
|
|
240 |
new_df['vert2'] = new_df['ay'] - new_df['y_roll'] |
|
|
241 |
new_df['vert3'] = new_df['fy'] - new_df['fy_roll'] |
|
|
242 |
# C9 |
|
|
243 |
new_df['std_mag2'] = np.sqrt(new_df['bx_std'] ** 2 + new_df['by_std'] ** 2 + new_df['bz_std'] ** 2) |
|
|
244 |
|
|
|
245 |
putList.append(new_df.fillna(0)) |
|
|
246 |
|
|
|
247 |
print('Completed... It took', time.time() - start, 'seconds.') |
|
|
248 |
|
|
|
249 |
|
|
|
250 |
prepare_data('f') # Prepare Fall Data |
|
|
251 |
prepare_data('a') # Prepare ADL Data |
|
|
252 |
|
|
|
253 |
print('ADL dataframes:',len(adl_df_list), '\t\tFall dataframes:', len(fall_df_list)) |
|
|
254 |
print('ADL windows:', len(windowList_adl), '\t\tFall windows:', len(windowList_fall)) |
|
|
255 |
|
|
|
256 |
wList_f = windowList_fall[:] # Copy of fall window list |
|
|
257 |
wList_a = windowList_adl[:] |
|
|
258 |
|
|
|
259 |
# putting together falls and adls |
|
|
260 |
fall_df = pd.concat(fall_df_list) |
|
|
261 |
adl_df = pd.concat(adl_df_list) |
|
|
262 |
all_df = pd.concat([fall_df, adl_df]).fillna(0) # Filling nulls with zeroes |
|
|
263 |
|
|
|
264 |
horiz_std_mag_THRESHOLD = 155 # Set threshold for Horizontal Standard Deviation Magnitude |
|
|
265 |
horiz_vec_mag_THRESHOLD = 400 # Set threshold for Horizontal Sum Vector Magnitude |
|
|
266 |
vector_mag_THRESHOLD = 750 # Set threshold for Sum Vector Magnitude |
|
|
267 |
gyro_horiz_std_THRESHOLD = 150 |
|
|
268 |
belowThresh = 0 # Keep track of windows below thresholds |
|
|
269 |
aboveThresh = 0 # Keep track of windows above thresholds |
|
|
270 |
i = 0 # placeholder |
|
|
271 |
lastActInd = 0 # Last activity index |
|
|
272 |
lastTrialNum = 0 # Last trial number |
|
|
273 |
fallTrialList = [] # |
|
|
274 |
listInFallTrialList = [] # |
|
|
275 |
missedFalls = 0 |
|
|
276 |
|
|
|
277 |
for window in wList_f: # Iterate through fall windows |
|
|
278 |
windNum = i # Set window index |
|
|
279 |
respWindNum = i % 22 # Set respective window index (0-21) |
|
|
280 |
activityIndex = int(i / (22 * 5)) # Calculate activity index (0-13/14?) |
|
|
281 |
trialNum = (int(i / 22)) % 5 # Calculate trial number (0-4) |
|
|
282 |
|
|
|
283 |
# Custom setting for thresholds, currently need to pass 2/3 thresholds to pass |
|
|
284 |
if ((window['horiz_std_mag9'].max() >= horiz_std_mag_THRESHOLD) * 1 + ( |
|
|
285 |
window['horiz_vector_mag9'].max() >= horiz_vec_mag_THRESHOLD) * 1 |
|
|
286 |
+ (window['vm'].max() >= vector_mag_THRESHOLD) * 1 + ( |
|
|
287 |
window['gyro_horiz_std_mag'].max() >= gyro_horiz_std_THRESHOLD) * 1 >= 1): |
|
|
288 |
|
|
|
289 |
aboveThresh += 1 |
|
|
290 |
|
|
|
291 |
if (activityIndex == lastActInd): |
|
|
292 |
if (trialNum == lastTrialNum): |
|
|
293 |
listInFallTrialList.append(respWindNum) |
|
|
294 |
lastTrialNum = trialNum |
|
|
295 |
lastActInd = activityIndex |
|
|
296 |
else: |
|
|
297 |
fallTrialList.append(listInFallTrialList) |
|
|
298 |
listInFallTrialList = [] |
|
|
299 |
listInFallTrialList.append(respWindNum) |
|
|
300 |
lastActInd = activityIndex |
|
|
301 |
diff = (trialNum - lastTrialNum) |
|
|
302 |
absdiff = diff % 5 |
|
|
303 |
|
|
|
304 |
if absdiff > 1: |
|
|
305 |
for j in range(absdiff - 1): |
|
|
306 |
fallTrialList.append([]) |
|
|
307 |
missedFalls += 1 |
|
|
308 |
|
|
|
309 |
lastTrialNum = trialNum |
|
|
310 |
else: |
|
|
311 |
diff = (trialNum - lastTrialNum) |
|
|
312 |
absdiff = diff % 5 |
|
|
313 |
activity_diff = (activityIndex - lastActInd) |
|
|
314 |
|
|
|
315 |
fallTrialList.append(listInFallTrialList) |
|
|
316 |
if (activity_diff > 0) & (trialNum != 0): |
|
|
317 |
for k in range(3 - absdiff): |
|
|
318 |
fallTrialList.append([]) |
|
|
319 |
missedFalls += 1 |
|
|
320 |
if absdiff > 1: |
|
|
321 |
for j in range(absdiff - 1): |
|
|
322 |
fallTrialList.append([]) |
|
|
323 |
missedFalls += 1 |
|
|
324 |
listInFallTrialList = [] |
|
|
325 |
listInFallTrialList.append(respWindNum) |
|
|
326 |
lastTrialNum = trialNum |
|
|
327 |
lastActInd = activityIndex |
|
|
328 |
else: |
|
|
329 |
belowThresh += 1 |
|
|
330 |
i += 1 |
|
|
331 |
|
|
|
332 |
fallTrialList.append(listInFallTrialList) |
|
|
333 |
|
|
|
334 |
print('below:', belowThresh) |
|
|
335 |
print('above:', aboveThresh) |
|
|
336 |
|
|
|
337 |
|
|
|
338 |
FTL = fallTrialList[:] |
|
|
339 |
print('len fallTrialList:', len(FTL)) |
|
|
340 |
print('missed falls:', missedFalls) |
|
|
341 |
print('false alarms:', len(falseAlarms), ':', falseAlarms) |
|
|
342 |
|
|
|
343 |
|
|
|
344 |
fall_windows = pd.concat(wList_f) |
|
|
345 |
adl_windows = pd.concat(wList_a) |
|
|
346 |
all_windows = pd.concat([fall_windows, adl_windows]) |
|
|
347 |
|
|
|
348 |
|
|
|
349 |
print('COMPLETED') |
|
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
# Titles for fall activities |
|
|
354 |
fall_titles = ['Fall forward while walking caused by a slip', 'Fall backward while walking caused by a slip', |
|
|
355 |
'Lateral fall while walking caused by a slip', 'Fall forward while walking caused by a trip', |
|
|
356 |
'Fall forward while jogging caused by a trip', 'Vertical fall while walking caused by fainting', |
|
|
357 |
'Fall while walking, with use of hands in a table to dampen fall, caused by fainting', |
|
|
358 |
'Fall forward when trying to get up', 'Lateral fall when trying to get up', |
|
|
359 |
'Fall forward when trying to sit down', 'Fall backward when trying to sit down', 'Lateral fall when trying to sit down', |
|
|
360 |
'Fall forward while sitting, caused by fainting or falling asleep', |
|
|
361 |
'Fall backward while sitting, caused by fainting or falling asleep', |
|
|
362 |
'Lateral fall while sitting, caused by fainting or falling asleep'] |
|
|
363 |
# Titles for ADLs |
|
|
364 |
adl_titles = ['Walking slowly', 'Walking quickly', 'Jogging slowly', 'Jogging quickly', 'Walking upstairs and downstairs slowly', |
|
|
365 |
'Walking upstairs and downstairs quickly','Slowly sit in a half height chair, wait a moment, and up slowly', |
|
|
366 |
'Quickly sit in a half height chair, wait a moment, and up quickly', |
|
|
367 |
'Slowly sit in a low height chair, wait a moment, and up slowly','Quickly sit in a low height chair, wait a moment, and up quickly', |
|
|
368 |
'Sitting a moment, trying to get up, and collapse into a chair', |
|
|
369 |
'Sitting a moment, lying slowly, wait a moment, and sit again','Sitting a moment, lying quickly, wait a moment, and sit again', |
|
|
370 |
'Being on oneís back change to lateral position, wait a moment, and change to oneís back', |
|
|
371 |
'Standing, slowly bending at knees, and getting up', 'Standing, slowly bending without bending knees, and getting up', |
|
|
372 |
'Standing, get into a car, remain seated and get out of the car','Stumble while walking', |
|
|
373 |
'Gently jump without falling (trying to reach a high object)'] |
|
|
374 |
|
|
|
375 |
dailies = ['D01', 'D02', 'D03', 'D04', 'D05', 'D06', 'D07', 'D08', 'D09', 'D10', 'D11', 'D12', 'D13', 'D14', 'D15', |
|
|
376 |
'D16', 'D17', 'D18', 'D19'] |
|
|
377 |
falls = ['F01', 'F02', 'F03', 'F04', 'F05', 'F06', 'F07', 'F08', 'F09', 'F10', 'F11', 'F12', 'F13', 'F14', 'F15'] |
|
|
378 |
|
|
|
379 |
fs = 200.0 # frequency samplet |
|
|
380 |
subjectCodes = ['SA01', 'SA02', 'SA03', 'SA04'] |
|
|
381 |
|
|
|
382 |
|
|
|
383 |
def get_trial_time(index, kind): |
|
|
384 |
r = 5 |
|
|
385 |
if kind == 'f': |
|
|
386 |
n = 3000.0 |
|
|
387 |
t = np.linspace(0, 15.0, n, endpoint=False) |
|
|
388 |
else: |
|
|
389 |
if index in list(range(0, 4)): |
|
|
390 |
T = 100 # seconds |
|
|
391 |
elif index in [4, 5, 16]: |
|
|
392 |
T = 25 |
|
|
393 |
else: |
|
|
394 |
T = 12 |
|
|
395 |
|
|
|
396 |
n = int(T * fs) # total number of samples |
|
|
397 |
t = np.linspace(0, T, n, endpoint=False) |
|
|
398 |
|
|
|
399 |
if index <= 3: |
|
|
400 |
r = 1 |
|
|
401 |
|
|
|
402 |
return t, int(n), int(r) |
|
|
403 |
|
|
|
404 |
|
|
|
405 |
## index w <-- activity = F[w+1], eg. index=4 gives a df that contains activity F05 |
|
|
406 |
## This makes sense since data_list[0] contains activity F01 |
|
|
407 |
|
|
|
408 |
def plot_trials(index, subjectIndex, kind): |
|
|
409 |
if kind == 'f': |
|
|
410 |
correctList, correctTitles, correctCodes = fall_df_list, fall_titles, falls |
|
|
411 |
else: |
|
|
412 |
correctList, correctTitles, correctCodes = adl_df_list, adl_titles, dailies |
|
|
413 |
|
|
|
414 |
new_df = correctList[index + 15 * subjectIndex] |
|
|
415 |
t, n, r = get_trial_time(index, kind) |
|
|
416 |
|
|
|
417 |
T = int(n / fs) |
|
|
418 |
l = list(range(0, int(1000 * T), int(1000 * 0.625))) |
|
|
419 |
l = np.array(l) / 1000 |
|
|
420 |
xcoords = list(l) |
|
|
421 |
|
|
|
422 |
plt.figure(figsize=(15, 2 * r)) |
|
|
423 |
|
|
|
424 |
for i in range(0, r): |
|
|
425 |
curr_df = new_df[i * n:i * n + n] |
|
|
426 |
if len(curr_df) != 0: |
|
|
427 |
if len(curr_df) != n: |
|
|
428 |
curr_df = curr_df.append(curr_df.iloc[len(curr_df) - 1]) |
|
|
429 |
plt.subplot(r, 1, i + 1) |
|
|
430 |
plt.plot(t, curr_df['fx'], 'b-', label='x') |
|
|
431 |
plt.plot(t, curr_df['fy'], 'r-', label='y') |
|
|
432 |
plt.plot(t, curr_df['fz'], 'y-', label='z') |
|
|
433 |
for xc in xcoords: |
|
|
434 |
plt.axvline(x=xc) |
|
|
435 |
plt.grid() |
|
|
436 |
labs = list(range(0, len(xcoords) - 2)) |
|
|
437 |
plt.xticks(xcoords, labs) |
|
|
438 |
plt.legend() |
|
|
439 |
plt.ylabel('trial' + str(i + 1)) |
|
|
440 |
if i == 0: |
|
|
441 |
plt.title(subjectCodes[subjectIndex] + ' - ' + correctCodes[index] + ':' + correctTitles[index]) |
|
|
442 |
|
|
|
443 |
if kind == 'f': |
|
|
444 |
tempA = FTL[5 * index + i + 75 * subjectIndex] |
|
|
445 |
if len(tempA) > 0: |
|
|
446 |
shadeStart = tempA[0] |
|
|
447 |
shadeFin = tempA[len(tempA) - 1] + 2 |
|
|
448 |
plt.axvspan(shadeStart * .625, shadeFin * .625, color='red', alpha=0.5) |
|
|
449 |
|
|
|
450 |
plt.subplots_adjust(hspace=0.35) |
|
|
451 |
plt.show() |
|
|
452 |
|
|
|
453 |
|
|
|
454 |
def plot_one_from_each(index, kind): |
|
|
455 |
if kind == 'f': |
|
|
456 |
correctList, correctTitles, correctCodes = fall_df_list, fall_titles, falls |
|
|
457 |
else: |
|
|
458 |
correctList, correctTitles, correctCodes = adl_df_list, adl_titles, dailies |
|
|
459 |
|
|
|
460 |
plt.figure(figsize=(15, 10)) |
|
|
461 |
|
|
|
462 |
for i in range(0, 5): |
|
|
463 |
if (index + i + 1) == len(correctList): return |
|
|
464 |
new_df = correctList[index + i] |
|
|
465 |
t, n, r = get_trial_time(index, kind) |
|
|
466 |
|
|
|
467 |
try: |
|
|
468 |
curr_df = new_df[0:n] |
|
|
469 |
if len(curr_df) != n: |
|
|
470 |
curr_df = curr_df.append(curr_df.iloc[len(curr_df) - 1]) |
|
|
471 |
plt.subplot(5, 1, i + 1) |
|
|
472 |
plt.plot(t, curr_df['bx'], 'b-', label='x') |
|
|
473 |
plt.plot(t, curr_df['by'], 'r-', label='y') |
|
|
474 |
plt.plot(t, curr_df['bz'], 'y-', label='z') |
|
|
475 |
plt.grid() |
|
|
476 |
plt.legend() |
|
|
477 |
plt.ylabel('Acc') |
|
|
478 |
plt.title(correctCodes[index + i] + ' ' + correctTitles[index + i]) |
|
|
479 |
except: |
|
|
480 |
print('') |
|
|
481 |
|
|
|
482 |
plt.subplots_adjust(hspace=0.4) |
|
|
483 |
plt.show() |
|
|
484 |
|
|
|
485 |
|
|
|
486 |
def plot_feats(index, tri, kind): |
|
|
487 |
if kind == 'f': |
|
|
488 |
correctList, correctTitles, correctCodes = fall_df_list, fall_titles, falls |
|
|
489 |
else: |
|
|
490 |
correctList, correctTitles, correctCodes = adl_df_list, adl_titles, dailies |
|
|
491 |
|
|
|
492 |
t, n, r = get_trial_time(index, kind) |
|
|
493 |
|
|
|
494 |
new_df = correctList[index] |
|
|
495 |
curr_df = new_df[tri * n:tri * n + n] |
|
|
496 |
|
|
|
497 |
feat_list = ['vector_mag', 'vector_mag2', 'horiz_mag', 'vert', 'std_mag9', 'horiz_std_mag9', |
|
|
498 |
'peak_diff', 'HorizSigMagArea', 'angle_from_horiz', 'gyro_horiz_std_mag'] |
|
|
499 |
colour_list = ['b-', 'r-', 'k-', 'c-', 'C2', 'C4', 'C1', 'C5', 'C6', 'C7'] |
|
|
500 |
|
|
|
501 |
x = len(feat_list) |
|
|
502 |
plt.figure(figsize=(15, 2 * x)) |
|
|
503 |
|
|
|
504 |
for i, feat, colour in zip(range(0, x), feat_list, colour_list): |
|
|
505 |
plt.subplot(x, 1, i + 1) |
|
|
506 |
plt.plot(t, curr_df[feat], colour, label=feat) |
|
|
507 |
plt.grid() |
|
|
508 |
plt.legend() |
|
|
509 |
plt.ylabel(feat) |
|
|
510 |
if i == 0: plt.title(correctCodes[index] + ' ' + correctTitles[index]) |
|
|
511 |
|
|
|
512 |
plt.subplots_adjust(hspace=0.35) |
|
|
513 |
plt.show() |
|
|
514 |
|
|
|
515 |
|
|
|
516 |
def plot_trial(index, tri, kind): |
|
|
517 |
if kind == 'f': |
|
|
518 |
correctList, correctTitles, correctCodes = fall_df_list, fall_titles, falls |
|
|
519 |
else: |
|
|
520 |
correctList, correctTitles, correctCodes = adl_df_list, adl_titles, dailies |
|
|
521 |
|
|
|
522 |
new_df = correctList[index] |
|
|
523 |
|
|
|
524 |
t, n, r = get_trial_time(index, kind) |
|
|
525 |
plt.figure(figsize=(15, 24)) |
|
|
526 |
|
|
|
527 |
xtypes = ['ax', 'fx', 'bx', 'gx'] |
|
|
528 |
ytypes = ['ay', 'fy', 'by', 'gy'] |
|
|
529 |
ztypes = ['az', 'fz', 'bz', 'gz'] |
|
|
530 |
ylabs = ['raw acc (m/s^2)', 'filtered', 'filt differential', 'gyro'] |
|
|
531 |
for i in range(0, len(xtypes)): |
|
|
532 |
curr_df = new_df[tri * n:tri * n + n] |
|
|
533 |
if len(curr_df) != n: |
|
|
534 |
curr_df = curr_df.append(curr_df.iloc[len(curr_df) - 1]) |
|
|
535 |
plt.subplot(12, 1, i + 1) |
|
|
536 |
plt.plot(t, curr_df[xtypes[i]], 'b-', label='x') |
|
|
537 |
plt.plot(t, curr_df[ytypes[i]], 'r-', label='y') |
|
|
538 |
plt.plot(t, curr_df[ztypes[i]], 'y-', label='z') |
|
|
539 |
plt.grid() |
|
|
540 |
plt.legend() |
|
|
541 |
plt.ylabel(ylabs[i]) |
|
|
542 |
if i == 0: plt.title(correctCodes[index] + ' ' + correctTitles[index]) |
|
|
543 |
|
|
|
544 |
curr_df = correctList[index][0:int(n)] |
|
|
545 |
if len(curr_df) != n: |
|
|
546 |
curr_df = curr_df.append(curr_df.iloc[len(curr_df) - 1]) |
|
|
547 |
|
|
|
548 |
feat_list = ['vector_mag', 'vector_mag2', 'horiz_mag', 'vert', 'std_mag9', 'horiz_std_mag9', |
|
|
549 |
'peak_diff', 'HorizSigMagArea', 'angle_from_horiz', 'gyro_horiz_std_mag'] |
|
|
550 |
colour_list = ['b-', 'r-', 'k-', 'c-', 'C2', 'C4', 'C1', 'C5', 'C6', 'C7'] |
|
|
551 |
|
|
|
552 |
x = len(feat_list) + len(xtypes) |
|
|
553 |
plt.figure(figsize=(15, 2 * x)) |
|
|
554 |
|
|
|
555 |
for i, feat, colour in zip(range(0, x), feat_list, colour_list): |
|
|
556 |
plt.subplot(12, 1, i + 1) |
|
|
557 |
plt.plot(t, curr_df[feat], colour, label=feat) |
|
|
558 |
plt.grid() |
|
|
559 |
plt.legend() |
|
|
560 |
plt.ylabel(feat) |
|
|
561 |
if i == 0: plt.title(correctCodes[index] + ' ' + correctTitles[index]) |
|
|
562 |
|
|
|
563 |
plt.subplots_adjust(hspace=0.35) |
|
|
564 |
plt.show() |
|
|
565 |
|