Example #1
0
def _c(ca, i, j, P, Q):
    """
    Recursive caller for discrete Frechet distance

    This is the recursive caller as as defined in [1]_.

    Parameters
    ----------
    ca : array_like
        distance like matrix
    i : int
        index
    j : int
        index
    P : array_like
        array containing path P
    Q : array_like
        array containing path Q

    Returns
    -------
    df : float
        discrete frechet distance

    Notes
    -----
    This should work in N-D space. Thanks to MaxBareiss
    https://gist.github.com/MaxBareiss/ba2f9441d9455b56fbc9

    References
    ----------
    .. [1] Thomas Eiter and Heikki Mannila. Computing discrete Frechet
        distance. Technical report, 1994.
        http://www.kr.tuwien.ac.at/staff/eiter/et-archive/cdtr9464.pdf
        http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.90.937&rep=rep1&type=pdf
    """
    if ca[i, j] > -1:
        return ca[i, j]
    elif i == 0 and j == 0:
        ca[i, j] = minkowski_distance(P[0], Q[0], p=pnorm)
    elif i > 0 and j == 0:
        ca[i, j] = max(_c(ca, i - 1, 0, P, Q),
                       minkowski_distance(P[i], Q[0], p=pnorm))
    elif i == 0 and j > 0:
        ca[i, j] = max(_c(ca, 0, j - 1, P, Q),
                       minkowski_distance(P[0], Q[j], p=pnorm))
    elif i > 0 and j > 0:
        ca[i, j] = max(
            min(_c(ca, i - 1, j, P, Q), _c(ca, i - 1, j - 1, P, Q),
                _c(ca, i, j - 1, P, Q)), minkowski_distance(P[i],
                                                            Q[j],
                                                            p=pnorm))
    else:
        ca[i, j] = float("inf")
    return ca[i, j]
def _c(ca, i, j, P, Q):
    """
    https://github.com/cjekel/similarity_measures/blob/master/similaritymeasures/similaritymeasures.py
    Recursive caller for discrete Frechet distance
    This is the recursive caller as as defined in [1]_.
    
    Parameters
    ----------
    ca : array_like
        distance like matrix
        
    i : int
        index
        
    j : int
        index
        
    P : array_like
        array containing path P
        
    Q : array_like
        array containing path Q
        
    Returns
    -------
    df : float
        discrete frechet distance
        
    Notes
    -----
    This should work in N-D space. Thanks to MaxBareiss
    https://gist.github.com/MaxBareiss/ba2f9441d9455b56fbc9
    
    """
    if ca[i, j] > -1:
        return ca[i, j]
    elif i == 0 and j == 0:
        ca[i, j] = minkowski_distance(P[0], Q[0], p=pnorm)
    elif i > 0 and j == 0:
        ca[i, j] = max(_c(ca, i - 1, 0, P, Q),
                       minkowski_distance(P[i], Q[0], p=pnorm))
    elif i == 0 and j > 0:
        ca[i, j] = max(_c(ca, 0, j - 1, P, Q),
                       minkowski_distance(P[0], Q[j], p=pnorm))
    elif i > 0 and j > 0:
        ca[i, j] = max(
            min(_c(ca, i - 1, j, P, Q), _c(ca, i - 1, j - 1, P, Q),
                _c(ca, i, j - 1, P, Q)), minkowski_distance(P[i],
                                                            Q[j],
                                                            p=pnorm))
    else:
        ca[i, j] = float("inf")
    return ca[i, j]
Example #3
0
def calculateKNNgraphDistanceMatrixPairwise(featureMatrix, para):
    r"""
    KNNgraphPairwise:  measuareName:k
    Pairwise:5
    Minkowski-Pairwise:5:1
    """
    measureName = ''
    k = 5
    if para != None:
        parawords = para.split(':')
        measureName = parawords[0]

    distMat = None
    if measureName == 'Pairwise':
        distMat = distance_matrix(featureMatrix, featureMatrix)
        k = int(parawords[1])
    elif measureName == 'Minkowski-Pairwise':
        p = int(parawords[2])
        distMat = minkowski_distance(featureMatrix, featureMatrix, p=p)
        k = int(parawords[1])
    else:
        print('meausreName in KNNgraph does not recongnized')
    edgeList = []

    for i in np.arange(distMat.shape[0]):
        res = distMat[:, i].argsort()[:k]
        for j in np.arange(k):
            edgeList.append((i, res[j]))

    return edgeList
