forked from uncertainty-toolbox/uncertainty-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_viz.py
109 lines (85 loc) · 3.4 KB
/
test_viz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Tests for visualizations.
"""
import pytest
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from uncertainty_toolbox.viz import (
plot_xy,
plot_intervals,
plot_intervals_ordered,
plot_calibration,
plot_adversarial_group_calibration,
plot_sharpness,
plot_residuals_vs_stds,
filter_subset,
)
@pytest.fixture
def get_test_set():
y_pred = np.array([1, 2, 3])
y_std = np.array([0.1, 0.5, 1])
y_true = np.array([1.5, 3, 2])
x = np.array([4, 5, 6.5])
return y_pred, y_std, y_true, x
@pytest.fixture
def get_fig_ax():
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
return fig, ax
def test_plot_xy_returns(get_test_set, get_fig_ax):
"""Test if plot_xy returns correct type."""
y_pred, y_std, y_true, x = get_test_set
fig, ax = get_fig_ax
ax = plot_xy(y_pred, y_std, y_true, x, ax=ax)
assert isinstance(ax, matplotlib.axes.Axes)
def test_plot_intervals_returns(get_test_set, get_fig_ax):
"""Test if plot_intervals returns correct type."""
y_pred, y_std, y_true, _ = get_test_set
fig, ax = get_fig_ax
ax = plot_intervals(y_pred, y_std, y_true, ax=ax)
assert isinstance(ax, matplotlib.axes.Axes)
def test_plot_intervals_ordered_returns(get_test_set, get_fig_ax):
"""Test if plot_intervals_ordered returns correct type."""
y_pred, y_std, y_true, _ = get_test_set
fig, ax = get_fig_ax
ax = plot_intervals_ordered(y_pred, y_std, y_true, ax=ax)
assert isinstance(ax, matplotlib.axes.Axes)
def test_plot_calibration_returns(get_test_set, get_fig_ax):
"""Test if plot_calibration returns correct type."""
y_pred, y_std, y_true, _ = get_test_set
fig, ax = get_fig_ax
ax = plot_calibration(y_pred, y_std, y_true, ax=ax)
assert isinstance(ax, matplotlib.axes.Axes)
def test_plot_adversarial_group_calibration_returns(get_test_set, get_fig_ax):
"""Test if plot_adversarial_group_calibration returns correct type."""
y_pred, y_std, y_true, _ = get_test_set
fig, ax = get_fig_ax
ax = plot_adversarial_group_calibration(y_pred, y_std, y_true, ax=ax)
assert isinstance(ax, matplotlib.axes.Axes)
def test_plot_sharpness_returns(get_test_set, get_fig_ax):
"""Test if plot_adversarial_group_calibration returns correct type."""
_, y_std, _, _ = get_test_set
fig, ax = get_fig_ax
ax = plot_sharpness(y_std, ax=ax)
assert isinstance(ax, matplotlib.axes.Axes)
def test_plot_sharpness_nsubset_returns(get_test_set, get_fig_ax):
"""Test if plot_adversarial_group_calibration returns correct type."""
_, y_std, _, _ = get_test_set
fig, ax = get_fig_ax
_test_n_subset = 2
ax = plot_sharpness(y_std, n_subset=_test_n_subset, ax=ax)
assert isinstance(ax, matplotlib.axes.Axes)
def test_plot_residuals_vs_stds_returns(get_test_set, get_fig_ax):
"""Test if plot_residuals_vs_stds returns correct type."""
y_pred, y_std, y_true, _ = get_test_set
fig, ax = get_fig_ax
ax = plot_residuals_vs_stds(y_pred, y_std, y_true, ax=ax)
assert isinstance(ax, matplotlib.axes.Axes)
def test_filter_subset(get_test_set):
"""Test if filter_subset returns correct number of subset elements."""
y_pred, y_std, y_true, _ = get_test_set
_test_n_subset = 2
[y_pred, y_std, y_true] = filter_subset([y_pred, y_std, y_true], _test_n_subset)
assert len(y_pred) == _test_n_subset
assert len(y_std) == _test_n_subset
assert len(y_true) == _test_n_subset