-
Notifications
You must be signed in to change notification settings - Fork 1
/
E4_cvxEDA.py
159 lines (134 loc) · 5.51 KB
/
E4_cvxEDA.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import time
import numpy as np
import pandas as pd
import pylab as pl
import edas
import cvxEDA
import os
import datetime
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['text.usetex']=False #禁用LaTex
def correct_eda(file,mit_file):
'''
EDA数据修正
:return:
'''
#源文件处理
data = pd.read_csv(file,header=None)
begin_time = int(data.iat[0,0].astype('int')) #开始时间 第1行,第1列
hz = int(data.iat[1,0].astype('int')) #赫兹 第2行,第1列
eda_data = data.iloc[2:] #EDA数据
data_reshape = []
for i in range(len(eda_data)):
data_time = int(begin_time + i*1/hz) #计算时间戳
data_reshape.append([data_time,eda_data.iat[i,0]])
#print(data_reshape)
#MIT检测文件处理
mit_data = []
data_mit = pd.read_csv(mit_file)
for t in range(len(data_mit['StartTime'])):
starttimeArray = time.strptime(data_mit['StartTime'][t], "%Y-%m-%d %H:%M:%S")
starttimeStamp = int(time.mktime(starttimeArray)) + 28800 #时间加东八区
Label_mit = data_mit['BinaryLabels'][t] #读取标签
for s in range(0,5):
for s1 in range(0,4):
time_process = starttimeStamp + s
mit_data.append([time_process,Label_mit])
#print(mit_data)
#print(len(data_reshape),len(mit_data))
#文件比对合并
data_combine = []
#BUG-0904 EDA.csv不足4hz数据修复
if len(mit_data)>len(data_reshape):
loop_range = len(data_reshape)
else:
loop_range = len(mit_data)
for c in range(loop_range): #以mit结果为准
#print(c,data_reshape[c][0],mit_data[c][0])
if data_reshape[c][0]==mit_data[c][0]:
timestamp,EDA,Label = data_reshape[c][0],data_reshape[c][1],mit_data[c][1]
data_combine.append([timestamp,EDA,Label]) #timestamp,EDA,Label
#print(data_combine) #输出合并后的数据
data_frame = pd.DataFrame(data_combine,columns=['TimeStamp','EDA','Label'])
artifact_data = data_frame[data_frame['Label'] == -1]
#clean_data = data_frame[data_frame['Label'] == 1]
print("伪影率:{}%".format(round(len(artifact_data)/len(data_frame)*100,2)))
df_clear = data_frame.drop(data_frame[data_frame['Label'] == -1].index)
#print(df_clear)
return df_clear
def mit_detection():
'''
eda-explorer
:return:
'''
numClassifiers = 1
if numClassifiers == 1:
temp_clf = 1
if temp_clf == 1:
print('Binary Classifier selected')
classifierList = ['Binary']
elif temp_clf == 2:
print('Multiclass Classifier selected')
classifierList = ['Multiclass']
else:
classifierList = ['Binary', 'Multiclass']
pd.set_option('mode.chained_assignment', None) # Ignore the warning message
#数据分类标签
labels, data = edas.classify(classifierList)
#保存数据为csv
saveDataInput = 'y'
if saveDataInput == 'y':
outputPath = edas.get_user_input('\tEnter Path to E4 directory again: ')
outputLabelFilename = "MIT_Labels"
#保存标签
fullOutputPath = os.path.join(outputPath, outputLabelFilename)
if fullOutputPath[-4:] != '.csv':
fullOutputPath = fullOutputPath + '.csv'
featureLabels = pd.DataFrame(labels, index=pd.date_range(start=data.index[0], periods=len(labels), freq='5s'),
columns=classifierList)
featureLabels.reset_index(inplace=True)
featureLabels.rename(columns={'index': 'StartTime'}, inplace=True)
featureLabels['EndTime'] = featureLabels['StartTime'] + datetime.timedelta(seconds=5)
featureLabels.index.name = 'EpochNum'
cols = ['StartTime', 'EndTime']
cols.extend(classifierList)
featureLabels = featureLabels[cols]
featureLabels.rename(columns={'Binary': 'BinaryLabels', 'Multiclass': 'MulticlassLabels'},
inplace=True)
featureLabels.to_csv(fullOutputPath)
print("Labels saved to " + fullOutputPath)
#print("Remember! The first column is timestamps and the second column is the labels (-1 for artifact, 0 for questionable, 1 for clean)")
MIT_Labels_Path = "{0}\{1}.csv".format(outputPath,outputLabelFilename)
return MIT_Labels_Path,outputPath #返回MITlabels和数据文件夹路径
if __name__ == '__main__':
MIT_Labels_Path,outputPath = mit_detection()
#修正数据
correct_data = correct_eda(r'{}\EDA.csv'.format(outputPath),MIT_Labels_Path)
df = pd.DataFrame(correct_data,columns=['TimeStamp','EDA','Label'])
#df.to_csv(r'{}\Tonic.csv'.format(outputPath),index=None)
'''
CVXEDA
'''
y = np.array(df['EDA'])
# print(type(y))
#print(y)
Fs = 4 # 频率
[r, p, t, l, d, e, obj] = cvxEDA.cvxEDA(y, 1. / Fs)
tm = pl.arange(1., len(y) + 1.) / Fs
#print(len(y), len(t))
# Python3
pl.figure(1)
pl.title("Raw data,Phasic & Tonic")
pl.plot(tm,y, label="Raw data")
pl.plot(tm,r, label="Phasic")
pl.plot(tm,t, label="Tonic")
pl.legend()
# pl.plot(tm,r)
pl.show()
new_list = zip(df['TimeStamp'],y,t)
new_df = pd.DataFrame(new_list,columns=['TimeStamp','Raw_EDA','Tonic_EDA'])
new_df.to_csv('{}\Tonic.csv'.format(outputPath),index=None)
#程序运行结束
print('Completed!')
input('Press Enter to exit.')