From 6cf3ef4ced94d2490db378e2102734255aed7384 Mon Sep 17 00:00:00 2001
From: Joerg Martin <joerg.martin@ptb.de>
Date: Wed, 9 Feb 2022 16:28:24 +0100
Subject: [PATCH] deviation_scatter added

---
 Experiments/plot_coverage_vs_q.py     |  44 ++++-----
 Experiments/plot_deviation_scatter.py |  80 +++++++++++++++++
 Experiments/plot_prediction.py        | 125 +++++++++++++-------------
 3 files changed, 165 insertions(+), 84 deletions(-)
 create mode 100644 Experiments/plot_deviation_scatter.py

diff --git a/Experiments/plot_coverage_vs_q.py b/Experiments/plot_coverage_vs_q.py
index 5cb7f8f..3ae205a 100644
--- a/Experiments/plot_coverage_vs_q.py
+++ b/Experiments/plot_coverage_vs_q.py
@@ -231,25 +231,25 @@ def compute_coverages(data, eiv, number_of_draws):
         q_range=q_range,
         noisy_y = False)
     return numerical_coverage, theoretical_coverage
-
-# loop through data
-for data, color in tqdm(zip(datasets, colors)):
-    # compute coverages
-    eiv_coverages = compute_coverages(data=data, eiv=True,
-            number_of_draws=[100,5])
-    noneiv_coverages = compute_coverages(data=data, eiv=False,
-            number_of_draws=100)
-    # create plots
-    coverage_diagonal_plot(eiv_coverages, noneiv_coverages, 
-            color=color, against_theoretical=False, label=data)
-
-# add diagonal
-x_diag = np.linspace(0.0, 1.0)
-plt.plot(x_diag, x_diag, color='k', linestyle='dotted' )
-
-# add legend
-plt.legend()
-
-# save and show
-plt.savefig('results/figures/true_coverage_vs_q.pdf')
-plt.show()
+if __name__ == '__main__':
+    # loop through data
+    for data, color in tqdm(zip(datasets, colors)):
+        # compute coverages
+        eiv_coverages = compute_coverages(data=data, eiv=True,
+                number_of_draws=[100,5])
+        noneiv_coverages = compute_coverages(data=data, eiv=False,
+                number_of_draws=100)
+        # create plots
+        coverage_diagonal_plot(eiv_coverages, noneiv_coverages, 
+                color=color, against_theoretical=False, label=data)
+
+    # add diagonal
+    x_diag = np.linspace(0.0, 1.0)
+    plt.plot(x_diag, x_diag, color='k', linestyle='dotted' )
+
+    # add legend
+    plt.legend()
+
+    # save and show
+    plt.savefig('results/figures/true_coverage_vs_q.pdf')
+    plt.show()
diff --git a/Experiments/plot_deviation_scatter.py b/Experiments/plot_deviation_scatter.py
new file mode 100644
index 0000000..be33ea5
--- /dev/null
+++ b/Experiments/plot_deviation_scatter.py
@@ -0,0 +1,80 @@
+"""
+Plot predictions with uncertainties for (simulated) datasets with a ground 
+truth. At the moment it is assumed that the used datasets have an output
+dimension equal to 1. Plots are only produced for datasets with input dimension
+and output dimension 1.
+"""
+import importlib
+import os
+import json
+
+import torch
+
+import numpy as np
+import matplotlib
+import matplotlib.pyplot as plt
+
+from EIVArchitectures import Networks
+from EIVTrainingRoutines import train_and_store
+from plot_prediction import compute_predictions_and_uncertainties, data_list, list_x_range, list_number_of_draws
+from plot_coverage_vs_q import colors
+
+font = {'family' : 'DejaVu Sans',
+        'weight' : 'normal',
+        'size'   : 16*0.80}
+
+matplotlib.rc('font', **font)
+
+
+list_x_range = [torch.linspace(-1.0,1.0, 200),
+                torch.linspace(-1.0,1.0, 200),
+                torch.linspace(-1.0,1.0, 200),
+        torch.linspace(-0.2,0.8, 200)]
+plt.figure(1)
+plt.clf()
+for data, x_range, number_of_draws, color in zip(data_list,
+        list_x_range, list_number_of_draws, colors):
+    eiv_plotting_dictionary = compute_predictions_and_uncertainties(
+            data=data,
+            x_range=x_range,
+            eiv=True,
+            number_of_draws=number_of_draws[0])
+    noneiv_plotting_dictionary = compute_predictions_and_uncertainties(
+            data=data,
+            x_range=x_range,
+            eiv=False,
+            number_of_draws=number_of_draws[1])
+    _, y_values = eiv_plotting_dictionary['range_points']
+    noisy_x_points, _ = eiv_plotting_dictionary['noisy_range_points']
+    func = eiv_plotting_dictionary['func'] 
+    func_noisy_x_points = func(torch.tensor(noisy_x_points)).numpy()
+    y_values, func_noisy_x_points = y_values.flatten(), func_noisy_x_points.flatten()
+    eiv_pred = eiv_plotting_dictionary['prediction'].flatten()
+    noneiv_pred = noneiv_plotting_dictionary['prediction'].flatten()
+    assert eiv_pred.shape == noneiv_pred.shape
+    assert y_values.shape == eiv_pred.shape
+    assert func_noisy_x_points.shape == eiv_pred.shape
+    eiv_true_deviation = np.abs(eiv_pred - y_values).flatten()
+    eiv_noisy_deviation = np.abs(eiv_pred - func_noisy_x_points).flatten()
+    noneiv_true_deviation = np.abs(noneiv_pred - y_values).flatten()
+    noneiv_noisy_deviation = np.abs(noneiv_pred - func_noisy_x_points).flatten()
+    plt.loglog(noneiv_true_deviation, noneiv_noisy_deviation,'x', color=color, markersize=5.0)
+    plt.loglog(eiv_true_deviation, eiv_noisy_deviation,'+', color=color, markersize=5.0)
+
+
+plt.ylim(bottom=1e-4)
+plt.xlim(left=1e-4)
+plt.xlabel(r'$|$'+'prediction'+r'$-g(\zeta)$'+r'$|$') 
+plt.ylabel(r'$|$'+'prediction'+r'$-g(x)$'+r'$|$') 
+
+        
+
+
+# plt.ylim([0,1.0])
+# plt.xlim([0,1.0])
+x_values = np.linspace(0, 1)
+plt.plot(x_values, x_values, 'k--')
+plt.gca().set_aspect('equal', adjustable='box')
+plt.tight_layout()
+plt.savefig(f'results/figures/deviation_scatter.pdf')
+plt.show()
diff --git a/Experiments/plot_prediction.py b/Experiments/plot_prediction.py
index f1f985f..78e2212 100644
--- a/Experiments/plot_prediction.py
+++ b/Experiments/plot_prediction.py
@@ -242,81 +242,82 @@ zoom_point = 14
 x_zoom_radius = 0.4
 y_zoom_radius = x_zoom_radius
 
