-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAndriiMSc_General_Motif.py
More file actions
134 lines (113 loc) · 4.5 KB
/
AndriiMSc_General_Motif.py
File metadata and controls
134 lines (113 loc) · 4.5 KB
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
import numpy as np
import pandas as pd
from scipy.signal import find_peaks
import stumpy
import os
from scipy.signal import savgol_filter
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [30,15]
plt.rcParams['xtick.direction'] = 'out'
plt.rcParams["font.size"] = '20'
#upload the data
Ref_path = r''
Ref_files = os.listdir(Ref_path)
i = 0
Ref_acceleration = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
NumOfPeaks = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
acceleration = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
time = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
peaks_array_higher = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
peaks_array_lower = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
peaks_array_filtered = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
indx_array = [1,1,1,1,1,1,1,1,1,1]
profile_match_array = [1,1,1,1,1,1,1,1,1,1]
Ref_Acceleration_39 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
acceleration_39 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Resam_Acc = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Chopped_Acc = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
distance_profile_array = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Ref_Location = 'Ankle'
def Ref_SavGol_39 (i):
#applying the Sav_Gol filter
ACC_filtered = savgol_filter(Ref_df.averagea, 39, 3)
#the filtered signal is stored into an array
Ref_Acceleration_39[i] = ACC_filtered
return Ref_Acceleration_39[i]
for Ref_file in Ref_files:
if Ref_file.endswith(".xlsx") and Ref_file.startswith(Ref_Location):
Ref_df = pd.read_excel(os.path.join(Ref_path, Ref_file))
Ref_df.columns = ['time','stamp','battery', 'pressure','temperature','ax','ay','az','gx','gy','gz','mx','my','mz','averagea']
Ref_acceleration[i] = Ref_df.averagea
time[i] = Ref_df.time
Ref_Acceleration_39[i] = Ref_SavGol_39(i)
i += 1
#---------------------------------------------------------Motif Definiton---------------------------------------------------#
#ANKLE
gaitCyclePattern = Ref_Acceleration_39[0][564:942] #single dataset
#SHANK
#gaitCyclePattern = Ref_Acceleration_39[0][556:921] #single dataset
#FOOT
#gaitCyclePattern = Ref_Acceleration_39[0][483:844] #single dataset
#---------------------------------------------------------Matrix Profiling-------------------------------------------------------------#
def SavGol_39 (i):
#applying the Sav_Gol filter
ACC_filtered = savgol_filter(df.averagea, 39, 3)
#the filtered signal is stored into an array
acceleration_39[i] = ACC_filtered
#time[i] = df.time
return acceleration_39[i]
def PlotMatch(i):
plt.plot(acceleration[i], label = 'Filtered data: ' + str(i))
plt.plot(range(indx_array[i],indx_array[i] + len (gaitCyclePattern)), acceleration[i][indx_array[i]:indx_array[i]+len(gaitCyclePattern)], label = 'Motif', linewidth = 2)
plt.suptitle('Match in ' + name + "'s dataset number: " + str(i+1), fontsize='30')
plt.grid(True, 'both')
plt.legend()
plt.show()
path = r''
files = os.listdir(path)
location = 'Foot'
name = path.split('\\')
name = name[-1]
#Upload the data and apply Sav_Gol filter upon it
i = 0
for file in files:
if file.endswith(".xlsx") and file.startswith(location):
df = pd.read_excel(os.path.join(path, file))
df.columns = ['time','stamp','battery', 'pressure','temperature','ax','ay','az','gx','gy','gz','mx','my','mz','averagea']
acceleration[i] = SavGol_39(i)
i += 1
#Set the threshold based on the location of the sensor
if Ref_Location == 'Ankle':
y = 13
elif Ref_Location == 'Shank':
y = 12
elif Ref_Location == 'Foot':
y = 13
#Run the matrix profiling through all datasets
i = 0
for i in range (0,10):
distance_profile = stumpy.mass(gaitCyclePattern, acceleration[i])
distance_profile_array[i] = distance_profile
indx = np.argmin(distance_profile)
indx_array[i] = indx
i += 1
#Chop out the identified Motif similar dataset
i = 0
for i in range(0,10):
profile_match = acceleration[i][indx_array[i]:indx_array[i]+len(gaitCyclePattern)]
profile_match_array[i] = profile_match
i += 1
#Find peaks in the identified motif
i = 0
for i in range (0,10):
peaks,_ = find_peaks(profile_match_array[i], height=y)
peaks_array_filtered[i]=peaks
NumOfPeaks[i] = len(peaks)
i+=1
#Plot Motif over Raw data and print Peaks
i = 0
for i in range (0,10):
PlotMatch(i)
print('Peaks: ', peaks_array_filtered[i])
print('Number of Peaks in Profile Match: ', NumOfPeaks[i])
i += 1