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
y = query['q'] yinterp = np.array(np.interp(xvals, x, y)) xvals = np.array(xvals) ax.scatter(x=xvals, y=yinterp, marker='.', c='g', label='after interp') ax.legend(fontsize='30') # 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({'t': xvals, 'q2': 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) query2.loc[len(query2) - 1, 'close_index'] = len(query) - 1 for i in range(len(query2) - 1): for j in range(len(query['t2']) - 1): if query['t2'][j] <= query2['t'][i] < query['t2'][j + 1]: if abs(query2['q2'][i] - query['q'][j]) < abs(query2['q2'][i] - query['q'][j + 1]): query2.loc[i, 'close_index'] = j else: query2.loc[i, 'close_index'] = j + 1 if sub_len % 2 == 0: raise Exception("Sub_len must be odd number!") refer_subsequences = samplingSequences(reference_norm, sub_len)