forked from aug08/4D-CRNN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataProcess_DEAP_1D_X89.py
90 lines (77 loc) · 3.6 KB
/
dataProcess_DEAP_1D_X89.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
import time
from sklearn import preprocessing
from scipy.signal import butter, lfilter
import scipy.io as sio
import numpy as np
import os
import math
import sys
def read_file(file):
file = sio.loadmat(file)
trial_data = file['data']
base_data = file["base_data"]
return trial_data, base_data, file["arousal_labels"], file["valence_labels"]
def get_vector_deviation(vector1, vector2):
return vector1 - vector2
def get_dataset_deviation(trial_data, base_data):
new_dataset = np.empty([0, 128])
for i in range(0, 4800):
base_index = i // 120
# print(base_index)
base_index = 39 if base_index == 40 else base_index
new_record = get_vector_deviation(trial_data[i], base_data[base_index]).reshape(1, 128)
# print(new_record.shape)
new_dataset = np.vstack([new_dataset, new_record])
# print("new shape:",new_dataset.shape)
return new_dataset
def data_1Dto2D(data, Y=8, X=9):
data_2D = np.zeros([Y, X])
data_2D[0] = (0, 0, data[1], data[0], 0, data[16], data[17], 0, 0)
data_2D[1] = (data[3], 0, data[2], 0, data[18], 0, data[19], 0, data[20])
data_2D[2] = (0, data[4], 0, data[5], 0, data[22], 0, data[21], 0)
data_2D[3] = (data[7], 0, data[6], 0, data[23], 0, data[24], 0, data[25])
data_2D[4] = (0, data[8], 0, data[9], 0, data[27], 0, data[26], 0)
data_2D[5] = (data[11], 0, data[10], 0, data[15], 0, data[28], 0, data[29])
data_2D[6] = (0, 0, 0, data[12], 0, data[30], 0, 0, 0)
data_2D[7] = (0, 0, 0, data[13], data[14], data[31], 0, 0, 0)
# return shape:9*9
return data_2D
def pre_process(path, y_n):
# DE feature vector dimension of each band
data_3D = np.empty([0, 8, 9])
sub_vector_len = 32
trial_data, base_data, arousal_labels, valence_labels = read_file(path)
if y_n == "yes":
data = get_dataset_deviation(trial_data, base_data)
data = preprocessing.scale(data, axis=1, with_mean=True, with_std=True, copy=True)
else:
data = preprocessing.scale(trial_data, axis=1, with_mean=True, with_std=True, copy=True)
# convert 128 vector ---> 4*9*9 cube
for vector in data:
for band in range(0, 4):
data_2D_temp = data_1Dto2D(vector[band * sub_vector_len:(band + 1) * sub_vector_len])
data_2D_temp = data_2D_temp.reshape(1, 8, 9)
# print("data_2d_temp shape:",data_2D_temp.shape)
data_3D = np.vstack([data_3D, data_2D_temp])
data_3D = data_3D.reshape(-1, 4, 8, 9)
print("final data shape:", data_3D.shape)
return data_3D, arousal_labels, valence_labels
if __name__ == '__main__':
dataset_dir = "/home/kaka/Desktop/sfy_file/eeg_emotion/nonCrossSubject/data/DEAP/all_0.5/"
use_baseline = "yes"
if use_baseline == "yes":
result_dir = "/home/kaka/Desktop/sfy_file/eeg_emotion/nonCrossSubject/data/DEAP/with_base_0.5/"
if os.path.isdir(result_dir) == False:
os.makedirs(result_dir)
else:
result_dir = "/home/kaka/Desktop/sfy_file/eeg_emotion/nonCrossSubject/data/DEAP/without_base_0.5/"
if os.path.isdir(result_dir) == False:
os.makedirs(result_dir)
for file in os.listdir(dataset_dir):
print("processing: ", file, "......")
file_path = os.path.join(dataset_dir, file)
data, arousal_labels, valence_labels = pre_process(file_path, use_baseline)
print("final shape:", data.shape)
sio.savemat(result_dir + file,
{"data": data, "valence_labels": valence_labels, "arousal_labels": arousal_labels})
# break