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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
from __future__ import annotations
import math
from typing import TYPE_CHECKING
import numpy as np
import pytest
from contourpy import ContourGenerator, FillType, LineType, ZInterp, contour_generator, max_threads
from . import util_test
if TYPE_CHECKING:
from numpy.typing import ArrayLike
from contourpy._contourpy import CoordinateArray
@pytest.fixture
def xyz_3x3_as_lists() -> tuple[list[list[int]], ...]:
x = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
y = [[0, 0, 0], [1, 1, 1], [2, 2, 2]]
z = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
return x, y, z
@pytest.fixture
def xyz_2x3_as_lists() -> tuple[list[list[int]], ...]: # shape (2, 3)
x = [[0, 1, 2], [0, 1, 2]]
y = [[0, 0, 0], [1, 1, 1]]
z = [[0, 1, 2], [3, 4, 5]]
return x, y, z
@pytest.fixture
def xyz_7x5_as_arrays() -> tuple[CoordinateArray, ...]: # shape (7, 5)
x, y = np.meshgrid([0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6])
z = x + y
return x, y, z
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize("z", ([1], [[[1]]]))
def test_ndim_z(
xyz_3x3_as_lists: tuple[list[list[int]], ...],
name: str,
z: ArrayLike,
) -> None:
x, y, _ = xyz_3x3_as_lists
with pytest.raises(TypeError):
contour_generator(x, y, z, name=name)
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize("all_xyz", ([[1]], [[1], [2]], [[1, 2]]))
def test_z_shape_too_small(all_xyz: ArrayLike, name: str) -> None:
with pytest.raises(TypeError):
contour_generator(z=all_xyz, name=name)
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize("wrong_ndim", (None, [1], [[[1]]]))
def test_diff_ndim_xy(
xyz_3x3_as_lists: tuple[list[list[int]], ...],
name: str,
wrong_ndim: ArrayLike,
) -> None:
x, y, z = xyz_3x3_as_lists
with pytest.raises(TypeError):
contour_generator(wrong_ndim, y, z, name=name)
with pytest.raises(TypeError):
contour_generator(x, wrong_ndim, z, name=name)
@pytest.mark.parametrize("name", util_test.all_names())
def test_xy_None(xyz_3x3_as_lists: tuple[list[list[int]], ...], name: str) -> None:
_, _, z = xyz_3x3_as_lists
contour_generator(None, None, z, name=name)
@pytest.mark.parametrize("name", util_test.all_names())
def test_xy_not_specified(xyz_3x3_as_lists: tuple[list[list[int]], ...], name: str) -> None:
_, _, z = xyz_3x3_as_lists
contour_generator(z=z, name=name)
@pytest.mark.parametrize("name", util_test.all_names())
def test_xy_1d(name: str) -> None:
z = [[0, 1, 2], [3, 4, 5]]
contour_generator([0, 1, 2], [0, 1], z, name=name)
with pytest.raises(TypeError):
contour_generator([0, 1], [0, 1], z, name=name)
with pytest.raises(TypeError):
contour_generator([0, 1, 2, 3], [0, 1], z, name=name)
with pytest.raises(TypeError):
contour_generator([0, 1, 2], [0], z, name=name)
with pytest.raises(TypeError):
contour_generator([0, 1, 2], [0, 1, 2], z, name=name)
@pytest.mark.parametrize("name", util_test.all_names())
def test_xy_ndim_more_than_2(name: str) -> None:
z = [[0, 1, 2], [3, 4, 5]]
msg = "Inputs x and y must be None, 1D or 2D, not 3D"
with pytest.raises(TypeError, match=msg):
contour_generator([[[1]]], [[[2]]], z, name=name)
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize("diff_shape", ([[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2], [3, 4], [5, 6]]))
def test_xyz_diff_shapes(
xyz_3x3_as_lists: tuple[list[list[int]], ...],
name: str,
diff_shape: ArrayLike,
) -> None:
x, y, z = xyz_3x3_as_lists
with pytest.raises(TypeError):
contour_generator(diff_shape, y, z, name=name)
with pytest.raises(TypeError):
contour_generator(x, diff_shape, z, name=name)
with pytest.raises(TypeError):
contour_generator(x, y, diff_shape, name=name)
@pytest.mark.parametrize("name", util_test.corner_mask_names())
def test_corner_mask(xyz_3x3_as_lists: tuple[list[list[int]], ...], name: str) -> None:
x, y, z = xyz_3x3_as_lists
for corner_mask in [False, True]:
cont_gen = contour_generator(x, y, z, name=name, corner_mask=corner_mask)
assert cont_gen.corner_mask == corner_mask
def test_corner_mask_not_supported(xyz_3x3_as_lists: tuple[list[list[int]], ...]) -> None:
name = "mpl2005"
x, y, z = xyz_3x3_as_lists
msg = f"{name} contour generator does not support corner_mask=True"
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, corner_mask=True)
@pytest.mark.parametrize("name", util_test.all_names())
def test_chunk_size_negative(xyz_3x3_as_lists: tuple[list[list[int]], ...], name: str) -> None:
x, y, z = xyz_3x3_as_lists
msg = "chunk_size cannot be negative"
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, chunk_size=-1)
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, chunk_size=(-1, 0))
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, chunk_size=(0, -1))
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize("chunk_size", np.arange(9))
def test_chunk_size_1d(
xyz_7x5_as_arrays: tuple[CoordinateArray, ...],
name: str,
chunk_size: int,
) -> None:
x, y, z = xyz_7x5_as_arrays
ny, nx = z.shape
cont_gen = contour_generator(x, y, z, name=name, chunk_size=chunk_size)
ret_y_chunk_size, ret_x_chunk_size = cont_gen.chunk_size
ret_y_chunk_count, ret_x_chunk_count = cont_gen.chunk_count
if chunk_size == 0:
assert ret_x_chunk_size == nx-1
assert ret_y_chunk_size == ny-1
else:
assert ret_x_chunk_size == min(chunk_size, nx-1)
assert ret_y_chunk_size == min(chunk_size, ny-1)
assert ret_y_chunk_count*ret_y_chunk_size >= ny-1
assert (ret_y_chunk_count-1)*ret_y_chunk_size < ny-1
assert ret_x_chunk_count*ret_x_chunk_size >= nx-1
assert (ret_x_chunk_count-1)*ret_x_chunk_size < nx-1
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize("x_chunk_size", np.arange(9))
@pytest.mark.parametrize("y_chunk_size", np.arange(9))
def test_chunk_size_2d(
xyz_7x5_as_arrays: tuple[CoordinateArray, ...],
name: str,
x_chunk_size: int,
y_chunk_size: int,
) -> None:
x, y, z = xyz_7x5_as_arrays
ny, nx = z.shape
cont_gen = contour_generator(x, y, z, name=name, chunk_size=(y_chunk_size, x_chunk_size))
ret_y_chunk_size, ret_x_chunk_size = cont_gen.chunk_size
ret_y_chunk_count, ret_x_chunk_count = cont_gen.chunk_count
if x_chunk_size == 0:
assert ret_x_chunk_size == nx-1
else:
assert ret_x_chunk_size == min(x_chunk_size, nx-1)
if y_chunk_size == 0:
assert ret_y_chunk_size == ny-1
else:
assert ret_y_chunk_size == min(y_chunk_size, ny-1)
assert ret_y_chunk_count*ret_y_chunk_size >= ny-1
assert (ret_y_chunk_count-1)*ret_y_chunk_size < ny-1
assert ret_x_chunk_count*ret_x_chunk_size >= nx-1
assert (ret_x_chunk_count-1)*ret_x_chunk_size < nx-1
def test_chunk_size_and_count(xyz_7x5_as_arrays: tuple[CoordinateArray, ...]) -> None:
x, y, z = xyz_7x5_as_arrays
name = "serial"
msg = "Only one of chunk_size, chunk_count and total_chunk_count should be set"
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, chunk_size=1, chunk_count=1)
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, chunk_size=1, total_chunk_count=1)
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, chunk_count=1, total_chunk_count=1)
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize(
"chunk_count, ret_chunk_count",
[[0, (1, 1)], [1, (1, 1)], [2, (2, 2)], [3, (3, 2)], [4, (3, 4)], [9, (6, 4)]],
)
def test_chunk_count_1d(
xyz_7x5_as_arrays: tuple[CoordinateArray, ...],
name: str,
chunk_count: int,
ret_chunk_count: tuple[int, int],
) -> None:
x, y, z = xyz_7x5_as_arrays
ny, nx = z.shape
cont_gen = contour_generator(x, y, z, name=name, chunk_count=chunk_count)
ret_y_chunk_size, ret_x_chunk_size = cont_gen.chunk_size
assert cont_gen.chunk_count == ret_chunk_count
ret_y_chunk_count, ret_x_chunk_count = ret_chunk_count
assert ret_y_chunk_count*ret_y_chunk_size >= ny-1
assert (ret_y_chunk_count-1)*ret_y_chunk_size < ny-1
assert ret_x_chunk_count*ret_x_chunk_size >= nx-1
assert (ret_x_chunk_count-1)*ret_x_chunk_size < nx-1
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize(
"chunk_count, ret_chunk_count",
[[(0, 1), (1, 1)], [(1, 0), (1, 1)], [(2, 3), (2, 2)], [(3, 2), (3, 2)], [(1, 9), (1, 4)],
[(9, 1), (6, 1)]],
)
def test_chunk_count_2d(
xyz_7x5_as_arrays: tuple[CoordinateArray, ...],
name: str,
chunk_count: tuple[int, int],
ret_chunk_count: tuple[int, int],
) -> None:
x, y, z = xyz_7x5_as_arrays
ny, nx = z.shape
cont_gen = contour_generator(x, y, z, name=name, chunk_count=chunk_count)
ret_y_chunk_size, ret_x_chunk_size = cont_gen.chunk_size
assert cont_gen.chunk_count == ret_chunk_count
ret_y_chunk_count, ret_x_chunk_count = ret_chunk_count
assert ret_y_chunk_count*ret_y_chunk_size >= ny-1
assert (ret_y_chunk_count-1)*ret_y_chunk_size < ny-1
assert ret_x_chunk_count*ret_x_chunk_size >= nx-1
assert (ret_x_chunk_count-1)*ret_x_chunk_size < nx-1
@pytest.mark.parametrize("name", util_test.all_names())
@pytest.mark.parametrize(
"total_chunk_count, ret_chunk_count",
[[0, (1, 1)], [1, (1, 1)], [2, (2, 1)], [3, (3, 1)], [4, (2, 2)], [6, (3, 2)], [9, (3, 2)],
[25, (6, 4)]],
)
def test_total_chunk_count(
xyz_7x5_as_arrays: tuple[CoordinateArray, ...],
name: str,
total_chunk_count: int,
ret_chunk_count: tuple[int, int],
) -> None:
x, y, z = xyz_7x5_as_arrays
ny, nx = z.shape
cont_gen = contour_generator(x, y, z, name=name, total_chunk_count=total_chunk_count)
ret_y_chunk_size, ret_x_chunk_size = cont_gen.chunk_size
assert cont_gen.chunk_count == ret_chunk_count
ret_y_chunk_count, ret_x_chunk_count = ret_chunk_count
assert ret_y_chunk_count*ret_y_chunk_size >= ny-1
assert (ret_y_chunk_count-1)*ret_y_chunk_size < ny-1
assert ret_x_chunk_count*ret_x_chunk_size >= nx-1
assert (ret_x_chunk_count-1)*ret_x_chunk_size < nx-1
def test_name_invalid(xyz_3x3_as_lists: tuple[list[list[int]], ...]) -> None:
x, y, z = xyz_3x3_as_lists
name = "some invalid name"
msg = f"Unrecognised contour generator name: {name}"
with pytest.raises(ValueError, match=msg):
_ = contour_generator(x, y, z, name=name)
@pytest.mark.parametrize("name", ["mpl2005", "mpl2014"])
@pytest.mark.parametrize(
"line_type", [LineType.Separate, LineType.ChunkCombinedCode, LineType.ChunkCombinedOffset])
def test_line_type_not_supported(
xyz_3x3_as_lists: tuple[list[list[int]], ...],
name: str,
line_type: LineType,
) -> None:
x, y, z = xyz_3x3_as_lists
msg = f"{name} contour generator does not support line_type {line_type}"
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, line_type=line_type)
@pytest.mark.parametrize("name", ["mpl2005", "mpl2014"])
@pytest.mark.parametrize("fill_type", [
FillType.OuterOffset, FillType.ChunkCombinedCode, FillType.ChunkCombinedOffset,
FillType.ChunkCombinedCodeOffset, FillType.ChunkCombinedOffsetOffset])
def test_fill_type_not_supported(
xyz_3x3_as_lists: tuple[list[list[int]], ...],
name: str,
fill_type: FillType,
) -> None:
x, y, z = xyz_3x3_as_lists
msg = f"{name} contour generator does not support fill_type {fill_type}"
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, fill_type=fill_type)
@pytest.mark.parametrize("name", util_test.all_names())
def test_properties(xyz_3x3_as_lists: tuple[list[list[int]], ...], name: str) -> None:
# Test that each ContourGenerator class implements all the required properties.
x, y, z = xyz_3x3_as_lists
cont_gen = contour_generator(x, y, z, name=name)
_, _ = cont_gen.chunk_count
_, _ = cont_gen.chunk_size
_ = cont_gen.corner_mask
_ = cont_gen.fill_type
_ = cont_gen.line_type
_ = cont_gen.quad_as_tri
_ = cont_gen.thread_count
_ = cont_gen.z_interp
@pytest.mark.parametrize("name", util_test.quad_as_tri_names())
def test_quad_as_tri(xyz_3x3_as_lists: tuple[list[list[int]], ...], name: str) -> None:
x, y, z = xyz_3x3_as_lists
for quad_as_tri in [False, True]:
cont_gen = contour_generator(x, y, z, name=name, quad_as_tri=quad_as_tri)
assert cont_gen.quad_as_tri == quad_as_tri
def test_quad_as_tri_not_supported(xyz_3x3_as_lists: tuple[list[list[int]], ...]) -> None:
x, y, z = xyz_3x3_as_lists
name = "mpl2005"
msg = f"{name} contour generator does not support quad_as_tri=True"
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, quad_as_tri=True)
@pytest.mark.parametrize("chunk_size", [0, 1, 2])
@pytest.mark.parametrize("thread_count", [0, 1, 2])
def test_thread_count(
xyz_7x5_as_arrays: tuple[CoordinateArray, ...],
chunk_size: int,
thread_count: int,
) -> None:
name = "threaded"
x, y, z = xyz_7x5_as_arrays
cont_gen = contour_generator(
x, y, z, name=name, chunk_size=chunk_size, thread_count=thread_count)
ret_thread_count = cont_gen.thread_count
ret_chunk_count = math.prod(cont_gen.chunk_count)
max_thread_count = max_threads()
if chunk_size == 0:
assert ret_chunk_count == 1
assert ret_thread_count == 1
elif thread_count == 0:
assert ret_thread_count == min(max_thread_count, ret_chunk_count)
else:
assert ret_thread_count == min(max_thread_count, ret_chunk_count, ret_thread_count)
@pytest.mark.parametrize("name", util_test.all_names(exclude="threaded"))
def test_thread_count_not_supported(
xyz_3x3_as_lists: tuple[list[list[int]], ...],
name: str,
) -> None:
x, y, z = xyz_3x3_as_lists
thread_count = 2
msg = f"{name} contour generator does not support thread_count {thread_count}"
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, thread_count=thread_count)
def test_enums_as_strings(xyz_3x3_as_lists: tuple[list[list[int]], ...]) -> None:
x, y, z = xyz_3x3_as_lists
cg = contour_generator(
x, y, z, line_type="SeparateCode", fill_type="ChunkCombinedCodeOffset", z_interp="Log")
assert cg.line_type == LineType.SeparateCode
assert cg.fill_type == FillType.ChunkCombinedCodeOffset
assert cg.z_interp == ZInterp.Log
@pytest.mark.parametrize("name", util_test.all_names())
def test_is_contour_generator(name: str, xyz_3x3_as_lists: tuple[list[list[int]], ...]) -> None:
x, y, z = xyz_3x3_as_lists
cg = contour_generator(x, y, z, name=name)
assert isinstance(cg, ContourGenerator)
@pytest.mark.parametrize("name", util_test.all_names())
def test_z_interp_none_to_linear(name: str, xyz_3x3_as_lists: tuple[list[list[int]], ...]) -> None:
x, y, z = xyz_3x3_as_lists
cg = contour_generator(x, y, z, name=name, z_interp=None)
assert cg.z_interp == ZInterp.Linear
@pytest.mark.parametrize("name", ["mpl2005", "mpl2014"])
def test_z_interp_not_supported(name: str, xyz_3x3_as_lists: tuple[list[list[int]], ...]) -> None:
x, y, z = xyz_3x3_as_lists
msg = f"{name} contour generator does not support z_interp ZInterp.Log"
with pytest.raises(ValueError, match=msg):
contour_generator(x, y, z, name=name, z_interp=ZInterp.Log)
|