Example #4
0
    def _compute_distances(self):
        """Compute distances between points.
        """
        # For each ridge the distance of the points enclosing the ridge:
        p1 = self.vor.points[self.vor.ridge_points[:, 0]]
        p2 = self.vor.points[self.vor.ridge_points[:, 1]]
        self.ridge_distances = sp.minkowski_distance(p1, p2)

        # For each point all its Voronoi distances:
        self.neighbor_points = [[] for k in range(len(self.vor.points))]
        self.neighbor_distances = [[] for k in range(len(self.vor.points))]
        for dist, points in zip(self.ridge_distances, self.vor.ridge_points):
            self.neighbor_points[points[0]].append(points[1])
            self.neighbor_points[points[1]].append(points[0])
            self.neighbor_distances[points[0]].append(dist)
            self.neighbor_distances[points[1]].append(dist)
        for k in range(len(self.neighbor_points)):
            inx = np.argsort(self.neighbor_distances[k])
            self.neighbor_points[k] = np.array(self.neighbor_points[k])[inx]
            self.neighbor_distances[k] = np.array(
                self.neighbor_distances[k])[inx]

        # For each point the distance to its nearest neighbor:
        self.nearest_points = []
        self.nearest_distances = np.zeros(len(self.neighbor_distances))
        for k in range(len(self.neighbor_distances)):
            self.nearest_points.append(self.neighbor_points[k][0])
            self.nearest_distances[k] = self.neighbor_distances[k][0]
        self.mean_nearest_distance = np.mean(self.nearest_distances)
        self.mean_nearest_distance *= 1.0 - 2.0 * np.sqrt(
            1.0 / np.pi - 0.25) / np.sqrt(self.vor.npoints)
Example #5
0
    def query_radius(self, RA, DEC, r):
        """
        Find all data points within an angular aperture r around a reference
        point with coordiantes (RA, DEC) obeying the spherical geometry.

        Parameters
        ----------
        RA : float
            Right ascension of the reference point in degrees.
        DEC : float
            Declination of the reference point in degrees.
        r : float
            Maximum separation of data points from the reference point.

        Returns
        -------
        idx : array_like
            Positional indices of matching data points in the search tree data
            with sepration < r.
        dist : array_like
            Angular separation of matching data points from reference point.
        """
        point_sphere = self._position_sky2sphere(RA, DEC)
        # find all points that lie within r
        r_sphere = self._distance_sky2sphere(r)
        idx = self.tree.query_ball_point(point_sphere, r_sphere)
        # compute pair separation
        dist_sphere = minkowski_distance(self.tree.data[idx], point_sphere)
        dist = self._distance_sphere2sky(dist_sphere)
        return idx, dist
Example #6
0
    def test_single_query_all_neighbors(self, r):
        np.random.seed(1234)
        point = np.random.rand(self.kdtree.m)
        d, i = self.kdtree.query(point, k=None, distance_upper_bound=r)
        assert isinstance(d, list)
        assert isinstance(i, list)

        assert_array_equal(np.array(d) <= r, True)  # All within bounds
        # results are sorted by distance
        assert all(a <= b for a, b in zip(d, d[1:]))
        assert_allclose(  # Distances are correct
            d, minkowski_distance(point, self.kdtree.data[i, :]))

        # Compare to brute force
        dist = minkowski_distance(point, self.kdtree.data)
        assert_array_equal(sorted(i), (dist <= r).nonzero()[0])
Example #7
0
 def transform(self, X, y=None):
     assert isinstance(X, pd.DataFrame)
     if self.distance_type == "haversine":
         X["distance"] = haversine_vectorized(X, **dist_args)
     if self.distance_type == "euclidian":
         X["distance"] = minkowski_distance()
     return X[["distance"]]
