CSIT-1110: Integrate anomaly detection into PAL
[csit.git] / resources / tools / presentation / new / jumpavg / BitCountingGroupList.py
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 from BitCountingGroup import BitCountingGroup
15 from BitCountingMetadataFactory import BitCountingMetadataFactory
16
17
18 class BitCountingGroupList(object):
19
20     def __init__(self, group_list=[], bits=None):
21         """Create a group list from given list of groups.
22
23         :param group_list: List of groups to compose this group.
24         :param bits: Bit count if known, else None.
25         :type group_list: list of BitCountingGroup
26         :type bits: float or None
27         """
28         self.group_list = group_list
29         if bits is not None:
30             self.bits = bits
31             return
32         bits = 0.0
33         for group in group_list:
34             bits += group.metadata.bits
35         self.bits = bits
36
37     def __getitem__(self, index):
38         """Return group at the index. This makes self iterable.
39
40         :param index: The position in the array of groups.
41         :type index: int
42         :returns: Group at the position.
43         :rtype: BitCountingGroup
44         """
45         return self.group_list[index]
46
47     def with_group_appended(self, group):
48         """Create and return new group list with given group more than self.
49
50         The group argument object is updated with derivative metadata.
51
52         :param group: Next group to be appended to the group list.
53         :type group: BitCountingGroup
54         :returns: New group list with added group.
55         :rtype: BitCountingGroupList
56         """
57         group_list = list(self.group_list)
58         if group_list:
59             last_group = group_list[-1]
60             factory = BitCountingMetadataFactory(
61                 last_group.metadata_factory.max_value, last_group.metadata.avg)
62             group.metadata_factory = factory
63             group.metadata = factory.from_data(group.values)
64         group_list.append(group)
65         bits = self.bits + group.metadata.bits
66         return BitCountingGroupList(group_list, bits)
67
68     def with_value_added_to_last_group(self, value):
69         """Create and return new group list with value added to last group.
70
71         :param value: The run value to add to the last group.
72         :type value: float or od AvgStdevMetadata
73         :returns: New group list with the last group updated.
74         :rtype: BitCountingGroupList
75         """
76         last_group = self.group_list[-1]
77         bits_before = last_group.metadata.bits
78         last_group = last_group.with_run_added(value)
79         group_list = list(self.group_list)
80         group_list[-1] = last_group
81         bits = self.bits - bits_before + last_group.metadata.bits
82         return BitCountingGroupList(group_list, bits)