forked from hb2kang-zz/sEMG_Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
139 lines (102 loc) · 3.45 KB
/
preprocess.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
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
## sEMG Plot Function from text file
class sEMGData:
def __init__(self, path):
self.path = path
# file name
f = open(path, 'r')
basename = os.path.basename(f.name)
if 'mar' in basename:
self.output = 'mar'
elif 'pie' in basename:
self.output = 'pie'
elif 'sen' in basename:
self.output = 'sen'
# invalid_raise - sk
# numpy array
self.array = np.genfromtxt(self.path, skip_header=7, invalid_raise=False)
# DataFrame
self.dataframe = pd.DataFrame(self.array, columns=['RF','BF','VM','ST','FX'])
def return_array(self):
return self.array
def return_df(self):
return self.dataframe
def return_output(self):
return self.output
def plot_RF(self):
plt.plot(self.dataframe['RF'])
plt.title('RF - rectus femoris')
plt.show()
def plot_BF(self):
plt.plot(self.dataframe['BF'])
plt.title('BF - biceps femoris')
plt.show()
def plot_VM(self):
plt.plot(self.dataframe['VM'])
plt.title('VM - vastus internus')
plt.show()
def plot_ST(self):
plt.plot(self.dataframe['ST'])
plt.title('ST - semitendinosus')
plt.show()
def plot_FX(self):
plt.plot(self.dataframe['FX'])
plt.title('FX - knee flexion ()')
plt.show()
def sEMG_plot(self):
# Plot individual sEMG data
plt.plot(self.dataframe['RF'])
plt.title('RF - rectus femoris')
plt.show()
plt.plot(self.dataframe['BF'])
plt.title('BF - biceps femoris')
plt.show()
plt.plot(self.dataframe['VM'])
plt.title('VM - vastus internus')
plt.show()
plt.plot(self.dataframe['ST'])
plt.title('ST - semitendinosus')
plt.show()
plt.plot(self.dataframe['FX'])
plt.title('FX - knee flexion ()')
plt.show()
# Preprocessing Function
def preprocess(folder):
input_array = []
classes = []
# iterates through folder
for filename in os.listdir(folder):
if filename.endswith('.txt'):
# instantiates each sEMG data
sEMG = sEMGData(folder + filename)
input_array.append(sEMG.return_array())
classes.append(sEMG.return_output())
# finds the max length of a sEMG sequence
max_len = max([len(i) for i in input_array])
for i in input_array:
if (len(i) == max_len):
largest_array = i
# list of padded sEMG numpy arrays
padded_array = []
# loop through input_array and append into new padded array
for i in input_array:
# zero numpy array
zero_array = np.zeros(largest_array.shape)
zero_array[:i.shape[0],:i.shape[1]] = i
padded_array.append(zero_array)
# convert to 3d numpy array
padded_array = np.dstack(padded_array)
# transpose array
padded_array = padded_array.transpose(2,0,1)
return padded_array, classes
# Plotting function - based on muscle group
def plot_dataset(input_array, class_array, muscle_group):
for i in range(0, len(input_array)):
df = pd.DataFrame(input_array[i], columns=['RF','BF','VM','ST','FX'])
# show plot of each rectus femoris sEMG data
plt.plot(df[muscle_group])
plt.title(muscle_group + ' - ' + class_array[i])
plt.show()