Example #8
0
    def _compute_distances(self):
        """
        Compute distances between points.
        """
        # For each ridge the distance of the points enclosing the ridge:
        p1 = self.vor.points[self.vor.ridge_points[:,0]]
        p2 = self.vor.points[self.vor.ridge_points[:,1]]
        self.ridge_distances =  sp.minkowski_distance(p1, p2)

        # For each point all its Voronoi distances:
        self.neighbor_points = [[] for k in range(len(self.vor.points))]
        self.neighbor_distances = [[] for k in range(len(self.vor.points))]
        for dist, points in zip(self.ridge_distances, self.vor.ridge_points):
            self.neighbor_points[points[0]].append(points[1])
            self.neighbor_points[points[1]].append(points[0])
            self.neighbor_distances[points[0]].append(dist)
            self.neighbor_distances[points[1]].append(dist)
        for k in range(len(self.neighbor_points)):
            inx = np.argsort(self.neighbor_distances[k])
            self.neighbor_points[k] = np.array(self.neighbor_points[k])[inx]
            self.neighbor_distances[k] = np.array(self.neighbor_distances[k])[inx]

        # For each point the distance to its nearest neighbor:
        self.nearest_points = []
        self.nearest_distances = np.zeros(len(self.neighbor_distances))
        for k in range(len(self.neighbor_distances)):
            self.nearest_points.append(self.neighbor_points[k][0])
            self.nearest_distances[k] = self.neighbor_distances[k][0]
        self.mean_nearest_distance = np.mean(self.nearest_distances)
        self.mean_nearest_distance *= 1.0-2.0*np.sqrt(1.0/np.pi-0.25)/np.sqrt(self.vor.npoints)
Example #9
0
def frechet_dist(exp_data, num_data, p=2):
    n = len(exp_data)
    m = len(num_data)
    ca = np.ones((n, m))
    ca = np.multiply(ca, -1)
    ca[0, 0] = minkowski_distance(exp_data[0], num_data[0], p=p)
    for i in range(1, n):
        ca[i, 0] = max(ca[i - 1, 0],
                       minkowski_distance(exp_data[i], num_data[0], p=p))
    for j in range(1, m):
        ca[0, j] = max(ca[0, j - 1],
                       minkowski_distance(exp_data[0], num_data[j], p=p))
    for i in range(1, n):
        for j in range(1, m):
            ca[i, j] = max(min(ca[i - 1, j], ca[i, j - 1], ca[i - 1, j - 1]),
                           minkowski_distance(exp_data[i], num_data[j], p=p))
    return ca[n - 1, m - 1]
Example #10
0
 def fit(self, X, Y):
     X, Y = normalize(X, axis=1), normalize(Y,
                                            axis=1)  # unit length
     Xt, Yt = np.transpose(X), np.transpose(Y)
     # print('Reconstructor fit() called. Begin to fit from %s to %s.' % (str(X.shape), str(Y.shape)) )
     self.clf.fit(Xt, Yt)
     Y_hat = self.clf.coef_ @ X  #+ self.clf.intercept_.reshape(1,-1)
     return np.mean(minkowski_distance(
         Y, Y_hat)), self.clf.coef_, Y_hat
Example #11
0
def test_distance_matrix():
    m = 10
    n = 11
    k = 4
    np.random.seed(1234)
    xs = np.random.randn(m, k)
    ys = np.random.randn(n, k)
    ds = distance_matrix(xs, ys)
    assert_equal(ds.shape, (m, n))
    for i in range(m):
        for j in range(n):
            assert_almost_equal(minkowski_distance(xs[i], ys[j]), ds[i, j])
Example #12
0
def test_distance_matrix():
    m = 10
    n = 11
    k = 4
    np.random.seed(1234)
    xs = np.random.randn(m,k)
    ys = np.random.randn(n,k)
    ds = distance_matrix(xs,ys)
    assert_equal(ds.shape, (m,n))
    for i in range(m):
        for j in range(n):
            assert_almost_equal(minkowski_distance(xs[i],ys[j]),ds[i,j])
Example #13
0
File: cof.py Project: yzhao062/pyod
 def _cof_memory(self, X):
     """
     Connectivity-Based Outlier Factor (COF) Algorithm
     This function is called internally to calculate the
     Connectivity-Based Outlier Factor (COF) as an outlier
     score for observations.
     This function uses a memory efficient implementation at the cost of 
     speed.
     :return: numpy array containing COF scores for observations.
              The greater the COF, the greater the outlierness.
     """
     #dist_matrix = np.array(distance_matrix(X, X))
     sbn_path_index = np.zeros((X.shape[0], self.n_neighbors_),
                               dtype=np.int64)
     ac_dist, cof_ = np.zeros((X.shape[0])), np.zeros((X.shape[0]))
     for i in range(X.shape[0]):
         #sbn_path = np.argsort(dist_matrix[i])
         sbn_path = np.argsort(minkowski_distance(X[i, :], X, p=2))
         sbn_path_index[i, :] = sbn_path[1:self.n_neighbors_ + 1]
         cost_desc = np.zeros((self.n_neighbors_))
         for j in range(self.n_neighbors_):
             #cost_desc.append(
             #    np.min(dist_matrix[sbn_path[j + 1]][sbn_path][:j + 1]))
             cost_desc[j] = np.min(
                 minkowski_distance(X[sbn_path[j + 1]], X,
                                    p=2)[sbn_path][:j + 1])
         acd = np.zeros((self.n_neighbors_))
         for _h, cost_ in enumerate(cost_desc):
             neighbor_add1 = self.n_neighbors_ + 1
             acd[_h] = ((2. * (neighbor_add1 - (_h + 1))) /
                        (neighbor_add1 * self.n_neighbors_)) * cost_
         ac_dist[i] = np.sum(acd)
     for _g in range(X.shape[0]):
         cof_[_g] = (ac_dist[_g] * self.n_neighbors_) / np.sum(
             ac_dist[sbn_path_index[_g]])
     return np.nan_to_num(cof_)
