• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#    Copyright 2015-2017 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
16"""Trigger is a representation of the following:
17
18    - Event(s) (:mod:`trappy.base.Base`)
19    - An associated value
20        - scalar
21        - vector
22    - A set of filters
23        - value based
24        - function based
25"""
26
27import types
28from trappy.utils import listify
29import pandas as pd
30
31
32class Trigger(object):
33    """Trigger is an event-value relationship which
34    accepts a trace object to "generate" qualified data
35
36    :param trace: A trappy FTrace object
37    :type trace: :mod:`trappy.trace.FTrace`
38
39    :param template: A trappy Event to act as a trigger
40    :type template: trappy.Base
41
42    :param filters: Key value filter pairs
43    :type filters: dict
44
45    The filter can either have a function:
46    ::
47
48        def function_based_filter(elem):
49            if condition:
50                return True
51            else:
52                return False
53
54    or a value/list of values
55    ::
56
57        f = {}
58        f["data_column_a"] = function_based_filter
59        f["data_column_b"] = value
60
61    function_based_filter is anything that behaves like a function,
62    i.e. a callable.
63
64    :param value: Value can be a string or a numeric
65    :type value: str, int, float
66
67    :param pivot: This is the column around which the data will be
68        pivoted
69    :type pivot: str
70    """
71
72    def __init__(self, trace, template, filters, value, pivot):
73
74        self.template = template
75        self._filters = filters
76        self._value = value
77        self._pivot = pivot
78        self.trace = trace
79
80    def generate(self, pivot_val):
81        """Generate the trigger data for a given pivot value
82        and a trace index
83
84        :param pivot_val: The pivot to generate data for
85        :type pivot_val: hashable
86        """
87
88        trappy_event = getattr(self.trace, self.template.name)
89        data_frame = trappy_event.data_frame
90        data_frame = data_frame[data_frame[self._pivot] == pivot_val]
91
92        mask = [True for _ in range(len(data_frame))]
93
94        for key, value in self._filters.iteritems():
95            if hasattr(value, "__call__"):
96                mask = mask & (data_frame[key].apply(value))
97            else:
98                mask = apply_filter_kv(key, value, data_frame, mask)
99
100        data_frame = data_frame[mask]
101
102        if isinstance(self._value, str):
103            return data_frame[value]
104        else:
105            return pd.Series(self._value, index=data_frame.index)
106
107
108def apply_filter_kv(key, value, data_frame, mask):
109    """Internal function to apply a key value
110    filter to a data_frame and update the initial
111    condition provided in mask.
112
113    :param value: The value to checked for
114
115    :param data_frame: The data to be filtered
116    :type data_frame: :mod:`pandas.DataFrame`
117
118    :param mask: Initial Condition Mask
119    :type mask: :mod:`pandas.Series`
120
121    :return: A **mask** to index the data frame
122    """
123
124    value = listify(value)
125    if key not in data_frame.columns:
126        return mask
127    else:
128        for val in value:
129            mask = mask & (data_frame[key] == val)
130        return mask
131