Ejemplo n.º 1
0
def compare(origin, dest, knn):
    """Compare two cities."""
    global ORIGIN
    global DEST
    origin = 'barcelona' if origin not in c.SHORT_KEY else origin
    dest = 'helsinki' if dest not in c.SHORT_KEY else dest
    ORIGIN = cn.gather_info(origin, knn, raw_features=True)
    DEST = cn.gather_info(dest, knn, raw_features=True)
    return f.render_template('cnn.html', origin=origin, dest=dest, knn=knn,
                             lbbox=c.BBOXES[origin], rbbox=c.BBOXES[dest])
Ejemplo n.º 2
0
def neighborhoods(origin, dest):
    """Match neighborhoods."""
    global ORIGIN
    global DEST
    origin = 'paris' if origin not in c.SHORT_KEY else origin
    dest = 'helsinki' if dest not in c.SHORT_KEY else dest
    ORIGIN = cn.gather_info(origin, 1, raw_features=True)
    DEST = cn.gather_info(dest, 1, raw_features=True)
    return f.render_template('nei.html', origin=origin, dest=dest,
                             lbbox=c.BBOXES[origin], rbbox=c.BBOXES[dest])
Ejemplo n.º 3
0
def neighborhoods(origin, dest):
    """Match neighborhoods."""
    global ORIGIN
    global DEST
    origin = 'paris' if origin not in c.SHORT_KEY else origin
    dest = 'helsinki' if dest not in c.SHORT_KEY else dest
    ORIGIN = cn.gather_info(origin, 1, raw_features=True)
    DEST = cn.gather_info(dest, 1, raw_features=True)
    return f.render_template('nei.html',
                             origin=origin,
                             dest=dest,
                             lbbox=c.BBOXES[origin],
                             rbbox=c.BBOXES[dest])
Ejemplo n.º 4
0
def compare(origin, dest, knn):
    """Compare two cities."""
    global ORIGIN
    global DEST
    origin = 'barcelona' if origin not in c.SHORT_KEY else origin
    dest = 'helsinki' if dest not in c.SHORT_KEY else dest
    ORIGIN = cn.gather_info(origin, knn, raw_features=True)
    DEST = cn.gather_info(dest, knn, raw_features=True)
    return f.render_template('cnn.html',
                             origin=origin,
                             dest=dest,
                             knn=knn,
                             lbbox=c.BBOXES[origin],
                             rbbox=c.BBOXES[dest])
Ejemplo n.º 5
0
def show_gold(city, neighborhood):
    """Show ground thruth for the given query."""
    global ORIGIN
    ORIGIN = cn.gather_info(city, 1, raw_features=True)
    return f.render_template('gold.html',
                             district=neighborhood,
                             bbox=c.BBOXES[city],
                             city=city)
Ejemplo n.º 6
0
def interpret_query(from_city, to_city, region, metric):
    """Load informations about cities and compute useful quantities."""
    # Load info of the first city
    suffix = '_tsne.mat' if metric == 'emd-tsne' else ''
    left = cn.gather_info(from_city+suffix, knn=1,
                          raw_features='lmnn' not in metric,
                          hide_category=metric != 'jsd')
    left_infos = load_surroundings(from_city)
    left_support = features_support(left['features'])

    # Compute info about the query region
    center, radius, _, contains = polygon_to_local(from_city, region)
    query = describe_region(center, radius, contains, left_infos[0], left)
    features, times, weights, vids = query
    # print('{} venues in query region.'.format(len(vids)))
    venue_proportion = 1.0*len(vids) / left['features'].shape[0]

    # And use them to define the metric that will be used
    theta = np.ones((1, left['features'].shape[1]))
    theta = np.array([[0.0396, 0.0396, 0.2932, 0.0396, 0.0396, 0.0396,
                       0.0396, 0.3404, 0.0396, 0.0396, 0.0396, 0.0396,
                       0.0396, 0.3564, 0.0396, 0.3564, 0.0396, 0.3564,
                       0.3564, 0.3564, 0.0396, 0.0396, 0.0396, 0.0396,
                       0.3564, 0.0396, 0.0396, 0.0396, 0.0396, 0.0396,
                       0.0396]])
    ltheta = len(theta.ravel())*[1, ]

    if 'emd' in metric:
        from emd import emd
        from emd_dst import dist_for_emd
        if 'tsne' in metric:
            from specific_emd_dst import dst_tsne as dist_for_emd
        if 'itml' in metric:
            from specific_emd_dst import dst_itml as dist_for_emd
        query_num = features_as_lists(features)

        @profile
        def regions_distance(r_features, r_weigths):
            if len(r_features) >= MAX_EMD_POINTS:
                return 1e20
            return emd((query_num, map(float, weights)),
                       (r_features, map(float, r_weigths)),
                       lambda a, b: float(dist_for_emd(a, b, ltheta)))
    elif 'cluster' in metric:
        from scipy.spatial.distance import cdist
        query_num = weighted_clusters(features, NB_CLUSTERS, weights)

        def regions_distance(r_features, r_weigths):
            r_cluster = weighted_clusters(r_features, NB_CLUSTERS, r_weigths)
            costs = cdist(query_num, r_cluster).tolist()
            return min_cost(costs)
    elif 'leftover' in metric:

        @profile
        def regions_distance(r_features, second_arg):
            r_weigths, idx = second_arg
            emd_leftover.write_matlab_problem(features, weights, r_features,
                                              r_weigths, idx)
            return -1
    else:
        query_num = features_as_density(features, weights, left_support)

        @profile
        def regions_distance(r_density, r_global):
            """Return distance of a region from `query_num`."""
            return proba_distance(query_num, times, r_density, r_global,
                                  theta)

    # Load info of the target city
    right = cn.gather_info(to_city+suffix, knn=2,
                           raw_features='lmnn' not in metric,
                           hide_category=metric != 'jsd')
    right_infos = load_surroundings(to_city)
    minx, miny, maxx, maxy = right_infos[1]
    right_city_size = (maxx - minx, maxy - miny)
    right_support = features_support(right['features'])
    global RIGHT_SUPPORT
    RIGHT_SUPPORT = right_support

    # given extents, compute threshold of candidate
    threshold = 0.7 * venue_proportion * right['features'].shape[0]
    right_desc = [right_city_size, right_support, right, right_infos]

    return [left, right, right_desc, regions_distance, vids, threshold]
