7037404c270e1871eca638709cb83355e26568fc
[csit.git] / resources / tools / presentation / utils.py
1 # Copyright (c) 2017 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 """General purpose utilities.
15 """
16
17 import numpy as np
18
19 from os import walk
20 from os.path import join
21 from math import sqrt
22
23
24 def mean(items):
25     """Calculate mean value from the items.
26
27     :param items: Mean value is calculated from these items.
28     :type items: list
29     :returns: MEan value.
30     :rtype: float
31     """
32
33     return float(sum(items)) / len(items)
34
35
36 def stdev(items):
37     """Calculate stdev from the items.
38
39     :param items: Stdev is calculated from these items.
40     :type items: list
41     :returns: Stdev.
42     :rtype: float
43     """
44
45     avg = mean(items)
46     variance = [(x - avg) ** 2 for x in items]
47     stddev = sqrt(mean(variance))
48     return stddev
49
50
51 def relative_change(nr1, nr2):
52     """Compute relative change of two values.
53
54     :param nr1: The first number.
55     :param nr2: The second number.
56     :type nr1: float
57     :type nr2: float
58     :returns: Relative change of nr1.
59     :rtype: float
60     """
61
62     return float(((nr2 - nr1) / nr1) * 100)
63
64
65 def remove_outliers(input_data, outlier_const):
66     """
67
68     :param input_data: Data from which the outliers will be removed.
69     :param outlier_const: Outlier constant.
70     :type input_data: list
71     :type outlier_const: float
72     :returns: The input list without outliers.
73     :rtype: list
74     """
75
76     data = np.array(input_data)
77     upper_quartile = np.percentile(data, 75)
78     lower_quartile = np.percentile(data, 25)
79     iqr = (upper_quartile - lower_quartile) * outlier_const
80     quartile_set = (lower_quartile - iqr, upper_quartile + iqr)
81     result_lst = list()
82     for y in data.tolist():
83         if quartile_set[0] <= y <= quartile_set[1]:
84             result_lst.append(y)
85     return result_lst
86
87
88 def get_files(path, extension=None, full_path=True):
89     """Generates the list of files to process.
90
91     :param path: Path to files.
92     :param extension: Extension of files to process. If it is the empty string,
93     all files will be processed.
94     :param full_path: If True, the files with full path are generated.
95     :type path: str
96     :type extension: str
97     :type full_path: bool
98     :returns: List of files to process.
99     :rtype: list
100     """
101
102     file_list = list()
103     for root, _, files in walk(path):
104         for filename in files:
105             if extension:
106                 if filename.endswith(extension):
107                     if full_path:
108                         file_list.append(join(root, filename))
109                     else:
110                         file_list.append(filename)
111             else:
112                 file_list.append(join(root, filename))
113
114     return file_list
115
116
117 def get_rst_title_char(level):
118     """Return character used for the given title level in rst files.
119
120     :param level: Level of the title.
121     :type: int
122     :returns: Character used for the given title level in rst files.
123     :rtype: str
124     """
125     chars = ('=', '-', '`', "'", '.', '~', '*', '+', '^')
126     if level < len(chars):
127         return chars[level]
128     else:
129         return chars[-1]