def test_process_column_complex_ranges(self):
        # Overlapping ranges and contexts consisting of multiple ranges
        query_ranges = [
            range(0, 10),
            range(1, 5),
            range(1, 2),
            range(4, 5), [range(1, 2), range(3, 4),
                          range(7, 9)]
        ]
        series_ranges = [
            range(0, 2),
            range(1, 3),
            range(2, 4),
            range(3, 6),
            range(4, 8),
            range(4, 10), [range(0, 3),
                           range(3, 5),
                           range(13, 15)]
        ]

        correct, correct_qi, correct_si = self.bruteforce_cdm(
            self.dist_matrix, query_ranges, series_ranges)

        cdm = ContextualMatrixProfile(
            GeneralStaticManager(series_ranges, query_ranges))
        self.mock_initialise(cdm)

        for column in range(0, self.dist_matrix.shape[1]):
            cdm.process_column(column,
                               np.atleast_2d(self.dist_matrix[:, column]))

        npt.assert_allclose(cdm.distance_matrix, correct)
        npt.assert_equal(cdm.match_index_query, correct_qi)
        npt.assert_equal(cdm.match_index_series, correct_si)
    def test_process_diagonal_partial_calculation(self):
        # Ranges selected so that the diagonals have a mix of the number of numbers that are filled in
        query_ranges = [
            range(0, 2),
            range(2, 5),
            range(7, 8),
            range(8, 9),
            range(9, 10)
        ]
        series_ranges = [range(0, 2), range(2, 4), range(4, 10), range(13, 14)]

        part_dist_matrix = np.full_like(self.dist_matrix, np.inf)

        cdm = ContextualMatrixProfile(
            GeneralStaticManager(series_ranges, query_ranges))
        self.mock_initialise(cdm)

        for diag in range(-8, self.dist_matrix.shape[1], 4):
            diag_ind = diag_indices_of(self.dist_matrix, diag)
            cdm.process_diagonal(diag,
                                 np.atleast_2d(self.dist_matrix[diag_ind]))
            part_dist_matrix[diag_ind] = self.dist_matrix[diag_ind]

        correct, correct_qi, correct_si = self.bruteforce_cdm(
            part_dist_matrix, query_ranges, series_ranges)

        npt.assert_allclose(cdm.distance_matrix, correct)
        npt.assert_equal(cdm.match_index_query, correct_qi)
        npt.assert_equal(cdm.match_index_series, correct_si)
    def test_process_column_context_falls_outside_distancematrix(self):
        query_ranges = [range(0, 8), range(8, 16), range(20, 30)]
        series_ranges = [range(0, 10), range(10, 20), range(30, 40)]

        correct, correct_qi, correct_si = self.bruteforce_cdm(
            self.dist_matrix, query_ranges, series_ranges)

        cdm = ContextualMatrixProfile(
            GeneralStaticManager(series_ranges, query_ranges))
        self.mock_initialise(cdm)

        for column in range(0, self.dist_matrix.shape[1]):
            cdm.process_column(column,
                               np.atleast_2d(self.dist_matrix[:, column]))

        npt.assert_allclose(cdm.distance_matrix, correct)
        npt.assert_equal(cdm.match_index_query, correct_qi)
        npt.assert_equal(cdm.match_index_series, correct_si)
    def test_process_diagonal_context_goes_beyond_distancematrix(self):
        query_ranges = [range(0, 8), range(8, 16)]
        series_ranges = [range(0, 10), range(10, 20)]

        correct, correct_qi, correct_si = self.bruteforce_cdm(
            self.dist_matrix, query_ranges, series_ranges)

        cdm = ContextualMatrixProfile(
            GeneralStaticManager(series_ranges, query_ranges))
        self.mock_initialise(cdm)

        for diag in range(-self.dist_matrix.shape[0] + 1,
                          self.dist_matrix.shape[1]):
            diag_ind = diag_indices_of(self.dist_matrix, diag)
            cdm.process_diagonal(diag,
                                 np.atleast_2d(self.dist_matrix[diag_ind]))

        npt.assert_allclose(cdm.distance_matrix, correct)
        npt.assert_equal(cdm.match_index_query, correct_qi)
        npt.assert_equal(cdm.match_index_series, correct_si)
    def test_process_column_partial_calculation(self):
        # Ranges selected so that the contexts will receive 0, 1, 2 or 3 columns
        query_ranges = [range(0, 2), range(2, 5), range(7, 10)]
        series_ranges = [range(0, 2), range(2, 4), range(4, 6), range(9, 14)]

        part_dist_matrix = np.full_like(self.dist_matrix, np.inf)

        cdm = ContextualMatrixProfile(
            GeneralStaticManager(series_ranges, query_ranges))
        self.mock_initialise(cdm)

        for column in [2, 3, 4, 5, 10, 11, 12]:
            cdm.process_column(column,
                               np.atleast_2d(self.dist_matrix[:, column]))
            part_dist_matrix[:, column] = self.dist_matrix[:, column]

        correct, correct_qi, correct_si = self.bruteforce_cdm(
            part_dist_matrix, query_ranges, series_ranges)

        npt.assert_allclose(cdm.distance_matrix, correct)
        npt.assert_equal(cdm.match_index_query, correct_qi)
        npt.assert_equal(cdm.match_index_series, correct_si)
    def test_streaming_process_diagonal(self):
        cdm = ContextualMatrixProfile(MockContextManager())
        cdm.initialise(1, 4, 6)

        v = self.dist_matrix[0:4, 0:6]
        cdm.process_diagonal(0, np.atleast_2d(v[diag_indices_of(v, 0)]))
        cdm.process_diagonal(2, np.atleast_2d(v[diag_indices_of(v, 2)]))
        cdm.process_diagonal(-1, np.atleast_2d(v[diag_indices_of(v, -1)]))

        npt.assert_equal(cdm.distance_matrix,
                         [[3.1, 0.5, 8., np.Inf], [np.Inf, 5.8, 1., np.Inf]])
        npt.assert_equal(cdm.match_index_query,
                         [[2, 0, 2, -1], [-1, 3, 3, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[1, 2, 4, -1], [-1, 2, 5, -1]])

        cdm.shift_series(1)
        cdm.shift_query(2)
        v = self.dist_matrix[2:6, 1:7]

        npt.assert_equal(cdm.distance_matrix,
                         [[3.1, 0.5, 8., np.Inf], [np.Inf, 5.8, 1., np.Inf]])
        npt.assert_equal(cdm.match_index_query,
                         [[2, 0, 2, -1], [-1, 3, 3, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[1, 2, 4, -1], [-1, 2, 5, -1]])

        cdm.process_diagonal(5, np.atleast_2d(v[diag_indices_of(v, 5)]))
        cdm.process_diagonal(1, np.atleast_2d(v[diag_indices_of(v, 1)]))
        cdm.process_diagonal(0, np.atleast_2d(v[diag_indices_of(v, 0)]))
        cdm.process_diagonal(-3, np.atleast_2d(v[diag_indices_of(v, -3)]))

        npt.assert_equal(cdm.distance_matrix,
                         [[3.1, 0.5, 8., 2.7], [8.1, 5.8, 1., np.Inf]])
        npt.assert_equal(cdm.match_index_query, [[2, 0, 2, 2], [5, 3, 3, -1]])
        npt.assert_equal(cdm.match_index_series, [[1, 2, 4, 6], [1, 2, 5, -1]])

        cdm.shift_series(1)
        cdm.shift_query(2)
        v = self.dist_matrix[4:8, 2:8]

        npt.assert_equal(
            cdm.distance_matrix,
            [[5.8, 1., np.Inf, np.Inf], [np.Inf, np.Inf, np.Inf, np.Inf]])
        npt.assert_equal(cdm.match_index_query,
                         [[3, 3, -1, -1], [-1, -1, -1, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[2, 5, -1, -1], [-1, -1, -1, -1]])

        cdm.process_diagonal(-2, np.atleast_2d(v[diag_indices_of(v, -2)]))
        cdm.process_diagonal(0, np.atleast_2d(v[diag_indices_of(v, 0)]))
        cdm.process_diagonal(1, np.atleast_2d(v[diag_indices_of(v, 1)]))
        cdm.process_diagonal(2, np.atleast_2d(v[diag_indices_of(v, 2)]))
        cdm.process_diagonal(3, np.atleast_2d(v[diag_indices_of(v, 3)]))
        cdm.process_diagonal(4, np.atleast_2d(v[diag_indices_of(v, 4)]))

        npt.assert_equal(cdm.distance_matrix,
                         [[1.2, 1., 1.2, np.Inf], [1.2, 3.7, 2.5, np.Inf]])
        npt.assert_equal(cdm.match_index_query, [[6, 3, 4, -1], [6, 6, 7, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[2, 5, 6, -1], [2, 4, 6, -1]])
    def test_streaming_process_column(self):
        cdm = ContextualMatrixProfile(MockContextManager())
        cdm.initialise(1, 4, 6)

        cdm.process_column(0, np.atleast_2d(self.dist_matrix[0:4, 0]))
        cdm.process_column(2, np.atleast_2d(self.dist_matrix[0:4, 2]))

        npt.assert_equal(
            cdm.distance_matrix,
            [[7.2, 0.5, np.Inf, np.Inf], [5.3, 5.8, np.Inf, np.Inf]])
        npt.assert_equal(cdm.match_index_query,
                         [[2, 0, -1, -1], [3, 3, -1, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[0, 2, -1, -1], [0, 2, -1, -1]])

        cdm.shift_series(1)
        cdm.shift_query(2)

        npt.assert_equal(
            cdm.distance_matrix,
            [[7.2, 0.5, np.Inf, np.Inf], [5.3, 5.8, np.Inf, np.Inf]])
        npt.assert_equal(cdm.match_index_query,
                         [[2, 0, -1, -1], [3, 3, -1, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[0, 2, -1, -1], [0, 2, -1, -1]])

        cdm.process_column(0, np.atleast_2d(self.dist_matrix[2:6, 1]))
        cdm.process_column(1, np.atleast_2d(self.dist_matrix[2:6, 2]))
        cdm.process_column(3, np.atleast_2d(self.dist_matrix[2:6, 4]))

        npt.assert_equal(cdm.distance_matrix,
                         [[3.1, 0.5, 8.0, np.Inf], [4.8, 2.3, 2.1, np.Inf]])
        npt.assert_equal(cdm.match_index_query, [[2, 0, 2, -1], [4, 5, 4, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[1, 2, 4, -1], [1, 2, 4, -1]])

        cdm.shift_series(1)
        cdm.shift_query(2)

        npt.assert_equal(
            cdm.distance_matrix,
            [[2.3, 2.1, np.Inf, np.Inf], [np.Inf, np.Inf, np.Inf, np.Inf]])
        npt.assert_equal(cdm.match_index_query,
                         [[5, 4, -1, -1], [-1, -1, -1, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[2, 4, -1, -1], [-1, -1, -1, -1]])

        cdm.process_column(0, np.atleast_2d(self.dist_matrix[4:8, 2]))
        cdm.process_column(3, np.atleast_2d(self.dist_matrix[4:8, 5]))
        cdm.process_column(4, np.atleast_2d(self.dist_matrix[4:8, 6]))
        cdm.process_column(5, np.atleast_2d(self.dist_matrix[4:8, 7]))

        npt.assert_equal(cdm.distance_matrix,
                         [[1.2, 2.1, 1.2, np.Inf], [1.2, 7.5, 2.5, np.Inf]])
        npt.assert_equal(cdm.match_index_query, [[6, 4, 4, -1], [6, 7, 7, -1]])
        npt.assert_equal(cdm.match_index_series,
                         [[2, 4, 6, -1], [2, 5, 6, -1]])