示例#1
0
 def _compute_preferences(self):
     """ compute preferred distribution of each human for park, stores, etc."""
     for h in self.humans:
         h.stores_preferences = [
             (compute_distance(h.household, s) + 1e-1)**-1
             for s in self.stores
         ]
         h.parks_preferences = [
             (compute_distance(h.household, s) + 1e-1)**-1
             for s in self.parks
         ]
示例#2
0
文件: query.py 项目: Hanyu-Li/SpikeDB
def find_similar_input(collection, _result, r0, threshold=10, display=True):
    """
    find data set within result with similar input waveform to r
    """
    if r0.__class__ == dict:
        std = np.array(r0.get('input_signal'))
    elif r0.__class__ == str:
        (fnhead, fnum) = parse_fname(r0)
        r0_entry = collection.find_one( {'fnhead' : fnhead, 'fnum' : fnum } )
        std = np.array(r0_entry.get('input_signal'))
    filtered_result = []
    result = _result.clone()
    for r_candidate in result:
        try:
            comp = np.array(r_candidate.get('input_signal'))
            dist = compute_distance(comp, std)
            print dist
            if dist < threshold:
                if display == True:
                    plot(comp)
                    show()
                    print r_candidate.get('fnhead'), r_candidate.get('fnum')
                    print dist
                filtered_result = filtered_result + [r_candidate]

        except:
            print 'input signal not found'
    return filtered_result
示例#3
0
 def __get_neighbors(self, X, idx):
     neighs = []
     for i in range(len(X)):
         d = compute_distance(X[idx], X[i])
         if d <= self.eps and i != idx:
             neighs.append(i)
     return np.asarray(neighs)
示例#4
0
def readJSON(city):
    gvals = open(
        '/Users/pranavramkrishnan/Desktop/Research/maps/static/data/' +
        str(city) + '/green/greenery_compressed.json', 'r')
    streets = open(
        '/Users/pranavramkrishnan/Desktop/Research/maps/static/data/' +
        str(city) + '/green/street_map_compressed.json', 'r')
    output = open(
        '/Users/pranavramkrishnan/Desktop/Research/maps/static/data/' +
        str(city) + '/green/paths/data.csv', 'a+')
    green_data = json.load(gvals)
    smap = json.load(streets)
    rawData = {}
    seg_greens = []
    seg_dists = []
    for segment in green_data['segments']:
        startLat = float(segment[0])
        startLng = float(segment[1])
        endLat = float(segment[2])
        endLng = float(segment[3])
        segmentID = str(segment[4])
        segmentGreen = smap[segmentID][1]
        if startLat == endLat and startLng == endLng:
            segmentDist = 0.0
        else:
            segmentDist = utils.compute_distance([startLat, startLng],
                                                 [endLat, endLng])
        seg_greens.append(segmentGreen)
        seg_dists.append(segmentDist)
        output.write(
            str(startLat) + ',' + str(startLng) + ',' + str(endLat) + ',' +
            str(endLng) + ',' + str(segmentGreen) + ',' + str(segmentDist) +
            '\n')
 def compute_distance(self, shop1: Shop, shop2: Shop):
     if (shop1.identifier, shop2.identifier) not in self._costs:
         self._costs[(shop1.identifier,
                      shop2.identifier)] = compute_distance(
                          shop1.coords, shop2.coords)
         self._costs[(shop2.identifier,
                      shop1.identifier)] = self._costs[(shop1.identifier,
                                                        shop2.identifier)]
     return self._costs[(shop1.identifier, shop2.identifier)]
示例#6
0
 def predict(self, X):
     dist = compute_distance(X, self.X)
     dist = [[tuple(pair) for pair in line] for line in dist.tolist()]
     dist = [sorted(line, key=lambda pair: pair[0]) for line in dist]
     neighs = np.matrix([[pair[1] for pair in line] for line in dist])
     dists = np.matrix([[pair[0] for pair in line] for line in dist])
     k_neighs = neighs[:, :self.K].astype(np.int)
     total_dist = np.sum(dists[:, :self.K], axis=1)
     weights = dists[:, :self.K] / total_dist
     neigh_vals = self.y[k_neighs]
     return (np.multiply(weights, np.squeeze(neigh_vals))).sum(axis=1)
