| 1 |
"""Tests for module plot for visualization""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import numpy as np |
| 8 |
import pytest |
| 9 |
|
| 10 |
|
| 11 |
try: |
| 12 |
import matplotlib |
| 13 |
|
| 14 |
matplotlib.use("Agg") |
| 15 |
nogo = False |
| 16 |
except ImportError: |
| 17 |
nogo = True |
| 18 |
|
| 19 |
|
| 20 |
@pytest.mark.skipif(nogo, reason="Matplotlib not installed") |
| 21 |
def test_plot1D_mat(): |
| 22 |
import ot |
| 23 |
import ot.plot |
| 24 |
|
| 25 |
n_bins = 100 |
| 26 |
|
| 27 |
|
| 28 |
x = np.arange(n_bins, dtype=np.float64) |
| 29 |
|
| 30 |
|
| 31 |
a = ot.datasets.make_1D_gauss(n_bins, m=20, s=5) |
| 32 |
b = ot.datasets.make_1D_gauss(n_bins, m=60, s=10) |
| 33 |
|
| 34 |
|
| 35 |
M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) |
| 36 |
M /= M.max() |
| 37 |
|
| 38 |
ot.plot.plot1D_mat(a, b, M) |
| 39 |
ot.plot.plot1D_mat(a, b, M, plot_style="xy") |
| 40 |
|
| 41 |
with pytest.raises(AssertionError): |
| 42 |
ot.plot.plot1D_mat(a, b, M, plot_style="NotAValidStyle") |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.skipif(nogo, reason="Matplotlib not installed") |
| 46 |
def test_rescale_for_imshow_plot(): |
| 47 |
import ot |
| 48 |
import ot.plot |
| 49 |
|
| 50 |
n = 7 |
| 51 |
a_x, b_x = -1, 3 |
| 52 |
x = np.linspace(a_x, b_x, n) |
| 53 |
a_y, b_y = 2, 6 |
| 54 |
y = np.linspace(a_y, b_y, n) |
| 55 |
|
| 56 |
x_rescaled, y_rescaled = ot.plot.rescale_for_imshow_plot(x, y, n) |
| 57 |
assert x_rescaled.shape == (n,) |
| 58 |
assert y_rescaled.shape == (n,) |
| 59 |
|
| 60 |
x_rescaled, y_rescaled = ot.plot.rescale_for_imshow_plot( |
| 61 |
x, y, n, m=n, a_y=a_y + 1, b_y=b_y - 1 |
| 62 |
) |
| 63 |
assert x_rescaled.shape[0] <= n |
| 64 |
assert y_rescaled.shape[0] <= n |
| 65 |
with pytest.raises(AssertionError): |
| 66 |
ot.plot.rescale_for_imshow_plot(x[3:], y, n) |
| 67 |
|
| 68 |
|
| 69 |
@pytest.mark.skipif(nogo, reason="Matplotlib not installed") |
| 70 |
def test_plot2D_samples_mat(): |
| 71 |
import ot |
| 72 |
import ot.plot |
| 73 |
|
| 74 |
n_bins = 50 |
| 75 |
|
| 76 |
mu_s = np.array([0, 0]) |
| 77 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 78 |
|
| 79 |
mu_t = np.array([4, 4]) |
| 80 |
cov_t = np.array([[1, -0.8], [-0.8, 1]]) |
| 81 |
|
| 82 |
rng = np.random.RandomState(42) |
| 83 |
xs = ot.datasets.make_2D_samples_gauss(n_bins, mu_s, cov_s, random_state=rng) |
| 84 |
xt = ot.datasets.make_2D_samples_gauss(n_bins, mu_t, cov_t, random_state=rng) |
| 85 |
|
| 86 |
G = 1.0 * (rng.rand(n_bins, n_bins) < 0.01) |
| 87 |
|
| 88 |
ot.plot.plot2D_samples_mat(xs, xt, G, thr=1e-5) |
| 89 |
ot.plot.plot2D_samples_mat(xs, xt, G, thr=1e-5, alpha=0.5) |