Example #14
0
def classify0(inX, dataSet, labels, k):
    #    dataSetSize = dataSet.shape[0]
    #    diffMat = np.tile(inX, (dataSetSize, 1)) - dataSet
    #    sqDiffMat = diffMat **2
    #    sqDistances = sqDiffMat.sum(axis=1)
    #    distances = sqDistances**0.5
    #    print('distances', distances)
    distances = minkowski_distance(inX, dataSet)
    sortedDistIndicies = distances.argsort()
    classCount = {}

    for i in range(k):
        voteIlabel = labels[sortedDistIndicies[i]]
        classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
    sortedClassCount = sorted(classCount.items(),
                              key=operator.itemgetter(1),
                              reverse=True)
    return sortedClassCount[0][0]
Example #15
0
    def _compute_distances(self):
        """
        Compute distances between points.
        """
        # For each ridge the distance of the points enclosing the ridge:
        p1 = self.vor.points[self.vor.ridge_points[:, 0]]
        p2 = self.vor.points[self.vor.ridge_points[:, 1]]
        self.ridge_distances = sp.minkowski_distance(p1, p2)

        # For each point all its Voronoi distances:
        self.neighbor_points = [[] for k in range(len(self.vor.points))]
        self.neighbor_distances = [[] for k in range(len(self.vor.points))]
        for dist, points in zip(self.ridge_distances, self.vor.ridge_points):
            self.neighbor_points[points[0]].append(points[1])
            self.neighbor_points[points[1]].append(points[0])
            self.neighbor_distances[points[0]].append(dist)
            self.neighbor_distances[points[1]].append(dist)
        for k in range(len(self.neighbor_points)):
            inx = np.argsort(self.neighbor_distances[k])
            self.neighbor_points[k] = np.array(self.neighbor_points[k])[inx]
            self.neighbor_distances[k] = np.array(
                self.neighbor_distances[k])[inx]

        # For each point the distance to its nearest neighbor:
        self.nearest_points = []
        self.nearest_distances = np.zeros(len(self.neighbor_distances))
        for k in range(len(self.neighbor_distances)):
            self.nearest_points.append(self.neighbor_points[k][0])
            self.nearest_distances[k] = self.neighbor_distances[k][0]
        self.mean_nearest_distance = np.mean(self.nearest_distances)

        ## # estimate aspect ratio
        ## # XXX better would be a ratio of the two main PCI axis
        ## bbox = self.vor.max_bound - self.vor.min_bound
        ## aspect_ratio = bbox[1]/bbox[0]
        ## if aspect_ratio > 1.0:
        ##     aspect_ratio = 1.0/aspect_ratio
        ## ratio = np.sqrt(0.5*(1.0+1.0/aspect_ratio))
        ## self.mean_nearest_distance *= 1.0 - ratio*2.0*np.sqrt(1.0/np.pi-0.25)/np.sqrt(self.vor.npoints)
        ## # this is not the right correction factor!

        self.mean_nearest_distance *= 1.0 - 2.0 * np.sqrt(
            1.0 / np.pi - 0.25) / np.sqrt(self.vor.npoints)
