Skip to content
Snippets Groups Projects
Commit 6cf3ef4c authored by Jörg Martin's avatar Jörg Martin
Browse files

deviation_scatter added

parent 44d5a0cb
No related branches found
No related tags found
No related merge requests found
...@@ -231,25 +231,25 @@ def compute_coverages(data, eiv, number_of_draws): ...@@ -231,25 +231,25 @@ def compute_coverages(data, eiv, number_of_draws):
q_range=q_range, q_range=q_range,
noisy_y = False) noisy_y = False)
return numerical_coverage, theoretical_coverage return numerical_coverage, theoretical_coverage
if __name__ == '__main__':
# loop through data # loop through data
for data, color in tqdm(zip(datasets, colors)): for data, color in tqdm(zip(datasets, colors)):
# compute coverages # compute coverages
eiv_coverages = compute_coverages(data=data, eiv=True, eiv_coverages = compute_coverages(data=data, eiv=True,
number_of_draws=[100,5]) number_of_draws=[100,5])
noneiv_coverages = compute_coverages(data=data, eiv=False, noneiv_coverages = compute_coverages(data=data, eiv=False,
number_of_draws=100) number_of_draws=100)
# create plots # create plots
coverage_diagonal_plot(eiv_coverages, noneiv_coverages, coverage_diagonal_plot(eiv_coverages, noneiv_coverages,
color=color, against_theoretical=False, label=data) color=color, against_theoretical=False, label=data)
# add diagonal # add diagonal
x_diag = np.linspace(0.0, 1.0) x_diag = np.linspace(0.0, 1.0)
plt.plot(x_diag, x_diag, color='k', linestyle='dotted' ) plt.plot(x_diag, x_diag, color='k', linestyle='dotted' )
# add legend # add legend
plt.legend() plt.legend()
# save and show # save and show
plt.savefig('results/figures/true_coverage_vs_q.pdf') plt.savefig('results/figures/true_coverage_vs_q.pdf')
plt.show() plt.show()
"""
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()
...@@ -242,81 +242,82 @@ zoom_point = 14 ...@@ -242,81 +242,82 @@ zoom_point = 14
x_zoom_radius = 0.4 x_zoom_radius = 0.4
y_zoom_radius = x_zoom_radius y_zoom_radius = x_zoom_radius
fignum = 0 if __name__ == '__main__':
for data, x_range, color, number_of_draws in zip(data_list, fignum = 0
list_x_range, list_color, list_number_of_draws): for data, x_range, color, number_of_draws in zip(data_list,
fignum += 1 list_x_range, list_color, list_number_of_draws):
eiv_plotting_dictionary = compute_predictions_and_uncertainties( fignum += 1
data=data, eiv_plotting_dictionary = compute_predictions_and_uncertainties(
x_range=x_range, data=data,
eiv=True, x_range=x_range,
number_of_draws=number_of_draws[0]) eiv=True,
noneiv_plotting_dictionary = compute_predictions_and_uncertainties( number_of_draws=number_of_draws[0])
data=data, noneiv_plotting_dictionary = compute_predictions_and_uncertainties(
x_range=x_range, data=data,
eiv=False, x_range=x_range,
number_of_draws=number_of_draws[1]) eiv=False,
input_dim = eiv_plotting_dictionary['input_dim'] number_of_draws=number_of_draws[1])
if input_dim == 1: input_dim = eiv_plotting_dictionary['input_dim']
plt.figure(fignum) if input_dim == 1:
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()
plt.figure(fignum) plt.figure(fignum)
plt.clf() 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) 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,'-', plt.plot(x_values, eiv_pred,'-',
color=color[0], linewidth=linewidth) color=color[0], linewidth=linewidth)
plt.fill_between(x_values.flatten(), eiv_pred-k * eiv_unc, plt.fill_between(x_values.flatten(), eiv_pred-k * eiv_unc,
eiv_pred + k * eiv_unc, eiv_pred + k * eiv_unc,
color=color[0], alpha=0.5) 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,'-', plt.plot(x_values.flatten(), noneiv_pred,'-',
color=color[1], linewidth=linewidth) color=color[1], linewidth=linewidth)
plt.fill_between(x_values.flatten(), noneiv_pred-k * noneiv_unc, plt.fill_between(x_values.flatten(), noneiv_pred-k * noneiv_unc,
noneiv_pred + k * noneiv_unc, noneiv_pred + k * noneiv_unc,
color=color[1], alpha=0.5) 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.tight_layout()
plt.savefig(f'results/figures/prediction_{data}_zoom.pdf') plt.savefig(f'results/figures/prediction_{data}.pdf')
else: if data == zoom_example:
# multidimensional handling not included yet fignum += 1
pass 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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment