def testSearchSimilarPatterns_merge(self):
        template_start_time = 1
        template_duration = 2
        template = np.ones((n_leads, template_duration * sampling_freq))

        template_span = similarity_pb2.TimeSpan()
        template_span.start_time = template_start_time
        template_span.duration = template_duration
        seen_events = [template_span]

        set_slice_value(self.base_data, template, template_start_time,
                        sampling_freq)

        target_1_start_time = 5
        set_slice_value(self.base_data, template, target_1_start_time,
                        sampling_freq)

        target_2_start_time = 8.5
        set_slice_value(self.base_data, template, target_2_start_time,
                        sampling_freq)

        patterns_found = similarity.SearchSimilarPatterns(
            self.base_data,
            template_start_time,
            template_duration,
            seen_events,
            sampling_freq,
            top_n=2,
            merge_close_results=True,
            merge_threshold=2)

        target_2_end_time = target_2_start_time + template_duration
        merged_duration = target_2_end_time - target_1_start_time

        merged_targets_span = similarity_pb2.TimeSpan()
        merged_targets_span.start_time = target_1_start_time
        merged_targets_span.duration = merged_duration
        self.assertTrue(overlaps(merged_targets_span, patterns_found[0]))
    def testSearchSimilarPatterns_ignoreSeen(self):
        template_start_time = 1
        template_duration = 1

        seen_event = similarity_pb2.TimeSpan()
        seen_event.start_time = 10
        seen_event.duration = 2.5

        patterns_found = similarity.SearchSimilarPatterns(self.base_data,
                                                          template_start_time,
                                                          template_duration,
                                                          [seen_event],
                                                          sampling_freq,
                                                          top_n=10)

        for pattern in patterns_found:
            end_time = pattern.start_time + pattern.duration
            message = 'Overlaps with event between %s-%s' % (
                pattern.start_time, end_time)
            self.assertFalse(overlaps(seen_event, pattern), message)