Ejemplo n.º 7
0
def show_gold(city, neighborhood):
    """Show ground thruth for the given query."""
    global ORIGIN
    ORIGIN = cn.gather_info(city, 1, raw_features=True)
    return f.render_template('gold.html', district=neighborhood,
                             bbox=c.BBOXES[city], city=city)
Ejemplo n.º 8
0
def interpret_query(from_city, to_city, region, metric):
    """Load informations about cities and compute useful quantities."""
    # Load info of the first city
    suffix = '_tsne.mat' if metric == 'emd-tsne' else ''
    left = cn.gather_info(from_city + suffix,
                          knn=1,
                          raw_features='lmnn' not in metric,
                          hide_category=metric != 'jsd')
    left_infos = load_surroundings(from_city)
    left_support = features_support(left['features'])

    # Compute info about the query region
    center, radius, _, contains = polygon_to_local(from_city, region)
    query = describe_region(center, radius, contains, left_infos[0], left)
    features, times, weights, vids = query
    # print('{} venues in query region.'.format(len(vids)))
    venue_proportion = 1.0 * len(vids) / left['features'].shape[0]

    # And use them to define the metric that will be used
    theta = np.ones((1, left['features'].shape[1]))
    theta = np.array([[
        0.0396, 0.0396, 0.2932, 0.0396, 0.0396, 0.0396, 0.0396, 0.3404, 0.0396,
        0.0396, 0.0396, 0.0396, 0.0396, 0.3564, 0.0396, 0.3564, 0.0396, 0.3564,
        0.3564, 0.3564, 0.0396, 0.0396, 0.0396, 0.0396, 0.3564, 0.0396, 0.0396,
        0.0396, 0.0396, 0.0396, 0.0396
    ]])
    ltheta = len(theta.ravel()) * [
        1,
    ]

    if 'emd' in metric:
        from emd import emd
        from emd_dst import dist_for_emd
        if 'tsne' in metric:
            from specific_emd_dst import dst_tsne as dist_for_emd
        if 'itml' in metric:
            from specific_emd_dst import dst_itml as dist_for_emd
        query_num = features_as_lists(features)

        @profile
        def regions_distance(r_features, r_weigths):
            if len(r_features) >= MAX_EMD_POINTS:
                return 1e20
            return emd((query_num, map(float, weights)),
                       (r_features, map(float, r_weigths)),
                       lambda a, b: float(dist_for_emd(a, b, ltheta)))
    elif 'cluster' in metric:
        from scipy.spatial.distance import cdist
        query_num = weighted_clusters(features, NB_CLUSTERS, weights)

        def regions_distance(r_features, r_weigths):
            r_cluster = weighted_clusters(r_features, NB_CLUSTERS, r_weigths)
            costs = cdist(query_num, r_cluster).tolist()
            return min_cost(costs)
    elif 'leftover' in metric:

        @profile
        def regions_distance(r_features, second_arg):
            r_weigths, idx = second_arg
            emd_leftover.write_matlab_problem(features, weights, r_features,
                                              r_weigths, idx)
            return -1
    else:
        query_num = features_as_density(features, weights, left_support)

        @profile
        def regions_distance(r_density, r_global):
            """Return distance of a region from `query_num`."""
            return proba_distance(query_num, times, r_density, r_global, theta)

    # Load info of the target city
    right = cn.gather_info(to_city + suffix,
                           knn=2,
                           raw_features='lmnn' not in metric,
                           hide_category=metric != 'jsd')
    right_infos = load_surroundings(to_city)
    minx, miny, maxx, maxy = right_infos[1]
    right_city_size = (maxx - minx, maxy - miny)
    right_support = features_support(right['features'])
    global RIGHT_SUPPORT
    RIGHT_SUPPORT = right_support

    # given extents, compute threshold of candidate
    threshold = 0.7 * venue_proportion * right['features'].shape[0]
    right_desc = [right_city_size, right_support, right, right_infos]

    return [left, right, right_desc, regions_distance, vids, threshold]