示例#7
0
def findClosestStation(point, city):
    bestDist = 10000
    bestStation = ''
    stations = origins[city].keys()
    for i in range(len(stations)):
        dist = utils.compute_distance(point, origins[city][stations[i]])
        if dist < bestDist:
            bestDist = dist
            bestStation = stations[i]

    return origins[city][bestStation], bestStation
示例#8
0
 def predict(self, X):
     # dist.shape = (Num test data, num train data, 2)
     dist = compute_distance(X, self.X, self.dist_type)
     dist = [[tuple(pair) for pair in line] for line in dist.tolist()]
     dist = [sorted(line, key=lambda pair: pair[0]) for line in dist]
     neighs = np.matrix([[pair[1] for pair in line] for line in dist])
     k_neighs = neighs[:, :self.K].astype(np.int)
     neigh_vals = self.y[k_neighs][:, :, 0]
     counts = np.apply_along_axis(np.bincount,
                                  axis=1,
                                  arr=neigh_vals,
                                  minlength=np.max(neigh_vals) + 1)
     winner_neigh_idx = np.argmax(counts, axis=1)
     return winner_neigh_idx
示例#9
0
    def _select_location(self, location_type, city):
        """
        Preferential exploration treatment to visit places
        rho, gamma are treated in the paper for normal trips
        Here gamma is multiplied by a factor to supress exploration for parks, stores.
        """
        if location_type == "park":
            S = self.visits.n_parks
            self.adjust_gamma = 1.0
            pool_pref = self.parks_preferences
            locs = city.parks
            visited_locs = self.visits.parks

        elif location_type == "stores":
            S = self.visits.n_stores
            self.adjust_gamma = 1.0
            pool_pref = self.stores_preferences
            locs = city.stores
            visited_locs = self.visits.stores

        elif location_type == "miscs":
            S = self.visits.n_miscs
            self.adjust_gamma = 1.0
            pool_pref = [(compute_distance(self.location, m) + 1e-1) ** -1 for m in city.miscs if
                         m != self.location]
            pool_locs = [m for m in city.miscs if m != self.location]
            locs = city.miscs
            visited_locs = self.visits.miscs

        else:
            raise ValueError(f'Unknown location_type:{location_type}')

        if S == 0:
            p_exp = 1.0
        else:
            p_exp = self.rho * S ** (-self.gamma * self.adjust_gamma)

        if self.rng.random() < p_exp and S != len(locs):
            # explore
            cands = [i for i in locs if i not in visited_locs]
            cands = [(loc, pool_pref[i]) for i, loc in enumerate(cands)]
        else:
            # exploit
            cands = [(i, count) for i, count in visited_locs.items()]

        cands, scores = zip(*cands)
        loc = self.rng.choice(cands, p=_normalize_scores(scores))
        visited_locs[loc] += 1
        return loc
 def get_filtered_shops(self, my_coords: Coords, desired_activities: set, max_radius: int = 1000, max_shops=100):
     """
     Aquesta funció retorna totes les botigues de les activitats que s'han triat. Ex: totes les carniseries,
     peixateries, etc. en el radi indicat
     :param my_coords:
     :param desired_activities:
     :param max_radius:
     :param max_shops:
     :return:
     """
     filtered_shops = defaultdict(list)
     for activity, shops in self.activity2shops.items():
         if activity in desired_activities:
             near_shops = list()
             for shop in shops:
                 dist = compute_distance(my_coords, shop.coords)
                 if dist <= max_radius:
                     near_shops.append((shop, dist))
             filtered_shops[activity] = [shop_dist[0] for shop_dist in
                                         sorted(near_shops, key=lambda x: x[1])[:max_shops]]
     return filtered_shops
