def time_series_variance(x): """ :param x: the time series to calculate the feature of :type x: pandas.Series :return: the value of this feature :return type: float """ return ts_feature_calculators.variance(x)
def TS_features12(signal): stand_deviation = ts.standard_deviation(signal) sum_reoccurring = ts.sum_of_reoccurring_data_points(signal) sum_r_value = ts.sum_of_reoccurring_values(signal) sum_v = ts.sum_values(signal) variance = ts.variance(signal) variance_larger_than_sd = ts.variance_larger_than_standard_deviation( signal) return stand_deviation, sum_reoccurring, sum_r_value, sum_v, variance, variance_larger_than_sd
def scalar_feature_extraction(column): retval = np.zeros([1, 10], dtype=float) retval[0][0] = tffe.count_above_mean(column.values) retval[0][1] = tffe.mean(column.values) retval[0][2] = tffe.maximum(column.values) retval[0][3] = tffe.median(column.values) retval[0][4] = tffe.minimum(column.values) retval[0][5] = tffe.sample_entropy(column.values) if (isNaN(retval[0][5])): retval[0][5] = 0 retval[0][6] = tffe.skewness(column.values) retval[0][7] = tffe.variance(column.values) retval[0][8] = tffe.longest_strike_above_mean(column.values) retval[0][9] = tffe.longest_strike_below_mean(column.values) return retval
def extract_heartrate_tsfresh(transformed: np.ndarray) -> np.ndarray: """Extract all tsfresh features from heart rate.""" ecg_features = [] print("Extracting TSFRESH statistics from heart rate signals...") for x in tqdm(transformed): vchange_quantiles_abs = change_quantiles(x[:, -1], 0, 0.8, True, "var") vfft_aggregated_k = list( fft_aggregated(x[:, -1], [{ "aggtype": "kurtosis" }]))[0][1] vmean_abs_change = mean_abs_change(x[:, -1]) vabsolute_sum_of_changes = absolute_sum_of_changes(x[:, -1]) vfft_aggregated_s = list( fft_aggregated(x[:, -1], [{ "aggtype": "skew" }]))[0][1] vfft_aggregated_c = list( fft_aggregated(x[:, -1], [{ "aggtype": "centroid" }]))[0][1] vvariance = variance(x[:, -1]) vvariation_coefficient = variation_coefficient(x[:, -1]) new_tsfresh = np.array([ vchange_quantiles_abs, vfft_aggregated_k, vmean_abs_change, vabsolute_sum_of_changes, vfft_aggregated_s, vfft_aggregated_c, vvariance, vvariation_coefficient, ]) ecg_features.append(np.concatenate(new_tsfresh, axis=0)) return np.array(ecg_features)
def log_variance(x): return np.log(fc.variance(x))
def variance(current_observation: pd.DataFrame, raw_key: str): return tsf.variance(current_observation[raw_key])
def main(): dirname = os.path.realpath('.') excelF = dirname + '\\Summary.xlsx' myworkbook = openpyxl.load_workbook(excelF) worksheet = myworkbook['SummaryPatients'] file = 1 for filename in glob.glob(dirname + "\*.txt"): data = open(filename, 'r') totalData = {} time = [] totalForceL = [] totalForceR = [] id = [] for line in data: tempForce = line.split() id.append(1) time.append(float(tempForce[0])) totalForceL.append(float(tempForce[17])) totalForceR.append(float(tempForce[18])) totalData["id"] = id totalData["time"] = time totalData["totalForceL"] = totalForceL totalData["totalForceR"] = totalForceR dataPandas = pd.DataFrame.from_dict(totalData) extracted_features = {} #extract_featuresL = extract_features(dataPandas, column_id="id", column_kind=None, column_value=None) worksheet['A' + str(file + 1)] = file if 'Pt' in filename: worksheet['B' + str(file + 1)] = 1 else: worksheet['B' + str(file + 1)] = 0 worksheet['C' + str(file + 1)] = tf.abs_energy( totalData["totalForceL"]) worksheet['D' + str(file + 1)] = tf.abs_energy( totalData["totalForceR"]) worksheet['E' + str(file + 1)] = tf.kurtosis(totalData["totalForceL"]) worksheet['F' + str(file + 1)] = tf.kurtosis(totalData["totalForceR"]) worksheet['G' + str(file + 1)] = tf.skewness(totalData["totalForceL"]) worksheet['H' + str(file + 1)] = tf.skewness(totalData["totalForceR"]) worksheet['I' + str(file + 1)] = tf.median(totalData["totalForceL"]) worksheet['J' + str(file + 1)] = tf.median(totalData["totalForceR"]) worksheet['K' + str(file + 1)] = tf.mean(totalData["totalForceL"]) worksheet['L' + str(file + 1)] = tf.mean(totalData["totalForceR"]) worksheet['M' + str(file + 1)] = tf.variance(totalData["totalForceL"]) worksheet['N' + str(file + 1)] = tf.variance(totalData["totalForceR"]) temp = tf.fft_aggregated(totalData["totalForceL"], [{ "aggtype": "centroid" }, { "aggtype": "variance" }, { "aggtype": "skew" }, { "aggtype": "kurtosis" }]) int = 0 for list in temp: if int == 0: worksheet['O' + str(file + 1)] = list[1] if int == 1: worksheet['P' + str(file + 1)] = list[1] if int == 2: worksheet['Q' + str(file + 1)] = list[1] if int == 3: worksheet['R' + str(file + 1)] = list[1] int += 1 temp2 = tf.fft_aggregated(totalData["totalForceR"], [{ "aggtype": "centroid" }, { "aggtype": "variance" }, { "aggtype": "skew" }, { "aggtype": "kurtosis" }]) int = 0 for list in temp2: if int == 0: worksheet['S' + str(file + 1)] = list[1] if int == 1: worksheet['T' + str(file + 1)] = list[1] if int == 2: worksheet['U' + str(file + 1)] = list[1] if int == 3: worksheet['V' + str(file + 1)] = list[1] int += 1 file += 1 myworkbook.save(excelF)
def get_variance(arr): res = np.array([variance(arr)]) res = np.nan_to_num(res) return res
def main(): dirname = os.path.realpath('.') filename = dirname + '\\GaPt07_01.txt' data = open(filename, 'r') totalData = {} time = [] totalForceL = [] totalForceR = [] id = [] for line in data: tempForce = line.split() id.append(1) time.append(float(tempForce[0])) totalForceL.append(float(tempForce[17])) totalForceR.append(float(tempForce[18])) totalData["id"] = id totalData["time"] = time totalData["totalForceL"] = totalForceL totalData["totalForceR"] = totalForceR dataPandas = pd.DataFrame.from_dict(totalData) extracted_features = {} #extract_featuresL = extract_features(dataPandas, column_id="id", column_kind=None, column_value=None) extracted_features["absEnergyL"] = tf.abs_energy(totalData["totalForceL"]) extracted_features["absEnergyR"] = tf.abs_energy(totalData["totalForceR"]) extracted_features["kurtosisL"] = tf.kurtosis(totalData["totalForceL"]) extracted_features["kurtosisR"] = tf.kurtosis(totalData["totalForceR"]) extracted_features["skewnessL"] = tf.skewness(totalData["totalForceL"]) extracted_features["skewnessR"] = tf.skewness(totalData["totalForceR"]) extracted_features["medianL"] = tf.median(totalData["totalForceL"]) extracted_features["medianR"] = tf.median(totalData["totalForceR"]) extracted_features["meanL"] = tf.mean(totalData["totalForceL"]) extracted_features["meanR"] = tf.mean(totalData["totalForceR"]) extracted_features["varianceL"] = tf.variance(totalData["totalForceL"]) extracted_features["varianceR"] = tf.variance(totalData["totalForceR"]) temp = tf.fft_aggregated(totalData["totalForceL"], [{ "aggtype": "centroid" }, { "aggtype": "variance" }, { "aggtype": "skew" }, { "aggtype": "kurtosis" }]) int = 0 for list in temp: if int == 0: extracted_features["fftCentroidL"] = list if int == 1: extracted_features["fftVarianceL"] = list if int == 2: extracted_features["fftSkewL"] = list if int == 3: extracted_features["fftKurtosisL"] = list int += 1 temp2 = tf.fft_aggregated(totalData["totalForceR"], [{ "aggtype": "centroid" }, { "aggtype": "variance" }, { "aggtype": "skew" }, { "aggtype": "kurtosis" }]) int = 0 for list in temp2: if int == 0: extracted_features["fftCentroidR"] = list if int == 1: extracted_features["fftVarianceR"] = list if int == 2: extracted_features["fftSkewR"] = list if int == 3: extracted_features["fftKurtosisR"] = list int += 1