Indexer.py revision b95a4c5504771224bfe6a4abec759c00d2612a73
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"""Indexers are responsible for providing indexes for
17   aggregations and provide specific functions like
18   unification and resampling.
19"""
20
21import pandas as pd
22import numpy as np
23from trappy.utils import listify
24from trappy.stats import StatConf
25
26class Indexer(object):
27    """Indexer base class is an encapsulation
28    around the pandas Index object with some
29    special functionality
30
31    :param index: Pandas index object. This can be
32                    non-unoform and non-unique
33    :type index: :mod:`pandas.Index`
34
35    :param runs: trappy Run list/singular object
36    :type runs: :mod:`trappy.run.Run`
37    """
38
39    def __init__(self, index):
40        self.index = index
41
42    def series(self):
43        """Returns an empty series with the initialized index
44        """
45        return pd.Series(np.zeros(len(self.index)), index=self.index)
46
47    def get_uniform(self, delta=StatConf.DELTA_DEFAULT):
48        """
49        :param delta: Difference between two indices. This has a
50            default value specified in StatConf.DELTA_DEFAULT
51        :type delta: float
52
53        :return: A uniformly spaced index.
54        """
55
56        uniform_start = self.index.values[0]
57        uniform_end = self.index.values[-1]
58        new_index = np.arange(uniform_start, uniform_end, delta)
59        return new_index
60
61def get_unified_indexer(indexers):
62    """Unify the List of Indexers
63
64    :param indexers: A list of indexers
65    :type indexers: :mod:`trappy.stats.Indexer.Indexer`
66
67    :return: A :mod:`pandas.Indexer.Indexer`
68        with a unfied index
69    """
70
71    new_index = indexers[0].index
72
73    for idx in indexers[1:]:
74        new_index = new_index.union(idx.index)
75
76    return Indexer(new_index)
77
78class MultiTriggerIndexer(Indexer):
79    """"The index unifies the indices of all trigger
80     events.
81
82    :param triggers: A (list or single) trigger
83    :type triggers: :mod:`trappy.stats.Trigger.Trigger`
84    """
85
86    def __init__(self, triggers):
87
88        self._triggers = listify(triggers)
89        super(MultiTriggerIndexer, self).__init__(self._unify())
90
91    def _unify(self):
92        """Function to unify all the indices of each trigger
93        """
94
95        idx = pd.Index([])
96        for trigger in self._triggers:
97            run = trigger.run
98            trappy_event = getattr(run, trigger.template.name)
99            idx = idx.union(trappy_event.data_frame.index)
100
101
102        return pd.Index(np.unique(idx.values))
103