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_diagonals_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
        max_diag = min(len(query) - m + 1,
                       len(series) - m + 1)  # Maximum length of a diagonal

        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_diagonals(partial=20)
        npt.assert_(20 <= np.count_nonzero(
            ~np.isnan(consumer1.distance_matrix) < 20 + max_diag))
        npt.assert_(20 <= np.count_nonzero(
            ~np.isnan(consumer2.distance_matrix) < 20 + max_diag))
        npt.assert_(20 <= np.count_nonzero(
            ~np.isnan(consumer3.distance_matrix) < 20 + max_diag))

        # For 20 items, at least 3 diagonals are calculated
        for diagonal in calc._diagonal_calc_order[:3]:
            diag_indices = diag_indices_of(distance_matrix1, diagonal)
            npt.assert_equal(consumer1.distance_matrix[diag_indices],
                             distance_matrix1[diag_indices])

        calc.calculate_diagonals(partial=.8)
        npt.assert_(160 <= np.count_nonzero(
            ~np.isnan(consumer1.distance_matrix) < 160 + max_diag))
        npt.assert_(160 <= np.count_nonzero(
            ~np.isnan(consumer2.distance_matrix) < 160 + max_diag))
        npt.assert_(160 <= np.count_nonzero(
            ~np.isnan(consumer3.distance_matrix) < 160 + max_diag))

        calc.calculate_diagonals()
        npt.assert_(
            200 == np.count_nonzero(~np.isnan(consumer1.distance_matrix)))
        npt.assert_(
            200 == np.count_nonzero(~np.isnan(consumer2.distance_matrix)))
        npt.assert_(
            200 == np.count_nonzero(~np.isnan(consumer3.distance_matrix)))
        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_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)
    def test_simple_calculate_self_join_diagonals(self):
        series = np.arange(23)
        m = 4
        buffer = 2

        # This test verifies that no values below the main diagonal are used.
        distance_matrix = np.triu(
            np.arange(1., 401.).reshape((20, 20)), buffer + 1)

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

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

        npt.assert_array_less(np.full(17, buffer), calc._diagonal_calc_order)

        calc.calculate_diagonals()
        expected = distance_matrix + distance_matrix.T
        expected[expected == 0] = np.nan
        npt.assert_equal(consumer.distance_matrix, expected)
예제 #6
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
from distancematrix.consumer.matrix_profile_lr import MatrixProfileLR

import numpy as np
from src.series import Series

m = 10

# Create some random data
np.random.seed(123)
data = np.array(np.random.rand(100))

# Calculate the matrix profile
calculator = AnytimeCalculator(m, [data])
generator = calculator.add_generator(
    0, ZNormEuclidean(noise_std=0.))  # Generator 0 works on channel 0
consumer = calculator.add_consumer([0], MatrixProfileLR())
calculator.calculate_diagonals()

# The matrix profile
left_matrix_profile = consumer.matrix_profile_left
profile_index_left = consumer.profile_index_left


class TestSeries(TestCase):
    def test_it_should_open_the_result_file_as_appending(self):
        mock = mock_open()

        with patch('builtins.open', mock):
            series = Series('key', [m], 1000, './test')

        mock.assert_called_once_with('./test/10.txt', 'a')