Example #1
0
 def test_2D_point_init(self):
     coords1 = [-7, -4]
     coords2 = [17, 6]
     coords3 = [3, 4]
     coords4 = [0, 0]
     p1 = Point(coords1)
     p2 = Point(coords2)
     p3 = Point(coords3)
     p4 = Point(coords4)
Example #2
0
 def test_3D_point_init(self):
     coords1 = [-1, 2, 3]
     coords2 = [4, 0, -3]
     coords3 = [7, 4, 3]
     coords4 = [17, 6, 2]
     p1 = Point(coords1, 0)
     p2 = Point(coords2, 0)
     p3 = Point(coords3, 0)
     p4 = Point(coords4, 0)
Example #3
0
 def test_ITWD(self):
     p1 = Point([-7, -4])
     p2 = Point([5, 6])
     p3 = Point([3, 4])
     p4 = Point([-3, 5])
     t1 = Trajectory([p1, p2])
     t2 = Trajectory([p3, p4])
     self.assertEqual(45, dtwD(t1, t2))
     t1 = Trajectory([p1, p2, p3, p4])
     self.assertEqual(0, dtwD(t1, t1))
Example #4
0
def multidimScale(L1, func, dim):
    n = len(L1)
    D = np.zeros((n, n))
    for i in range(n):
        for j in range(n):
            if i <= j:
                D[i][j] = func(L1[i], L1[j])
            else:
                D[i][j] = D[j][i]
    C = np.identity(n) - (1. / n) * np.ones((n, n))
    B = -0.5 * np.matmul(C, np.matmul(D, C))
    evals, evecs = np.linalg.eig(B)
    evals = np.ndarray.tolist(evals)
    evecs = np.ndarray.tolist(np.matrix.transpose(evecs))
    print(evals)
    evaltemp = []
    evectemp = []
    for i in range(len(evals)):
        if np.imag(evals[i]) == 0:
            evaltemp.append(np.real(evals[i]))
            evectemp.append(evecs[i])
    evals = evaltemp
    evecs = evectemp
    print(evals)
    zipped = sorted(zip(evals, evecs), reverse=True)[:dim]
    evals = [x[0] for x in zipped]
    evecs = np.real(np.asarray([x[1] for x in zipped]))
    for i in range(dim):
        if evals[i] >= 0:
            evecs[i] *= evals[i]**0.5
    evecs = np.matrix.transpose(evecs)
    pts = [Point(np.ndarray.tolist(x)[:dim]) for x in evecs]
    return pts
Example #5
0
def interpolateBetweenPoints(p1, p2, t):
    coords = []
    dT = p2.t - p1.t
    for i in range(len(p1)):
        slope = (p2[i] - p1[i]) / dT
        coords.append(p1[i] + slope * (t - p1.t))
    return Point(coords, t)
Example #6
0
 def test_3D_point_dist(self):
     coords1 = [-1, 2, 3]
     coords2 = [4, 0, -3]
     coords3 = [7, 4, 3]
     coords4 = [17, 6, 2]
     p1 = Point(coords1, 0)
     p2 = Point(coords2, 0)
     p3 = Point(coords3, 0)
     p4 = Point(coords4, 0)
     self.assertEqual(p1.dist(p2), 65**(0.5))
     self.assertEqual(p1.dist(p1), 0.0)
     self.assertEqual(p3.dist(p4), 105**0.5)
Example #7
0
 def test_2D_point_dist(self):
     coords1 = [-7, -4]
     coords2 = [17, 6]
     coords3 = [3, 4]
     coords4 = [0, 0]
     p1 = Point(coords1)
     p2 = Point(coords2)
     p3 = Point(coords3)
     p4 = Point(coords4)
     self.assertEqual(p1.dist(p2), 26.0)
     self.assertEqual(p1.dist(p1), 0.0)
     self.assertEqual(p3.dist(p4), 5.0)
Example #8
0
 def testdiscretefrechet(self):
     L1 = [Point([1, 1]), Point([2, 1]), Point([2, 2])]
     L2 = [Point([2, 2]), Point([0, 1]), Point([2, 4])]
     P = Trajectory(L1)
     Q = Trajectory(L1)
     R = Trajectory(L2)
     self.assertEqual(frechetDist(P, Q), 0.0)
     self.assertEqual(frechetDist(P, R), 2.0)
Example #9
0
 def _critB(self, i, j):
     dirv = Point([x - y for x, y in zip(self.Q[j + 1], self.Q[j])])
     mag = math.sqrt(dot(dirv, dirv))
     n = Point([x / mag for x in dirv])
     qp = Point([w - z for w, z in zip(self.Q[j], self.P[i])])
     c = dot(n, qp)
     v = Point([c * x for x in n])
     return qp.dist_sq(v)
