def SSSTSR_search(whole_ts, left_idx, right_idx, query, sequence, ts, rtv_len, uuid, invariance): # TODO: remove the hardcode somooth_window_len = -1 zero_thresh=0.000005 # 3b00277d-d513-43a7-a54f-942a402c507e zero_thresh=0.001 minSize = 5 a = SSSTSR_Class(ts=whole_ts, smooth_window_len = somooth_window_len, zero_thresh=zero_thresh) a.build_seg(plotSwitch = False, minSize=minSize) a.seg_enhance_represent() print "Number of segments: " + str(len(a.enhance_seg)) # b = SSSTSR_Class(ts=query, smooth_window_len = somooth_window_len, zero_thresh=zero_thresh, scale_train_ts = a) # b.build_seg(plotSwitch = False, minSize=minSize) # b.seg_enhance_represent() seg_left_idx = a.get_seg_num_from_idx(left_idx) seg_right_idx = a.get_seg_num_from_idx(right_idx) knn_candidates_segmentwise = segmentwise_match_invariance(a.enhance_seg[seg_left_idx:seg_right_idx+1], a.enhance_seg[:seg_left_idx], 1, 3, offset_inv = invariance['offset'], longitude_inv=invariance['longitudinal'], amplitude_inv=invariance['amplitude'], lineardrift_inv=invariance['linear']) output = {} output['n'] = 3 output['list'] = [] # add query ts query_dict = {} query_dict['ts'] = sample_sensor_data(ts[left_idx:right_idx], rtv_len) query_dict['start_time'] = ts[left_idx][0] query_dict['end_time'] = ts[right_idx-1][0] output['query'] = query_dict segs = a.get_seg_ts() for i, candidate in enumerate(knn_candidates_segmentwise): dist = candidate[1] pos = candidate[0] len_matched_word = seg_right_idx - seg_left_idx + 1 match_str_startpos = (segs[pos])[0] match_str_endpos = (segs[pos+len_matched_word-1])[1] search_ts = sample_sensor_data(ts[match_str_startpos:match_str_endpos], rtv_len) rtv = {} print dist rtv['dist'] = dist rtv['ts'] = search_ts rtv['start_time'] = search_ts[0][0] rtv['end_time'] = search_ts[-1][0] output['list'].append(copy.deepcopy(rtv)) return output
def SSSTSR_DTW_search(whole_ts, left_idx, right_idx, query, sequence, ts, rtv_len, uuid, invariance): # TODO: remove the hardcode somooth_window_len = -1 zero_thresh=0.000005 # 3b00277d-d513-43a7-a54f-942a402c507e zero_thresh=0.001 minSize = 5 a = SSSTSR_Class(ts=whole_ts, smooth_window_len = somooth_window_len, zero_thresh=zero_thresh) a.build_seg(plotSwitch = False, minSize=minSize) a.seg_enhance_represent() print len(a.enhance_seg) # b = SSSTSR_Class(ts=query, smooth_window_len = somooth_window_len, zero_thresh=zero_thresh, scale_train_ts = a) # b.build_seg(plotSwitch = False, minSize=minSize) # b.seg_enhance_represent() seg_left_idx = a.get_seg_num_from_idx(left_idx) seg_right_idx = a.get_seg_num_from_idx(right_idx) match_pos_list, matching_words, match_dist_list, search_path = DTW_seg_match(a.enhance_seg[seg_left_idx:seg_right_idx+1], a.enhance_seg[:seg_left_idx],3, offset_inv = invariance['offset'], longitude_inv=invariance['longitudinal'], amplitude_inv=invariance['amplitude'], lineardrift_inv=invariance['linear']) output = {} output['n'] = 3 output['list'] = [] # add query ts query_dict = {} query_dict['ts'] = sample_sensor_data(ts[left_idx:right_idx], rtv_len) query_dict['start_time'] = ts[left_idx][0] query_dict['end_time'] = ts[right_idx-1][0] output['query'] = query_dict segs = a.get_seg_ts() for i in range(len(match_pos_list)): len_matched_word = len(matching_words[i]) pos = match_pos_list[i] dist = match_dist_list[i] match_str_startpos = (segs[pos])[0] match_str_endpos = (segs[pos+len_matched_word-1])[1] search_ts = sample_sensor_data(ts[match_str_startpos:match_str_endpos], rtv_len) rtv = {} rtv['dist'] = dist rtv['ts'] = search_ts rtv['start_time'] = search_ts[0][0] rtv['end_time'] = search_ts[-1][0] output['list'].append(copy.deepcopy(rtv)) return output