-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataModule.py
More file actions
201 lines (156 loc) · 6.91 KB
/
Copy pathDataModule.py
File metadata and controls
201 lines (156 loc) · 6.91 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os.path
import numpy as np
import pandas as pd
import pytorch_lightning as pl
import pywt
import scipy.signal as signal
import torch.utils.data
from constants import DATALOADER_NUM_WORKERS
from hyperparameters import BATCH_SIZE, DATA_LEN, DATASET_SHIFT_SIZE, SEQ_LEN, LOSS_INDICES, DATASET_UPSCALE_FACTOR
from torch.utils.data import DataLoader, random_split
from tqdm import tqdm
data_seq_len = SEQ_LEN - 1
def power_spectral_density(emg_data, fs=1000):
psd = []
for channel_data in emg_data.T:
f, pxx = signal.welch(channel_data, fs, nperseg=50)
psd.append(pxx)
return np.array(psd)
def median_frequency(psd, f):
mdf = []
for channel_psd in psd:
cumsum = np.cumsum(channel_psd)
median_idx = np.where(cumsum >= cumsum[-1] / 2)[0][0]
mdf.append(f[median_idx])
return np.array(mdf)
def mean_frequency(psd, f):
mnf = []
for channel_psd in psd:
mean_freq = np.sum(channel_psd * f) / np.sum(channel_psd)
mnf.append(mean_freq)
return np.array(mnf)
def peak_frequency(psd, f):
pf = []
for channel_psd in psd:
max_idx = np.argmax(channel_psd)
pf.append(f[max_idx])
return np.array(pf)
def extract_frequency_features(emg_data, fs=1000):
psd = power_spectral_density(emg_data, fs)
f = np.linspace(0, fs / 2, len(psd[0]))
mdf = median_frequency(psd, f)
mnf = mean_frequency(psd, f)
pf = peak_frequency(psd, f)
return mdf, mnf, pf
class SEMGDataset(torch.utils.data.Dataset):
def __init__(self, data_dir: str = "data"):
super().__init__()
# Read all csvs in data directory
data = []
for filename in os.listdir("data"):
if filename.endswith(".csv"):
data.append(pd.read_csv("data" + os.path.sep + filename, index_col=False))
# Load data from csv
data_np = []
for i in range(len(data)):
data_np.append(data[i].to_numpy(dtype=np.float32))
self.data = np.concatenate(data_np, axis=0)
self.train_percentage = 0.95
# Bandpass self.data from 10-25 Hz
fs = 50 # Sampling frequency
fmin = 20 # Minimum frequency to pass
fmax = 25 # Maximum frequency to pass
nyq = 0.5 * fs # Nyquist frequency
raw_samples = self.data[:, :8]
labels = self.data[:, 8:-3]
wrist_angles = self.data[:, -3:]
# Define filter parameters
# self.b, self.a = signal.butter(4, [fmin / nyq, fmax / nyq], btype='band')
self.samples = []
self.labels = []
self.wrist_angles = []
total_num_samples = self.data.shape[0]
for start in tqdm(range(0, int(total_num_samples * 0.1) - DATA_LEN, DATASET_SHIFT_SIZE)):
# for start in tqdm(range(0, total_num_samples - DATA_LEN, DATASET_SHIFT_SIZE)):
# Optional data augmentation
for aug in range(DATASET_UPSCALE_FACTOR):
sample = raw_samples[start : start + DATA_LEN].copy()
label = labels[start : start + DATA_LEN]
wrist_angle = wrist_angles[start : start + DATA_LEN]
if aug > 0:
std = aug * 0.5
# Augment data by adding noise to the sample
sample = sample + np.random.normal(0, std, sample.shape)
sample = self.preprocess_sample(sample, wrist_angle)
# Append to list
self.samples.append(sample)
self.labels.append(label)
self.wrist_angles.append(wrist_angle)
print("---")
print("Dataset length: ", self.__len__())
print("---")
def preprocess_sample(self, sample, wrist_angles):
# =============================================================================
# Preprocessing
# =============================================================================
# Apply filter to data along the first axis
# sample = signal.filtfilt(self.b, self.a, sample, axis=0)
coefficients_level4 = pywt.wavedec(sample, 'db2', 'smooth', level=4, axis=0)
cA, cD = coefficients_level4[0], coefficients_level4[1]
# Concat cA and cD
wavelet_features = np.concatenate((cA, cD), axis=0)
# Get features
# Time domain features
# Mean absolute value
mean_abs = np.mean(np.abs(sample), axis=0).reshape(1, -1)
# Root mean square
rms = np.sqrt(np.mean(np.square(sample), axis=0)).reshape(1, -1)
# Variance
var = np.var(sample, axis=0).reshape(1, -1)
mdf, mnf, pf = extract_frequency_features(sample, fs=50)
mdf = mdf.reshape(1, -1)
mnf = mnf.reshape(1, -1)
pf = pf.reshape(1, -1)
# Make array of zeros for wrist angle
wrist_angle = np.zeros(sample.shape)
wrist_angle[:, :3] = wrist_angles
# Prepend features to sample
sample = np.concatenate((mean_abs, rms, var, mdf, mnf, pf, wavelet_features, sample, wrist_angle), axis=0)
# replace all nan values with 0
sample = np.nan_to_num(sample, copy=False)
# Convert to float32
sample = sample.astype(np.float32)
return sample
def __getitem__(self, index):
sample = torch.tensor(self.samples[index], dtype=torch.float32)
label = torch.tensor(self.labels[index], dtype=torch.float32)
return {'sample': sample, 'label': label, 'wrist_angle': self.wrist_angles[index]}
def __len__(self):
return len(self.samples) * DATASET_UPSCALE_FACTOR - data_seq_len
class SEMGDataModule(pl.LightningDataModule):
def __init__(self, data_dir: str = "data", batch_size: int = 64):
super().__init__()
self.data_dir = data_dir
self.batch_size = batch_size
self.dataset = SEMGDataset(self.data_dir)
self.train = None
self.val = None
def setup(self, stage: str):
# Split dataset into train and validation
train_size = int(self.dataset.train_percentage * len(self.dataset))
val_size = len(self.dataset) - train_size
self.train, self.val = random_split(self.dataset, [train_size, val_size])
# Non-random split
# self.train = torch.utils.data.Subset(self.dataset, range(train_size))
# self.val = torch.utils.data.Subset(self.dataset, range(train_size, train_size + val_size))
def train_dataloader(self):
return DataLoader(self.train, batch_size=self.batch_size, num_workers=DATALOADER_NUM_WORKERS, shuffle=True, drop_last=True)
def val_dataloader(self):
return DataLoader(self.val, batch_size=self.batch_size, num_workers=DATALOADER_NUM_WORKERS, shuffle=False, drop_last=True)
# def test_dataloader(self):
# return DataLoader(self.test, batch_size=self.batch_size, num_workers=DATALOADER_NUM_WORKERS)
if __name__ == '__main__':
dm = SEMGDataModule(data_dir="data")
dm.setup(stage="fit")
# Get a sample from the dataset
item = dm.train[0]