CSIT-1133: Cosmetic improvements in trending plots
[csit.git] / resources / tools / presentation / new / utils.py
index 83f4f62..a2aa0dc 100644 (file)
@@ -17,8 +17,9 @@
 import multiprocessing
 import subprocess
 import numpy as np
-import pandas as pd
 import logging
+import csv
+import prettytable
 
 from os import walk, makedirs, environ
 from os.path import join, isdir
@@ -211,19 +212,21 @@ def archive_input_data(spec):
 def classify_anomalies(data):
     """Process the data and return anomalies and trending values.
 
-    Gathers data into groups with common trend value.
-    Decorates first value in the group to be an outlier, regression,
-    normal or progression.
+    Gather data into groups with average as trend value.
+    Decorate values within groups to be normal,
+    the first value of changed average as a regression, or a progression.
 
     :param data: Full data set with unavailable samples replaced by nan.
     :type data: pandas.Series
     :returns: Classification and trend values
     :rtype: 2-tuple, list of strings and list of floats
     """
-    bare_data = [sample for _, sample in data.iteritems()
-                 if not np.isnan(sample)]
+    # Nan mean something went wrong.
+    # Use 0.0 to cause that being reported as a severe regression.
+    bare_data = [0.0 if np.isnan(sample) else sample
+                 for _, sample in data.iteritems()]
     # TODO: Put analogous iterator into jumpavg library.
-    groups = BitCountingClassifier.classify(bare_data)
+    groups = BitCountingClassifier().classify(bare_data)
     groups.reverse()  # Just to use .pop() for FIFO.
     classification = []
     avgs = []
@@ -237,7 +240,7 @@ def classify_anomalies(data):
             continue
         if values_left < 1 or active_group is None:
             values_left = 0
-            while values_left < 1:  # To ignore empty groups.
+            while values_left < 1:  # Ignore empty groups (should not happen).
                 active_group = groups.pop()
                 values_left = len(active_group.values)
             avg = active_group.metadata.avg
@@ -251,6 +254,29 @@ def classify_anomalies(data):
     return classification, avgs
 
 
+def convert_csv_to_pretty_txt(csv_file, txt_file):
+    """Convert the given csv table to pretty text table.
+
+    :param csv_file: The path to the input csv file.
+    :param txt_file: The path to the output pretty text file.
+    :type csv_file: str
+    :type txt_file: str
+    """
+
+    txt_table = None
+    with open(csv_file, 'rb') as csv_file:
+        csv_content = csv.reader(csv_file, delimiter=',', quotechar='"')
+        for row in csv_content:
+            if txt_table is None:
+                txt_table = prettytable.PrettyTable(row)
+            else:
+                txt_table.add_row(row)
+        txt_table.align["Test case"] = "l"
+    if txt_table:
+        with open(txt_file, "w") as txt_file:
+            txt_file.write(str(txt_table))
+
+
 class Worker(multiprocessing.Process):
     """Worker class used to process tasks in separate parallel processes.
     """