def perform_search(from_city, to_city, region, metric): start = clock() for res, _, progress in nb.best_match(from_city, to_city, region, 900, progressive=True, metric=metric): # print(progress) try: distance, r_vids, center, radius = res except (ValueError, TypeError): import json desc = {"type": "Feature", "properties": {"nb_venues": len(res), "venues": res, "origin": from_city}, "geometry": region} with open('scratch.json', 'a') as out: out.write(json.dumps(desc, sort_keys=True, indent=2, separators=(',', ': '))+'\n') return if len(center) == 2: center = c.euclidean_to_geo(to_city, center) relevant = {'dst': distance, 'radius': radius, 'center': center, 'nb_venues': len(r_vids)} SEARCH_STATUS.update(dict(seen=False, progress=progress, res=relevant)) print("done search in {:.3f}".format(clock() - start)) SEARCH_STATUS.update(dict(seen=False, progress=1.0, done=True, res=relevant))
def batch_matching(query_city='paris'): """Match preselected regions of `query_city` into the other target cities""" import ujson global QUERY_NAME global OTMPDIR with open('static/ground_truth.json') as gt: regions = ujson.load(gt) districts = sorted(regions.keys()) cities = sorted(regions.values()[0]['gold'].keys()) assert query_city in cities cities.remove(query_city) OTMPDIR = os.path.join(OTMPDIR, 'www_comparaison_'+query_city) try: os.mkdir(OTMPDIR) except OSError: pass # cities = ['berlin'] # districts = ['montmartre', 'triangle'] for city in cities: print(city) for neighborhood in districts: # for _ in [1]: # for city, neighborhood in [('washington', 'marais'), ('washington', 'montmartre')]: print(neighborhood) possible_regions = regions[neighborhood]['gold'].get(query_city) rgeo = choose_query_region(possible_regions) if not rgeo: continue for metric in ['emd-itml', 'emd-tsne']: # for metric in ['jsd', 'emd', 'cluster', 'emd-lmnn', 'leftover']: print(metric) for radius in np.linspace(200, 500, 5): print(radius) QUERY_NAME = '{}_{}_{}_{}.my'.format(city, neighborhood, int(radius), metric) logging.info('will write: '+str(os.path.join(OTMPDIR, QUERY_NAME))) if os.path.isfile(os.path.join(OTMPDIR, QUERY_NAME)): continue res, values, _ = best_match(query_city, city, rgeo, radius, metric=metric).next() continue distance, r_vids, center, radius = res print(distance) if center is None: result = {'dst': distance, 'metric': metric, 'nb_venues': 0} else: center = cities.euclidean_to_geo(city, center) result = {'geo': {'type': 'circle', 'center': center, 'radius': radius}, 'dst': distance, 'metric': metric, 'nb_venues': len(r_vids)} regions[neighborhood][city].append(result) # outname = '{}_{}_{}_{}.png'.format(city, neighborhood, # int(radius), metric) # interpolate_distances(values, outname) with open('static/cpresets.js', 'w') as out: out.write('var PRESETS =' + ujson.dumps(regions) + ';')
def venues_to_geojson(vids, city): """Convert a list of venues id into a GeoJSON polygon""" mask = itemgetter(*vids)(cities_index[city]) locs = cities_venues[city][mask, :] hull = locs[ConvexHull(locs).vertices, :] geohull = c.euclidean_to_geo(city, hull) return sgeo.mapping(sgeo.Polygon(np.fliplr(geohull)))
def circle_to_poly(city, geo, resolution=4): """Discretize a circle (LngLat) to a Shapely polygon (LatLng)""" center, radius = geo['center'], geo['radius'] local_center = c.GEO_TO_2D[city](list(reversed(center))) approx = sgeo.Point(*local_center).buffer(radius, resolution) local_exterior = c.euclidean_to_geo(city, np.array(approx.exterior.coords)) return sgeo.Polygon(np.fliplr(local_exterior).tolist())
def to_json(city, cell, pos, alt=False): """Convert a `cell` ranked `pos` in `city` to a GeoJSONish dict""" distance, venues, center, radius, metric = cell suffix = '_alt' if alt else '' center = cities.euclidean_to_geo(city, center) return {'geo': {'type': 'circle', 'center': center, 'radius': radius}, 'dst': distance, 'metric': metric+suffix, 'venues': venues, 'pos': pos}
def to_json(city, cell, pos, alt=False): """Convert a `cell` ranked `pos` in `city` to a GeoJSONish dict""" distance, venues, center, radius, metric = cell suffix = '_alt' if alt else '' center = cities.euclidean_to_geo(city, center) return { 'geo': { 'type': 'circle', 'center': center, 'radius': radius }, 'dst': distance, 'metric': metric + suffix, 'venues': venues, 'pos': pos }
def perform_search(from_city, to_city, region, metric): start = clock() for res, _, progress in nb.best_match(from_city, to_city, region, 900, progressive=True, metric=metric): # print(progress) try: distance, r_vids, center, radius = res except (ValueError, TypeError): import json desc = { "type": "Feature", "properties": { "nb_venues": len(res), "venues": res, "origin": from_city }, "geometry": region } with open('scratch.json', 'a') as out: out.write( json.dumps( desc, sort_keys=True, indent=2, separators=(',', ': ')) + '\n') return if len(center) == 2: center = c.euclidean_to_geo(to_city, center) relevant = { 'dst': distance, 'radius': radius, 'center': center, 'nb_venues': len(r_vids) } SEARCH_STATUS.update(dict(seen=False, progress=progress, res=relevant)) print("done search in {:.3f}".format(clock() - start)) SEARCH_STATUS.update( dict(seen=False, progress=1.0, done=True, res=relevant))
NTEST = 2000 city, districts = sys.argv[1], [] city_info = load_data(city) gold_list = city_info[-1] districts = sorted([nn for nn, gold in gold_list.iteritems() if city in gold['gold']]) try: os.mkdir('random') except OSError: pass for district in districts: savename = 'random/{}_{}.my'.format(city, district) print(savename) if os.path.isfile(savename): continue distrib, best_score, best_region = [], 0, None for i in range(NTEST): regions, score = mock_random_list(city, district, city_info) if score > best_score: best_score, best_region = score, regions distrib.append(score) p.save_var(savename, distrib) outjson = [{ 'pos': rank+1, 'metric': 'random', 'dst': -1, 'venues': r[1], 'geo': mapping(Polygon(np.fliplr(c.euclidean_to_geo(city, r[0]))))} for rank, r in enumerate(best_region)] filename = 'static/random_{}_{}.json'.format(city, district) with open(filename, 'w') as f: json.dump(outjson, f, sort_keys=True, indent=2, separators=(',', ': '))
savename = 'random/{}_{}.my'.format(city, district) print(savename) if os.path.isfile(savename): continue distrib, best_score, best_region = [], 0, None for i in range(NTEST): regions, score = mock_random_list(city, district, city_info) if score > best_score: best_score, best_region = score, regions distrib.append(score) p.save_var(savename, distrib) outjson = [{ 'pos': rank + 1, 'metric': 'random', 'dst': -1, 'venues': r[1], 'geo': mapping(Polygon(np.fliplr(c.euclidean_to_geo(city, r[0])))) } for rank, r in enumerate(best_region)] filename = 'static/random_{}_{}.json'.format(city, district) with open(filename, 'w') as f: json.dump(outjson, f, sort_keys=True, indent=2, separators=(',', ': '))
def batch_matching(query_city='paris'): """Match preselected regions of `query_city` into the other target cities""" import ujson global QUERY_NAME global OTMPDIR with open('static/ground_truth.json') as gt: regions = ujson.load(gt) districts = sorted(regions.keys()) cities = sorted(regions.values()[0]['gold'].keys()) assert query_city in cities cities.remove(query_city) OTMPDIR = os.path.join(OTMPDIR, 'www_comparaison_' + query_city) try: os.mkdir(OTMPDIR) except OSError: pass # cities = ['berlin'] # districts = ['montmartre', 'triangle'] for city in cities: print(city) for neighborhood in districts: # for _ in [1]: # for city, neighborhood in [('washington', 'marais'), ('washington', 'montmartre')]: print(neighborhood) possible_regions = regions[neighborhood]['gold'].get(query_city) rgeo = choose_query_region(possible_regions) if not rgeo: continue for metric in ['emd-itml', 'emd-tsne']: # for metric in ['jsd', 'emd', 'cluster', 'emd-lmnn', 'leftover']: print(metric) for radius in np.linspace(200, 500, 5): print(radius) QUERY_NAME = '{}_{}_{}_{}.my'.format( city, neighborhood, int(radius), metric) logging.info('will write: ' + str(os.path.join(OTMPDIR, QUERY_NAME))) if os.path.isfile(os.path.join(OTMPDIR, QUERY_NAME)): continue res, values, _ = best_match(query_city, city, rgeo, radius, metric=metric).next() continue distance, r_vids, center, radius = res print(distance) if center is None: result = { 'dst': distance, 'metric': metric, 'nb_venues': 0 } else: center = cities.euclidean_to_geo(city, center) result = { 'geo': { 'type': 'circle', 'center': center, 'radius': radius }, 'dst': distance, 'metric': metric, 'nb_venues': len(r_vids) } regions[neighborhood][city].append(result) # outname = '{}_{}_{}_{}.png'.format(city, neighborhood, # int(radius), metric) # interpolate_distances(values, outname) with open('static/cpresets.js', 'w') as out: out.write('var PRESETS =' + ujson.dumps(regions) + ';')