def test_simple_calculate_columns(self):
        query = np.arange(13)
        series = np.arange(23)
        m = 4

        distance_matrix1 = np.arange(200.).reshape((10, 20))
        distance_matrix2 = np.ones((10, 20), dtype=np.float)
        distance_matrix3 = np.full((10, 20), np.nan, dtype=np.float)
        distance_matrix4 = np.full((10, 20), 5., dtype=np.float)

        calc = AnytimeCalculator(m, series, query)
        calc.add_generator(0, MockGenerator(distance_matrix1))
        calc.add_generator(0, MockGenerator(distance_matrix2))
        calc.add_generator(0, MockGenerator(distance_matrix3))
        calc.add_generator(0, MockGenerator(distance_matrix4))

        consumer1 = DistanceMatrix()
        consumer2 = DistanceMatrix()
        consumer3 = SummingConsumer()
        calc.add_consumer([0], consumer1)
        calc.add_consumer([3], consumer2)
        calc.add_consumer([0, 1, 3], consumer3)

        calc.calculate_columns()

        npt.assert_equal(consumer1.distance_matrix, distance_matrix1)
        npt.assert_equal(consumer2.distance_matrix, distance_matrix4)
        npt.assert_equal(
            consumer3.distance_matrix,
            distance_matrix1 + distance_matrix2 + distance_matrix4)
    def test_simple_calculate_columns_partial(self):
        query = np.arange(13)
        series = np.arange(23)
        m = 4

        distance_matrix1 = np.arange(200.).reshape((10, 20))
        distance_matrix2 = np.ones((10, 20), dtype=np.float)
        distance_matrix3 = np.full((10, 20), np.nan, dtype=np.float)
        distance_matrix4 = np.full((10, 20), 5., dtype=np.float)
        summed_matrix = distance_matrix1 + distance_matrix2 + distance_matrix4

        calc = AnytimeCalculator(m, series, query)
        calc.add_generator(0, MockGenerator(distance_matrix1))
        calc.add_generator(0, MockGenerator(distance_matrix2))
        calc.add_generator(0, MockGenerator(distance_matrix3))
        calc.add_generator(0, MockGenerator(distance_matrix4))

        consumer1 = DistanceMatrix()
        consumer2 = DistanceMatrix()
        consumer3 = SummingConsumer()
        calc.add_consumer([0], consumer1)
        calc.add_consumer([3], consumer2)
        calc.add_consumer([0, 1, 3], consumer3)

        # Calculates [3..12[
        calc.calculate_columns(start=3, upto=0.6)
        npt.assert_equal(consumer1.distance_matrix,
                         copy_columns(distance_matrix1, range(3, 12)))
        npt.assert_equal(consumer2.distance_matrix,
                         copy_columns(distance_matrix4, range(3, 12)))
        npt.assert_equal(consumer3.distance_matrix,
                         copy_columns(summed_matrix, range(3, 12)))

        # Calculates [12..15[
        calc.calculate_columns(upto=15)
        npt.assert_equal(consumer1.distance_matrix,
                         copy_columns(distance_matrix1, range(3, 15)))
        npt.assert_equal(consumer2.distance_matrix,
                         copy_columns(distance_matrix4, range(3, 15)))
        npt.assert_equal(consumer3.distance_matrix,
                         copy_columns(summed_matrix, range(3, 15)))

        # Calculates [15..20[
        calc.calculate_columns()
        npt.assert_equal(consumer1.distance_matrix,
                         copy_columns(distance_matrix1, range(3, 20)))
        npt.assert_equal(consumer2.distance_matrix,
                         copy_columns(distance_matrix4, range(3, 20)))
        npt.assert_equal(consumer3.distance_matrix,
                         copy_columns(summed_matrix, range(3, 20)))

        # Calculates [0..3[
        calc.calculate_columns(start=0., upto=3)
        npt.assert_equal(consumer1.distance_matrix, distance_matrix1)
        npt.assert_equal(consumer2.distance_matrix, distance_matrix4)
        npt.assert_equal(consumer3.distance_matrix, summed_matrix)
    def test_simple_calculate_self_join_columns(self):
        series = np.arange(23)
        m = 4
        buffer = 2

        distance_matrix = np.arange(1., 401.).reshape((20, 20))

        calc = AnytimeCalculator(m, series, trivial_match_buffer=buffer)
        calc.add_generator(0, MockGenerator(distance_matrix))

        consumer = DistanceMatrix()
        calc.add_consumer([0], consumer)

        calc.calculate_columns()

        expected = distance_matrix.copy()
        for diag in range(-buffer, buffer + 1):
            expected[diag_indices_of(expected, diag)] = np.inf

        npt.assert_equal(consumer.distance_matrix, expected)
Ejemplo n.º 4
0
def find_consensus_motif_bruteforce(series_list, m) -> CMResult:
    result = CMResult(np.inf, -1, -1)

    for series_idx, series in enumerate(series_list):
        radii = np.zeros(len(series) - m + 1)
        for series2_idx, series2 in enumerate(series_list):
            if series_idx == series2_idx:
                continue

            calc = AnytimeCalculator(m, series, series2)
            calc.add_generator(0, ZNormEuclidean())
            mp_cons = calc.add_consumer([0], MatrixProfileLR())
            calc.calculate_columns()
            mp = mp_cons.matrix_profile()

            radii = np.maximum(radii, mp)

        subseq_idx = np.argmin(radii)
        subseq_radius = radii[subseq_idx]
        if subseq_radius < result.radius:
            result = CMResult(subseq_radius, series_idx, subseq_idx)

    return result