def pkg_dtw(file_name, line_num, df): file_name = 'data/' + file_name y_list = load_data(file_name, line_num) query, reference = cal_warped_signals(y_list) # plot warped signal # downsample times xvals, yinterp = get_warped_signals(query, cf.ds_time) # calculate the corresponding point pair query.drop(['shift', 't'], axis=1) query2 = pd.DataFrame({'t': xvals, 'q': yinterp}) query2['close_index'] = 0 true_align_dict = get_true_aligned(cf.ds_time, query, query2) group_num_dict = get_group_number(true_align_dict, query) d, cost_matrix, acc_cost_matrix, path = dtw(reference[['t', 'q']].values, query2[['t', 'q']].values, dist=norm) fact_align_dict = get_fact_align(path) reverse_dict = get_reverse_dict(path) error_rate = get_k_accuracy(true_align_dict, fact_align_dict, group_num_dict) SS1 = get_SS1(fact_align_dict, cf.ds_time) SS2 = get_SS2(fact_align_dict, reverse_dict, cf.ds_time) df.loc[line_num] = [error_rate, SS1, SS2] return df
def event_dtw(file_name, line_num, df): file_name = 'data/' + file_name y_list = load_data(file_name, line_num) query, reference = cal_warped_signals(y_list) reference['upslope'] = 0 reference['downslope'] = 0 # plot warped signal # downsample times xvals, yinterp = get_warped_signals(query, cf.ds_time) # calculate the corresponding point pair query.drop('shift', axis=1) query.drop('t', axis=1) query2 = pd.DataFrame({'t': xvals, 'q': yinterp}) query2['close_index'] = 0 query2['upslope'] = 0 query2['downslope'] = 0 true_align_dict = get_true_aligned(cf.ds_time, query, query2) group_num_dict = get_group_number(true_align_dict, query) raw_reference_uslope, reference_upslope = get_upslope_endings( reference['q'], cf.refer_percent) raw_query_uslope, query_upslope = get_upslope_endings( query2['q'], cf.query_percent) raw_reference_downlope, reference_downslope = get_downslope_endings( reference['q'], cf.refer_percent) raw_query_downlope, query_downslope = get_downslope_endings( query2['q'], cf.query_percent) rising_edge_grps = edge_matching(reference, query2, reference_upslope, query_upslope) down_edge_grps = edge_matching(reference, query2, reference_downslope, query_downslope) calculate_event(rising_edge_grps, reference, query2, True) calculate_event(down_edge_grps, reference, query2, False) d, cost_matrix, acc_cost_matrix, path = dtw( reference[['t', 'q', 'upslope', 'downslope']].values, query2[['t', 'q', 'upslope', 'downslope']].values, dist=norm) fact_align_dict = get_fact_align(path) reverse_dict = get_reverse_dict(path) error_rate = get_k_accuracy(true_align_dict, fact_align_dict, group_num_dict) SS1 = get_SS1(fact_align_dict, cf.ds_time) SS2 = get_SS2(fact_align_dict, reverse_dict, cf.ds_time) df.loc[line_num] = [error_rate, SS1, SS2] return df
def pkg_shapedtw(file_name, line_num, df): file_name = 'data/' + file_name y_list = load_data(file_name, line_num) query, reference = cal_warped_signals(y_list) # plot warped signal xvals, yinterp = get_warped_signals(query, cf.ds_time) # normalize the signal reference_norm = stats.zscore(reference['q']) yinterp_norm = stats.zscore(yinterp) # store the corresponding point pair query.drop(['shift', 't'], axis=1) query2 = pd.DataFrame({'t': xvals, 'q': yinterp}) query2['close_index'] = 0 true_align_dict = get_true_aligned(cf.ds_time, query, query2) group_num_dict = get_group_number(true_align_dict, query) refer_subsequences = samplingSequences(reference_norm, sub_len) query_subsequences = samplingSequences(yinterp_norm, int(sub_len / cf.ds_time)) refer_descriptors = np.zeros((len(refer_subsequences), nBlocks * 8)) query_descriptors = np.zeros((len(query_subsequences), nBlocks * 8)) refer_nsubsequences = len(refer_subsequences) query_nsubsequences = len(query_subsequences) for i in range(refer_nsubsequences): sub_seq = refer_subsequences[i] refer_descriptors[i] = cal_descriptor(sub_seq, sub_len) for i in range(query_nsubsequences): sub_seq = query_subsequences[i] query_descriptors[i] = cal_descriptor(sub_seq, int(sub_len / cf.ds_time)) d, cost_matrix, acc_cost_matrix, path = dtw(refer_descriptors, query_descriptors, dist=norm) fact_align_dict = get_fact_align(path) reverse_dict = get_reverse_dict(path) error_rate = get_k_accuracy(true_align_dict, fact_align_dict, group_num_dict) SS1 = get_SS1(fact_align_dict, cf.ds_time) SS2 = get_SS2(fact_align_dict, reverse_dict, cf.ds_time) df.loc[line_num] = [error_rate, SS1, SS2] return df
import pandas as pd import math import numpy as np from scipy import stats from parameter_cal import cf from dtw import dtw from scipy.misc import * from sdtw.config import sub_len, nBlocks from sdtw.utils import norm, cal_refer_query_descriptor from parameter_cal.utils import get_fact_align, get_reverse_dict, get_k_accuracy_same, get_W, get_SS2, get_true_align, get_link_graph from parameter_cal.utils import load_data, plot_warped_signals, cal_warped_signals import matplotlib.pyplot as plt y_list = load_data(True) query, reference = cal_warped_signals(y_list) # plot warped signal xvals, yinterp = plot_warped_signals(reference, query, 1) # normalize the signal reference_norm = stats.zscore(reference['q']) yinterp_norm = stats.zscore(yinterp) # store the corresponding point pair query.drop('shift', axis=1) query.drop('t', axis=1) query2 = pd.DataFrame(yinterp) query2['aligned_index'] = 0 query2['t'] = query['t'] query2.columns = ['q', 'aligned_index', 't']
import math from parameter_cal import cf from dtw import dtw from downsample.utils import get_true_aligned, get_group_number from parameter_cal.utils import get_SS2, get_group_devi, get_SS1, get_fact_align, get_reverse_dict, get_link_graph from parameter_cal.utils import load_data, plot_warped_signals, cal_warped_signals from downsample.utils import slope_col, reference_slope_col, get_k_accuracy from debug.dbd_cf import debug_file, debug_line def norm(x, y): return math.fabs(x[1] - y[1]) # generate warped signal y_list = load_data(debug_file, debug_line) y_list = y_list[:-(len(y_list) % cf.ds_time)] query, reference = cal_warped_signals(y_list, 'right') # plot warped signal # downsample times xvals, yinterp = plot_warped_signals(reference, query, cf.ds_time) query2 = pd.DataFrame({'t': xvals, 'q': yinterp}) # calculate the corresponding point pair query.drop(['shift', 't'], axis=1) query2['close_index'] = 0 true_align_dict = get_true_aligned(cf.ds_time, query, query2) group_num_dict = get_group_number(true_align_dict, query) reference_slope_col(reference, cf.ds_time)
pairs.append([refer_index, index]) get = 1 break if get == 1: continue return pairs def norm(x, y): # return math.fabs(x[1] - y[1]) return math.fabs(x[1] - y[1]) + math.fabs(x[2] - y[2]) + math.fabs(x[3] - y[3]) # generate warped signal y_list = load_data('data/Beef_TRAIN') query, reference = cal_warped_signals(y_list) reference['upslope'] = 0 reference['downslope'] = 0 # plot warped signal # downsample times xvals, yinterp = plot_warped_signals(reference, query) # calculate the corresponding point pair query.drop('shift', axis=1) query.drop('t', axis=1) query2 = pd.DataFrame({'t': xvals, 'q': yinterp}) query2['close_index'] = 0 query2['upslope'] = 0