1#    Copyright 2015-2016 ARM Limited
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16import pandas as pd
17import trappy
18from utils_tests import TestBART
19from bart.common.signal import SignalCompare
20import numpy as np
21
22
23class TestSignalCompare(TestBART):
24
25    def __init__(self, *args, **kwargs):
26        super(TestSignalCompare, self).__init__(*args, **kwargs)
27
28    def test_conditional_compare(self):
29        """Test conditional_compare"""
30
31        # Refer to the example in
32        # bart.common.signal.SignalCompare.conditional_compare
33        # doc-strings which explains the calculation for the
34        # data set below
35        A = [0, 0, 0, 3, 3, 0, 0, 0]
36        B = [0, 0, 2, 2, 2, 2, 1, 1]
37
38        trace = trappy.BareTrace()
39        df = pd.DataFrame({"A": A, "B": B})
40        trace.add_parsed_event("event", df)
41
42        s = SignalCompare(trace, "event:A", "event:B")
43        expected = (1.5, 2.0 / 7)
44        self.assertEqual(
45            s.conditional_compare(
46                "event:A > event:B",
47                method="rect"),
48            expected)
49
50    def test_get_overshoot(self):
51        """Test get_overshoot"""
52
53        A = [0, 0, 0, 3, 3, 0, 0, 0]
54        B = [0, 0, 2, 2, 2, 2, 1, 1]
55
56        trace = trappy.BareTrace()
57        df = pd.DataFrame({"A": A, "B": B})
58        trace.add_parsed_event("event", df)
59
60        s = SignalCompare(trace, "event:A", "event:B")
61        expected = (1.5, 2.0 / 7)
62        self.assertEqual(
63            s.get_overshoot(method="rect"),
64            expected)
65
66        A = [0, 0, 0, 1, 1, 0, 0, 0]
67        B = [0, 0, 2, 2, 2, 2, 1, 1]
68
69        df = pd.DataFrame({"A": A, "B": B})
70        trace.event.data_frame = df
71        s = SignalCompare(trace, "event:A", "event:B")
72
73        expected = (float("nan"), 0.0)
74        result = s.get_overshoot(method="rect")
75        self.assertTrue(np.isnan(result[0]))
76        self.assertEqual(result[1], expected[1])
77
78    def test_get_undershoot(self):
79        """Test get_undershoot"""
80
81        A = [0, 0, 0, 1, 1, 1, 1, 1]
82        B = [2, 2, 2, 2, 2, 2, 2, 2]
83
84        trace = trappy.BareTrace()
85        df = pd.DataFrame({"A": A, "B": B})
86        trace.add_parsed_event("event", df)
87
88        s = SignalCompare(trace, "event:A", "event:B")
89        expected = (4.0 / 14.0, 1.0)
90        self.assertEqual(
91            s.get_undershoot(method="rect"),
92            expected)
93
94        A = [3, 3, 3, 3, 3, 3, 3, 3]
95        B = [2, 2, 2, 2, 2, 2, 1, 1]
96
97        df = pd.DataFrame({"A": A, "B": B})
98        trace.event.data_frame = df
99        s = SignalCompare(trace, "event:A", "event:B")
100
101        expected = (float("nan"), 0.0)
102        result = s.get_undershoot(method="rect")
103        self.assertTrue(np.isnan(result[0]))
104        self.assertEqual(result[1], expected[1])
105