Trigger.py revision 435457c8af9d69383ba45e0bd7da022d967a8dea
1# Copyright 2015-2015 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 1. Event 19 2. An associated value 20 3. A set of filters 21""" 22 23import types 24from trappy.plotter.Utils import listify 25 26 27class Trigger(object): 28 """The tigger is an event relationship which 29 accepts a run object to "generate" qualified data 30 31 The filter can either have a function 32 33 def function_based_filter(elem): 34 if condition: 35 return True 36 else: 37 return False 38 39 or value 40 41 f = {} 42 f["data_column_a"] = function_based_filter 43 f["data_column_b"] = value 44 """ 45 46 def __init__(self, run, template, filters, value, pivot): 47 """ 48 Args: 49 run (trappy.Run): A trappy Run object 50 template (trappy.Base): A trappy Event to act as a trigger 51 filters (dict): Key value filter pairs 52 value: Value can be a string or a numeric 53 pivot: This is the column around which the data will be 54 pivoted 55 """ 56 57 self.template = template 58 self._filters = filters 59 self.value = value 60 self._pivot = pivot 61 self.run = run 62 63 def generate(self, pivot_val): 64 """Generate the trigger data for a given pivot value 65 and a run index 66 67 Args: 68 pivot_val: The pivot to generate data for 69 """ 70 71 72 trappy_event = getattr(self.run, self.template.name) 73 data_frame = trappy_event.data_frame 74 75 mask = (data_frame[self._pivot] == pivot_val) 76 for key in self._filters: 77 78 operator = self._filters[key] 79 80 if isinstance(operator, types.FunctionType): 81 mask = mask & (data_frame[key].apply(operator)) 82 else: 83 value = operator 84 mask = apply_filter_kv(key, value, data_frame, mask) 85 86 return data_frame[mask] 87 88 89def apply_filter_kv(key, value, data_frame, mask): 90 """Internal function to apply a key value 91 filter to a data_frame and update the initial 92 condition provided in mask. 93 94 Returns: 95 Mask to index the data frame 96 """ 97 98 value = listify(value) 99 if key not in data_frame.columns: 100 return mask 101 else: 102 for val in value: 103 mask = mask & (data_frame[key] == val) 104 return mask 105