Пример #1
0
 def test_one_length_one_seq(self, numPairs=100):
     for pair in range(numPairs):
         xs = randn(randint(1, 10))
         y = randn()
         minCost, path = dtw.dtw(xs, [y], eucCost)
         assert_allclose(minCost, sum([ abs(x - y) for x in xs]))
         assert path == [ (i, 0) for i in range(len(xs)) ]
Пример #2
0
 def test_one_length_one_seq(self, numPairs=100):
     for pair in range(numPairs):
         xs = randn(randint(1, 10))
         y = randn()
         minCost, path = dtw.dtw(xs, [y], eucCost)
         assert_allclose(minCost, sum([abs(x - y) for x in xs]))
         assert path == [(i, 0) for i in range(len(xs))]
Пример #3
0
 def test_length_one_seq(self, numPairs=100):
     for pair in range(numPairs):
         x = randn()
         y = randn()
         minCost, path = dtw.dtw([x], [y], eucCost)
         assert_allclose(minCost, abs(x - y))
         assert path == [(0, 0)]
Пример #4
0
 def test_length_one_seq(self, numPairs=100):
     for pair in range(numPairs):
         x = randn()
         y = randn()
         minCost, path = dtw.dtw([x], [y], eucCost)
         assert_allclose(minCost, abs(x - y))
         assert path == [(0, 0)]
Пример #5
0
    def test_brute_force_small(self, numPairs=100):
        for pair in range(numPairs):
            dim = randint(0, 3) if randBool() else randint(0, 10)
            xs = randSeq(dim=dim, minLength=1, ensureShort=True)
            ys = randSeq(dim=dim, minLength=1, ensureShort=True)

            childrenDict, startNode = getDtwDag(len(xs), len(ys))
            minCostGood = min([
                getPathCost(path, xs, ys, eucCost)
                for path, _ in getDagPathIterator(childrenDict, startNode)
            ])

            minCost, _ = dtw.dtw(xs, ys, eucCost)
            assert_allclose(minCost, minCostGood)
Пример #6
0
    def test_brute_force_small(self, numPairs=100):
        for pair in range(numPairs):
            dim = randint(0, 3) if randBool() else randint(0, 10)
            xs = randSeq(dim=dim, minLength=1, ensureShort=True)
            ys = randSeq(dim=dim, minLength=1, ensureShort=True)

            childrenDict, startNode = getDtwDag(len(xs), len(ys))
            minCostGood = min([
                getPathCost(path, xs, ys, eucCost)
                for path, _ in getDagPathIterator(childrenDict, startNode)
            ])

            minCost, _ = dtw.dtw(xs, ys, eucCost)
            assert_allclose(minCost, minCostGood)
Пример #7
0
    def test_logSpecDbDist(self, numPoints=100):
        # (FIXME : not a proper unit test: doesn't really check correctness)
        for _ in range(numPoints):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            x = randn(size)
            y = randn(size)

            # check fast and slow versions agree
            assert_allclose(mtf.logSpecDbDist(x, y), mt.logSpecDbDist(x, y))

            # check both versions raise an error where appropriate
            y = randn(size + 1)
            if size > 1:
                self.assertRaises(ValueError, mt.logSpecDbDist, x, y)
            self.assertRaises(AssertionError, mtf.logSpecDbDist, x, y)
Пример #8
0
    def test_logSpecDbDist(self, numPoints=100):
        # (FIXME : not a proper unit test: doesn't really check correctness)
        for _ in range(numPoints):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            x = randn(size)
            y = randn(size)

            # check fast and slow versions agree
            assert_allclose(mtf.logSpecDbDist(x, y), mt.logSpecDbDist(x, y))

            # check both versions raise an error where appropriate
            y = randn(size + 1)
            if size > 1:
                self.assertRaises(ValueError, mt.logSpecDbDist, x, y)
            self.assertRaises(AssertionError, mtf.logSpecDbDist, x, y)
