def label_transition_data(filename_to_label, **kwargs): radius = kwargs['radius'] if 'radius' in kwargs else RADIUS filename = kwargs['filename'] if 'filename' in kwargs else None window = kwargs['window'] if 'window' in kwargs else WINDOW n = kwargs['n'] if 'n' in kwargs else N transition_probabilities = viterbi_training.run_viterbi(filename_to_label,filename=filename, radius=radius, window=window,n=n) results = read_resulting_path('matched_files/Shopping2Rental_matched.csv') if filename is not None: with open(filename, 'w') as f: f.write('Label, Distance Score, Backtrack Score \n') for t, time in enumerate(transition_probabilities): for seg1 in transition_probabilities[t]: for seg2 in transition_probabilities[t][seg1]: [way2_id,way2_index] = str(seg2).split(',') start_node = get_node_id(int(way2_id), int(way2_index)) end_node = get_node_id(int(way2_id), int(way2_index)+1) node_ids=(start_node[0],end_node[0]) node_ids2 = (end_node[0],start_node[0]) if ((node_ids == results[t]) or (node_ids2 == results[t])): f.write('1, '+str(transition_probabilities[t][seg1][seg2][0]) + ', ' + str(transition_probabilities[t][seg1][seg2][1]) +'\n') else: f.write('-1, ' +str(transition_probabilities[t][seg1][seg2][0]) + ', ' + str(transition_probabilities[t][seg1][seg2][1]) +'\n') return return
def get_node_ids(matches): node_ids = [] for i, match in enumerate(matches): if match['way'] is None: node_ids.append(None) continue # Don't query the same point twice if i == 0 or match['way'] != matches[i-1]['way'] or match['index_of_segment'] != matches[i-1]['index_of_segment']: start_node = get_node_id(match['way'], match['index_of_segment']) start_node = re.findall(r'\d+', str(start_node))[0] end_node = get_node_id(match['way'], match['index_of_segment'] + 1) end_node = re.findall(r'\d+', str(end_node))[0] node_ids.append((start_node, end_node)) return node_ids
def get_node_ids(matches): node_ids = [] for i, match in enumerate(matches): if match['way'] is None: node_ids.append(None) continue # Don't query the same point twice if i == 0 or match['way'] != matches[i - 1]['way'] or match[ 'index_of_segment'] != matches[i - 1]['index_of_segment']: start_node = get_node_id(match['way'], match['index_of_segment']) start_node = re.findall(r'\d+', str(start_node))[0] end_node = get_node_id(match['way'], match['index_of_segment'] + 1) end_node = re.findall(r'\d+', str(end_node))[0] node_ids.append((start_node, end_node)) return node_ids
def label_emission_data(file_to_label, **kwargs): radius = kwargs['radius'] if 'radius' in kwargs else RADIUS filename = kwargs['filename'] if 'filename' in kwargs else None window = kwargs['window'] if 'window' in kwargs else WINDOW n = kwargs['n'] if 'n' in kwargs else N distance_only = kwargs[ 'distance_only'] if 'distance_only' in kwargs else False tmpfile = 'matched_files/Rental2Youssef_matched.csv' #viterbi.run_viterbi(file_to_label,filename=tmpfile, radius=radius, window=window,n=n) observations = read_observations(file_to_label) results = read_resulting_path(tmpfile) labeled = [] for t, obs in enumerate(observations): possible_segments, prob, point = emission_probability.compute_emission_probabilities( obs, radius, n) for i, seg in enumerate(possible_segments): start_node = get_node_id(seg['way_osm_id'], seg['index_in_way']) end_node = get_node_id(seg['way_osm_id'], seg['index_in_way'] + 1) node_ids = (start_node[0], end_node[0]) node_ids2 = (end_node[0], start_node[0]) if ((node_ids == results[t]) or (node_ids2 == results[t])): if (distance_only): labeled.append(seg['distance']) else: labeled.append((node_ids, seg['distance_score'], seg['tangent_score'], 1)) else: if (distance_only == False): labeled.append((node_ids, seg['distance_score'], seg['tangent_score'], -1)) if filename is not None: with open(filename, 'w') as f: f.write('Label, Distance Score, Tangent Score \n') for l in labeled: if (distance_only): f.write(str(l) + '\n') else: f.write( str(l[3]) + ', ' + str(l[1]) + ', ' + str(l[2]) + '\n') return return labeled
def label_transition_data(filename_to_label, **kwargs): radius = kwargs['radius'] if 'radius' in kwargs else RADIUS filename = kwargs['filename'] if 'filename' in kwargs else None window = kwargs['window'] if 'window' in kwargs else WINDOW n = kwargs['n'] if 'n' in kwargs else N transition_probabilities = viterbi_training.run_viterbi(filename_to_label, filename=filename, radius=radius, window=window, n=n) results = read_resulting_path('matched_files/Shopping2Rental_matched.csv') if filename is not None: with open(filename, 'w') as f: f.write('Label, Distance Score, Backtrack Score \n') for t, time in enumerate(transition_probabilities): for seg1 in transition_probabilities[t]: for seg2 in transition_probabilities[t][seg1]: [way2_id, way2_index] = str(seg2).split(',') start_node = get_node_id(int(way2_id), int(way2_index)) end_node = get_node_id(int(way2_id), int(way2_index) + 1) node_ids = (start_node[0], end_node[0]) node_ids2 = (end_node[0], start_node[0]) if ((node_ids == results[t]) or (node_ids2 == results[t])): f.write('1, ' + str(transition_probabilities[t] [seg1][seg2][0]) + ', ' + str(transition_probabilities[t][seg1][seg2] [1]) + '\n') else: f.write('-1, ' + str(transition_probabilities[t] [seg1][seg2][0]) + ', ' + str(transition_probabilities[t][seg1][seg2] [1]) + '\n') return return
def label_emission_data(file_to_label, **kwargs): radius = kwargs['radius'] if 'radius' in kwargs else RADIUS filename = kwargs['filename'] if 'filename' in kwargs else None window = kwargs['window'] if 'window' in kwargs else WINDOW n = kwargs['n'] if 'n' in kwargs else N distance_only = kwargs['distance_only'] if 'distance_only' in kwargs else False tmpfile = 'matched_files/Rental2Youssef_matched.csv' #viterbi.run_viterbi(file_to_label,filename=tmpfile, radius=radius, window=window,n=n) observations = read_observations(file_to_label) results = read_resulting_path(tmpfile) labeled =[] for t, obs in enumerate(observations): possible_segments, prob, point = emission_probability.compute_emission_probabilities(obs,radius,n) for i,seg in enumerate(possible_segments): start_node = get_node_id(seg['way_osm_id'],seg['index_in_way']) end_node = get_node_id(seg['way_osm_id'],seg['index_in_way']+1) node_ids=(start_node[0],end_node[0]) node_ids2 = (end_node[0],start_node[0]) if ((node_ids == results[t]) or (node_ids2 == results[t])): if (distance_only): labeled.append(seg['distance']) else: labeled.append((node_ids,seg['distance_score'],seg['tangent_score'], 1)) else: if (distance_only == False): labeled.append((node_ids,seg['distance_score'],seg['tangent_score'], -1)) if filename is not None: with open(filename, 'w') as f: f.write('Label, Distance Score, Tangent Score \n') for l in labeled: if (distance_only): f.write(str(l) + '\n') else: f.write(str(l[3]) + ', ' + str(l[1]) +', '+str(l[2]) + '\n') return return labeled