Example #10
0
 def _critCQ(self, k, l):
     p = Point([0.5 * x + 0.5 * y for x, y in zip(self.Q[k], self.Q[l])])
     n = Point([y - x for x, y in zip(self.Q[k], self.Q[l])])
     d = dict()
     B = getBasis(p, n)
     for x in range(len(self.P) - 1):
         M, M2 = getMatrix(B, self.P[x]), getMatrix(B, self.P[x+1])
         d1, d2 = getMatrixDeterminant(M), getMatrixDeterminant(M2)
         if d1 * d2 <= 0:
             d1, d2 = abs(d1), abs(d2)
             if d1 - d2 == 0:
                 q = Point([0.5 * x + 0.5 * y for x, y in zip(self.P[x], self.P[x+1])])
             else:
                 q = Point([(d2 * x + d1 * y)/(d1 + d2) for x, y in zip(self.P[x], self.P[x+1])])
             d[x] = q.dist_sq(self.Q[k])
     return d
Example #11
0
 def _BF_ij(self, i, j, epsilon):
     left, right = 0, 1
     lp = Point([left * x + (1-left) * y for x, y in zip(self.P[i+1], self.P[i])])
     rp = Point([right * x + (1-right) * y for x, y in zip(self.P[i+1], self.P[i])])
     d, loop = lp.dist_sq(self.Q[j]), 1
     prime = self.epsDot(self.P[i+1], self.P[i], self.Q[j], left)
     while d != epsilon and loop < 52:
         if d < epsilon and left == 0:
             loop = 52
         else:
             left = self._editLB(left, loop, d, epsilon, prime)
         loop += 1
         lp = Point([left * x + (1 - left) * y for x, y in zip(self.P[i + 1], self.P[i])])
         prime = self.epsDot(self.P[i + 1], self.P[i], self.Q[j], left)
         d = lp.dist_sq(self.Q[j])
     if abs(d - epsilon) > 0.00000000002 and left != 0:
         left = 2
     d, loop = rp.dist_sq(self.Q[j]), 1
     prime = self.epsDot(self.P[i + 1], self.P[i], self.Q[j], right)
     while d != epsilon and loop < 52:
         if d < epsilon and right == 1:
             loop = 52
         else:
             right = self._editRT(right, loop, d, epsilon, prime)
         loop += 1
         rp = Point([right * x + (1-right) * y for x,y in zip(self.P[i+1], self.P[i])])
         prime = self.epsDot(self.P[i + 1], self.P[i], self.Q[j], right)
         d = rp.dist_sq(self.Q[j])
     if abs(d - epsilon) > 0.00000000002 and right != 1:
         right = 2
     return (left, right)
Example #12
0
 def _LF_ij(self, i, j, epsilon):
     bottom, top = 0, 1
     bp = Point([bottom * x + (1 - bottom) * y for x, y in zip(self.Q[j + 1], self.Q[j])])
     tp = Point([top * x + (1 - top) * y for x, y in zip(self.Q[j + 1], self.Q[j])])
     d, loop = bp.dist_sq(self.P[i]), 1
     prime = self.epsDot(self.Q[j + 1], self.Q[j], self.P[i], bottom)
     while d != epsilon and loop < 52:
         if d < epsilon and bottom == 0:
             loop = 52
         else:
             bottom = self._editLB(bottom, loop, d, epsilon, prime)
         loop += 1
         bp = Point([bottom * x + (1 - bottom) * y for x, y in zip(self.Q[j + 1], self.Q[j])])
         prime = self.epsDot(self.Q[j + 1], self.Q[j], self.P[i], bottom)
         d = bp.dist_sq(self.P[i])
     if abs(d - epsilon) > 0.00000000002 and bottom != 0:
         bottom = 2
     d, loop = tp.dist_sq(self.P[i]), 1
     prime = self.epsDot(self.Q[j + 1], self.Q[j], self.P[i], top)
     while d != epsilon and loop < 52:
         if d < epsilon and top == 1:
             loop = 52
         else:
             top = self._editRT(top, loop, d, epsilon, prime)
         loop += 1
         tp = Point([top * x + (1 - top) * y for x, y in zip(self.Q[j + 1], self.Q[j])])
         prime = self.epsDot(self.Q[j + 1], self.Q[j], self.P[i], top)
         d = tp.dist_sq(self.P[i])
     if abs(d - epsilon) > 0.00000000002 and top != 1:
         top = 2
     return (bottom, top)
Example #13
0
 def make_pt_obj(self, data):
     pt = Point(data[1:], data[0])
     self._points.append(pt)
