~javiljoen/lttb-numpy

lttb-numpy/tests/test_validators.py -rw-r--r-- 3.4 KiB
9463159eJA Viljoen Remove builds.sr.ht script 1 year, 4 months ago
                                                                                
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
import pytest

from lttb.validators import (
    contains_no_nans,
    has_two_columns,
    validate,
    x_is_regular,
    x_is_sorted,
    x_is_strictly_increasing,
)


@pytest.fixture(scope="module")
def valid_data():
    nrows = 10
    ys = np.random.standard_normal(nrows)
    xs = np.linspace(1, nrows, nrows)
    return np.array([xs, ys]).T


def test_has_two_columns_passes_for_2d_array_with_2_columns(valid_data):
    assert has_two_columns(valid_data) is None


def test_has_two_columns_fails_for_1d_array():
    array1d = np.random.standard_normal(10)

    with pytest.raises(ValueError):
        has_two_columns(array1d)


def test_has_two_columns_fails_for_3d_array():
    array3d = np.random.standard_normal((3, 10, 2))

    with pytest.raises(ValueError):
        has_two_columns(array3d)


def test_has_two_columns_fails_for_2d_array_with_1_column():
    matrix1c = np.random.standard_normal((10, 1))

    with pytest.raises(ValueError):
        has_two_columns(matrix1c)


def test_has_two_columns_fails_for_2d_array_with_3_columns():
    matrix3c = np.random.standard_normal((10, 3))

    with pytest.raises(ValueError):
        has_two_columns(matrix3c)


def test_x_is_strictly_increasing_passes_for_valid_data(valid_data):
    assert x_is_strictly_increasing(valid_data) is None


def test_x_is_strictly_increasing_fails_with_repeated_xs():
    data = np.array([[1, 1, 2, 2], np.random.standard_normal(4)]).T

    with pytest.raises(ValueError):
        x_is_strictly_increasing(data)


def test_x_is_sorted_passes_for_valid_data(valid_data):
    assert x_is_sorted(valid_data) is None


def test_x_is_sorted_passes_with_repeated_xs():
    data = np.array([[1, 1, 2, 2], np.random.standard_normal(4)]).T
    assert x_is_sorted(data) is None


def test_x_is_sorted_fails_if_xs_not_sorted():
    data = np.array([[1, 4, 3, 2], np.random.standard_normal(4)]).T

    with pytest.raises(ValueError):
        x_is_sorted(data)


def test_x_is_regular_passes_with_valid_data(valid_data):
    assert x_is_regular(valid_data) is None


def test_x_is_regular_fails_if_x_intervals_are_not_constant():
    data = np.array([[1, 2, 4, 9], np.random.standard_normal(4)]).T

    with pytest.raises(ValueError):
        x_is_regular(data)


def test_contains_no_nans_passes_with_valid_data(valid_data):
    assert contains_no_nans(valid_data) is None


def test_contains_no_nans_fails_if_nan_in_xs():
    data = np.array([[0, 1, 2, np.nan], [0.0, 1.0, 2.0, 3.0]]).T

    with pytest.raises(ValueError):
        contains_no_nans(data)


def test_contains_no_nans_fails_if_nan_in_ys():
    data = np.array([[0, 1, 2, 3], [1.0, np.nan, 2.6, np.nan]]).T

    with pytest.raises(ValueError):
        contains_no_nans(data)


def test_validate_multiple_criteria_passes_for_valid_data(valid_data):
    validate(valid_data, [has_two_columns, x_is_regular])


def test_validate_raises_with_multiple_messages():
    data = np.random.standard_normal((4, 3))  # 3 columns
    data[:, 0] = [1, 4, 2, 9]  # unsorted x values
    data[2, 1] = np.nan  # missing y value
    validators = [has_two_columns, x_is_sorted, x_is_regular, contains_no_nans]

    with pytest.raises(ValueError) as exc:
        validate(data, validators)

    assert exc.match(
        "data does not have 2 columns; "
        "data is not sorted on the first column; "
        "first column is not regularly spaced; "
        "data contains NaN values"
    )