def test_simple_calculate_diagonals(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)

        calc.calculate_diagonals()

        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_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)
    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)
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')