def calculate_hjorth(list_values): mobility, complexity = pyeeg.hjorth(list_values) if np.isnan(mobility): mobility = 0 if np.isnan(complexity): complexity = 0 return [mobility, complexity]
def hr_extract(file_path, time_length): hr_mat = np.genfromtxt(file_path, dtype=float, delimiter=',', usecols=(1, ), skip_header=True) num = hr_mat.shape[0] noise = np.random.normal(0, 1.1, num) hr_mat = hr_mat + noise col = time_length row = int(num / col) hr_mat = hr_mat[:row * col].reshape(row, col) num_row = hr_mat.shape[0] result = np.zeros((num_row, 5)) for i in range(num_row): hr = hr_mat[i, :] if np.any(np.isnan(hr)): indices = np.where(np.isnan(hr))[0] for j in indices: hr[j] = hr[j - 1] power = np.sum(np.power(hr, 4)) k = kfd(hr) hjm = pyeeg.hjorth(hr) pfd = pyeeg.pfd(hr) result[i, 0] = power result[i, 1] = k result[i, 2] = hjm[0] result[i, 3] = hjm[1] result[i, 4] = pfd result = result[:, 1:] return result
def plot_hjorth(): window = int(fe.frequency) * 30 yc, ym = [], [] for i in xrange(fe.data.shape[0]): Yc, Ym = [], [] for j in range(window, fe.data.shape[1], window): dslice = fe.data[i, :j] fodsli = fe.fod['e' + str(i)][:j] hmob, hcom = pyeeg.hjorth(dslice, fodsli) Yc.append(hcom) Ym.append(hmob) yc.append(Yc) ym.append(Ym) X = range(window, fe.data.shape[1], window) plt.subplot(121) plt.title('hjorth complexity') plt.xlabel("Sample size window") for Y in yc: plt.plot(X, Y, hold=True) plt.subplot(122) plt.title('hjorth mobility') plt.xlabel("Sample size window") for Y in ym: plt.plot(X, Y, hold=True) plt.show()
def ecg_extract(file_path, time_length): ecg_mat = np.genfromtxt(file_path, dtype=float, delimiter=',', usecols=(1, ), skip_header=True) num = ecg_mat.shape[0] col = time_length * 250 row = int(num / col) ecg_mat = ecg_mat[1:row * col + 1].reshape(row, col) num_row = ecg_mat.shape[0] result = np.zeros((num_row, 5)) for i in range(num_row): ecg = ecg_mat[i, :] if np.any(np.isnan(ecg)): indices = np.where(np.isnan(ecg))[0] for j in indices: ecg[j] = ecg[j - 1] power = np.sum(np.power(ecg, 4)) k = kfd(ecg) hjm = pyeeg.hjorth(ecg) pfd = pyeeg.pfd(ecg) result[i, 0] = power result[i, 1] = k result[i, 2] = hjm[0] result[i, 3] = hjm[1] result[i, 4] = pfd return result
def mean_hjorth_mob_comp(df): mob_vals = [] comp_vals = [] for col in df.columns: col = df[col].to_numpy() mob_col, comp_col = p.hjorth(col) mob_vals.append(mob_col) comp_vals.append(comp_col) return np.mean(mob_vals), np.mean(comp_vals)
def set_hjorth(self): for e in xrange(self.data.shape[0]): if np.all(self.data[e][:1000] == 0): continue mob_name = 'hjorthmob_e' + str(e) com_name = 'hjorthcom_e' + str(e) mobility_value, complexity_value = pyeeg.hjorth(self.data[e], self.fod['e' + str(e)]) self.features[mob_name] = mobility_value self.features[com_name] = complexity_value
def _hjorth(samples, **kwargs): """ ValueError: Error when checking input: expected flatten_1_input to have 2 dimensions, but got array with shape (30520, 1, 2) Only for local usage: mobility / complexity """ try: differential_sequence = kwargs[DIFF_SEQ] except KeyError: differential_sequence = DIFF_SEQ_DEFAULT return pyeeg.hjorth(samples, D=differential_sequence)
def main(): path = '/home/vitorroriz/sensorsig/rawdata_01-03-18/' FS = 128 person = 'v' class_label = "br" class_label2 = "bl" recordings = clf.read_recordings(path, person) class_index = clf.label_dictionary[class_label] class_index2 = clf.label_dictionary[class_label2] power_ratio_list = [] power_ratio_list_2 = [] hjorth_m_list = [] hjorth_m_list2 = [] hjorth_c_list = [] hjorth_c_list2 = [] channel = 10 for file_index in range(20): power_ratio_list.append( power_ratio(recordings[class_index][file_index])) power_ratio_list_2.append( power_ratio(recordings[class_index2][file_index])) m, c = pyeeg.hjorth(recordings[class_index][file_index][:, channel]) hjorth_m_list.append(m) hjorth_c_list.append(c) m2, c2 = pyeeg.hjorth(recordings[class_index2][file_index][:, channel]) hjorth_m_list2.append(m2) hjorth_c_list2.append(c2) plt.plot(hjorth_m_list, 'bo') plt.hold(True) plt.plot(hjorth_m_list2, 'rx') plt.show()
def store_hjorth(data, classes_used_list, channels_used_list): # Calculate the Hjorth parameters for the specified channels and classes # Create the features and labels matrices for training and testing no_files_training = 15 features = [] labels = [] for file_index in range(20): for class_label in classes_used_list: class_index = label_dictionary[class_label] for window_start_index in range(0, N_SAMPLES_IN_RECORDING, WINDOW_SIZE - OVERLAPPING_SIZE): if window_start_index + WINDOW_SIZE <= N_SAMPLES_IN_RECORDING: recording_window = data[class_index][file_index][ window_start_index:window_start_index + WINDOW_SIZE] channels_hjorth_list = [] for channel_index in channels_used_list: mobility, complexity = pyeeg.hjorth( recording_window[:, channel_index]) channels_hjorth_list.append(mobility) channels_hjorth_list.append(complexity) features.append(np.asarray(channels_hjorth_list)) labels.append(class_index) features = np.asarray(features) labels = np.asarray(labels) no_samples = features.shape[0] border_index = int(no_samples * 0.75) features_train = features[0:border_index] features_test = features[border_index:no_samples] labels_train = labels[0:border_index] labels_test = labels[border_index:no_samples] return features_train, labels_train, features_test, labels_test
def get_features(signal): #print(signal) freq_cutoffs = [3, 8, 12, 27, 50] features = [] features.append(rms(signal)) s = lpf(signal, SAMPLING_RATE, freq_cutoffs[0]) features.append(rms(s)) for i in range(len(freq_cutoffs) - 1): s = bp(signal, SAMPLING_RATE, freq_cutoffs[i], freq_cutoffs[i + 1]) features.append(rms(s)) fourier = np.fft.rfft(signal * np.hamming(signal.size)) features.extend(abs(fourier)) wsize = 64 X = mne.time_frequency.stft(signal, wsize, verbose=False) freqs = np.reshape(abs(X), X.size) features.extend(freqs) features.append(pyeeg.hurst(signal)) features.append(pyeeg.hfd(signal, 10)) e = pyeeg.spectral_entropy(signal, np.append(0.5, freq_cutoffs), SAMPLING_RATE) features.append(e) features.extend(pyeeg.hjorth(signal)) features.append(pyeeg.pfd(signal)) features.append(pyeeg.mean(signal)) features.append(scipy.stats.skew(signal)) features.append(scipy.stats.kurtosis(signal)) #features.extend(signal) return features
def get_features(signal): #print(signal) freq_cutoffs = [3, 8, 12, 27, 50] features = [] features.append(rms(signal)) s = lpf(signal, SAMPLING_RATE, freq_cutoffs[0]) features.append(rms(s)) for i in range(len(freq_cutoffs)-1): s = bp(signal, SAMPLING_RATE, freq_cutoffs[i], freq_cutoffs[i+1]) features.append(rms(s)) fourier = np.fft.rfft(signal * np.hamming(signal.size)) features.extend(abs(fourier)) wsize = 64 X = mne.time_frequency.stft(signal, wsize, verbose=False) freqs = np.reshape(abs(X), X.size) features.extend(freqs) features.append(pyeeg.hurst(signal)) features.append(pyeeg.hfd(signal, 10)) e = pyeeg.spectral_entropy(signal, np.append(0.5, freq_cutoffs), SAMPLING_RATE) features.append(e) features.extend(pyeeg.hjorth(signal)) features.append(pyeeg.pfd(signal)) features.append(pyeeg.mean(signal)) features.append(scipy.stats.skew(signal)) features.append(scipy.stats.kurtosis(signal)) #features.extend(signal) return features
def eeg_features(data): data = np.asarray(data) res = np.zeros([22]) Kmax = 5 # M = 10 # R = 0.3 Band = [1, 5, 10, 15, 20, 25] Fs = 256 power, power_ratio = pyeeg.bin_power(data, Band, Fs) f, P = welch(data, fs=Fs, window='hanning', noverlap=0, nfft=int(256.)) # Signal power spectrum area_freq = cumtrapz(P, f, initial=0) res[0] = np.sqrt(np.sum(np.power(data, 2)) / data.shape[0]) # amplitude RMS res[1] = statistics.stdev(data)**2 # variance res[2] = kurtosis(data) # kurtosis res[3] = skew(data) # skewness res[4] = max(data) # max amplitude res[5] = min(data) # min amplitude res[6] = len(argrelextrema( data, np.greater)[0]) # number of local extrema or peaks res[7] = ((data[:-1] * data[1:]) < 0).sum() # number of zero crossings res[8] = pyeeg.hfd(data, Kmax) # Higuchi Fractal Dimension res[9] = pyeeg.pfd(data) # Petrosian Fractal Dimension res[10] = pyeeg.hurst(data) # Hurst exponent res[11] = pyeeg.spectral_entropy( data, Band, Fs, Power_Ratio=power_ratio) # spectral entropy (1.21s) res[12] = area_freq[-1] # total power res[13] = f[np.where(area_freq >= res[12] / 2)[0][0]] # median frequency res[14] = f[np.argmax(P)] # peak frequency res[15], res[16] = pyeeg.hjorth(data) # Hjorth mobility and complexity res[17] = power_ratio[0] res[18] = power_ratio[1] res[19] = power_ratio[2] res[20] = power_ratio[3] res[21] = power_ratio[4] # res[22] = pyeeg.samp_entropy(data, M, R) # sample entropy # res[23] = pyeeg.ap_entropy(data, M, R) # approximate entropy (1.14s) return (res)
def getonefeatures(data): listdata = list(data) # Std fstd = np.array(list(map(lambda x: np.std(x), listdata))) # Approximate-Entropy(ApEN) m = 3 r = 0.2 * fstd fae = np.array( list(map(lambda x, y: pyeeg.ap_entropy(x, m, y), listdata, list(r)))) # Power # fpower,fre = pyeeg.bin_power(data, [1,30], fs) # print(fpower) # print("特征--{std:%.4f,AE:%.4f,Power:%.4f}"%(fstd,fae,fpower)) # First-order Diff ??? # firstoderlist = pyeeg.first_order_diff(data) # Hjorth fhjormod_com = np.array(list(map(lambda x: pyeeg.hjorth(x), listdata))) fhjor_act = np.array(list(map(lambda x: np.var(x), listdata))) # Spectrum Entropy # fse = pyeeg.spectral_entropy(data, [0,fs/2], fs) # print(fse) # Power Spectral Density fpsd = np.array(list(map(lambda x: np.max(plt.psd(x, fs)[0]), listdata))) # Features Stack featurestmp = np.stack( (fstd, fae, fhjormod_com[:, 0], fhjormod_com[:, 1], fhjor_act, fpsd), axis=1) temprow, tmpcol = featurestmp.shape features = np.reshape(featurestmp, (temprow * tmpcol, )) return features
def get_features(sig, sampling_rate = 250.0): #print(signal) # freq_cutoffs = [3, 8, 12, 27, 40, 59, 61, 80, 100, 120] features = [] # features.append(rms(sig)) # s = lpf(sig, freq_cutoffs[0], sampling_rate) # features.append(rms(s)) # for i in range(len(freq_cutoffs)-1): # s = bp(sig, freq_cutoffs[i], freq_cutoffs[i+1], sampling_rate) # features.append(rms(s)) # fourier = np.fft.rfft(sig * np.hamming(sig.size)) # features.extend(abs(fourier)) # features.append(pyeeg.hurst(sig)) # features.append(pyeeg.hfd(sig, 10)) # e = pyeeg.spectral_entropy(sig, np.append(0.5, freq_cutoffs), sampling_rate) # features.append(e) # very fast, but slow overall =/ features.extend(pyeeg.hjorth(sig)) features.append(pyeeg.pfd(sig)) features.append(np.mean(sig)) features.append(np.std(sig)) features.append(scipy.stats.skew(sig)) features.append(scipy.stats.kurtosis(sig)) #features.extend(sig) return features
def get_features(sig, sampling_rate=250.0): #print(signal) # freq_cutoffs = [3, 8, 12, 27, 40, 59, 61, 80, 100, 120] features = [] # features.append(rms(sig)) # s = lpf(sig, freq_cutoffs[0], sampling_rate) # features.append(rms(s)) # for i in range(len(freq_cutoffs)-1): # s = bp(sig, freq_cutoffs[i], freq_cutoffs[i+1], sampling_rate) # features.append(rms(s)) # fourier = np.fft.rfft(sig * np.hamming(sig.size)) # features.extend(abs(fourier)) # features.append(pyeeg.hurst(sig)) # features.append(pyeeg.hfd(sig, 10)) # e = pyeeg.spectral_entropy(sig, np.append(0.5, freq_cutoffs), sampling_rate) # features.append(e) # very fast, but slow overall =/ features.extend(pyeeg.hjorth(sig)) features.append(pyeeg.pfd(sig)) features.append(np.mean(sig)) features.append(np.std(sig)) features.append(scipy.stats.skew(sig)) features.append(scipy.stats.kurtosis(sig)) #features.extend(sig) return features
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
"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,
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 ]
# Percentage of Zeroes PoZ = sum(sum((data == 0) == True)) / (240000 * 16) # Max Value Max = data.max() # Min Value Min = data.min() # Variance Variance = np.array(data.var(0)) # Variance of mean channel Mean_Channel_Variance = data.mean(1).var(0) # Hjorth Parameters Mobility = [] Complexity = [] for i in range(0, 16): first_diff_order = np.diff(data[:, i]) mob, comp = eeg.hjorth(data[:, i], first_diff_order.tolist()) Mobility.append(mob) Complexity.append(comp) del first_diff_order, mob, comp #hjorth.append(eeg.hjorth(data[ :, i], [])) # Max FFT Amplitude #np.fft.fft2(data).max(0) Max_FFT_AR = abs(np.fft.fft(data).max(0)) #Skewness Skewness = skew(data) #Kurtosis Kurt = kurtosis(data) #Power Spectral frequency Welch's method fs = 400 for i in range(0, 16): channel = data[:, i]
hfd_features_train = [] for i in range(X_train.shape[0]): ##print i h = hfd(X_train[i, ], 5) hfd_features_train.append(h) hfd_features_test = [] for i in range(X_test.shape[0]): ##print i h = hfd(X_test[i, ], 5) hfd_features_test.append(h) '''Hjorth Parameters ''' ###Okay hjorth_features_train = [] for i in range(X_train.shape[0]): ##print i h = hjorth(X_train[i, ]) hjorth_features_train.append(h) hjorth_features_test = [] for i in range(X_test.shape[0]): ##print i h = hjorth(X_test[i, ]) hjorth_features_test.append(h) '''Spectral Entropy ''' ##Okay spectral_entropy_features_train = [] for i in range(X_train.shape[0]): ##print i h = spectral_entropy(X_train[i, ], [0.54, 5, 7, 12, 50], 173, Power_Ratio=None) spectral_entropy_features_train.append(h)
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
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 myFeaturesExtractor1( X, myM, myV, myMin, myMax): # X has to be a matrix where each row is a channel N = len(X) # number of channels # L = len(X[0]) # 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 featList[nMono * kChan + kFeat] = myMax[kChan] kFeat += 1 featList[nMono * kChan + kFeat] = myMin[kChan] 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 featList[nMono * kChan + kFeat] = pyeeg.dfa(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 featList[nMono * kChan + kFeat] = Tau kFeat += 1 PFD = pyeeg.pfd(mySig, D=None) hfd10 = pyeeg.hfd(mySig, 10) featList[nMono * kChan + kFeat] = PFD kFeat += 1 featList[nMono * kChan + kFeat] = hfd10 kFeat += 1 ## ======== Entropy ======================== # 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
def myFeaturesExtractor(X): # X has to be a matrix where each row is a channel N = len(X) # L = len(X[0]) # here we initialize the list of features // We will transform it to an array later featList = list() timeList =list () featName =list() for kChan in range(1): mySig = X[kChan , :] if kChan == 0: start=time.perf_counter_ns() #========== Stats ======================== myMean = np.mean(mySig) featList.append(myMean) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append("mean") start=end featList.append(max(mySig)) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" max") start=end featList.append(min(mySig)) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" min") start=end peak =max(abs(mySig)) featList.append(peak) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" peak") start=end myVar = np.var(mySig) featList.append(myVar) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" var") start=end myVar = np.var(mySig) myStd = np.sqrt(myVar) featList.append(myStd) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" std") start=end featList.append(sp.skew(mySig)) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" skew") start=end featList.append(sp.kurtosis(mySig)) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" kurt") start=end myRMS = rms(mySig) featList.append(myRMS) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" rms") start=end myRMS = rms(mySig) featList.append(peak/myRMS) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" fact") start=end myRMS = rms(mySig) featList.append(myRMS/myMean) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" papr") start=end featList.append(totVar(mySig)) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" totVar") start=end featList.append(pyeeg.dfa(mySig)) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" dfa") start=end featList.append(pyeeg.hurst(mySig)) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" hurst") start=end hMob , hComp = pyeeg.hjorth(mySig ) featList.append(hMob) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" Hmob") timeList.append(end -start) featName.append(" Hcomp") start=end featList.append(hComp) # ## ======== 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 # featList.append(Tau) # if kChan == 0: # end=time.perf_counter_ns() # # timeList.append(end -start) # featName.append(" dCorrTime") # start=end # f1 , f2 , f3 = dimension.fnn(mySig, dim=dim, tau=Tau, R=10.0, A=2.0, metric='chebyshev', window=10,maxnum=None, parallel=True) # myEmDim = firstTrue(f3 < fracThresh) ## if kChan == 0: ## end=time.perf_counter_ns() ## timeList.append(end -start) ## featName.append(" embDim") ## start=end # # 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.append(FInfo) # if kChan == 0: # end=time.perf_counter_ns() # # timeList.append(end -start) # featName.append(" FInfo") # start=end # # featList.append(myEmDim) PFD = pyeeg.pfd(mySig, D=None) featList.append(PFD) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" pfd") start=end hfd6 = pyeeg.hfd(mySig , 6) featList.append(hfd6) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" hfd6") start=end hfd10 = pyeeg.hfd(mySig , 10) featList.append(hfd10) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" hfd10") start=end # 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.append(np.mean(LyapExp)) # if kChan == 0: # end=time.perf_counter_ns() # # timeList.append(end -start) # featName.append("Lyapunov") # start=end ## ======== Entropy ======================== # here we compute bin power power, power_Ratio = pyeeg.bin_power(mySig , freqBins , Fs ) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append("Spectral") start=end featList.append( pyeeg.spectral_entropy(mySig, freqBins, Fs, Power_Ratio=power_Ratio)) if kChan == 0: end=time.perf_counter_ns() timeList.append(end -start) featName.append(" specEn") start=end # tolerance = myStd / 4 # entropyDim = max([myEmDim , PFD]) # featList.append( pyeeg.samp_entropy(mySig , entropyDim , tolerance ) ) # if kChan == 0: # end=time.perf_counter_ns() # # timeList.append(end -start) # featName.append(" sampEn") # start=end # featList.append( pyeeg.svd_entropy(mySig, Tau, myEmDim , W=W) ) # if kChan == 0: # end=time.perf_counter_ns() # # timeList.append(end -start) # featName.append(" svdEn") # start=end ## ======== Spectral ======================== appendArray2List(featList , power ) appendArray2List(featList , power_Ratio ) start=time.perf_counter_ns() connectome(X , featList) end=time.perf_counter_ns() timeList.append((end -start)/N/(N-1)*2) featName.append("connectivity") ll=list() ll.append(featName) ll.append(timeList) return np.asarray(featList) , ll
#vibration_array = np.zeros(fft_size) #for i in range(len(vibration_list)): # index = (vibration_list[i]-1) * sampling_rate # vibration_array[index:index+sampling_rate] = 1 (power_xf, power_xf_filtered, freqs, xfp) = return_filtered_epoch(time_series) dominant_f = return_dominant_freq(freqs, power_xf_filtered) (delta_ratio, theta_ratio, alpha_ratio, sigma_ratio, beta_ratio) = return_power_ratio(freqs, power_xf_filtered) (A5_mean, D5_mean, D4_mean, D3_mean, A5_std, D5_std, D4_std, D3_std, A5_pm, D5_pm, D4_pm, D3_pm, \ A5_ratio_mean, D5_ratio_mean, D4_ratio_mean, D3_ratio_mean) = return_DWT_feature(time_series) hurst_index = pyeeg.hurst(time_series) pfd_index = pyeeg.pfd(time_series) sp_entropy = pyeeg.spectral_entropy(time_series, [0.5, 3, 8, 12, 16, 30], sampling_rate, Power_Ratio = None) hj_activity, hj_mobility, hj_complexity = pyeeg.hjorth(time_series) fmax=getfmax(time_series) fmin=getfmin(time_series) fmean=getfmean(time_series) fstd=getfstd(time_series) fvar=getfvar(time_series) fskew=getfskew(time_series) fkur=getfkur(time_series) fmd=getfmd(time_series) zcnum=getzcnum(time_series) print [fmax, fmin, fmean, fstd, fvar, fskew, fkur, fmd, zcnum, dominant_f[0], delta_ratio, \ theta_ratio, alpha_ratio, sigma_ratio, beta_ratio, A5_mean, D5_mean, D4_mean, D3_mean, \ A5_std, D5_std, D4_std, D3_std, A5_pm, D5_pm, D4_pm, D3_pm, A5_ratio_mean, D5_ratio_mean, \ D4_ratio_mean, D3_ratio_mean, hurst_index, pfd_index, sp_entropy, hj_activity, hj_mobility, \
def Hjorth(self): resp = pyeeg.hjorth(self.channel_data) label = 'Hjorth_' labels = label + pd.Series(range(len(resp)), dtype=str) return [np.array(resp), labels.values]
def Hjorth(self): resp = pyeeg.hjorth(self.channel_data) label = 'Hjorth_' labels = label+pd.Series(range(len(resp)),dtype=str) return [np.array(resp),labels.values]
##print i h = hfd(X_train[i,], 5) hfd_features_train.append(h) hfd_features_test = [] for i in range(X_test.shape[0]): ##print i h = hfd(X_test[i,], 5) hfd_features_test.append(h) """Hjorth Parameters """ ###Okay hjorth_features_train = [] for i in range(X_train.shape[0]): ##print i h = hjorth(X_train[i,]) hjorth_features_train.append(h) hjorth_features_test = [] for i in range(X_test.shape[0]): ##print i h = hjorth(X_test[i,]) hjorth_features_test.append(h) """Spectral Entropy """ ##Okay spectral_entropy_features_train = [] for i in range(X_train.shape[0]): ##print i h = spectral_entropy(X_train[i,], [0.54, 5, 7, 12, 50], 173, Power_Ratio=None) spectral_entropy_features_train.append(h)
import pandas as pd import numpy as np import sys MIN_EPOCH_N = 256 * 5 MAX_EPOCH_N = 256 * 30 EPOCH_STEP = 256 * 5 N_REPLICATES = 5 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 Hjorth(x): resp = pyeeg.hjorth(x) return resp