-fignum = 0
-for data, x_range, color, number_of_draws in zip(data_list,
-        list_x_range, list_color, list_number_of_draws):
-    fignum += 1
-    eiv_plotting_dictionary = compute_predictions_and_uncertainties(
-            data=data,
-            x_range=x_range,
-            eiv=True,
-            number_of_draws=number_of_draws[0])
-    noneiv_plotting_dictionary = compute_predictions_and_uncertainties(
-            data=data,
-            x_range=x_range,
-            eiv=False,
-            number_of_draws=number_of_draws[1])
-    input_dim = eiv_plotting_dictionary['input_dim']
-    if input_dim == 1:
-        plt.figure(fignum)
-        plt.clf()
-        x_values, y_values = eiv_plotting_dictionary['range_points']
-        noisy_x_values, _ = eiv_plotting_dictionary['noisy_range_points']
-        plt.plot(x_values.flatten(), y_values.flatten(),'-', color='k', linewidth=linewidth)
-        eiv_pred = eiv_plotting_dictionary['prediction']
-        eiv_unc = eiv_plotting_dictionary['uncertainty']
-        plt.plot(x_values, eiv_pred,'-',
-                color=color[0], linewidth=linewidth)
-        plt.fill_between(x_values.flatten(), eiv_pred-k * eiv_unc,
-                eiv_pred + k * eiv_unc,
-                color=color[0], alpha=0.5)
-        noneiv_pred = noneiv_plotting_dictionary['prediction']
-        noneiv_unc = noneiv_plotting_dictionary['uncertainty']
-        plt.plot(x_values.flatten(), noneiv_pred,'-',
-                color=color[1], linewidth=linewidth)
-        plt.fill_between(x_values.flatten(), noneiv_pred-k * noneiv_unc,
-                noneiv_pred + k * noneiv_unc,
-                color=color[1], alpha=0.5)
-        plt.tight_layout()
-        plt.savefig(f'results/figures/prediction_{data}.pdf')
-        if data == zoom_example:
-            fignum += 1
-            func = eiv_plotting_dictionary['func'] 
-            x_point = x_values[zoom_point] 
-            y_point = func(torch.tensor(x_point)).numpy()
-            noisy_x_point = noisy_x_values[zoom_point]
-            func_noisy_x_point = func(torch.tensor(noisy_x_point)).numpy()
+if __name__ == '__main__':
+    fignum = 0
+    for data, x_range, color, number_of_draws in zip(data_list,
+            list_x_range, list_color, list_number_of_draws):
+        fignum += 1
+        eiv_plotting_dictionary = compute_predictions_and_uncertainties(
+                data=data,
+                x_range=x_range,
+                eiv=True,
+                number_of_draws=number_of_draws[0])
+        noneiv_plotting_dictionary = compute_predictions_and_uncertainties(
+                data=data,
+                x_range=x_range,
+                eiv=False,
+                number_of_draws=number_of_draws[1])
+        input_dim = eiv_plotting_dictionary['input_dim']
+        if input_dim == 1:
             plt.figure(fignum)
             plt.clf()
