File: test_internal.py

package info (click to toggle)
contourpy 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,688 kB
  • sloc: python: 7,998; cpp: 6,241; makefile: 13
file content (208 lines) | stat: -rw-r--r-- 6,846 bytes parent folder | download | duplicates (2)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Test internal API defined in wrap.cpp, mostly constructors. These are not normally called from
# client code as contour_generator() defined in Python is preferred.

from __future__ import annotations

from io import StringIO
import sys
from typing import TYPE_CHECKING, TypeAlias

import pytest

from contourpy._contourpy import (
    ContourGenerator,
    FillType,
    LineType,
    Mpl2005ContourGenerator,
    Mpl2014ContourGenerator,
    SerialContourGenerator,
    ThreadedContourGenerator,
    ZInterp,
)

if TYPE_CHECKING:
    KWArgs: TypeAlias = dict[str, int | bool | LineType | FillType | ZInterp]
    XYZMask: TypeAlias = tuple[list[list[int]], list[list[int]], list[list[int]], None]


def all_classes() -> list[type[ContourGenerator]]:
    return [Mpl2005ContourGenerator, Mpl2014ContourGenerator, SerialContourGenerator,
            ThreadedContourGenerator]


def default_kwargs(cls: type[ContourGenerator]) -> KWArgs:
    kwargs: KWArgs = {
        "x_chunk_size": 0,
        "y_chunk_size": 0,
    }

    if cls == Mpl2005ContourGenerator:
        pass
    elif cls == Mpl2014ContourGenerator:
        kwargs["corner_mask"] = True
    else:
        kwargs.update({
            "line_type": LineType.Separate,
            "fill_type": FillType.OuterOffset,
            "corner_mask": True,
            "quad_as_tri": False,
            "z_interp": ZInterp.Linear,
        })
        if cls == ThreadedContourGenerator:
            kwargs["thread_count"] = 1
    return kwargs


@pytest.fixture
def xyz_mask() -> XYZMask:
    x = [[0, 1], [0, 1]]
    y = [[0, 0], [1, 1]]
    z = [[1, 2], [3, 4]]
    return x, y, z, None


@pytest.mark.parametrize("cls", all_classes())
def test_default(cls: type[ContourGenerator], xyz_mask: XYZMask) -> None:
    kwargs = default_kwargs(cls)
    cls(*xyz_mask, **kwargs)


@pytest.mark.parametrize("cls", all_classes())
def test_xyz_ndim(cls: type[ContourGenerator], xyz_mask: XYZMask) -> None:
    x, y, z, mask = xyz_mask
    one_d = [1]
    three_d = [[[1]]]
    kwargs = default_kwargs(cls)
    msg = "x, y and z must all be 2D arrays"

    with pytest.raises(ValueError, match=msg):
        cls(one_d, y, z, mask, **kwargs)
    with pytest.raises(ValueError, match=msg):
        cls(three_d, y, z, mask, **kwargs)

    with pytest.raises(ValueError, match=msg):
        cls(x, one_d, z, mask, **kwargs)
    with pytest.raises(ValueError, match=msg):
        cls(x, three_d, z, mask, **kwargs)

    with pytest.raises(ValueError, match=msg):
        cls(x, y, one_d, mask, **kwargs)
    with pytest.raises(ValueError, match=msg):
        cls(x, y, three_d, mask, **kwargs)


@pytest.mark.parametrize("cls", all_classes())
def test_xyz_shape(cls: type[ContourGenerator], xyz_mask: XYZMask) -> None:
    x, y, z, mask = xyz_mask
    diff_shape = [[0, 1], [2, 3], [4, 5]]
    kwargs = default_kwargs(cls)
    msg = "x, y and z arrays must have the same shape"

    with pytest.raises(ValueError, match=msg):
        cls(diff_shape, y, z, mask, **kwargs)
    with pytest.raises(ValueError, match=msg):
        cls(x, diff_shape, z, mask, **kwargs)
    with pytest.raises(ValueError, match=msg):
        cls(x, y, diff_shape, mask, **kwargs)


