-
Notifications
You must be signed in to change notification settings - Fork 1
/
PMI.py
251 lines (234 loc) · 11.2 KB
/
PMI.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
import itertools
import numpy as np
import pandas as pd
from scipy.spatial.distance import euclidean
def s_entropy(freq_list):
''' This function computes the shannon entropy of a given frequency distribution.
USAGE: shannon_entropy(freq_list)
ARGS: freq_list = Numeric vector representing the frequency distribution
OUTPUT: A numeric value representing shannon's entropy'''
freq_list = [element for element in freq_list if element != 0]
sh_entropy = 0.0
for freq in freq_list:
sh_entropy += freq * np.log(freq)
sh_entropy = -sh_entropy
return(sh_entropy)
def s_entropy_withP(freq_list):
''' This function computes the shannon entropy of a given frequency distribution.
USAGE: shannon_entropy(freq_list)
ARGS: freq_list = Numeric vector representing the frequency distribution
OUTPUT: A numeric value representing shannon's entropy'''
n_zeros = np.count_nonzero(freq_list==0)
sh_entropy = n_zeros/len(freq_list)
freq_list = [element for element in freq_list if element != 0]
for freq in freq_list:
sh_entropy += freq * np.log(freq)
sh_entropy = -sh_entropy
return(sh_entropy)
def p_entropy_withP(op):
''' Different from P_entropy: the max_entropy is calculated using the len(non_zero(op))
USAGE: shannon_entropy(freq_list)
ARGS: freq_list = Numeric vector representing the frequency distribution
OUTPUT: A numeric value representing shannon's entropy'''
# op: ordinal pattern
# ordinal_pat_nonzero = [element for element in op if element != 0]
# max_entropy = np.log(len(ordinal_pat_nonzero))
# p = np.divide(np.array(ordinal_pat_nonzero), float(sum(ordinal_pat_nonzero)))
# return(s_entropy(p)/max_entropy)
ordinal_pat = op
max_entropy = np.log(len(ordinal_pat))
p = np.divide(np.array(ordinal_pat), float(sum(ordinal_pat)))
return(s_entropy_withP(p)/max_entropy)
def ordinal_patterns(ts, embdim, embdelay):
''' This function computes the ordinal patterns of a time series for a given embedding dimension and embedding delay.
USAGE: ordinal_patterns(ts, embdim, embdelay)
ARGS: ts = Numeric vector representing the time series, embdim = embedding dimension (3<=embdim<=7 prefered range), embdelay = embdding delay
OUPTUT: A numeric vector representing frequencies of ordinal patterns'''
time_series = ts
possible_permutations = list(itertools.permutations(range(embdim)))
lst = list()
for i in range(len(time_series) - embdelay * (embdim - 1)):
sorted_index_array = list(np.argsort(time_series[i:(embdim+i)]))
lst.append(sorted_index_array)
lst = np.array(lst)
element, freq = np.unique(lst, return_counts = True, axis = 0)
freq = list(freq)
if len(freq) != len(possible_permutations):
for i in range(len(possible_permutations)-len(freq)):
freq.append(0)
return(freq)
else:
return(freq)
def p_entropy(op):
# op: ordinal pattern
ordinal_pat = op
max_entropy = np.log(len(ordinal_pat))
p = np.divide(np.array(ordinal_pat), float(sum(ordinal_pat)))
return(s_entropy(p)/max_entropy)
def complexity(op):
''' This function computes the complexity of a time series defined as: Comp_JS = Q_o * JSdivergence * pe
Q_o = Normalizing constant
JSdivergence = Jensen-Shannon divergence
pe = permutation entopry
ARGS: ordinal pattern'''
pe = p_entropy(op)
constant1 = (0.5+((1 - 0.5)/len(op)))* np.log(0.5+((1 - 0.5)/len(op)))
constant2 = ((1 - 0.5)/len(op))*np.log((1 - 0.5)/len(op))*(len(op) - 1)
constant3 = 0.5*np.log(len(op))
Q_o = -1/(constant1+constant2+constant3)
temp_op_prob = np.divide(op, sum(op))
temp_op_prob2 = (0.5*temp_op_prob)+(0.5*(1/len(op)))
JSdivergence = (s_entropy(temp_op_prob2) - 0.5 * s_entropy(temp_op_prob) - 0.5 * np.log(len(op)))
Comp_JS = Q_o * JSdivergence * pe
return(Comp_JS)
def weighted_ordinal_patterns(ts, embdim, embdelay):
# take care of the variance within a observed pattern
time_series = ts
possible_permutations = list(itertools.permutations(range(embdim)))
temp_list = list()
wop = list()
for i in range(len(time_series) - embdelay * (embdim - 1)):
Xi = time_series[i:(embdim+i)]
Xn = time_series[(i+embdim-1): (i+embdim+embdim-1)]
Xi_mean = np.mean(Xi)
Xi_var = (Xi-Xi_mean)**2
weight = np.mean(Xi_var)
sorted_index_array = list(np.argsort(Xi))
temp_list.append([''.join(map(str, sorted_index_array)), weight])
result = pd.DataFrame(temp_list,columns=['pattern','weights'])
freqlst = dict(result['pattern'].value_counts())
for pat in (result['pattern'].unique()):
wop.append(np.sum(result.loc[result['pattern']==pat,'weights'].values))
return(wop)
def joint_ordinal_patterns(ts, embdim, embdelay):
''' This function computes the ordinal patterns of two time series for a given embedding dimension and embedding delay.
USAGE: joint_ordinal_patterns(ts, embdim, embdelay)
ARGS: ts = Numeric vector representing two time series ,shape =(2,ts),
embdim = embedding dimension (3<=embdim<=7 prefered range), embdelay = embdding delay
OUPTUT: A numeric vector representing frequencies of ordinal patterns'''
time_series_x = ts[0]
time_series_y = ts[1]
possible_permutations = list(itertools.permutations(range(embdim)))
possible_permutations_repeat = np.repeat(np.array(list(itertools.permutations(range(embdim)))),6,axis=0)
possible_permutations_interlace = np.array(list(itertools.permutations(range(embdim)))*6)
possible_joint_permutations = np.concatenate((possible_permutations_repeat,possible_permutations_interlace),axis=1)
lst_x = list()
lst_y = list()
lst = list()
for i in range(len(time_series_x) - embdelay * (embdim - 1)):
sorted_index_array = list(np.argsort(time_series_x[i:(embdim+i)]))
lst_x.append(sorted_index_array)
sorted_index_array = list(np.argsort(time_series_y[i:(embdim+i)]))
lst_y.append(sorted_index_array)
lst_x = np.array(lst_x)
lst_y = np.array(lst_y)
lst = np.concatenate ((lst_x,lst_y),axis=1)
element, freq = np.unique(lst, return_counts = True, axis = 0)
freq = list(freq)
if len(freq) != len(possible_joint_permutations):
for i in range(len(possible_joint_permutations)-len(freq)):
freq.append(0)
return(freq)
else:
return(freq)
def PMI_2chs(ts_2chs,embdim, embdelay):
op_x = ordinal_patterns(ts_2chs[0],embdim,embdelay)
op_y = ordinal_patterns(ts_2chs[1],embdim,embdelay)
op_xy = joint_ordinal_patterns(ts_2chs,embdim,embdelay)
p_x = p_entropy(op_x)
p_y = p_entropy(op_y)
p_xy = p_entropy(op_xy)
return (p_x+p_y-p_xy)
def PMI_2chs_withP(ts_2chs,embdim, embdelay):
op_x = ordinal_patterns(ts_2chs[0],embdim,embdelay)
op_y = ordinal_patterns(ts_2chs[1],embdim,embdelay)
op_xy = joint_ordinal_patterns(ts_2chs,embdim,embdelay)
p_x = p_entropy_withP(op_x)
p_y = p_entropy_withP(op_y)
p_xy = p_entropy_withP(op_xy)
return (p_x+p_y-p_xy)
def PMI_1epoch(epoch,embdim,embdelay):
''' This function computes the PMI for an epoch.
USAGE: PMI_1epoch(epoch, embdim, embdelay)
ARGS: epoch = Numpy arrayshape =(n_channels,ts),
embdim = embedding dimension (3<=embdim<=7 prefered range), embdelay = embdding delay
OUPTUT: PMI matrix
'''
PMI = np.zeros([epoch.shape[0],epoch.shape[0]])
for i in range(epoch.shape[0]):
for j in np.arange(i,epoch.shape[0]):
op_x = ordinal_patterns(epoch[i],embdim,embdelay)
op_y = ordinal_patterns(epoch[j],embdim,embdelay)
op_xy = joint_ordinal_patterns(np.array([epoch[i],epoch[j]]),embdim,embdelay)
p_x = p_entropy_withP(op_x)
p_y = p_entropy_withP(op_y)
p_xy = p_entropy_withP(op_xy)
PMI[i,j] = (p_x+p_y-p_xy)
return PMI
def PMI_epochs(epochs,embdim,embdelay):
''' This function computes the PMI for an epoch.
USAGE: PMI_1epoch(epoch, embdim, embdelay)
ARGS: epochs = Numpy arrayshape =(n_epochs,n_channels,ts),
embdim = embedding dimension (3<=embdim<=7 prefered range), embdelay = embdding delay
OUPTUT: PMI matrix
'''
PMI = np.zeros([epochs.shape[0],epochs.shape[1],epochs.shape[1]])
for epoch_idx in range(epochs.shape[0]):
# print('.', end='')
for ch1_idx in range(epochs.shape[1]):
for ch2_idx in np.arange(ch1_idx,epochs.shape[1]):
op_x = ordinal_patterns(epochs[epoch_idx][ch1_idx],embdim,embdelay)
op_y = ordinal_patterns(epochs[epoch_idx][ch2_idx],embdim,embdelay)
op_xy = joint_ordinal_patterns(np.array([epochs[epoch_idx][ch1_idx],epochs[epoch_idx][ch2_idx]]),
embdim,embdelay)
p_x = p_entropy_withP(op_x)
p_y = p_entropy_withP(op_y)
p_xy = p_entropy_withP(op_xy)
PMI[epoch_idx,ch1_idx,ch2_idx] = (p_x+p_y-p_xy)
return PMI
def SPMI_2chs(ts_2chs,embdim, embdelay):
op_x = ordinal_patterns(ts_2chs[0],embdim,embdelay)
op_y = ordinal_patterns(ts_2chs[1],embdim,embdelay)
op_xy = joint_ordinal_patterns(ts_2chs,embdim,embdelay)
p_x = p_entropy(op_x)
p_y = p_entropy(op_y)
p_xy = p_entropy(op_xy)
return (p_x+p_y-p_xy)/p_xy
def SPMI_1epoch(epoch,embdim,embdelay):
''' This function computes the SPMI for an epoch.
USAGE: PMI_1epoch(epoch, embdim, embdelay)
ARGS: epoch = Numpy arrayshape =(n_channels,ts),
embdim = embedding dimension (3<=embdim<=7 prefered range), embdelay = embdding delay
OUPTUT: PMI matrix
'''
SPMI = np.zeros([epoch.shape[0],epoch.shape[0]])
for i in range(epoch.shape[0]):
for j in np.arange(i,epoch.shape[0]):
op_x = ordinal_patterns(epoch[i],embdim,embdelay)
op_y = ordinal_patterns(epoch[j],embdim,embdelay)
op_xy = joint_ordinal_patterns(np.array([epoch[i],epoch[j]]),embdim,embdelay)
p_x = p_entropy_withP(op_x)
p_y = p_entropy_withP(op_y)
p_xy = p_entropy_withP(op_xy)
SPMI[i,j] = (p_x+p_y-p_xy)/p_xy
return SPMI
def SPMI_epochs(epochs,embdim,embdelay):
''' This function computes the SPMI for epochs.
USAGE: PMI_1epoch(epoch, embdim, embdelay)
ARGS: epochs = Numpy arrayshape =(n_epochs,n_channels,ts),
embdim = embedding dimension (3<=embdim<=7 prefered range), embdelay = embdding delay
OUPTUT: assymmetric SPMI matrix
'''
SPMI = np.zeros([epochs.shape[0],epochs.shape[1],epochs.shape[1]])
for epoch_idx in range(epochs.shape[0]):
for ch1_idx in range(epochs.shape[1]):
for ch2_idx in np.arange(ch1_idx,epochs.shape[1]):
op_x = ordinal_patterns(epochs[epoch_idx][ch1_idx],embdim,embdelay)
op_y = ordinal_patterns(epochs[epoch_idx][ch2_idx],embdim,embdelay)
op_xy = joint_ordinal_patterns(np.array([epochs[epoch_idx][ch1_idx],epochs[epoch_idx][ch2_idx]]),
embdim,embdelay)
p_x = p_entropy_withP(op_x)
p_y = p_entropy_withP(op_y)
p_xy = p_entropy_withP(op_xy)
SPMI[epoch_idx,ch1_idx,ch2_idx] = (p_x+p_y-p_xy)/p_xy
return SPMI