+            x_values, y_values = eiv_plotting_dictionary['range_points']
+            noisy_x_values, _ = eiv_plotting_dictionary['noisy_range_points']
             plt.plot(x_values.flatten(), y_values.flatten(),'-', color='k', linewidth=linewidth)
+            eiv_pred = eiv_plotting_dictionary['prediction']
+            eiv_unc = eiv_plotting_dictionary['uncertainty']
             plt.plot(x_values, eiv_pred,'-',
                     color=color[0], linewidth=linewidth)
             plt.fill_between(x_values.flatten(), eiv_pred-k * eiv_unc,
                     eiv_pred + k * eiv_unc,
                     color=color[0], alpha=0.5)
+            noneiv_pred = noneiv_plotting_dictionary['prediction']
+            noneiv_unc = noneiv_plotting_dictionary['uncertainty']
             plt.plot(x_values.flatten(), noneiv_pred,'-',
                     color=color[1], linewidth=linewidth)
             plt.fill_between(x_values.flatten(), noneiv_pred-k * noneiv_unc,
                     noneiv_pred + k * noneiv_unc,
                     color=color[1], alpha=0.5)
-            plt.axvline(x_point, color='black', linestyle='dotted')
-            plt.axhline(y_point, color='black', linestyle='dotted')
-            plt.axvline(noisy_x_point, color='gray', linestyle='dashed')
-            plt.axhline(func_noisy_x_point, color='gray', linestyle='dashed')
-            plt.text(x_point - 0.1 * x_zoom_radius,y_point-0.9 * y_zoom_radius, r'$\zeta$', color='k')
-            plt.text(noisy_x_point - 0.1 * x_zoom_radius,y_point-0.9 * y_zoom_radius, r'$x$', color='gray')
-            plt.text(x_point - 0.92 * x_zoom_radius,y_point-0.13 * y_zoom_radius, r'$g(\zeta)$', color='k')
-            plt.text(x_point - 0.92 * x_zoom_radius,func_noisy_x_point-0.13 * y_zoom_radius, r'$g(x)$', color='gray')
-            plt.gca().set_xlim(left=x_point - x_zoom_radius, 
-                    right=x_point + x_zoom_radius)
-            plt.gca().set_ylim(bottom=y_point - y_zoom_radius,
-                    top=y_point + y_zoom_radius)
-            plt.gca().set_aspect('equal', adjustable='box')
             plt.tight_layout()
-            plt.savefig(f'results/figures/prediction_{data}_zoom.pdf')
-    else:
-        # multidimensional handling not included yet
-        pass
+            plt.savefig(f'results/figures/prediction_{data}.pdf')
+            if data == zoom_example:
+                fignum += 1
+                func = eiv_plotting_dictionary['func'] 
+                x_point = x_values[zoom_point] 
+                y_point = func(torch.tensor(x_point)).numpy()
+                noisy_x_point = noisy_x_values[zoom_point]
+                func_noisy_x_point = func(torch.tensor(noisy_x_point)).numpy()
+                plt.figure(fignum)
+                plt.clf()
+                plt.plot(x_values.flatten(), y_values.flatten(),'-', color='k', linewidth=linewidth)
+                plt.plot(x_values, eiv_pred,'-',
+                        color=color[0], linewidth=linewidth)
+                plt.fill_between(x_values.flatten(), eiv_pred-k * eiv_unc,
+                        eiv_pred + k * eiv_unc,
+                        color=color[0], alpha=0.5)
+                plt.plot(x_values.flatten(), noneiv_pred,'-',
+                        color=color[1], linewidth=linewidth)
+                plt.fill_between(x_values.flatten(), noneiv_pred-k * noneiv_unc,
+                        noneiv_pred + k * noneiv_unc,
+                        color=color[1], alpha=0.5)
+                plt.axvline(x_point, color='black', linestyle='dotted')
+                plt.axhline(y_point, color='black', linestyle='dotted')
+                plt.axvline(noisy_x_point, color='gray', linestyle='dashed')
+                plt.axhline(func_noisy_x_point, color='gray', linestyle='dashed')
+                plt.text(x_point - 0.1 * x_zoom_radius,y_point-0.9 * y_zoom_radius, r'$\zeta$', color='k')
+                plt.text(noisy_x_point - 0.1 * x_zoom_radius,y_point-0.9 * y_zoom_radius, r'$x$', color='gray')
+                plt.text(x_point - 0.92 * x_zoom_radius,y_point-0.13 * y_zoom_radius, r'$g(\zeta)$', color='k')
+                plt.text(x_point - 0.92 * x_zoom_radius,func_noisy_x_point-0.13 * y_zoom_radius, r'$g(x)$', color='gray')
+                plt.gca().set_xlim(left=x_point - x_zoom_radius, 
+                        right=x_point + x_zoom_radius)
+                plt.gca().set_ylim(bottom=y_point - y_zoom_radius,
+                        top=y_point + y_zoom_radius)
+                plt.gca().set_aspect('equal', adjustable='box')
+                plt.tight_layout()
+                plt.savefig(f'results/figures/prediction_{data}_zoom.pdf')
+        else:
+            # multidimensional handling not included yet
+            pass
 
 
-plt.show()
+    plt.show()
-- 
GitLab