示例#11
0
 def GET(self):
     c = Criminal()
     v = Victim()
     
     how_many = 3
     
     c_locs = c.get_last_locations("CriminalLocation", how_many)
     v_locs = v.get_last_locations("VictimLocation", how_many)
     
     distances = []
     for cl, vl in zip(c_locs, v_locs):
         #Compute distances
         lat1 = cl[0]
         lng1 = cl[1]
         lat2 = vl[0]
         lng2 = vl[1]
         d = utils.compute_distance(lat1, lng1, lat2, lng2)
         distances.append(d)
     
     #print distances
     return render.distances(distances, how_many)
 def __get_neighbors(self, X_new, X):
     distance = [compute_distance(X_new, data) for data in X]
     return np.argsort(distance)[:self.K]
示例#13
0
 def predict(self, X_new):
     return np.argmin(
         [compute_distance(X_new, center) for center in self.centroids])
示例#14
0
 def __get_centroids(self, X, labels):
     for k in range(self.K):
         group = X[labels == k]
         mean = np.mean(group, axis=0)
         idx = np.argmin([compute_distance(mean, data) for data in X])
         self.centroids[k] = X[idx]
示例#15
0

def get_user_id():
    return str(657476578)


@timeit
def do_something():
    logger.info("main function started")
    time.sleep(1)


if __name__ == "__main__":
    logging_utils = LoggingUtils(name="FeatureManagementService",
                                 log_level="INFO",
                                 log_record_factory_func=get_user_id)
    logger = logging_utils.init_logger_with_attribute("USERID")
    # logger = logging_utils.init_logger()
    logger.info('creating new userid')
    do_something()
    logger.info('creating an instance of auxiliary_module.Auxiliary')
    a = auxiliary.Auxiliary()
    logger.info('created an instance of auxiliary_module.Auxiliary')
    logger.info('calling auxiliary_module.Auxiliary.do_something')
    a.do_something()
    logger.info('finished auxiliary_module.Auxiliary.do_something')
    logger.info('calling auxiliary_module.some_function()')
    auxiliary.some_function()
    logger.info('done with auxiliary_module.some_function()')
    compute_distance()
示例#16
0
def jsonToGeoJSON():
    basePath = '/Users/pranavramkrishnan/Desktop/Research/maps/static/data/Cambridge/transportation/'
    jsonVals = open(basePath + 'buildings_central_final.json', 'r')
    jsonVals = jsonVals.readlines()[0].split('},')
    features = []

    central = [42.365504, -71.103819]
    for line in jsonVals:

        lineElements = line.split(':')
        center = lineElements[1].split('],')[0].split(',')
        centerLat = float(center[0][2:])
        centerLng = float(center[1])
        center = [centerLng, centerLat]
        # color = str(lineElements[2].split(',')[0])
        # color = color[2:len(color)-1]

        coordinates = processPolygonCoordinate(
            lineElements[3].split(']],')[0] + ']]')
        bestMode = lineElements[4].split(',')[0]
        bestMode = bestMode[2:len(bestMode) - 1]

        color = colorMapping(bestMode)

        datavals = lineElements[5].split('],')[0].split(',')
        datavals = [
            float(datavals[0][2:]),
            float(datavals[1]),
            float(datavals[2]),
            float(datavals[3]),
            float(datavals[4]),
            float(datavals[5]),
            float(datavals[6])
        ]

        dist = utils.compute_distance([centerLat, centerLng], central)

        data_id_str = lineElements[len(lineElements) - 1]
        if data_id_str[-2] == '}':
            data_id = int(data_id_str[:-2])
        else:
            data_id = int(data_id_str)

        relative_order = orderMapping(bestMode)
        polygon = geojson.Polygon(coordinates)
        properties = {
            "color": color,
            "bestMode": bestMode,
            "center": center,
            "distance": dist,
            "relative_order": relative_order
        }
        feature = geojson.Feature(geometry=polygon,
                                  properties=properties,
                                  id=data_id)
        features.append(feature)

    sorted_features = sorted(
        features,
        key=lambda k:
        (k.properties["relative_order"], k.properties["distance"]))
    featureColleciton = geojson.FeatureCollection(sorted_features)
    geojson_data = geojson.dumps(featureColleciton)
    output_file = open(basePath + 'central.geojson', 'w')
    output_file.write(geojson_data)
