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
if sub_len % 2 == 0: raise Exception("Sub_len must be odd number!") 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) query2.columns = ['t2', 'q', 'close_index'] # adapt to the get_link_graph get_link_graph(reference, query2, path, -3, 'downsampled shapedtw') fact_align_dict = get_fact_align(path) reverse_dict = get_reverse_dict(path) print("error rate of shapedtw is " + str(get_k_accuracy(true_align_dict, fact_align_dict, group_num_dict))) print("SS1 of shapedtw is " + str(get_SS1(path, cf.ds_time))) print("SS2 of shapedtw is " + str(get_SS2(fact_align_dict, reverse_dict, ds_time)))
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) slope_col(query2) d, cost_matrix, acc_cost_matrix, path = dtw(reference[['t', 'avg_slope']].values, query2[['t', 'slope']].values, dist=norm) get_link_graph(reference, query2, path, -3, 'Downsampled signal with dDTW', '(b) dDTW') fact_align_dict = get_fact_align(path) reverse_dict = get_reverse_dict(path) print("path = " + str(len(path[0]))) print('group = ' + str(get_k_accuracy(true_align_dict, fact_align_dict, group_num_dict))) print("SS1 of ddtw is " + str(get_SS1(fact_align_dict, cf.ds_time))) print("SS2 of ddtw is " + str(get_SS2(fact_align_dict, reverse_dict, cf.ds_time)))