-
Notifications
You must be signed in to change notification settings - Fork 60
/
signalfeatures.py
272 lines (199 loc) · 6.85 KB
/
signalfeatures.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
==================
Signal Features
==================
Temporal Features
Codigo: https://github.com/gilestrolab/pyrem/blob/master/src/pyrem/univariate.py
"""
print(__doc__)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft
import math
from scipy.signal import firwin, remez, kaiser_atten, kaiser_beta
from scipy.signal import butter, filtfilt, buttord
from scipy.signal import butter, lfilter
import matplotlib.pyplot as plt
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y
def psd(y):
# Number of samplepoints
N = 128
# sample spacing
T = 1.0 / 128.0
# From 0 to N, N*T, 2 points.
#x = np.linspace(0.0, 1.0, N)
#y = 1*np.sin(10.0 * 2.0*np.pi*x) + 9*np.sin(20.0 * 2.0*np.pi*x)
# Original Bandpass
fs = 128.0
fso2 = fs/2
#Nd,wn = buttord(wp=[9/fso2,11/fso2], ws=[8/fso2,12/fso2],
# gpass=3.0, gstop=40.0)
#b,a = butter(Nd,wn,'band')
#y = filtfilt(b,a,y)
y = butter_bandpass_filter(y, 8.0, 15.0, fs, order=6)
yf = fft(y)
#xf = np.linspace(0.0, int(1.0/(2.0*T)), int(N/2))
#import matplotlib.pyplot as plt
#plt.plot(xf, 2.0/N * np.abs(yf[0:int(N/2)]))
#plt.axis((0,60,0,1))
#plt.grid()
#plt.show()
return np.sum(np.abs(yf[0:int(N/2)]))
def crest_factor(x):
return np.max(np.abs(x))/np.sqrt(np.mean(np.square(x)))
def hjorth(a):
r"""
Compute Hjorth parameters [HJO70]_.
.. math::
Activity = m_0 = \sigma_{a}^2
.. math::
Complexity = m_2 = \sigma_{d}/ \sigma_{a}
.. math::
Morbidity = m_4 = \frac{\sigma_{dd}/ \sigma_{d}}{m_2}
Where:
:math:`\sigma_{x}^2` is the mean power of a signal :math:`x`. That is, its variance, if it's mean is zero.
:math:`a`, :math:`d` and :math:`dd` represent the original signal, its first and second derivatives, respectively.
.. note::
**Difference with PyEEG:**
Results is different from [PYEEG]_ which appear to uses a non normalised (by the length of the signal) definition of the activity:
.. math::
\sigma_{a}^2 = \sum{\mathbf{x}[i]^2}
As opposed to
.. math::
\sigma_{a}^2 = \frac{1}{n}\sum{\mathbf{x}[i]^2}
:param a: a one dimensional floating-point array representing a time series.
:type a: :class:`~numpy.ndarray` or :class:`~pyrem.time_series.Signal`
:return: activity, complexity and morbidity
:rtype: tuple(float, float, float)
Example:
>>> import pyrem as pr
>>> import numpy as np
>>> # generate white noise:
>>> noise = np.random.normal(size=int(1e4))
>>> activity, complexity, morbidity = pr.univariate.hjorth(noise)
"""
first_deriv = np.diff(a)
second_deriv = np.diff(a,2)
var_zero = np.mean(a ** 2)
var_d1 = np.mean(first_deriv ** 2)
var_d2 = np.mean(second_deriv ** 2)
activity = var_zero
morbidity = np.sqrt(var_d1 / var_zero)
complexity = np.sqrt(var_d2 / var_d1) / morbidity
return activity, morbidity, complexity
def pfd(a):
r"""
Compute Petrosian Fractal Dimension of a time series [PET95]_.
It is defined by:
.. math::
\frac{log(N)}{log(N) + log(\frac{N}{N+0.4N_{\delta}})}
.. note::
**Difference with PyEEG:**
Results is different from [PYEEG]_ which implemented an apparently erroneous formulae:
.. math::
\frac{log(N)}{log(N) + log(\frac{N}{N}+0.4N_{\delta})}
Where:
:math:`N` is the length of the time series, and
:math:`N_{\delta}` is the number of sign changes.
:param a: a one dimensional floating-point array representing a time series.
:type a: :class:`~numpy.ndarray` or :class:`~pyrem.time_series.Signal`
:return: the Petrosian Fractal Dimension; a scalar.
:rtype: float
Example:
>>> import pyrem as pr
>>> import numpy as np
>>> # generate white noise:
>>> noise = np.random.normal(size=int(1e4))
>>> pr.univariate.pdf(noise)
"""
diff = np.diff(a)
# x[i] * x[i-1] for i in t0 -> tmax
prod = diff[1:-1] * diff[0:-2]
# Number of sign changes in derivative of the signal
N_delta = np.sum(prod < 0)
n = len(a)
return np.log(n)/(np.log(n)+np.log(n/(n+0.4*N_delta)))
# Sampling frequency of 128 Hz
print('Temporal Features')
signals = pd.read_csv('data/blinking.dat', delimiter=' ', names = ['timestamp','counter','eeg','attention','meditation','blinking'])
print('Information structure:')
signals.head()
data = signals.values
print('Shape %2d,%2d:' % (signals.shape))
eeg = data[:,2]
# %%
ptp = abs(np.max(eeg)) + abs(np.min(eeg))
rms = np.sqrt(np.mean(eeg**2))
cf = crest_factor(eeg)
print ('Peak-To-Peak:' + str(ptp))
print ('Root Mean Square:' + str(rms))
print ('Crest Factor:' + str(cf))
from collections import Counter
from scipy import stats
entropy = stats.entropy(list(Counter(eeg).values()), base=2)
print('Shannon Entropy:' + str(entropy))
activity, complexity, morbidity = hjorth(eeg)
print('Activity:' + str(activity))
print('Complexity:' + str(complexity))
print('Mobidity:' + str(morbidity))
fractal = pfd(eeg)
print('Fractal:' + str(fractal))
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
peaks, _ = find_peaks(eeg, height=200)
plt.plot(eeg)
plt.plot(peaks, eeg[peaks], "x")
plt.plot(np.zeros_like(eeg), "--", color="gray")
plt.show()
N = 128
T = 1.0 / 128.0
# We can put an additional frequency component to verify that things are working ok
shamsignal = False
if (shamsignal):
x= np.linspace(0.0, 1.0, N)
eeg = eeg[:128] + 100*np.sin(10.0 * 2.0*np.pi*x)
yf = fft(eeg)
xf = np.linspace(0.0, int(1.0/(2.0*T)), int(N/2))
plt.close()
plt.plot(xf, 2.0/N * np.abs(yf[0:int(N/2)]))
plt.grid()
plt.show()
print('PSD:' + str(psd(eeg[:128])))
# %%
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss
import numpy as np
x = np.array([-2.2, -1.4, -.8, .2, .4, .8, 1.2, 2.2, 2.9, 4.6])
y = np.array([0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
logr = LogisticRegression(solver='lbfgs')
logr.fit(x.reshape(-1, 1), y)
y_pred = logr.predict_proba(x.reshape(-1, 1))[:, 1].ravel()
loss = log_loss(y, y_pred)
# %%
from math import log
y_inv = np.asarray([1-val for val in y_pred])
y_i = np.asarray([1-val for val in y])
sum1 = [log(val) for val in y_pred]
sum2 = [log(val) for val in y_inv]
print(sum1)
print(sum2)
s1 = sum1 * y
s2 = sum2 * y_i
Hq = - 1.0 / len(y_pred) * (s1.sum()+ s2.sum())
# Logloss is binary cross entropy.
print('x = {}'.format(x))
print('y = {}'.format(y))
print('p(y) = {}'.format(np.round(y_pred, 2)))
print('Log Loss / Cross Entropy = {:.4f}'.format(loss))
print (Hq)
# %%