Example #16
0
    def test_vectorized_query_all_neighbors(self):
        query_shape = (2, 4)
        r = 1.1
        np.random.seed(1234)
        points = np.random.rand(*query_shape, self.kdtree.m)
        d, i = self.kdtree.query(points, k=None, distance_upper_bound=r)
        assert_equal(np.shape(d), query_shape)
        assert_equal(np.shape(i), query_shape)

        for idx in np.ndindex(query_shape):
            dist, ind = d[idx], i[idx]
            assert isinstance(dist, list)
            assert isinstance(ind, list)

            assert_array_equal(np.array(dist) <= r, True)  # All within bounds
            # results are sorted by distance
            assert all(a <= b for a, b in zip(dist, dist[1:]))
            assert_allclose(  # Distances are correct
                dist, minkowski_distance(points[idx], self.kdtree.data[ind]))
Example #17
0
    def ridge_lengths(self):
        """Length of Voronoi ridges between nearest neighbors.

        May be used, for example, as a weigth for `ridge_distances`.

        Returns
        -------
        distances: ndarray of floats
            The length of each ridge in `ridge_vertices`.
            np.inf if one vertex is at infinity.
        """
        ridges = np.zeros(len(self.vor.ridge_vertices))
        for k, p in enumerate(self.vor.ridge_vertices):
            if np.all(np.array(p) >= 0):
                p1 = self.vor.vertices[p[0]]
                p2 = self.vor.vertices[p[1]]
                ridges[k] = sp.minkowski_distance(p1, p2)
            else:
                ridges[k] = np.inf
        return ridges
Example #18
0
    def estimate(self, data):
        """Estimate circle model from data using total least squares.

        Parameters
        ----------
        data : (N, 2) array
            N points with ``(x, y)`` coordinates, respectively.

        Returns
        -------
        success : bool
            True, if model estimation succeeds.

        """

        _check_data_dim(data, dim=2)

        # to prevent integer overflow, cast data to float, if it isn't already
        float_type = np.promote_types(data.dtype, np.float32)
        data = data.astype(float_type, copy=False)

        # Adapted from a spherical estimator covered in a blog post by Charles
        # Jeckel (see also reference 1 above):
        # https://jekel.me/2015/Least-Squares-Sphere-Fit/
        A = np.append(data * 2,
                      np.ones((data.shape[0], 1), dtype=float_type),
                      axis=1)
        f = np.sum(data**2, axis=1)
        C, _, rank, _ = np.linalg.lstsq(A, f, rcond=None)

        if rank != 3:
            warn("Input data does not contain enough significant data points. "
                 "In scikit-image 1.0, this warning will become a ValueError.")

        center = C[0:2]
        distances = spatial.minkowski_distance(center, data)
        r = np.sqrt(np.mean(distances**2))

        self.params = tuple(center) + (r, )

        return True
Example #19
0
    def ridge_lengths(self):
        """
        Length of Voronoi ridges between nearest neighbors.

        May be used, for example, as a weigth for `ridge_distances`.

        Returns
        -------
        distances: ndarray of floats
            The length of each ridge in `ridge_vertices`.
            np.inf if one vertex is at infinity.
        """
        ridges = np.zeros(len(self.vor.ridge_vertices))
        for k, p in enumerate(self.vor.ridge_vertices):
            if np.all(np.array(p)>=0):
                p1 = self.vor.vertices[p[0]]
                p2 = self.vor.vertices[p[1]]
                ridges[k] = sp.minkowski_distance(p1, p2)
            else:
                ridges[k] = np.inf
        return ridges
Example #20
0
    def forward(self, obs_traj, obs_traj_rel, seq_start_end, epoch=0):
        """
        Inputs:
        - obs_traj: Tensor of shape (obs_len, batch, 2)
        - obs_traj_rel: Tensor of shape (obs_len, batch, 2)
        - seq_start_end: A list of tuples which delimit sequences within batch.
        Output:
        - pred_traj_rel: Tensor of shape (self.pred_len, batch, 2)
        """
        batch_size = obs_traj.size(1)
        self.total_trajs += batch_size
        #count trajs under threshold
        obs_traj = obs_traj.permute(1, 0, 2)
        final_dist = np.fromiter(
            (minkowski_distance(x[0], x[-1]) for x in obs_traj), float)
        mask = final_dist < self.threshold
        self.total_trajs_under_threshold += sum(mask)

        obs_traj_embedding = self.spatial_embedding(obs_traj_rel.view(-1, 2))
        obs_traj_embedding = obs_traj_embedding.view(
            -1, batch_size, self.embedding_dim).permute(1, 2, 0)
        state = self.convs(obs_traj_embedding).view(
            batch_size, self.obs_len * self.embedding_dim)
        pred_traj_fake_rel = self.hidden2pos(state).reshape(
            batch_size, self.pred_len, 2).permute(1, 0, 2)

        if not self.training:
            pred_traj_fake_rel = pred_traj_fake_rel.permute(1, 0, 2)
            mask = final_dist < self.threshold
            for index, elem in enumerate(mask):
                #True if < threshold
                if elem:
                    #set everything to 0
                    pred_traj_fake_rel[index] = pred_traj_fake_rel[
                        index] != pred_traj_fake_rel[index]
            pred_traj_fake_rel = pred_traj_fake_rel.permute(1, 0, 2)

        obs_traj = obs_traj.permute(1, 0, 2)

        return pred_traj_fake_rel
