Python library for calculating contours of 2D quadrilateral grids
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Functions for converting between different contour data formats and processing chunked results. These utilities enable interoperability between different output formats and efficient handling of large datasets processed in chunks.
Convert contour data between different line and fill formats.
def convert_lines(
lines,
line_type_from: LineType | str,
line_type_to: LineType | str
):
"""
Convert contour line data from one format to another.
Parameters:
- lines: Line contour data in source format
- line_type_from: Source LineType format
- line_type_to: Target LineType format
Returns:
Line contour data in target format
"""
def convert_filled(
filled,
fill_type_from: FillType | str,
fill_type_to: FillType | str
):
"""
Convert filled contour data from one format to another.
Parameters:
- filled: Filled contour data in source format
- fill_type_from: Source FillType format
- fill_type_to: Target FillType format
Returns:
Filled contour data in target format
"""
def convert_multi_lines(
multi_lines: list,
line_type_from: LineType | str,
line_type_to: LineType | str
) -> list:
"""
Convert multiple line contour datasets between formats.
Parameters:
- multi_lines: List of line contour datasets
- line_type_from: Source LineType format
- line_type_to: Target LineType format
Returns:
List of converted line contour datasets
"""
def convert_multi_filled(
multi_filled: list,
fill_type_from: FillType | str,
fill_type_to: FillType | str
) -> list:
"""
Convert multiple filled contour datasets between formats.
Parameters:
- multi_filled: List of filled contour datasets
- fill_type_from: Source FillType format
- fill_type_to: Target FillType format
Returns:
List of converted filled contour datasets
"""Move chunked contour data into a single chunk for simplified processing.
def dechunk_lines(
lines,
line_type: LineType | str
):
"""
Move chunked line contour data into the first chunk.
Individual contour lines are unchanged, only the chunk organization
is modified. Non-chunked data and single-chunk data are returned unmodified.
Parameters:
- lines: Chunked line contour data
- line_type: LineType format of the data
Returns:
Line contour data consolidated into single chunk
"""
def dechunk_filled(
filled,
fill_type: FillType | str
):
"""
Move chunked filled contour data into the first chunk.
Individual polygons are unchanged, only the chunk organization
is modified. Non-chunked data and single-chunk data are returned unmodified.
Parameters:
- filled: Chunked filled contour data
- fill_type: FillType format of the data
Returns:
Filled contour data consolidated into single chunk
"""
def dechunk_multi_lines(
multi_lines: list,
line_type: LineType | str
) -> list:
"""
Dechunk multiple line contour datasets.
Parameters:
- multi_lines: List of chunked line contour datasets
- line_type: LineType format of the data
Returns:
List of dechunked line contour datasets
"""
def dechunk_multi_filled(
multi_filled: list,
fill_type: FillType | str
) -> list:
"""
Dechunk multiple filled contour datasets.
Parameters:
- multi_filled: List of chunked filled contour datasets
- fill_type: FillType format of the data
Returns:
List of dechunked filled contour datasets
"""import contourpy
# Create contour generator with one format
cont_gen = contourpy.contour_generator(
X, Y, Z,
line_type="Separate",
fill_type="OuterCode"
)
# Generate contours
lines = cont_gen.lines(1.0)
filled = cont_gen.filled(0.5, 1.5)
# Convert to different formats
lines_with_codes = contourpy.convert_lines(
lines, "Separate", "SeparateCode"
)
filled_with_offsets = contourpy.convert_filled(
filled, "OuterCode", "OuterOffset"
)# Generate multiple contour levels
levels = [1.0, 2.0, 3.0, 4.0]
multi_lines = cont_gen.multi_lines(levels)
multi_filled = cont_gen.multi_filled(levels)
# Convert all levels to different format
multi_lines_coded = contourpy.convert_multi_lines(
multi_lines, "Separate", "SeparateCode"
)
multi_filled_offset = contourpy.convert_multi_filled(
multi_filled, "OuterCode", "OuterOffset"
)# Create chunked contour generator
chunked_gen = contourpy.contour_generator(
X, Y, Z,
chunk_size=(25, 25),
line_type="ChunkCombinedCode",
fill_type="ChunkCombinedCodeOffset"
)
# Generate chunked contours
chunked_lines = chunked_gen.lines(2.0)
chunked_filled = chunked_gen.filled(1.0, 3.0)
# Consolidate chunks into single chunk
single_chunk_lines = contourpy.dechunk_lines(
chunked_lines, "ChunkCombinedCode"
)
single_chunk_filled = contourpy.dechunk_filled(
chunked_filled, "ChunkCombinedCodeOffset"
)# Complete processing pipeline: chunked -> dechunked -> converted
chunked_gen = contourpy.contour_generator(
X, Y, Z,
chunk_count=(4, 4),
line_type="ChunkCombinedOffset"
)
# Generate multiple levels with chunking
levels = [0.5, 1.0, 1.5, 2.0]
chunked_multi = chunked_gen.multi_lines(levels)
# Dechunk the data
dechunked_multi = contourpy.dechunk_multi_lines(
chunked_multi, "ChunkCombinedOffset"
)
# Convert to simple separate format
final_lines = contourpy.convert_multi_lines(
dechunked_multi, "ChunkCombinedOffset", "Separate"
)Install with Tessl CLI
npx tessl i tessl/pypi-contourpy