示例#17
0
def load_datasets(dataset_name, dataset_dir, do_pca, pca_dims, add_bias,
                  remove_mean, density_sigma, interp_sigma, dist_sigma):
    print(dataset_name)

    im_files = None
    explain_files = None
    class_names = None
    explain_interp = None  # for the explanation 1.0 means easy to interpret and 0.0 means hard

    if dataset_name == 'iris':
        iris = datasets.load_iris()
        X = iris.data
        Y = iris.target
    elif dataset_name == 'wine':
        wine = datasets.load_wine()
        X = wine.data
        Y = wine.target
    elif dataset_name == 'breast_cancer':
        bc = datasets.load_breast_cancer()
        X = bc.data
        Y = bc.target
    elif dataset_name == '2d_outlier':
        num_exs = 100
        sig = 0.005
        pt = 0.3
        cls1 = np.random.multivariate_normal([pt, pt], [[sig, 0], [0, sig]],
                                             int(num_exs * 0.8))
        cls2 = np.random.multivariate_normal([-pt, -pt], [[sig, 0], [0, sig]],
                                             int(num_exs * 0.8))
        # add "noise"
        cls1n = np.random.multivariate_normal([pt, pt],
                                              [[sig * 10, 0], [0, sig * 10]],
                                              int(num_exs * 0.2))
        cls2n = np.random.multivariate_normal([-pt, -pt],
                                              [[sig * 10, 0], [0, sig * 10]],
                                              int(num_exs * 0.2))
        X = np.vstack((cls1, cls1n, cls2, cls2n))
        Y = np.ones(X.shape[0]).astype(np.int)
        Y[:int(num_exs * 0.8) + int(num_exs * 0.2)] = 0
    elif dataset_name == '3blobs':
        num_exs = 80
        cls1 = np.random.multivariate_normal([1.0, -1.0],
                                             [[0.12, 0], [0, 0.12]], num_exs)
        cls2 = np.random.multivariate_normal([-1.0, -1.0],
                                             [[0.12, 0], [0, 0.12]], num_exs)
        cls3 = np.random.multivariate_normal([-1.0, 1.0],
                                             [[0.12, 0], [0, 0.12]], num_exs)
        X = np.vstack((cls1, cls2, cls3))
        Y = np.ones(X.shape[0]).astype(np.int)
        Y[:num_exs] = 0
    elif dataset_name == 'blobs_2_class':
        X, Y = make_blobs(n_samples=200, centers=2, random_state=0)
    elif dataset_name == 'blobs_3_class':
        X, Y = make_blobs(n_samples=300, centers=3, random_state=0)
    elif dataset_name == 'butterflies_crop_monarch':
        X, Y, im_files, explain_files, class_names, explain_interp = load_data(
            dataset_dir, dataset_name, interp_sigma)
        for i in range(len(class_names)):
            if class_names[i] != b'Monarch':
                class_names[i] = 'Not_monarch'
            else:
                class_names[i] = 'Monarch'
                t = i
        class_names = np.delete(class_names, [2, 3, 4])
        Y[Y == t] = 1
        Y[Y != 1] = 0
    elif dataset_name == 'chinese_chars_binary':
        X, Y, im_files, explain_files, class_names, explain_interp = load_data(
            dataset_dir, dataset_name, interp_sigma)
        for i in range(len(class_names)):
            if class_names[i] != b'grass':
                class_names[i] = 'not_grass'
            else:
                class_names[i] = 'grass'
                t = i
        class_names = np.delete(class_names, [2])
        Y[Y == t] = 1
        Y[Y != 1] = 0
    elif dataset_name == 'woodpecker':
        X, Y, im_files, explain_files, class_names, explain_interp = load_data(
            dataset_dir, dataset_name, interp_sigma)
        for i in range(len(class_names)):
            if class_names[i] != 'Red_bellied_Woodpecker':
                class_names[i] = 'Not_red_bellied_Woodpecker'
            else:
                class_names[i] = 'Red_bellied_Woodpecker'
                t = i
        class_names = np.delete(class_names, [2])
        Y[Y == t] = 1
        Y[Y != 1] = 0
    else:
        X, Y, im_files, explain_files, class_names, explain_interp = load_data(
            dataset_dir, dataset_name, interp_sigma)

    if im_files is None:
        im_files = np.asarray([''] * X.shape[0])
    if explain_files is None:
        explain_files = np.asarray([''] * X.shape[0])
    if class_names is None:
        class_names = np.asarray([''] * np.unique(Y).shape[0])
    if explain_interp is None:
        explain_interp = np.ones(X.shape[0])

    # standardize
    if remove_mean:
        X = X - X.mean(0)
        X = X / X.std(0)

    # do PCA
    if do_pca and X.shape[1] > 2:
        pca = PCA(n_components=2)
        pca.fit(X)
        X = pca.transform(X)
        X = X - X.mean(0)
        X = X / X.std(0)

    # add 1 for bias (intercept) term
    if add_bias:
        X = np.hstack((X, np.ones(X.shape[0])[..., np.newaxis]))

    # balance datasets - same number of examples per class
    X, Y, im_files, explain_files, explain_interp = balance_data(
        X, Y, im_files, explain_files, explain_interp)

    # train test split
    dataset_train, dataset_test = make_train_test_split(
        X, Y, im_files, explain_files, class_names, explain_interp)

    # density of points
    dataset_train['X_density'] = ut.compute_density(dataset_train['X'],
                                                    dataset_train['Y'],
                                                    density_sigma, True)

    #distance of points
    dataset_train['X_distance'] = ut.compute_distance(dataset_train['X'],
                                                      dataset_train['Y'],
                                                      dist_sigma, True)
    #print dataset_train['X_distance']

    print('train split')
    print(dataset_train['X'].shape[0], 'instances')
    print(dataset_train['X'].shape[1], 'features')
    print(np.unique(dataset_train['Y']).shape[0], 'classes')

    return dataset_train, dataset_test