Example #21
0
        def fit(self, X, Y):
            """
            Args:
            X : numpy.ndarray num_x * dim
            Y : numpy.ndarray num_y * dim
            """
            X, Y = normalize(X, axis=1), normalize(Y, axis=1)  # unit length

            num_x = len(X)
            dim = X.shape[1]
            if dim >= num_x:
                X = torch.from_numpy(X)
                Y = torch.from_numpy(Y)
                coef, Y_hat = self.gels(X, Y)
                coef, Y_hat = coef.cpu().numpy(), Y_hat.cpu().numpy()
            else:
                coef, Y_hat = self.lrfit(X, Y)

            dist_to_projection = np.mean(minkowski_distance(Y, Y_hat))
            assert dist_to_projection <= 1

            return dist_to_projection, coef, Y_hat
Example #22
0
 def distance(self, a, b, p):
     return minkowski_distance(a, b, p)
Example #23
0
def test_distance_vectorization():
    np.random.seed(1234)
    x = np.random.randn(10,1,3)
    y = np.random.randn(1,7,3)
    assert_equal(minkowski_distance(x,y).shape,(10,7))
Example #24
0
def test_distance_linf():
    assert_almost_equal(minkowski_distance([0,0],[1,1],np.inf),1)
 def distance(self, x, y, p):
     return minkowski_distance(np.zeros(x.shape), self.pbcs(x - y), p)
Example #26
0
def test_distance_vectorization():
    np.random.seed(1234)
    x = np.random.randn(10, 1, 3)
    y = np.random.randn(1, 7, 3)
    assert_equal(minkowski_distance(x, y).shape, (10, 7))
Example #27
0
def test_distance_l1():
    assert_almost_equal(minkowski_distance([0, 0], [1, 1], 1), 2)
Example #28
0
def distance_box(a, b, p, boxsize):
    diff = a - b
    diff[diff > 0.5 * boxsize] -= boxsize
    diff[diff < -0.5 * boxsize] += boxsize
    d = minkowski_distance(diff, 0, p)
    return d
Example #29
0
    def randomRoute(self, dist=1000, np_random=None):
        # pick random road as center and pick goal a certain distance away
        if np_random is None: np_random = np.random.RandomState()

        # keep looping until full route found
        while True:
            idx = np_random.randint(self.roads_tree.n)
            if self.roads_tree.idxs[idx][1] == 0: idx_dir = +1
            elif idx + 1 == self.roads_tree.n or self.roads_tree.idxs[
                    idx + 1][1] == 0:
                idx_dir = -1
            else:
                idx_dir = np_random.choice([-1, +1])

            route = [self.roads_tree.data[idx].copy()]
            route_idxs = [idx]
            route_dist = 0
            while route_dist < dist:
                if idx + idx_dir >= 0 and idx + idx_dir < self.roads_tree.n and self.roads_tree.idxs[
                        idx + idx_dir][0] == self.roads_tree.idxs[idx][0]:
                    next_idx = idx + idx_dir
                    dist_to_next = minkowski_distance(
                        self.roads_tree.data[idx],
                        self.roads_tree.data[next_idx])
                else:
                    neighbors = self.roads_tree.query(
                        self.roads_tree.data[idx], k=4)
                    intersect = list(
                        np.where(neighbors[0] <= SAME_POINT_THRESHOLD, 1, 0))
                    idxs = list(neighbors[1])
                    removes = [
                        idx_ for idx_, intersect_ in zip(idxs, intersect)
                        if intersect_ == 0 or idx_ == idx or idx_ in route_idxs
                    ]
                    for i in removes:
                        idxs.remove(i)
                    if len(idxs) == 0: route_dist = -1
                    else:
                        next_idx = np_random.choice(idxs)
                        i = list(neighbors[1]).index(next_idx)
                        dist_to_next = neighbors[0][i]
                        if self.roads_tree.idxs[next_idx][1] == 0: idx_dir = +1
                        elif next_idx + 1 == self.roads_tree.n or self.roads_tree.idxs[
                                next_idx + 1][1] == 0:
                            idx_dir = -1
                        else:
                            idx_dir = np.random.choice([-1, +1])

                if route_dist < 0: break
                route.append(self.roads_tree.data[next_idx].copy())
                route_idxs.append(next_idx)
                route_dist += dist_to_next
                idx = next_idx

            if route_dist < 0: continue
            else: break

        roads_idxs = []
        self.cropped_roads = []
        for idx in route_idxs:
            road_no = self.roads_tree.idxs[idx][0]
            if road_no not in roads_idxs:
                roads_idxs.append(road_no)
                self.cropped_roads.append(self.roads[road_no])

        # print("route generated by randomRoute")
        # print(route)
        return route
