Python library for calculating contours of 2D quadrilateral grids
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Direct access to specific contour generation algorithms. These classes provide the core contour calculation functionality and are typically created through the contour_generator() factory function, but can also be instantiated directly when specific algorithm control is needed.
Abstract base class that defines the standard interface for all contour generation algorithms.
class ContourGenerator:
"""Base class for all contour generation algorithms."""
def lines(self, level: float) -> LineReturn:
"""Generate contour lines at specified level."""
def filled(self, lower_level: float, upper_level: float) -> FillReturn:
"""Generate filled contours between two levels."""
def multi_lines(self, levels) -> list[LineReturn]:
"""Generate multiple contour lines at different levels."""
def multi_filled(self, levels) -> list[FillReturn]:
"""Generate multiple filled contours at different levels."""
def create_contour(self, level: float) -> LineReturn:
"""Alias for lines() method."""
def create_filled_contour(self, lower_level: float, upper_level: float) -> FillReturn:
"""Alias for filled() method."""
# Static capability checking methods
@staticmethod
def supports_corner_mask() -> bool:
"""Check if algorithm supports corner masking."""
@staticmethod
def supports_fill_type(fill_type: FillType) -> bool:
"""Check if algorithm supports given fill type."""
@staticmethod
def supports_line_type(line_type: LineType) -> bool:
"""Check if algorithm supports given line type."""
@staticmethod
def supports_quad_as_tri() -> bool:
"""Check if algorithm supports quad-as-triangle mode."""
@staticmethod
def supports_threads() -> bool:
"""Check if algorithm supports multithreading."""
@staticmethod
def supports_z_interp() -> bool:
"""Check if algorithm supports Z interpolation."""
# Properties
@property
def chunk_count(self) -> tuple[int, int]:
"""Chunk counts in (y, x) directions."""
@property
def chunk_size(self) -> tuple[int, int]:
"""Chunk sizes in (y, x) directions."""
@property
def corner_mask(self) -> bool:
"""Corner mask setting."""
@property
def fill_type(self) -> FillType:
"""Current fill type setting."""
@property
def line_type(self) -> LineType:
"""Current line type setting."""
@property
def quad_as_tri(self) -> bool:
"""Quad-as-triangle mode setting."""
@property
def thread_count(self) -> int:
"""Number of threads used."""
@property
def z_interp(self) -> ZInterp:
"""Z interpolation method."""
# Class attributes
default_fill_type: FillType
default_line_type: LineTypeModern serial algorithm with full feature support including all line types, fill types, corner masking, quad-as-triangle mode, and Z interpolation.
class SerialContourGenerator(ContourGenerator):
"""Modern serial contour generation algorithm with full feature support."""
def __init__(
self,
x: CoordinateArray,
y: CoordinateArray,
z: CoordinateArray,
mask: MaskArray,
*,
corner_mask: bool,
line_type: LineType,
fill_type: FillType,
quad_as_tri: bool,
z_interp: ZInterp,
x_chunk_size: int = 0,
y_chunk_size: int = 0,
) -> None:
"""
Parameters:
- x: 2D array of x-coordinates
- y: 2D array of y-coordinates
- z: 2D array of z-values to contour
- mask: 2D boolean mask array
- corner_mask: Enable corner masking for masked arrays
- line_type: Output format for contour lines
- fill_type: Output format for filled contours
- quad_as_tri: Treat quads as 4 triangles
- z_interp: Z-value interpolation method
- x_chunk_size: Chunk size in x-direction
- y_chunk_size: Chunk size in y-direction
"""Modern multithreaded algorithm with the same capabilities as SerialContourGenerator plus parallel processing support.
class ThreadedContourGenerator(ContourGenerator):
"""Modern multithreaded contour generation algorithm."""
def __init__(
self,
x: CoordinateArray,
y: CoordinateArray,
z: CoordinateArray,
mask: MaskArray,
*,
corner_mask: bool,
line_type: LineType,
fill_type: FillType,
quad_as_tri: bool,
z_interp: ZInterp,
x_chunk_size: int = 0,
y_chunk_size: int = 0,
thread_count: int = 0,
) -> None:
"""
Parameters:
- x: 2D array of x-coordinates
- y: 2D array of y-coordinates
- z: 2D array of z-values to contour
- mask: 2D boolean mask array
- corner_mask: Enable corner masking for masked arrays
- line_type: Output format for contour lines
- fill_type: Output format for filled contours
- quad_as_tri: Treat quads as 4 triangles
- z_interp: Z-value interpolation method
- x_chunk_size: Chunk size in x-direction
- y_chunk_size: Chunk size in y-direction
- thread_count: Number of threads (0 = auto-detect maximum)
"""Legacy Matplotlib 2014 algorithm with corner masking support but limited to default line/fill types.
class Mpl2014ContourGenerator(ContourGenerator):
"""Legacy Matplotlib 2014 algorithm with corner masking."""
def __init__(
self,
x: CoordinateArray,
y: CoordinateArray,
z: CoordinateArray,
mask: MaskArray,
*,
corner_mask: bool,
x_chunk_size: int = 0,
y_chunk_size: int = 0,
) -> None:
"""
Parameters:
- x: 2D array of x-coordinates
- y: 2D array of y-coordinates
- z: 2D array of z-values to contour
- mask: 2D boolean mask array
- corner_mask: Enable corner masking for masked arrays
- x_chunk_size: Chunk size in x-direction
- y_chunk_size: Chunk size in y-direction
"""Legacy Matplotlib 2005 algorithm with minimal features and limited chunking support.
class Mpl2005ContourGenerator(ContourGenerator):
"""Legacy Matplotlib 2005 algorithm with minimal features."""
def __init__(
self,
x: CoordinateArray,
y: CoordinateArray,
z: CoordinateArray,
mask: MaskArray,
*,
x_chunk_size: int = 0,
y_chunk_size: int = 0,
) -> None:
"""
Parameters:
- x: 2D array of x-coordinates
- y: 2D array of y-coordinates
- z: 2D array of z-values to contour
- mask: 2D boolean mask array
- x_chunk_size: Chunk size in x-direction
- y_chunk_size: Chunk size in y-direction
"""import contourpy
import numpy as np
# Create sample data
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2
mask = None
# Create SerialContourGenerator directly
cont_gen = contourpy.SerialContourGenerator(
X, Y, Z, mask,
corner_mask=False,
line_type=contourpy.LineType.Separate,
fill_type=contourpy.FillType.OuterCode,
quad_as_tri=False,
z_interp=contourpy.ZInterp.Linear,
x_chunk_size=0,
y_chunk_size=0
)# Check what features an algorithm supports
print(f"Supports threading: {contourpy.ThreadedContourGenerator.supports_threads()}")
print(f"Supports corner mask: {contourpy.Mpl2005ContourGenerator.supports_corner_mask()}")
print(f"Supports Z interp: {contourpy.SerialContourGenerator.supports_z_interp()}")# Generate multiple contour levels efficiently
levels = [1.0, 2.0, 3.0, 4.0, 5.0]
multi_lines = cont_gen.multi_lines(levels)
multi_filled = cont_gen.multi_filled(levels)
# Each result is a list with one entry per level
for i, level in enumerate(levels):
lines_at_level = multi_lines[i]
filled_at_level = multi_filled[i]Install with Tessl CLI
npx tessl i tessl/pypi-contourpy