示例#18
0
def RPE(traj_gt,
        traj_est,
        param_max_pairs=10000,
        param_fixed_delta=False,
        param_delta=1.00,
        param_delta_unit="m",
        param_offset=0.00):
    """
    This method computes the Relative Pose Error (RPE) and Drift Per Distance Travelled (DDT)
    Ref: Sturm et al. (2012), Scona et al. (2017)

    Input:
    traj_gt -- the first trajectory (ground truth)
    traj_est -- the second trajectory (estimated trajectory)
    param_max_pairs -- number of relative poses to be evaluated
    param_fixed_delta -- false: evaluate over all possible pairs
                         true: only evaluate over pairs with a given distance (delta)
    param_delta -- distance between the evaluated pairs
    param_delta_unit -- unit for comparison:
                        "s": seconds
                        "m": meters
                        "rad": radians
                        "deg": degrees
                        "f": frames
    param_offset -- time offset between two trajectories (to traj_xyz_gt the delay)
    param_scale -- scale to be applied to the second trajectory

    Output:
    list of compared poses and the resulting translation and rotation error
    """

    stamps_gt = list(traj_gt.keys())
    stamps_est = list(traj_est.keys())
    stamps_gt.sort()
    stamps_est.sort()

    stamps_est_return = []
    for t_est in stamps_est:
        t_gt = stamps_gt[utils.find_closest_index(stamps_gt,
                                                  t_est + param_offset)]
        t_est_return = stamps_est[utils.find_closest_index(
            stamps_est, t_gt - param_offset)]
        t_gt_return = stamps_gt[utils.find_closest_index(
            stamps_gt, t_est_return + param_offset)]
        if not t_est_return in stamps_est_return:
            stamps_est_return.append(t_est_return)
    if (len(stamps_est_return) < 2):
        raise Exception(
            "Number of overlap in the timestamps is too small. Did you run the evaluation on the right files?"
        )

    if param_delta_unit == "s":
        index_est = list(traj_est.keys())
        index_est.sort()
    elif param_delta_unit == "m":
        index_est = utils.distances_along_trajectory(traj_est)
    elif param_delta_unit == "rad":
        index_est = utils.rotations_along_trajectory(traj_est, 1)
    elif param_delta_unit == "deg":
        index_est = utils.rotations_along_trajectory(traj_est, 180 / np.pi)
    elif param_delta_unit == "f":
        index_est = range(len(traj_est))
    else:
        raise Exception("Unknown unit for delta: '%s'" % param_delta_unit)

    if not param_fixed_delta:
        if (param_max_pairs == 0 or len(traj_est) < np.sqrt(param_max_pairs)):
            pairs = [(i, j) for i in range(len(traj_est))
                     for j in range(len(traj_est))]
        else:
            pairs = [(random.randint(0,
                                     len(traj_est) - 1),
                      random.randint(0,
                                     len(traj_est) - 1))
                     for i in range(param_max_pairs)]
    else:
        pairs = []
        for i in range(len(traj_est)):
            j = utils.find_closest_index(index_est, index_est[i] + param_delta)
            if j != len(traj_est) - 1:
                pairs.append((i, j))
        if (param_max_pairs != 0 and len(pairs) > param_max_pairs):
            pairs = random.sample(pairs, param_max_pairs)

    gt_interval = np.median(
        [s - t for s, t in zip(stamps_gt[1:], stamps_gt[:-1])])
    gt_max_time_difference = 2 * gt_interval

    result = []
    diff_pose = []
    for i, j in pairs:
        stamp_est_0 = stamps_est[i]
        stamp_est_1 = stamps_est[j]

        stamp_gt_0 = stamps_gt[utils.find_closest_index(
            stamps_gt, stamp_est_0 + param_offset)]
        stamp_gt_1 = stamps_gt[utils.find_closest_index(
            stamps_gt, stamp_est_1 + param_offset)]

        if (abs(stamp_gt_0 -
                (stamp_est_0 + param_offset)) > gt_max_time_difference
                or abs(stamp_gt_1 -
                       (stamp_est_1 + param_offset)) > gt_max_time_difference):
            continue

        gt_delta = utils.transform_diff(traj_gt[stamp_gt_1],
                                        traj_gt[stamp_gt_0])
        est_delta = utils.transform_diff(traj_est[stamp_est_1],
                                         traj_est[stamp_est_0])
        error44 = utils.transform_diff(est_delta, gt_delta)

        gt_distance_travelled = utils.compute_distance(gt_delta)
        # check if the distance is not nan or inf
        gt_distance_travelled = gt_distance_travelled if (
            not 0) else utils._EPS

        diff_pose.append(error44)

        trans = utils.compute_distance(error44)
        rot = utils.compute_angle(error44)

        result.append(
            [stamp_est_0, stamp_est_1, stamp_gt_0, stamp_gt_1, trans, rot])

    if len(result) < 2:
        raise Exception(
            "Couldn't find matching timestamp pairs between groundtruth and estimated trajectory!"
        )

    stamps = np.array(result)[:, 0]
    trans_error = np.array(result)[:, 4]
    rot_error = np.array(result)[:, 5]

    errors = np.matrix([SE3Lib.TranToVec(dT) for dT in diff_pose]).transpose()

    return errors, trans_error, rot_error, gt_distance_travelled
 def __find_closest_cluster(self, data):
     dist = [compute_distance(data, c) for c in self.centroids]
     return np.argmin(dist)
