CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-contourpy

Python library for calculating contours of 2D quadrilateral grids

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

contour-generation.mddocs/

Contour Generation

Core functionality for creating contour generators and computing contours. The contour_generator() function is the main factory function that creates appropriate algorithm instances based on the specified parameters.

Capabilities

Main Factory Function

Creates and returns a ContourGenerator object with the class and properties determined by the function arguments.

def contour_generator(
    x: ArrayLike | None = None,
    y: ArrayLike | None = None,
    z: ArrayLike | np.ma.MaskedArray | None = None,
    *,
    name: str = "serial",
    corner_mask: bool | None = None,
    line_type: LineType | str | None = None,
    fill_type: FillType | str | None = None,
    chunk_size: int | tuple[int, int] | None = None,
    chunk_count: int | tuple[int, int] | None = None,
    total_chunk_count: int | None = None,
    quad_as_tri: bool = False,
    z_interp: ZInterp | str | None = ZInterp.Linear,
    thread_count: int = 0,
) -> ContourGenerator:
    """
    Create and return a ContourGenerator object.

    Parameters:
    - x: array-like of shape (ny, nx) or (nx,), optional - X-coordinates of z values
    - y: array-like of shape (ny, nx) or (ny,), optional - Y-coordinates of z values  
    - z: array-like of shape (ny, nx) - 2D gridded values to calculate contours of
    - name: str - Algorithm name ("serial", "threaded", "mpl2005", "mpl2014")
    - corner_mask: bool, optional - Enable/disable corner masking for masked arrays
    - line_type: LineType or str, optional - Format of contour line data
    - fill_type: FillType or str, optional - Format of filled contour data
    - chunk_size: int or tuple(int, int), optional - Chunk size in (y, x) directions
    - chunk_count: int or tuple(int, int), optional - Chunk count in (y, x) directions
    - total_chunk_count: int, optional - Total number of chunks
    - quad_as_tri: bool - Treat quads as 4 triangles (default False)
    - z_interp: ZInterp or str, optional - Z value interpolation method
    - thread_count: int - Number of threads for threaded algorithm (default 0)

    Returns:
    ContourGenerator instance configured according to parameters
    """

Basic Usage Examples

Simple contour generation:

import contourpy
import numpy as np

# Create sample data
x = np.linspace(0, 10, 50)
y = np.linspace(0, 10, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)

# Create contour generator with default settings
cont_gen = contourpy.contour_generator(X, Y, Z)

# Generate contour lines
lines = cont_gen.lines(0.5)

Algorithm selection:

# Use threaded algorithm for better performance
cont_gen = contourpy.contour_generator(X, Y, Z, name="threaded", thread_count=4)

# Use legacy Matplotlib 2014 algorithm
cont_gen = contourpy.contour_generator(X, Y, Z, name="mpl2014")

Custom output formats:

# Configure output formats
cont_gen = contourpy.contour_generator(
    X, Y, Z,
    line_type="SeparateCode",  # Include path codes with lines
    fill_type="OuterOffset"    # Use offset format for filled contours
)

Working with masked data:

# Create masked array
Z_masked = np.ma.masked_where(Z < 0.2, Z)

# Enable corner masking for better masked contours
cont_gen = contourpy.contour_generator(X, Y, Z_masked, corner_mask=True)

Chunking for large datasets:

# Use chunking for memory efficiency
cont_gen = contourpy.contour_generator(
    X, Y, Z,
    chunk_size=(20, 20),  # Process in 20x20 chunks
    name="threaded",      # Use threaded algorithm
    thread_count=2        # Use 2 threads
)

Algorithm Selection

Four algorithms are available:

  • "serial" (default): Modern serial algorithm with full feature support
  • "threaded": Modern multithreaded algorithm for parallel processing
  • "mpl2005": Legacy Matplotlib 2005 algorithm (limited chunking support)
  • "mpl2014": Legacy Matplotlib 2014 algorithm with corner masking support

Chunking Options

Control memory usage and enable parallelization:

  • chunk_size: Specify chunk dimensions directly
  • chunk_count: Specify number of chunks in each direction
  • total_chunk_count: Specify total number of chunks (auto-distributed)

Only one of these options should be specified at a time.

Interpolation Methods

Control how Z values are interpolated:

  • ZInterp.Linear (default): Linear interpolation
  • ZInterp.Log: Logarithmic interpolation

Thread Configuration

For the "threaded" algorithm:

  • thread_count=0: Use maximum available threads (auto-detected)
  • thread_count=N: Use N specific threads
  • Requires sufficient chunks for effective parallelization

Install with Tessl CLI

npx tessl i tessl/pypi-contourpy

docs

algorithm-classes.md

contour-generation.md

data-conversion.md

index.md

types-configuration.md

tile.json