a56ee3e105536cf8a501453a4a7074fca4545e63
[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     print(quartile_set)
86     print(input_data)
87     print(result_lst)
88     print("")
89     return result_lst
90
91
92 def get_files(path, extension=None, full_path=True):
93     """Generates the list of files to process.
94
95     :param path: Path to files.
96     :param extension: Extension of files to process. If it is the empty string,
97     all files will be processed.
98     :param full_path: If True, the files with full path are generated.
99     :type path: str
100     :type extension: str
101     :type full_path: bool
102     :returns: List of files to process.
103     :rtype: list
104     """
105
106     file_list = list()
107     for root, _, files in walk(path):
108         for filename in files:
109             if extension:
110                 if filename.endswith(extension):
111                     if full_path:
112                         file_list.append(join(root, filename))
113                     else:
114                         file_list.append(filename)
115             else:
116                 file_list.append(join(root, filename))
117
118     return file_list
119
120
121 def get_rst_title_char(level):
122     """Return character used for the given title level in rst files.
123
124     :param level: Level of the title.
125     :type: int
126     :returns: Character used for the given title level in rst files.
127     :rtype: str
128     """
129     chars = ('=', '-', '`', "'", '.', '~', '*', '+', '^')
130     if level < len(chars):
131         return chars[level]
132     else:
133         return chars[-1]