Example #30
0
from scipy.special import kl_div

path = '/Users/satwik/classes/cs412/hw1/data.libraries.inventories.txt'
table = np.full((2,100), np.inf)

with open(path, 'r') as dat:
    i = 0
    for row in dat:
        if i == 0:
            i+=1
            continue
        cols = row.split()
        table[i-1,] = cols[1:]
        i+=1

h1 = minkowski_distance(table[0,], table[1,], 1)
h2 = minkowski_distance(table[0,], table[1,], 2)
h_inf = minkowski_distance(table[0,], table[1,], np.inf)


cosine_sim = np.dot(table[0,], table[1,]) / (np.linalg.norm(table[0,]) * np.linalg.norm(table[1,]))
# print(table)
print("Q3 ---")
print("h1: " + str(h1))
print("h2: " + str(h2))
print("h_inf: " + str(h_inf))
print("cosine_sim: " + str(cosine_sim))

prob_distribution = table / table.sum(axis=1).reshape(2,1)
KL = np.sum(kl_div(prob_distribution[0,], prob_distribution[1,]))
print("KL: " + str(KL))
Example #31
0
 def distance(self, x, y, p):
     return minkowski_distance(np.zeros(x.shape), self.pbcs(x - y), p)
def frechet_dist(exp_data, num_data, p=2):
    r"""
    Compute the discrete Frechet distance

    Compute the Discrete Frechet Distance between two N-D curves according to
    [1]_. The Frechet distance has been defined as the walking dog problem.
    From Wikipedia: "In mathematics, the Frechet distance is a measure of
    similarity between curves that takes into account the location and
    ordering of the points along the curves. It is named after Maurice Frechet.
    https://en.wikipedia.org/wiki/Fr%C3%A9chet_distance

    Parameters
    ----------
    exp_data : array_like
        Curve from your experimental data. exp_data is of (M, N) shape, where
        M is the number of data points, and N is the number of dimmensions
    num_data : array_like
        Curve from your numerical data. num_data is of (P, N) shape, where P
        is the number of data points, and N is the number of dimmensions
    p : float, 1 <= p <= infinity
        Which Minkowski p-norm to use. Default is p=2 (Eculidean).
        The manhattan distance is p=1.

    Returns
    -------
    df : float
        discrete Frechet distance

    References
    ----------
    .. [1] Thomas Eiter and Heikki Mannila. Computing discrete Frechet
        distance. Technical report, 1994.
        http://www.kr.tuwien.ac.at/staff/eiter/et-archive/cdtr9464.pdf
        http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.90.937&rep=rep1&type=pdf

    Notes
    -----
    Your x locations of data points should be exp_data[:, 0], and the y
    locations of the data points should be exp_data[:, 1]. Same for num_data.

    Thanks to Arbel Amir for the issue, and Sen ZHANG for the iterative code
    https://github.com/cjekel/similarity_measures/issues/6

    Examples
    --------
    >>> # Generate random experimental data
    >>> x = np.random.random(100)
    >>> y = np.random.random(100)
    >>> exp_data = np.zeros((100, 2))
    >>> exp_data[:, 0] = x
    >>> exp_data[:, 1] = y
    >>> # Generate random numerical data
    >>> x = np.random.random(100)
    >>> y = np.random.random(100)
    >>> num_data = np.zeros((100, 2))
    >>> num_data[:, 0] = x
    >>> num_data[:, 1] = y
    >>> df = frechet_dist(exp_data, num_data)

    """
    n = len(exp_data)
    m = len(num_data)
    ca = np.ones((n, m))
    ca = np.multiply(ca, -1)
    ca[0, 0] = minkowski_distance(exp_data[0], num_data[0], p=p)
    for i in range(1, n):
        ca[i, 0] = max(ca[i-1, 0], minkowski_distance(exp_data[i], num_data[0],
                                                      p=p))
    for j in range(1, m):
        ca[0, j] = max(ca[0, j-1], minkowski_distance(exp_data[0], num_data[j],
                                                      p=p))
    for i in range(1, n):
        for j in range(1, m):
            ca[i, j] = max(min(ca[i-1, j], ca[i, j-1], ca[i-1, j-1]),
                           minkowski_distance(exp_data[i], num_data[j], p=p))
    return ca[n-1, m-1]