示例#20
0
    def __init__(self, p=None):
        """
        构建模型参数,加载数据
            把前90%分为8:1用作train和valid,来选择超参数, 不用去管剩下的10%.
            把前90%作为train,剩下的是test,把valid时学到的参数拿过来跑程序.
            valid和test部分,程序是一样的,区别在于送入的数据而已。
        :param p: 一个标示符,没啥用
        :return:
        """
        # 1. 建立各参数。要调整的地方都在 p 这了,其它函数都给写死。
        if not p:
            t = 't'  # 写1就是valid, 写0就是test
            assert 't' == t or 'v' == t  # no other case
            p = OrderedDict([
                ('dataset', 'test.txt'),
                ('mode', 'test' if 't' == t else 'valid'),
                ('split', -1 if 't' == t else -2),  # test预测最后一个。
                ('at_nums', [5, 10, 15, 20]),
                ('epochs', 10),  # 调程序用50,test时用100.
                ('latent_size', 20),
                ('alpha', 0.01),
                ('lambda', 0.001),  # dist2pre: 两个数据集都用0.001,并且用SGD。
                ('loss_weight', [0.5, 0.5]),

                # foursquare: 已是最佳值
                ('dd', 150 / 1000.0),  # 150m
                ('UD', 5),  # 截断距离5km,lambda_s的维度。
                # gowalla: 已是最佳值
                # ('dd',                  100 / 1000.0),    # 200m
                # ('UD',                  20),    # 截断距离40km,lambda_s的维度。
                ('mini_batch', 0),  # 0:one_by_one, 1:mini_batch. 全都用逐条。
                ('gru', 2),  # 0:bpr, 1:gru, 2:dist2pre-linear
                # 3:dist2pre-nonlinear
                ('batch_size_train', 1),  #
                ('batch_size_test', 128),  # user * item 矩阵太大了,分成多次计算。 768
                ('process_data_folder', 'process_data/Foursquare/'),
                ('percent', 0.7)
            ])
            for i in p.items():
                print(i)
        dist_num = int(p['UD'] / p['dd'])  # 1520 = 38*1000/25。idx=[0, 1519+1]
        print(dist_num)

        # 2. 加载数据
        # 因为train/set里每项的长度不等,无法转换为完全的(n, m)矩阵样式,所以shared会报错.
        [(user_num, item_num), pois_cordis, (aux_tra_buys, aux_tes_buys),(aux_tra_dist, aux_tes_dist),
         (tra_buys, tes_buys), (tra_dist, tes_dist)] = \
            load_data(os.path.join(PATH, p['dataset']), p['mode'], p['split'], p['dd'], dist_num, p['process_data_folder'],
                      p['percent'])

        # TODO 修改处
        # 加载离线处理好的加噪数据,并计算相应的距离
        obfuscated_pois = read_from_pkl(
            p['process_data_folder'] + 'auxiliary_domain/', 'obfuscated_pois')
        confidence_matrix = read_from_pkl(
            p['process_data_folder'] + 'auxiliary_domain/',
            'confidence_matrix')

        # 辅助域训练数据为加噪后的数据,测试数据保留原数据
        aux_tra_buys = obfuscated_pois
        aux_tra_dist = compute_distance(aux_tra_buys, pois_cordis, p['dd'],
                                        dist_num)

        aux_tra_num = len(aux_tra_buys)

        # 将辅助域的数据与目标域的数据合并一起带入训练
        aux_tra_buys.extend(tra_buys)
        aux_tes_buys.extend(tes_buys)
        aux_tra_dist.extend(tra_dist)
        aux_tes_dist.extend(tes_dist)

        tra_buys = aux_tra_buys
        tes_buys = aux_tes_buys
        tra_dist = aux_tra_dist
        tes_dist = aux_tes_dist

        # 正样本加masks
        tra_buys_masks, tra_masks = fun_data_buys_masks(tra_buys,
                                                        tail=[item_num
                                                              ])  # 预测时算用户表达用
        tes_buys_masks, tes_masks = fun_data_buys_masks(tes_buys,
                                                        tail=[item_num])

        # 预测时用
        tra_dist_masks, _ = fun_data_buys_masks(tra_dist, tail=[dist_num])
        tes_dist_masks, _ = fun_data_buys_masks(tes_dist, tail=[dist_num])
        # 负样本加masks
        tra_buys_neg_masks = fun_random_neg_masks_tra(
            item_num, tra_buys_masks)  # 训练时用(逐条、mini-batch均可)
        tes_buys_neg_masks = fun_random_neg_masks_tes(item_num, tra_buys_masks,
                                                      tes_buys_masks)  # 预测时用
        # 计算负样本与上一个正样本的距离间隔,并加masks
        tra_dist_neg_masks = fun_compute_dist_neg(tra_buys_masks, tra_masks,
                                                  tra_buys_neg_masks,
                                                  pois_cordis, p['dd'],
                                                  dist_num)
        # 每个user训练序列里最后一个poi和all pois的距离落在哪个区间里。
        usrs_last_poi_to_all_intervals = fun_compute_distance(
            tra_buys_masks, tra_masks, pois_cordis, p['dd'], dist_num)

        # print(tra_dist[0][:5])        # [1520, 274, 0, 428, 142], 38km/25m=1520
        # print(tra_dist_masks[1020][:sum(tra_masks[1020])])

        # 3. 创建类变量
        self.p = p
        self.user_num, self.item_num, self.dist_num = user_num, item_num, dist_num
        self.pois_cordis = pois_cordis
        self.tra_buys_masks, self.tra_masks, self.tra_buys_neg_masks = tra_buys_masks, tra_masks, tra_buys_neg_masks
        self.tes_buys_masks, self.tes_masks, self.tes_buys_neg_masks = tes_buys_masks, tes_masks, tes_buys_neg_masks
        self.tra_dist_masks = tra_dist_masks
        self.tes_dist_masks = tes_dist_masks
        self.tra_dist_neg_masks = tra_dist_neg_masks
        self.ulptai = usrs_last_poi_to_all_intervals
        self.aux_tra_num = aux_tra_num
        self.confidence_matrix = np.array(confidence_matrix)
