def set_fisher_info(self): for e in xrange(self.data.shape[0]): if np.all(self.data[e][:1000] == 0): continue name = 'fisher_e' + str(e) value = pyeeg.fisher_info(W = self.svd_embed_seq['svd_embed_seq_e' + str(e)]) self.features[name] = value
def features(mat): Kmax = 5 Tau = 4 DE = 10 M = 10 R = 0.3 Band = np.arange(1, 86) Fs = 173 DFA = pyeeg.dfa(mat) HFD = pyeeg.hfd(mat, Kmax) SVD_Entropy = pyeeg.svd_entropy(mat, Tau, DE) Fisher_Information = pyeeg.fisher_info(mat, Tau, DE) PFD = pyeeg.pfd(mat) sleep(0.01) return (DFA, HFD, SVD_Entropy, Fisher_Information, PFD)
def features(mat): Kmax = 5 Tau = 4 DE = 10 M = 10 R = 0.3 Band = np.arange(1, 86) Fs = 173 DFA = pyeeg.dfa(mat) HFD = pyeeg.hfd(mat, Kmax) SVD_Entropy = pyeeg.svd_entropy(mat, Tau, DE) Fisher_Information = pyeeg.fisher_info(mat, Tau, DE) #ApEn = pyeeg.ap_entropy(mat, M, R) # very slow PFD = pyeeg.pfd(mat) Spectral_Entropy = pyeeg.spectral_entropy(mat, Band, Fs, Power_Ratio=None) sleep(0.01) return (DFA, HFD, SVD_Entropy, Fisher_Information, PFD, Spectral_Entropy)
def advanced_statistics(signal, fs=128): K_boundary = 10 # to be tuned t_fisher = 12 # to be tuned d_fisher = 40 # to be tuned features_num = 11 threshold = 0.0009 advanced_stats = np.zeros((signal.shape[0], features_num)) print("Gathering advanced statistics...") for i in tqdm((np.arange(signal.shape[0]))): feat_array = np.array([ pyeeg.fisher_info(signal[i, :], t_fisher, d_fisher), pyeeg.pfd(signal[i, :]), pyeeg.dfa(signal[i, :]), pyeeg.hfd(signal[i, :], K_boundary), np.sum((abs(signal[i, :])**(-0.3)) > 20), np.sum((abs(signal[i, :])) > threshold), np.std(abs(signal[i, :])**(0.05)), np.sqrt(np.mean(np.power(np.diff(signal[i, :]), 2))), np.mean(np.abs(np.diff(signal[i, :]))), np.mean(signal[i, :]**5), np.sum(signal[i, :]**2) ]) advanced_stats[i, :] = feat_array return advanced_stats
SPECT_ENT_BANDS = 2 ** np.arange(0,8)/2 fun_to_test = [ {"times":100,"name":"hfd", "is_original":True,"fun": lambda x: pyeeg.hfd(x,2**3)}, {"times":100,"name":"hfd", "is_original":False,"fun": lambda x: univ.hfd(x,2**3)}, {"times":100,"name":"hjorth", "is_original":True,"fun": lambda x: pyeeg.hjorth(x)}, {"times":100,"name":"hjorth", "is_original":False,"fun": lambda x: univ.hjorth(x)}, {"times":100,"name":"pfd", "is_original":True, "fun":lambda x: pyeeg.pfd(x)}, {"times":100,"name":"pfd", "is_original":False, "fun":lambda x: pyeeg.pfd(x)}, {"times":2,"name":"samp_ent", "is_original":True, "fun":lambda x: pyeeg.samp_entropy(x,2,1.5)}, {"times":10,"name":"samp_ent", "is_original":False, "fun":lambda x: univ.samp_entropy(x,2,1.5,relative_r=False)}, {"times":2,"name":"ap_ent", "is_original":True, "fun":lambda x: pyeeg.ap_entropy(x,2,1.5)}, {"times":10,"name":"ap_ent", "is_original":False, "fun":lambda x: univ.ap_entropy(x,2,1.5)}, {"times":10,"name":"svd_ent", "is_original":True, "fun":lambda x: pyeeg.svd_entropy(x,2,3)}, {"times":100,"name":"svd_ent", "is_original":False, "fun":lambda x: univ.svd_entropy(x,2,3)}, {"times":10,"name":"fisher_info", "is_original":True, "fun":lambda x: pyeeg.fisher_info(x,2,3)}, {"times":100, "name":"fisher_info", "is_original":False, "fun":lambda x: univ.fisher_info(x,2,3)}, {"times":100,"name":"spectral_entropy", "is_original":True, "fun":lambda x: pyeeg.spectral_entropy(x,SPECT_ENT_BANDS,256)}, {"times":100, "name":"spectral_entropy", "is_original":False, "fun":lambda x: univ.spectral_entropy(x,256, SPECT_ENT_BANDS)}, ] def make_one_rep(): ldfs = [] for n in range(MIN_EPOCH_N, MAX_EPOCH_N + 1, EPOCH_STEP): a = numpy.random.normal(size=n) for fun in fun_to_test: f = lambda : fun["fun"](a) t=Timer(f) numb = fun["times"]
def feature_wave(self, toolName=None, Fs=256): if (toolName == None): print('please select a tool') return if toolName in self.FeatureSet.dict[0]: index = self.FeatureSet.dict[0][toolName] else: index = -1 print(toolName) if toolName == 'DWT': answer_train = DWT(self.DataSet.trainSet_data[0], 'db4') answer_test = DWT(self.DataSet.testSet_data[0], 'db4') print('DWT feature extraction succeed db4') elif toolName == 'hurst': answer_train = [ pyeeg.hurst(i) for i in self.DataSet.trainSet_data[0] ] answer_test = [ pyeeg.hurst(i) for i in self.DataSet.testSet_data[0] ] print('hurst feature extraction succeed') elif toolName == 'dfa': answer_train = [ pyeeg.dfa(i, L=[4, 8, 16, 32, 64]) for i in self.DataSet.trainSet_data[0] ] answer_test = [ pyeeg.dfa(i, L=[4, 8, 16, 32, 64]) for i in self.DataSet.testSet_data[0] ] print('dfa feature extraction succeed') elif toolName == 'fisher_info': answer_train = [ pyeeg.fisher_info(i, 2, 20) for i in self.DataSet.trainSet_data[0] ] answer_test = [ pyeeg.fisher_info(i, 2, 20) for i in self.DataSet.testSet_data[0] ] print('fisher_info feature extraction succeed') elif toolName == 'svd_entropy': answer_train = [ pyeeg.svd_entropy(i, 2, 20) for i in self.DataSet.trainSet_data[0] ] answer_test = [ pyeeg.svd_entropy(i, 2, 20) for i in self.DataSet.testSet_data[0] ] print('svd_entropy feature extraction succeed') elif toolName == 'spectral_entropy': bandlist = [0.5, 4, 7, 12, 30, 100] answer_train = [ pyeeg.spectral_entropy(i, bandlist, Fs) for i in self.DataSet.trainSet_data[0] ] answer_test = [ pyeeg.spectral_entropy(i, bandlist, Fs) for i in self.DataSet.testSet_data[0] ] print('spectral_entropy feature extraction succeed') elif toolName == 'hjorth': # 得到两个量 第一个是 mobility 第二个是 complexity answer_train = [ pyeeg.hjorth(i) for i in self.DataSet.trainSet_data[0] ] answer_test = [ pyeeg.hjorth(i) for i in self.DataSet.testSet_data[0] ] answer_train = np.array(answer_train) answer_test = np.array(answer_test) for i in answer_train: i[1] = i[1] / 100 for i in answer_test: i[1] = i[1] / 100 #只取Mobility answer_train = np.array(answer_train[:, 0]) answer_test = np.array(answer_test[:, 0]) print('hjorth feature extraction succeed') elif toolName == 'hfd': answer_train = [ pyeeg.hfd(i, 8) for i in self.DataSet.trainSet_data[0] ] answer_test = [ pyeeg.hfd(i, 8) for i in self.DataSet.testSet_data[0] ] print('hfd feature extraction succeed') elif toolName == 'pfd': answer_train = [ pyeeg.pfd(i) for i in self.DataSet.trainSet_data[0] ] answer_test = [pyeeg.pfd(i) for i in self.DataSet.testSet_data[0]] print('pfd feature extraction succeed') elif toolName == 'bin_power': bandlist = [0.5, 4, 7, 12] #,30,100] answer_train = [ pyeeg.bin_power(i, bandlist, Fs) for i in self.DataSet.trainSet_data[0] ] answer_test = [ pyeeg.bin_power(i, bandlist, Fs) for i in self.DataSet.testSet_data[0] ] print('bin_power feature extraction succeed') else: print('does not have this kind of mode') answer_train = np.array(answer_train) answer_train = answer_train.reshape(len(answer_train), -1) answer_test = np.array(answer_test) answer_test = answer_test.reshape(len(answer_test), -1) if index == -1: #print(len(self.FeatureSet.feature.trainSet_feat[0]),len(answer_train)) self.FeatureSet.feature.trainSet_feat[0] = np.column_stack( (self.FeatureSet.feature.trainSet_feat[0], answer_train)) self.FeatureSet.feature.testSet_feat[0] = np.column_stack( (self.FeatureSet.feature.testSet_feat[0], answer_test)) self.FeatureSet.dict[0][toolName] = [ self.FeatureSet.size[0], self.FeatureSet.size[0] + len(answer_train[0]) ] self.FeatureSet.size[0] += len(answer_train[0]) else: self.FeatureSet.feature.trainSet_feat[0][:, index[0]:index[1]] = [ i for i in answer_train ] self.FeatureSet.feature.testSet_feat[0][:, index[0]:index[1]] = [ i for i in answer_test ]
svd_entropy_features_train = [] for i in range(X_train.shape[0]): ##print i h = svd_entropy(X_train[i, ], 4, 10, W=None) svd_entropy_features_train.append(h) svd_entropy_features_test = [] for i in range(X_test.shape[0]): ##print i h = svd_entropy(X_test[i, ], 4, 10, W=None) svd_entropy_features_test.append(h) '''Fisher Information ''' ##Okay fisher_info_features_train = [] for i in range(X_train.shape[0]): ##print i h = fisher_info(X_train[i, ], 4, 10, W=None) fisher_info_features_train.append(h) fisher_info_features_test = [] for i in range(X_test.shape[0]): ##print i h = fisher_info(X_test[i, ], 4, 10, W=None) fisher_info_features_test.append(h) ''' Detrended Fluctuation Analysis''' ###Okay dfa_features_train = [] for i in range(X_train.shape[0]): ##print i h = dfa(X_train[i, ], Ave=None, L=None) dfa_features_train.append(h) dfa_features_test = []
h = svd_entropy(X_train[i,], 4, 10, W=None) svd_entropy_features_train.append(h) svd_entropy_features_test = [] for i in range(X_test.shape[0]): ##print i h = svd_entropy(X_test[i,], 4, 10, W=None) svd_entropy_features_test.append(h) """Fisher Information """ ##Okay fisher_info_features_train = [] for i in range(X_train.shape[0]): ##print i h = fisher_info(X_train[i,], 4, 10, W=None) fisher_info_features_train.append(h) fisher_info_features_test = [] for i in range(X_test.shape[0]): ##print i h = fisher_info(X_test[i,], 4, 10, W=None) fisher_info_features_test.append(h) """ Detrended Fluctuation Analysis""" ###Okay dfa_features_train = [] for i in range(X_train.shape[0]): ##print i h = dfa(X_train[i,], Ave=None, L=None)
def calculate_features(samples): data = samples if not samples: print("no samples") return [] band = [0.5, 4, 7, 12, 30] a = randn(4097) # approx = pyeeg.ap_entropy(data, 5, 1) approx = 0 DFA = pyeeg.dfa(data) first_order_diff = [data[i] - data[i - 1] for i in range(1, len(data))] fisher_info = pyeeg.fisher_info(data, 1, 1, W=None) embed_seq = pyeeg.embed_seq(data, 1, 1) hfd = pyeeg.hfd(data, 6) hjorth = pyeeg.hjorth(data, D=None) hurst = pyeeg.hurst(data) PFD = pyeeg.pfd(data) sam_ent = pyeeg.samp_entropy(data, 1, 2) spectral_entropy = pyeeg.spectral_entropy(data, band, 256, Power_Ratio=None) svd = pyeeg.svd_entropy(data, 6, 4, W=None) PSI = pyeeg.bin_power(data, band, 256) # # Power Spectral Intensity (PSI) and Relative Intensity Ratio (RIR) Two 1- D v ec t o rs # # print("bin_power = ", PSI) # # Petrosian Fractal Dimension (PFD) Ascalar # print("PFD = ", PFD) # # Higuchi Fractal Dimension (HFD) Ascalar # print("hfd = ", hfd) # # Hjorth mobility and complexity Two s c a la rs # print("hjorth = ", hjorth) # # Spectral Entropy (Shannon’s entropy of RIRs) Ascalar # print("spectral_entropy = ", spectral_entropy) # # SVD Entropy Ascalar # print("svd = ", svd) # # Fisher Information Ascalar # print("fisher_info = ", fisher_info) # # Approximate Entropy (ApEn) Ascalar # print("approx entrophy = ", approx) # # Detrended Fluctuation Analysis (DFA) Ascalar # print("DFA = ", DFA) # # HurstExponent(Hurst) Ascalar # print("Hurst_Exponent = ", hurst) # # Build a set of embedding sequences from given time series X with lag Tau and embedding dimension # print("embed_seq = ", embed_seq) # # Compute the first order difference of a time series. # print("first_order_diff = ", first_order_diff) return { 'approximate': approx, 'DFA': DFA, 'fisher_info': fisher_info, 'embed_seq': embed_seq, 'hfd': hfd, 'hjorth': hjorth, 'hurst': hurst, 'PFD': PFD, 'sam_ent': sam_ent, 'spectral_entropy': spectral_entropy, 'svd': svd, 'PSI': PSI, 'first_order_diff': first_order_diff }
def compute_pyeeg_feats(self, rec): # these values are taken from the tuh paper TAU, DE, Kmax = 4, 10, 5 pwrs, pwrrs, pfds, hfds, mblts, cmplxts, ses, svds, fis, hrsts = [], [], [], [], [], [], [], [], [], [] dfas, apes = [], [] for window_id, window in enumerate(rec.signals): for window_electrode_id, window_electrode in enumerate(window): # taken from pyeeg code / paper electrode_diff = list(np.diff(window_electrode)) M = pyeeg.embed_seq(window_electrode, TAU, DE) W = scipy.linalg.svd(M, compute_uv=False) W /= sum(W) power, power_ratio = self.bin_power(window_electrode, self.bands, rec.sampling_freq) pwrs.extend(list(power)) # mean of power ratio is 1/(len(self.bands)-1) pwrrs.extend(list(power_ratio)) pfd = pyeeg.pfd(window_electrode, electrode_diff) pfds.append(pfd) hfd = pyeeg.hfd(window_electrode, Kmax=Kmax) hfds.append(hfd) mobility, complexity = pyeeg.hjorth(window_electrode, electrode_diff) mblts.append(mobility) cmplxts.append(complexity) se = self.spectral_entropy(window_electrode, self.bands, rec.sampling_freq, power_ratio) ses.append(se) svd = pyeeg.svd_entropy(window_electrode, TAU, DE, W=W) svds.append(svd) fi = pyeeg.fisher_info(window_electrode, TAU, DE, W=W) fis.append(fi) # this crashes... # ape = pyeeg.ap_entropy(electrode, M=10, R=0.3*np.std(electrode)) # apes.append(ape) # takes very very long to compute # hurst = pyeeg.hurst(electrode) # hrsts.append(hurst) # takes very very long to compute # dfa = pyeeg.dfa(electrode) # dfas.append(dfa) pwrs = np.asarray(pwrs).reshape(rec.signals.shape[0], rec.signals.shape[1], len(self.bands) - 1) pwrs = np.mean(pwrs, axis=0) pwrrs = np.asarray(pwrrs).reshape(rec.signals.shape[0], rec.signals.shape[1], len(self.bands) - 1) pwrrs = np.mean(pwrrs, axis=0) pfds = np.asarray(pfds).reshape(rec.signals.shape[0], rec.signals.shape[1]) pfds = np.mean(pfds, axis=0) hfds = np.asarray(hfds).reshape(rec.signals.shape[0], rec.signals.shape[1]) hfds = np.mean(hfds, axis=0) mblts = np.asarray(mblts).reshape(rec.signals.shape[0], rec.signals.shape[1]) mblts = np.mean(mblts, axis=0) cmplxts = np.asarray(cmplxts).reshape(rec.signals.shape[0], rec.signals.shape[1]) cmplxts = np.mean(cmplxts, axis=0) ses = np.asarray(ses).reshape(rec.signals.shape[0], rec.signals.shape[1]) ses = np.mean(ses, axis=0) svds = np.asarray(svds).reshape(rec.signals.shape[0], rec.signals.shape[1]) svds = np.mean(svds, axis=0) fis = np.asarray(fis).reshape(rec.signals.shape[0], rec.signals.shape[1]) fis = np.mean(fis, axis=0) return list(pwrs.ravel()), list(pwrrs.ravel( )), pfds, hfds, mblts, cmplxts, ses, svds, fis, apes, hrsts, dfas
"times": 10, "name": "svd_ent", "is_original": True, "fun": lambda x: pyeeg.svd_entropy(x, 2, 3) }, { "times": 100, "name": "svd_ent", "is_original": False, "fun": lambda x: univ.svd_entropy(x, 2, 3) }, { "times": 10, "name": "fisher_info", "is_original": True, "fun": lambda x: pyeeg.fisher_info(x, 2, 3) }, { "times": 100, "name": "fisher_info", "is_original": False, "fun": lambda x: univ.fisher_info(x, 2, 3) }, { "times": 100, "name": "spectral_entropy", "is_original": True, "fun": lambda x: pyeeg.spectral_entropy(x, SPECT_ENT_BANDS, 256) }, { "times": 100,
def myFeaturesExtractor( X, myM, myV): # X has to be a matrix where each row is a channel N = len(X) # number of channels L = len(X[0]) maxtLyap = min(500, L // 2 + L // 4) lyapLags = np.arange(maxtLyap) / Fs # get number of features nFeatures = nMono * N + N * (N - 1) / 2 # here we initialize the list of features // We will transform it to an array later featList = np.zeros((int(nFeatures))) # deal with monovariate features first for kChan in range(N): kFeat = 0 mySig = X[kChan, :] #========== Stats ======================== myMean = myM[kChan] featList[nMono * kChan + kFeat] = myMean kFeat += 1 myMax = max(mySig) featList[nMono * kChan + kFeat] = myMax kFeat += 1 myMin = min(mySig) featList[nMono * kChan + kFeat] = myMin kFeat += 1 peak = max(abs(np.array([myMin, myMax]))) featList[nMono * kChan + kFeat] = peak kFeat += 1 myVar = myV[kChan] featList[nMono * kChan + kFeat] = myVar kFeat += 1 featList[nMono * kChan + kFeat] = sp.skew(mySig) kFeat += 1 featList[nMono * kChan + kFeat] = sp.kurtosis(mySig) kFeat += 1 myRMS = rms(mySig) featList[nMono * kChan + kFeat] = myRMS kFeat += 1 featList[nMono * kChan + kFeat] = peak / myRMS kFeat += 1 featList[nMono * kChan + kFeat] = totVar(mySig) kFeat += 1 featList[nMono * kChan + kFeat] = pyeeg.dfa(mySig) kFeat += 1 featList[nMono * kChan + kFeat] = pyeeg.hurst(mySig) kFeat += 1 hMob, hComp = pyeeg.hjorth(mySig) featList[nMono * kChan + kFeat] = hMob kFeat += 1 featList[nMono * kChan + kFeat] = hComp kFeat += 1 ## ======== fractal ======================== # Now we need to get the embeding time lag Tau and embeding dmension ac = delay.acorr(mySig, maxtau=maxTauLag, norm=True, detrend=True) Tau = firstTrue(ac < corrThresh) # embeding delay f1 , f2 , f3 = dimension.fnn(mySig, dim=dim, tau=Tau, R=10.0, A=2.0, metric='euclidean',\ window=10,maxnum=None, parallel=True) myEmDim = firstTrue(f3 < fracThresh) # Here we construct the Embeding Matrix Em Em = pyeeg.embed_seq(mySig, Tau, myEmDim) U, s, Vh = linalg.svd(Em) W = s / np.sum(s) # list of singular values in decreasing order FInfo = pyeeg.fisher_info(X, Tau, myEmDim, W=W) featList[nMono * kChan + kFeat] = FInfo kFeat += 1 featList[nMono * kChan + kFeat] = Tau kFeat += 1 featList[nMono * kChan + kFeat] = myEmDim kFeat += 1 #======================================== PFD = pyeeg.pfd(mySig, D=None) hfd6 = pyeeg.hfd(mySig, 6) hfd10 = pyeeg.hfd(mySig, 10) # Now we fit aline and get its slope to have Lyapunov exponent divAvg = lyapunov.mle(Em, maxt=maxtLyap, window=3 * Tau, metric='euclidean', maxnum=None) poly = np.polyfit(lyapLags, divAvg, 1, rcond=None, full=False, w=None, cov=False) LyapExp = poly[0] featList[nMono * kChan + kFeat] = PFD kFeat += 1 featList[nMono * kChan + kFeat] = hfd6 kFeat += 1 featList[nMono * kChan + kFeat] = hfd10 kFeat += 1 featList[nMono * kChan + kFeat] = LyapExp kFeat += 1 ## ======== Entropy ======================== tolerance = 1 / 4 entropyDim = max([myEmDim, PFD]) featList[nMono * kChan + kFeat] = pyeeg.samp_entropy( mySig, entropyDim, tolerance) kFeat += 1 featList[nMono * kChan + kFeat] = pyeeg.svd_entropy(mySig, Tau, myEmDim, W=W) kFeat += 1 # here we compute bin power power, power_Ratio = pyeeg.bin_power(mySig, freqBins, Fs) featList[nMono * kChan + kFeat] = pyeeg.spectral_entropy( mySig, freqBins, Fs, Power_Ratio=power_Ratio) kFeat += 1 ## ======== Spectral ======================== for kBin in range(len(freqBins) - 1): featList[nMono * kChan + kFeat] = power[kBin] kFeat += 1 featList[nMono * kChan + kFeat] = power_Ratio[kBin] kFeat += 1 # deal with multivariate features first #============ connectivity ================== corrList = connectome(X) nConnect = len(corrList) if N * (N - 1) / 2 != nConnect: raise ValueError('incorrect number of correlation coeffs') for kC in range(nConnect): featList[-nConnect + kC] = corrList[kC] return featList