Example #14
0
def contL_p(P, Q, p):
    P0 = P[0].t
    for x in range(len(P)):
        if P[x].t == 0:
            P[x].t = x / (len(P) - 1)
        else:
            P[x].t = P[x].t - P0
    Q0 = Q[0].t
    for y in range(len(Q)):
        if Q[y].t == 0:
            Q[y].t = y / (len(Q) - 1)
        else:
            Q[y].t = Q[y].t - Q0
    i, j, prev, total = 0, 0, 0, 0
    mark = 0
    if P[-1].t < Q[-1].t:
        P.pts.append(Point(P[-1].coords, Q[-1].t))
        mark = 1
    elif Q[-1].t < P[-1].t:
        Q.pts.append(Point(Q[-1].coords, P[-1].t))
        mark = 2
    while i < len(P) - 1 or j < len(Q) - 1:
        if P[i + 1].t < Q[j + 1].t:
            for k in range(len(P[i + 1])):
                fdir, gdir = (P[i + 1][k] - P[i][k]) / (P[i + 1].t - P[i].t), (
                    Q[j + 1][k] - Q[j][k]) / (Q[j + 1].t - Q[j].t)
                fstart, gstart = P[i][k] - P[i].t * fdir, Q[j][
                    k] - Q[j].t * gdir
                b = fstart - gstart
                a = fdir - gdir
                if p % 2 == 0:
                    total += _integral_p(prev, P[i + 1].t, p, a, b)
                elif a != 0 and prev < (0 - b) / a < P[i + 1].t:
                    if a * prev + b < 0:
                        total += _integral_p(
                            (0 - b) / a, prev, p, a, b) + _integral_p(
                                (0 - b) / a, P[i + 1].t, p, a, b)
                    else:
                        total += _integral_p(
                            prev, (0 - b) / a, p, a, b) + _integral_p(
                                P[i + 1].t, (0 - b) / a, p, a, b)
                else:
                    if a * (prev + P[i + 1].t) / 2 + b > 0:
                        total += _integral_p(prev, P[i + 1].t, p, a, b)
                    else:
                        total += _integral_p(P[i + 1].t, prev, p, a, b)
            prev = P[i + 1].t
            i += 1
        else:
            for k in range(len(Q[j + 1])):
                fdir, gdir = (P[i + 1][k] - P[i][k]) / (P[i + 1].t - P[i].t), (
                    Q[j + 1][k] - Q[j][k]) / (Q[j + 1].t - Q[j].t)
                fstart, gstart = P[i][k] - P[i].t * fdir, Q[j][
                    k] - Q[j].t * gdir
                b = fstart - gstart
                a = fdir - gdir
                if p % 2 == 0:
                    total += _integral_p(prev, Q[j + 1].t, p, a, b)
                elif a != 0 and prev < (0 - b) / a < Q[j + 1].t:
                    if a * prev + b < 0:
                        total += _integral_p(
                            (0 - b) / a, prev, p, a, b) + _integral_p(
                                (0 - b) / a, Q[j + 1].t, p, a, b)
                    else:
                        total += _integral_p(
                            prev, (0 - b) / a, p, a, b) + _integral_p(
                                Q[j + 1].t, (0 - b) / a, p, a, b)
                else:
                    if a * (prev + Q[j + 1].t) / 2 + b > 0:
                        total += _integral_p(prev, Q[j + 1].t, p, a, b)
                    else:
                        total += _integral_p(Q[j + 1].t, prev, p, a, b)
            prev = Q[j + 1].t
            if P[i + 1].t == Q[j + 1].t:
                i += 1
            j += 1
    if mark == 1:
        P.pts.pop()
    elif mark == 2:
        Q.pts.pop()
    if P0 != 0:
        for x in range(len(P)):
            P[x].t += P0
    if Q0 != 0:
        for y in range(len(Q)):
            Q[y].t += Q0
    return total**(1 / p)
Example #15
0
 def test_point_eq_degree(self):
     coords1 = [-7, -4]
     coords2 = [17, 6]
     coords3 = [7, 4, 3]
     coords4 = [17, 6, 2]
     p1 = Point(coords1)
     p2 = Point(coords2)
     p3 = Point(coords3, 0)
     p4 = Point(coords4, 0)
     self.assertTrue(p1.eq_degree(p2))
     self.assertTrue(p3.eq_degree(p4))
     self.assertFalse(p1.eq_degree(p3))
     self.assertFalse(p1.eq_degree(p4))
     self.assertFalse(p2.eq_degree(p3))
     self.assertFalse(p2.eq_degree(p4))
Example #16
0
 def testL_one_simple(self):
     a = Trajectory([Point((1, 2))])
     b = Trajectory([Point((5, 5))])
Example #17
0
 def test_1D_DTW(self):
     t1 = [1, 2, 2, 10, 2, 1]
     t2 = [3, 3, 5, 5, 2]
     self.assertEqual(45, dtw(t1, t2, -1, metricI))
     self.assertEqual(0, dtw(t1, t1, -1, metricI))
     t1 = Trajectory([
         Point([1]),
         Point([2]),
         Point([2]),
         Point([10]),
         Point([2]),
         Point([1])
     ])
     t2 = Trajectory(
         [Point([3]),
          Point([3]),
          Point([5]),
          Point([5]),
          Point([2])])
     self.assertEqual(45, dtw(t1, t2, -1, metricD))
     self.assertEqual(0, dtw(t1, t1, -1, metricD))