Пример #9
0
    def test_universal_properties(self, numPairs=100, numPathsPerPair=20):
        for pair in range(numPairs):
            dim = randint(0, 3) if randBool() else randint(0, 10)
            xs = randSeq(dim=dim, minLength=1)
            ys = randSeq(dim=dim, minLength=1)
            minCost, path = dtw.dtw(xs, ys, eucCost)

            # test cost along path agrees with minimum cost
            assert_allclose(minCost, getPathCost(path, xs, ys, eucCost))

            # test transpose
            minCost2, path2 = dtw.dtw(ys, xs, eucCost)
            assert_allclose(minCost2, minCost)
            # N.B. this is not a universal property but will almost always be
            #   true for the random sequences of floats that we generate.
            assert path2 == dtw.swapPath(path)

            # test path is a valid path
            assert dtw.isValidPath(path)
            assert path[-1] == (len(xs) - 1, len(ys) - 1)

            # test optimal subpaths property
            cutIndex = randint(len(path))
            iCut, jCut = path[cutIndex]
            pathA = path[:(cutIndex + 1)]
            pathB = path[cutIndex:]
            costA = getPathCost(pathA, xs, ys, eucCost)
            costB = getPathCost(pathB, xs, ys, eucCost)
            minCostA, _ = dtw.dtw(xs[:(iCut + 1)], ys[:(jCut + 1)], eucCost)
            minCostB, _ = dtw.dtw(xs[iCut:], ys[jCut:], eucCost)
            assert_allclose(costA, minCostA)
            assert_allclose(costB, minCostB)

            # test minCost <= cost for several randomly generated paths
            childrenDict, startNode = getDtwDag(len(xs), len(ys))
            for _ in range(numPathsPerPair):
                path = getRandomDagPath(childrenDict, startNode)
                cost = getPathCost(path, xs, ys, eucCost)
                assert minCost <= cost or np.allclose(minCost, cost)

            # minCost to itself should be zero
            assert dtw.dtw(xs, xs, eucCost)[0] == 0.0
Пример #10
0
    def test_universal_properties(self, numPairs=100, numPathsPerPair=20):
        for pair in range(numPairs):
            dim = randint(0, 3) if randBool() else randint(0, 10)
            xs = randSeq(dim=dim, minLength=1)
            ys = randSeq(dim=dim, minLength=1)
            minCost, path = dtw.dtw(xs, ys, eucCost)

            # test cost along path agrees with minimum cost
            assert_allclose(minCost, getPathCost(path, xs, ys, eucCost))

            # test transpose
            minCost2, path2 = dtw.dtw(ys, xs, eucCost)
            assert_allclose(minCost2, minCost)
            # N.B. this is not a universal property but will almost always be
            #   true for the random sequences of floats that we generate.
            assert path2 == dtw.swapPath(path)

            # test path is a valid path
            assert dtw.isValidPath(path)
            assert path[-1] == (len(xs) - 1, len(ys) - 1)

            # test optimal subpaths property
            cutIndex = randint(len(path))
            iCut, jCut = path[cutIndex]
            pathA = path[:(cutIndex + 1)]
            pathB = path[cutIndex:]
            costA = getPathCost(pathA, xs, ys, eucCost)
            costB = getPathCost(pathB, xs, ys, eucCost)
            minCostA, _ = dtw.dtw(xs[:(iCut + 1)], ys[:(jCut + 1)], eucCost)
            minCostB, _ = dtw.dtw(xs[iCut:], ys[jCut:], eucCost)
            assert_allclose(costA, minCostA)
            assert_allclose(costB, minCostB)

            # test minCost <= cost for several randomly generated paths
            childrenDict, startNode = getDtwDag(len(xs), len(ys))
            for _ in range(numPathsPerPair):
                path = getRandomDagPath(childrenDict, startNode)
                cost = getPathCost(path, xs, ys, eucCost)
                assert minCost <= cost or np.allclose(minCost, cost)

            # minCost to itself should be zero
            assert dtw.dtw(xs, xs, eucCost)[0] == 0.0