def apply_pearson(self, signal): def pearson(window): result = pearsonr(self.sample, window) # result contains return result[0] return signal.rolling(self.length, center=True).apply(pearson, raw=True)
def apply_min_error_L2_thresholded(self, signal, min_alpha=0.05, max_alpha=50.0): def apply_func(window): return min_errorL2_thresholded(self.sample, window, min_alpha, max_alpha)[0] return signal.rolling(self.length, center=True).apply(apply_func, raw=True)
def apply_min_max(self, signal): def apply_func(window): pos_min = window.argmin() pos_max = window.argmax() acc = 0 if (pos_min != 0 and pos_min != (self.length - 1)): acc -= 1 if (pos_max != 0 and pos_max != (self.length - 1)): acc += 1 return acc return signal.rolling(self.length, center=True).apply(apply_func, raw=True)
def Normalize(signal, rolling_period_trend=100, rolling_period_var=100): """Normalize the signal""" if rolling_period_trend > len(signal) or rolling_period_trend == 0: mean = signal-signal else: mean = signal.rolling(rolling_period_trend).mean() demean = signal - mean mean = mean.fillna(0) demean = demean.fillna(0) if rolling_period_var > len(signal) or rolling_period_var == 0: var = signal/signal else: var = demean.rolling(rolling_period_var).std() normalize = demean/var var = var.fillna(0) normalize = normalize.fillna(0) return mean, var, normalize
def zero_edge_detection(signal, rolling_size): mean_value = signal.rolling(rolling_size, min_periods=1).mean() step_index = np.nonzero((mean_value != 0) & (mean_value != 1))[0].tolist() return step_index
def process(dataset): data = {} dataset = dataset[[ 'FT7', 'FT8', 'T7', 'P1', 'P2', 'T8', 'O2', 'O1', 'label' ]] sleep_data = dataset[dataset['label'] == 2] noise_data = sleep_data + np.random.normal(0, .1, sleep_data.shape) noise_data['label'] = 2 dataset = pd.concat([dataset, noise_data], ignore_index=True) awake_data = dataset[dataset['label'] == 0] noise_data = awake_data + np.random.normal(0, .1, awake_data.shape) noise_data['label'] = 0 dataset = pd.concat([dataset, noise_data], ignore_index=True) for c in dataset.columns: if c != 'label': print("-----------{}-----------".format(c)) data[c + '_psd_theta_ma'] = [] data[c + '_se_theta_ma'] = [] data[c + '_psd_alpha_ma'] = [] data[c + '_se_alpha_ma'] = [] data[c + '_psd_gamma_ma'] = [] data[c + '_se_gamma_ma'] = [] data[c + '_psd_beta_ma'] = [] data[c + '_se_beta_ma'] = [] data[c + '_psd_theta_relative'] = [] data[c + '_psd_alpha_relative'] = [] data[c + '_psd_gamma_relative'] = [] data[c + '_psd_beta_relative'] = [] len_data = round(len(dataset[c].to_numpy()) / 1600) secs = dataset[c].shape[0] / 400 samps = int(secs * 250) print(samps) signal_resample = resample(dataset[c].to_numpy(), samps) signal = pd.Series(filter_band(signal_resample, 250, 1, 50)) signal_ma = signal.rolling(window=3).mean() signal_ma[0] = signal[0] signal_ma[1] = signal[1] signal_ma = signal_ma.to_numpy() for i in range(0, len_data): data[c + '_psd_theta_ma'].append( bandpower(signal_ma[i * 1000:(i + 1) * 1000], [4, 8], 'welch', None)) data[c + '_se_theta_ma'].append( spectral_entropy(signal_ma[i * 1000:(i + 1) * 1000], range(4, 8), 250)) data[c + '_psd_alpha_ma'].append( bandpower(signal_ma[i * 1000:(i + 1) * 1000], [8, 14], 'welch', None)) data[c + '_se_alpha_ma'].append( spectral_entropy(signal_ma[i * 1000:(i + 1) * 1000], range(8, 14), 250)) data[c + '_psd_beta_ma'].append( bandpower(signal_ma[i * 1000:(i + 1) * 1000], [14, 30], 'welch', None)) data[c + '_se_beta_ma'].append( spectral_entropy(signal_ma[i * 1000:(i + 1) * 1000], range(14, 30), 250)) data[c + '_psd_gamma_ma'].append( bandpower(signal_ma[i * 1000:(i + 1) * 1000], [30, 50], 'welch', None)) data[c + '_se_gamma_ma'].append( spectral_entropy(signal_ma[i * 1000:(i + 1) * 1000], range(30, 50), 250)) data[c + '_psd_theta_relative'].append( bandpower(signal_ma[i * 1000:(i + 1) * 1000], [4, 8], 'welch', None, relative=True)) data[c + '_psd_alpha_relative'].append( bandpower(signal_ma[i * 1000:(i + 1) * 1000], [8, 14], 'welch', None, relative=True)) data[c + '_psd_beta_relative'].append( bandpower(signal_ma[i * 1000:(i + 1) * 1000], [14, 30], 'welch', None, relative=True)) data[c + '_psd_gamma_relative'].append( bandpower(signal_ma[i * 1000:(i + 1) * 1000], [30, 50], 'welch', None, relative=True)) else: data[c] = [] for i in range(0, len_data): data[c].append(dataset[c][i * 1600]) df = pd.DataFrame(data) print(df) df.to_csv("../../Database/SEED-VIG/dataset.csv", sep=";", index=False)
def apply_min_error_L1(self, signal): def apply_func(window): return min_errorL1(self.sample, window)[0] return signal.rolling(self.length, center=True).apply(apply_func, raw=True)
def apply_cross_correlate(self, signal): def apply_func(window): return (self.sample * window).mean() return signal.rolling(self.length, center=True).apply(apply_func, raw=True)
def apply_min(self, signal): return signal.rolling(self.length, center=True).min()
def apply_rms(self, signal): def apply_func(window): return np.sqrt((window * window).mean()) return signal.rolling(self.length, center=True).apply(apply_func, raw=True)
def process(dict): data = {} for c in dict.keys(): print("-----------{}-----------".format(c)) data[c + '_psd_theta_ma'] = [] data[c + '_se_theta_ma'] = [] data[c + '_psd_alpha_ma'] = [] data[c + '_se_alpha_ma'] = [] data[c + '_psd_gamma_ma'] = [] data[c + '_se_gamma_ma'] = [] data[c + '_psd_delta_ma'] = [] data[c + '_se_delta_ma'] = [] data[c + '_psd_beta_ma'] = [] data[c + '_se_beta_ma'] = [] data[c + '_psd_theta_relative'] = [] data[c + '_psd_alpha_relative'] = [] data[c + '_psd_gamma_relative'] = [] data[c + '_psd_beta_relative'] = [] signal = pd.Series(filter_band(dict[c].to_numpy(), 250, 1, 50)) signal_ma = signal.rolling(window=3).mean() signal_ma[0] = signal[0] signal_ma[1] = signal[1] signal_ma = signal_ma.to_numpy() data[c + '_psd_theta_ma'].append( bandpower(signal_ma, [4, 8], 'welch', None)) data[c + '_se_theta_ma'].append( spectral_entropy(signal_ma, range(4, 8), 250)) data[c + '_psd_alpha_ma'].append( bandpower(signal_ma, [8, 14], 'welch', None)) data[c + '_se_alpha_ma'].append( spectral_entropy(signal_ma, range(8, 14), 250)) data[c + '_psd_beta_ma'].append( bandpower(signal_ma, [14, 30], 'welch', None)) data[c + '_se_beta_ma'].append( spectral_entropy(signal_ma, range(14, 30), 250)) data[c + '_psd_gamma_ma'].append( bandpower(signal_ma, [30, 40], 'welch', None)) data[c + '_se_gamma_ma'].append( spectral_entropy(signal_ma, range(30, 50), 250)) data[c + '_psd_delta_relative'].append( bandpower(signal, [0.5, 4], 'welch', None, relative=True)) data[c + '_psd_theta_relative'].append( bandpower(signal, [4, 8], 'welch', None, relative=True)) data[c + '_psd_alpha_relative'].append( bandpower(signal, [8, 14], 'welch', None, relative=True)) data[c + '_psd_beta_relative'].append( bandpower(signal, [14, 30], 'welch', None, relative=True)) data[c + '_psd_gamma_relative'].append( bandpower(signal, [30, 40], 'welch', None, relative=True)) return pd.DataFrame(data)