Example #33
0
holes = [(2,2), (7,2.5), (10,6), (4,5)]

room = {}
room['vertices'] = array(vertices)
room['segments'] = array(segments)
room['holes'] = array(holes)

tri = triangulate(room,  'pq20a0.1D')
X = tri['triangles'][:,0]
Y = tri['triangles'][:,1]
Z = tri['triangles'][:,2]
Xvert = [tri['vertices'][x] for x in X]
Yvert = [tri['vertices'][x] for x in Y]
Zvert = [tri['vertices'][x] for x in Z]
dataXY = minkowski_distance(Xvert, Yvert)
dataXZ = minkowski_distance(Xvert, Zvert)
dataYZ = minkowski_distance(Yvert, Zvert)

nvert = len(tri['vertices'])
G = lil_matrix((nvert, nvert))
for k in range(len(X)):
	G[X[k], Y[k]] = G[Y[k], X[k]] = dataXY[k]
	G[X[k], Z[k]] = G[Z[k], X[k]] = dataXZ[k]
	G[Y[k], Z[k]] = G[Z[k], Y[k]] = dataYZ[k]

dm, pred = shortest_path(G, return_predecessors=True, directed=True, unweighted=False)
# Last piece of the path
index = 2
path = [2]
while index != 20:
Example #34
0
def distance_box(a, b, p, boxsize):
    diff = a - b
    diff[diff > 0.5 * boxsize] -= boxsize
    diff[diff < -0.5 * boxsize] += boxsize
    d = minkowski_distance(diff, 0, p)
    return d
Example #35
0
def test_distance_l2():
    assert_almost_equal(minkowski_distance([0, 0], [1, 1], 2), np.sqrt(2))
Example #36
0
def test_distance_l2():
    assert_almost_equal(minkowski_distance([0,0],[1,1],2),np.sqrt(2))
Example #37
0
def test_distance_linf():
    assert_almost_equal(minkowski_distance([0, 0], [1, 1], np.inf), 1)
Example #38
0
def test_distance_l1():
    assert_almost_equal(minkowski_distance([0,0],[1,1],1),2)
Example #39
0
 def distance(self, a, b, p):
     return minkowski_distance(a, b, p)
def kernel_euclid(x,y,p=2, **kwds):
    return ssp.minkowski_distance(x[:,np.newaxis,:],y[np.newaxis,:,:],p)
Example #41
0
def kernel_euclid(x,y,p=2, **kwds):
    return ssp.minkowski_distance(x[:,np.newaxis,:],y[np.newaxis,:,:],p)
Example #42
0
import numpy as np
import scipy.spatial.distance as dist
from scipy.spatial import minkowski_distance
from utils import Distances

A = [1, 0, 0]
B = [0, 1, 0]



print('Scipy Euclidean distance is', dist.euclidean(A, B))
print('Euclidean distance is', Distances.euclidean_distance(A, B))
assert dist.euclidean(A, B) == Distances.euclidean_distance(A, B)

print('Scipy minkowski distance is', minkowski_distance(A, B, p=3))
print('minkowski distance is', Distances.minkowski_distance(A, B))
assert minkowski_distance(A, B, p=3) == Distances.minkowski_distance(A, B)

print('Scipy Canberra distance is', dist.canberra(A, B))
print('Canberra distance is', Distances.canberra_distance(A, B))
assert dist.canberra(A, B) == Distances.canberra_distance(A, B)

print('Scipy Cosine distance is', dist.cosine(A, B))
print('Cosine distance is', Distances.cosine_similarity_distance(A, B))
assert dist.cosine(A, B) == Distances.cosine_similarity_distance(A, B)

assert np.dot(A,B) == Distances.inner_product_distance(A,B)

print('All distance measures are the same!')