| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
import pytest |
| 5 |
|
| 6 |
import corner |
| 7 |
|
| 8 |
|
| 9 |
def test_invalid_quantiles_1(seed=42): |
| 10 |
np.random.seed(seed) |
| 11 |
with pytest.raises(ValueError): |
| 12 |
corner.quantile(np.random.rand(100), [-0.1, 5]) |
| 13 |
|
| 14 |
|
| 15 |
def test_invalid_quantiles_2(seed=42): |
| 16 |
np.random.seed(seed) |
| 17 |
with pytest.raises(ValueError): |
| 18 |
corner.quantile(np.random.rand(100), 5) |
| 19 |
|
| 20 |
|
| 21 |
def test_invalid_quantiles_3(seed=42): |
| 22 |
np.random.seed(seed) |
| 23 |
with pytest.raises(ValueError): |
| 24 |
corner.quantile(np.random.rand(100), [0.5, 1.0, 8.1]) |
| 25 |
|
| 26 |
|
| 27 |
def test_dimension_mismatch(seed=42): |
| 28 |
np.random.seed(seed) |
| 29 |
with pytest.raises(ValueError): |
| 30 |
corner.quantile( |
| 31 |
np.random.rand(100), [0.1, 0.5], weights=np.random.rand(3) |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
def test_valid_quantile(seed=42): |
| 36 |
np.random.seed(seed) |
| 37 |
x = np.random.rand(25) |
| 38 |
q = np.arange(0.1, 1.0, 0.111234) |
| 39 |
|
| 40 |
a = corner.quantile(x, q) |
| 41 |
b = np.percentile(x, 100 * q) |
| 42 |
assert np.allclose(a, b) |
| 43 |
|
| 44 |
|
| 45 |
def test_weighted_quantile(seed=42): |
| 46 |
np.random.seed(seed) |
| 47 |
x = np.random.rand(25) |
| 48 |
q = np.arange(0.1, 1.0, 0.111234) |
| 49 |
a = corner.quantile(x, q, weights=np.ones_like(x)) |
| 50 |
b = np.percentile(x, 100 * np.array(q)) |
| 51 |
assert np.allclose(a, b) |
| 52 |
|
| 53 |
q = [0.0, 1.0] |
| 54 |
a = corner.quantile(x, q, weights=np.random.rand(len(x))) |
| 55 |
assert np.allclose(a, (np.min(x), np.max(x))) |