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 16 """Allow the user to assert various conditions 17 based on the grammar defined in trappy.stats.grammar. The class is 18 also intended to have aggregator based functionality. This is not 19 implemented yet. 20 """ 21 22 from trappy.stats.grammar import Parser 23 import warnings 24 import numpy as np 25 import pandas as pd 26 27 # pylint: disable=invalid-name 28 29 30 class Analyzer(object): 31 32 """ 33 :param data: TRAPpy FTrace Object 34 :type data: :mod:`trappy.ftrace.FTrace` 35 36 :param config: A dictionary of variables, classes 37 and functions that can be used in the statements 38 :type config: dict 39 """ 40 41 def __init__(self, data, config, **kwargs): 42 self._parser = Parser(data, config, **kwargs) 43 44 def assertStatement(self, statement, select=None): 45 """Solve the statement for a boolean result 46 47 :param statement: A string representing a valid 48 :mod:`trappy.stats.grammar` statement 49 :type statement: str 50 51 :param select: If the result represents a boolean 52 mask and the data was derived from a TRAPpy event 53 with a pivot value. The :code:`select` can be 54 used to select a particular pivot value 55 :type select: :mod:`pandas.DataFrame` column 56 """ 57 58 result = self.getStatement(statement, select=select) 59 60 if isinstance(result, pd.DataFrame): 61 result = result.all().all() 62 elif not(isinstance(result, bool) or isinstance(result, np.bool_)): # pylint: disable=no-member 63 warnings.warn("solution of {} is not boolean".format(statement)) 64 65 return result 66 67 def getStatement(self, statement, reference=False, select=None): 68 """Evaluate the statement""" 69 70 result = self._parser.solve(statement) 71 72 # pylint: disable=no-member 73 if np.isscalar(result): 74 return result 75 # pylint: enable=no-member 76 77 if select is not None and len(result): 78 result = result[select] 79 if reference: 80 result = self._parser.ref(result) 81 82 return result 83