-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput_formatting.py
76 lines (57 loc) · 2.15 KB
/
input_formatting.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
import os
import glob
import h5py as h5
import numpy as np
from scipy.io import loadmat
def load_eeg(mat_file):
data = loadmat(mat_file)
return data['dataStruct'][0][0][0]
def extract_train_data():
print('extracting train data...')
in_dir = 'data/train'
out_file = 'data/train.h5'
data_key = 'data'
classes_key = 'classes'
patients_key = 'patients'
mat_files = glob.glob('%s/*/*.mat' % in_dir)
classes = [int(os.path.basename(mat_file).split('.')[-2].split('_')[-1]) for mat_file in mat_files]
patients = [int(os.path.basename(mat_file).split('.')[-2].split('_')[0]) for mat_file in mat_files]
n_samples = len(mat_files)
n_elements = 240000
n_ch = 16
with h5.File(out_file, 'w') as hdf:
hdf.create_dataset(data_key, (n_samples, n_elements, n_ch))
hdf.create_dataset(classes_key, data = classes)
hdf.create_dataset(patients_key, data = patients)
for i in range(n_samples):
if(i % 32 == 0):
print('%d/%d' % (i, n_samples))
data = load_eeg(mat_files[i])
hdf[data_key][i] = load_eeg(mat_files[i])
def extract_test_data():
print('extracting test data...')
in_dir = 'data/test'
out_file = 'data/test.h5'
data_key = 'data'
names_key = 'names'
patients_key = 'patients'
mat_files = glob.glob('%s/*/*.mat' % in_dir)
names = [os.path.basename(mat_file)for mat_file in mat_files]
patients = [name.split('.')[-2].split('_')[0] for name in names]
n_samples = len(mat_files)
n_elements = 240000
n_ch = 16
with h5.File(out_file, 'w') as hdf:
hdf.create_dataset(data_key, (n_samples, n_elements, n_ch))
hdf.create_dataset(names_key, data = names)
hdf.create_dataset(patients_key, data = patients)
for i in range(n_samples):
if(i % 32 == 0):
print('%d/%d' % (i, n_samples))
print(hdf[data_key][i].shape)
hdf[data_key][i] = load_eeg(mat_files[i])
def main():
extract_train_data()
extract_test_data()
if __name__ == '__main__':
main()