@pytest.mark.parametrize("cls", all_classes())
def test_xy_at_least_2x2(cls: type[ContourGenerator]) -> None:
    kwargs = default_kwargs(cls)
    msg = "x, y and z must all be at least 2x2 arrays"

    with pytest.raises(ValueError, match=msg):
        cls([[1, 2]], [[1, 2]], [[1, 2]], None, **kwargs)
    with pytest.raises(ValueError, match=msg):
        cls([[1], [2]], [[1], [2]], [[1], [2]], None, **kwargs)


@pytest.mark.parametrize("cls", all_classes())
def test_mask_2d(cls: type[ContourGenerator], xyz_mask: XYZMask) -> None:
    x, y, z, _ = xyz_mask
    kwargs = default_kwargs(cls)
    msg = "mask array must be a 2D array"

    with pytest.raises(ValueError, match=msg):
        cls(x, y, z, [False, True], **kwargs)
    with pytest.raises(ValueError, match=msg):
        cls(x, y, z, [[[False, True]]], **kwargs)


@pytest.mark.parametrize("cls", all_classes())
def test_mask_shape(cls: type[ContourGenerator], xyz_mask: XYZMask) -> None:
    x, y, z, _ = xyz_mask
    kwargs = default_kwargs(cls)
    msg = "If mask is set it must be a 2D array with the same shape as z"

    with pytest.raises(ValueError, match=msg):
        cls(x, y, z, [[False, True]], **kwargs)
    with pytest.raises(ValueError, match=msg):
        cls(x, y, z, [[False], [True]], **kwargs)


@pytest.mark.parametrize("cls", all_classes())
def test_chunk_size_not_negative(cls: type[ContourGenerator], xyz_mask: XYZMask) -> None:
    x, y, z, mask = xyz_mask
    kwargs = default_kwargs(cls)
    msg = "x_chunk_size and y_chunk_size cannot be negative"

    x_kwargs = kwargs.copy()
    x_kwargs.update({"x_chunk_size": -1})
    with pytest.raises(ValueError, match=msg):
        cls(x, y, z, mask, **x_kwargs)

    y_kwargs = kwargs.copy()
    y_kwargs.update({"y_chunk_size": -1})
    with pytest.raises(ValueError, match=msg):
        cls(x, y, z, mask, **y_kwargs)


@pytest.mark.parametrize("cls", [SerialContourGenerator, ThreadedContourGenerator])
def test_log_z_not_negative(cls: type[ContourGenerator], xyz_mask: XYZMask) -> None:
    x, y, _, mask = xyz_mask
    kwargs = default_kwargs(cls)
    kwargs["z_interp"] = ZInterp.Log
    msg = "z values must be positive if using ZInterp.Log"

    z = [[0, 1],[2, 3]]
    with pytest.raises(ValueError, match=msg):
        cls(x, y, z, mask, **kwargs)

    # Ignore z that are masked out
    mask = [[True, False], [False, False]]
    cls(x, y, z, mask, **kwargs)


@pytest.mark.skipif(sys.platform not in ("darwin", "linux"), reason="sysout redirect not available")
@pytest.mark.parametrize("cls", [SerialContourGenerator, ThreadedContourGenerator])
def test_write_cache(cls: type[ContourGenerator], xyz_mask: XYZMask) -> None:
    from wurlitzer import STDOUT, pipes

    x, y, z, _ = xyz_mask
    mask = [[False, False], [False, True]]
    kwargs = default_kwargs(cls)

    cg = cls(x, y, z, mask, **kwargs)

    cg.filled(2.5, 3.5)
    out = StringIO()
    with pipes(stdout=out, stderr=STDOUT):
        cg._write_cache()  # type: ignore[attr-defined]
    assert out.getvalue() == \
        "---------- Cache ----------\n" \
        "j=1 ...e10....... .SW.20.w..... \n" \
        "j=0 ....00....... ...n00....... \n" \
        "    i=0           i=1           \n" \
        "---------------------------\n"

    cg.lines(2.5)
    out = StringIO()
    with pipes(stdout=out, stderr=STDOUT):
        cg._write_cache()  # type: ignore[attr-defined]
    assert out.getvalue() == \
        "---------- Cache ----------\n" \
        "j=1 ...e10....... .SW.10.w..... \n" \
        "j=0 ....00....... ...n00....... \n" \
        "    i=0           i=1           \n" \
        "---------------------------\n"