From 2d32265d95b12cbbd41b655cc7b6ec56b21cc711 Mon Sep 17 00:00:00 2001
From: gospodnetic <petra.gospodnetic@gmail.com>
Date: Wed, 27 May 2020 16:52:37 +0200
Subject: [PATCH] Add peak and trough plots

---
 Vis.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 1 deletion(-)

diff --git a/Vis.py b/Vis.py
index df10624..633d42d 100644
--- a/Vis.py
+++ b/Vis.py
@@ -15,8 +15,8 @@ class Vis:
 
             fig = plt.figure()
             ax = fig.add_subplot(111)
+            per_method_coverage = []
             for log in logs:
-                per_method_coverage = []
                 per_method_coverage.append(
                     util.convert_to_percentage(
                         log.get_cumulative_coverage_per_vp()))
@@ -30,6 +30,14 @@ class Vis:
             ax.set_xlabel("Number of viewpoints")
             ax.set_ylabel("Coverage [%]")
             fig.suptitle(approach)
+
+            peaks, troughs = self.__stacked_coverage_find_peaks_and_troughs(per_method_coverage)
+            peaks = self.__filter_falling_peaks(peaks)
+
+            # plt.plot(peaks, c="#009e28", ls="--", lw=2, alpha=0.5)
+            ax.plot(peaks, c="blue", ls="--", lw=2, alpha=0.5)
+            ax.plot(troughs, c="#e36120", ls="--", lw=2, alpha=0.7)
+
             self.figures.append(fig)
 
     # It will show all currently created figures.
@@ -46,3 +54,72 @@ class Vis:
                 filename = output_path + label
             figure.savefig(filename)
 
+    # Stacked coverages are of different lengths, pad shorter ones by
+    # repeating the last value.
+    def __pad_to_matrix(self, stacked_coverage):
+        max_length = 0
+        for row in stacked_coverage:
+            if len(row) > max_length:
+                max_length = len(row)
+
+        matrix_stacked_coverage = []
+        for row in stacked_coverage:
+            if len(row) < max_length:
+                length_difference = max_length - len(row)
+                padding = np.full(length_difference, row[-1])
+                row = np.concatenate([row,padding])
+            matrix_stacked_coverage.append(row)
+
+        return matrix_stacked_coverage
+
+
+    # For each viewpoint ste, find maximal value from 2D data representing
+    # stacked multiple coverage arrays (coverage per VP contribution of multiple methods)
+    def __stacked_coverage_find_peaks_and_troughs(self, stacked_coverage):
+        plot_min_padded_to_max_length = True
+
+        # Find method with the most viewpoints (length of coverage vector = viewpoint count)
+        max_length = 0
+        min_length = len(stacked_coverage[0])
+        for row in stacked_coverage:
+            if len(row) > max_length:
+                max_length = len(row)
+            if len(row) < min_length:
+                min_length = len(row)
+
+        if plot_min_padded_to_max_length:
+            stacked_coverage = self.__pad_to_matrix(stacked_coverage)
+
+        peaks = []
+        troughs = []
+        for i in range(max_length):
+            max_val = 0
+            min_val = 100
+            for row in stacked_coverage:
+                # Check if there are enough elements.
+                if len(row) <= i:
+                    continue
+
+                if row[i] > max_val:
+                    max_val = row[i]
+                if row[i] < min_val:
+                    min_val = row[i]
+
+            peaks.append(max_val)
+            if plot_min_padded_to_max_length:
+                troughs.append(min_val)
+            elif i < min_length:
+                troughs.append(min_val)
+
+            # Plots minimum curve using all the coverages.
+            # else:
+            #     troughs.append(troughs[-1])
+        return peaks,troughs
+
+    # Cumulative values can not fall, and falling peaks will appear if some methods
+    # have more viewpoints than the others
+    def __filter_falling_peaks(self, peaks):
+        for i in range(1,len(peaks)):
+            if peaks[i] < peaks[i-1]:
+                peaks[i] = peaks[i-1]
+        return peaks
-- 
GitLab