示例#21
0
 def __get_samples_distance_matrix(self):
     distances = np.zeros((self.n, self.n))
     for i in range(self.n):
         for j in range(self.n):
             distances[i, j] = compute_distance(self.X[i], self.X[j])
     return distances
示例#22
0
    def test_distance_bad(self):

        assert compute_distance("ssd", "aaa", 53.339428, -6.257664,
                                "Christina McArdle") == [0, 0]
示例#23
0
    def test_distance_ok(self):

        assert compute_distance(
            52.986375, -6.043701, 53.339428, -6.257664,
            "Christina McArdle") == [36.271572836813576, 'Christina McArdle']
示例#24
0
 def _compute_travel_time(self, pos1, pos2):
     dis = utils.compute_distance(pos1, pos2)
     return dis / self._robot_speed
示例#25
0
def create_street_segments(city):
    basePath = '/Users/pranavramkrishnan/Desktop/Research/maps/static/data/' + str(
        city) + '/'
    streets = json.load(open(basePath + 'streets2.json', 'r'))
    features = []

    output = open(basePath + 'segs_test.csv', 'w')
    main_output = open(basePath + 'green/query_points2.csv', 'w')
    coordinates_list = []
    check = {}
    types = []
    types_to_ignore = [
        "EXPY", "FWY", "TER", "ALY", "HWY", "I", "KYS", "GRN", "Fwy",
        "PROMENADE", None
    ]
    query_points = []

    ds = []

    for feature in streets['features']:
        st_type = feature['properties']['STREETTYPE']
        types.append(st_type)
        if st_type not in types_to_ignore:
            name = feature['properties']['REGISTERED']
            name += ' '
            name += str(st_type)
            street_name = capitalize_name(name)
            # CNN = str(feature['properties']['CNN'])
            # F_NODE = float(feature['properties']["F_NODE_CNN"])
            # T_NODE = float(feature['properties']["T_NODE_CNN"])
            coordinates = feature['geometry']['coordinates']
            i = 0
            #total_dist = 0
            while (i < len(coordinates) - 1):
                start_coord = (float(coordinates[i][0]),
                               float(coordinates[i][1]))
                end_coord = (float(coordinates[i + 1][0]),
                             float(coordinates[i + 1][1]))

                seg_dist = utils.compute_distance(
                    [start_coord[1], start_coord[0]],
                    [end_coord[1], end_coord[0]]) * 5280
                ds.append(seg_dist)
                if seg_dist > 75:
                    query_points.append([
                        truncate_float(start_coord[1]),
                        truncate_float(start_coord[0]),
                        truncate_float(end_coord[1]),
                        truncate_float(end_coord[0]), street_name
                    ])

                i += 1

    print set(types)
    print min(ds), max(ds), sum(ds) / len(ds)

    for pt in query_points:
        line = ''
        for s in pt:
            line += str(s)
            line += ','
        main_output.write(line + '\n')

    points = {'segments': query_points, 'streets': []}
    final_json_data = json.dumps(points)
    output_file = open(basePath + '/green/greenery_test_test.json', 'w')
    output_file.write(final_json_data)