0
# Algorithm Classes
1
2
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.
3
4
## Capabilities
5
6
### Base ContourGenerator Class
7
8
Abstract base class that defines the standard interface for all contour generation algorithms.
9
10
```python { .api }
11
class ContourGenerator:
12
"""Base class for all contour generation algorithms."""
13
14
def lines(self, level: float) -> LineReturn:
15
"""Generate contour lines at specified level."""
16
17
def filled(self, lower_level: float, upper_level: float) -> FillReturn:
18
"""Generate filled contours between two levels."""
19
20
def multi_lines(self, levels) -> list[LineReturn]:
21
"""Generate multiple contour lines at different levels."""
22
23
def multi_filled(self, levels) -> list[FillReturn]:
24
"""Generate multiple filled contours at different levels."""
25
26
def create_contour(self, level: float) -> LineReturn:
27
"""Alias for lines() method."""
28
29
def create_filled_contour(self, lower_level: float, upper_level: float) -> FillReturn:
30
"""Alias for filled() method."""
31
32
# Static capability checking methods
33
@staticmethod
34
def supports_corner_mask() -> bool:
35
"""Check if algorithm supports corner masking."""
36
37
@staticmethod
38
def supports_fill_type(fill_type: FillType) -> bool:
39
"""Check if algorithm supports given fill type."""
40
41
@staticmethod
42
def supports_line_type(line_type: LineType) -> bool:
43
"""Check if algorithm supports given line type."""
44
45
@staticmethod
46
def supports_quad_as_tri() -> bool:
47
"""Check if algorithm supports quad-as-triangle mode."""
48
49
@staticmethod
50
def supports_threads() -> bool:
51
"""Check if algorithm supports multithreading."""
52
53
@staticmethod
54
def supports_z_interp() -> bool:
55
"""Check if algorithm supports Z interpolation."""
56
57
# Properties
58
@property
59
def chunk_count(self) -> tuple[int, int]:
60
"""Chunk counts in (y, x) directions."""
61
62
@property
63
def chunk_size(self) -> tuple[int, int]:
64
"""Chunk sizes in (y, x) directions."""
65
66
@property
67
def corner_mask(self) -> bool:
68
"""Corner mask setting."""
69
70
@property
71
def fill_type(self) -> FillType:
72
"""Current fill type setting."""
73
74
@property
75
def line_type(self) -> LineType:
76
"""Current line type setting."""
77
78
@property
79
def quad_as_tri(self) -> bool:
80
"""Quad-as-triangle mode setting."""
81
82
@property
83
def thread_count(self) -> int:
84
"""Number of threads used."""
85
86
@property
87
def z_interp(self) -> ZInterp:
88
"""Z interpolation method."""
89
90
# Class attributes
91
default_fill_type: FillType
92
default_line_type: LineType
93
```
94
95
### SerialContourGenerator
96
97
Modern serial algorithm with full feature support including all line types, fill types, corner masking, quad-as-triangle mode, and Z interpolation.
98
99
```python { .api }
100
class SerialContourGenerator(ContourGenerator):
101
"""Modern serial contour generation algorithm with full feature support."""
102
103
def __init__(
104
self,
105
x: CoordinateArray,
106
y: CoordinateArray,
107
z: CoordinateArray,
108
mask: MaskArray,
109
*,
110
corner_mask: bool,
111
line_type: LineType,
112
fill_type: FillType,
113
quad_as_tri: bool,
114
z_interp: ZInterp,
115
x_chunk_size: int = 0,
116
y_chunk_size: int = 0,
117
) -> None:
118
"""
119
Parameters:
120
- x: 2D array of x-coordinates
121
- y: 2D array of y-coordinates
122
- z: 2D array of z-values to contour
123
- mask: 2D boolean mask array
124
- corner_mask: Enable corner masking for masked arrays
125
- line_type: Output format for contour lines
126
- fill_type: Output format for filled contours
127
- quad_as_tri: Treat quads as 4 triangles
128
- z_interp: Z-value interpolation method
129
- x_chunk_size: Chunk size in x-direction
130
- y_chunk_size: Chunk size in y-direction
131
"""
132
```
133
134
### ThreadedContourGenerator
135
136
Modern multithreaded algorithm with the same capabilities as SerialContourGenerator plus parallel processing support.
137
138
```python { .api }
139
class ThreadedContourGenerator(ContourGenerator):
140
"""Modern multithreaded contour generation algorithm."""
141
142
def __init__(
143
self,
144
x: CoordinateArray,
145
y: CoordinateArray,
146
z: CoordinateArray,
147
mask: MaskArray,
148
*,
149
corner_mask: bool,
150
line_type: LineType,
151
fill_type: FillType,
152
quad_as_tri: bool,
153
z_interp: ZInterp,
154
x_chunk_size: int = 0,
155
y_chunk_size: int = 0,
156
thread_count: int = 0,
157
) -> None:
158
"""
159
Parameters:
160
- x: 2D array of x-coordinates
161
- y: 2D array of y-coordinates
162
- z: 2D array of z-values to contour
163
- mask: 2D boolean mask array
164
- corner_mask: Enable corner masking for masked arrays
165
- line_type: Output format for contour lines
166
- fill_type: Output format for filled contours
167
- quad_as_tri: Treat quads as 4 triangles
168
- z_interp: Z-value interpolation method
169
- x_chunk_size: Chunk size in x-direction
170
- y_chunk_size: Chunk size in y-direction
171
- thread_count: Number of threads (0 = auto-detect maximum)
172
"""
173
```
174
175
### Mpl2014ContourGenerator
176
177
Legacy Matplotlib 2014 algorithm with corner masking support but limited to default line/fill types.
178
179
```python { .api }
180
class Mpl2014ContourGenerator(ContourGenerator):
181
"""Legacy Matplotlib 2014 algorithm with corner masking."""
182
183
def __init__(
184
self,
185
x: CoordinateArray,
186
y: CoordinateArray,
187
z: CoordinateArray,
188
mask: MaskArray,
189
*,
190
corner_mask: bool,
191
x_chunk_size: int = 0,
192
y_chunk_size: int = 0,
193
) -> None:
194
"""
195
Parameters:
196
- x: 2D array of x-coordinates
197
- y: 2D array of y-coordinates
198
- z: 2D array of z-values to contour
199
- mask: 2D boolean mask array
200
- corner_mask: Enable corner masking for masked arrays
201
- x_chunk_size: Chunk size in x-direction
202
- y_chunk_size: Chunk size in y-direction
203
"""
204
```
205
206
### Mpl2005ContourGenerator
207
208
Legacy Matplotlib 2005 algorithm with minimal features and limited chunking support.
209
210
```python { .api }
211
class Mpl2005ContourGenerator(ContourGenerator):
212
"""Legacy Matplotlib 2005 algorithm with minimal features."""
213
214
def __init__(
215
self,
216
x: CoordinateArray,
217
y: CoordinateArray,
218
z: CoordinateArray,
219
mask: MaskArray,
220
*,
221
x_chunk_size: int = 0,
222
y_chunk_size: int = 0,
223
) -> None:
224
"""
225
Parameters:
226
- x: 2D array of x-coordinates
227
- y: 2D array of y-coordinates
228
- z: 2D array of z-values to contour
229
- mask: 2D boolean mask array
230
- x_chunk_size: Chunk size in x-direction
231
- y_chunk_size: Chunk size in y-direction
232
"""
233
```
234
235
## Usage Examples
236
237
### Direct algorithm instantiation:
238
```python
239
import contourpy
240
import numpy as np
241
242
# Create sample data
243
x = np.linspace(0, 10, 100)
244
y = np.linspace(0, 10, 100)
245
X, Y = np.meshgrid(x, y)
246
Z = X**2 + Y**2
247
mask = None
248
249
# Create SerialContourGenerator directly
250
cont_gen = contourpy.SerialContourGenerator(
251
X, Y, Z, mask,
252
corner_mask=False,
253
line_type=contourpy.LineType.Separate,
254
fill_type=contourpy.FillType.OuterCode,
255
quad_as_tri=False,
256
z_interp=contourpy.ZInterp.Linear,
257
x_chunk_size=0,
258
y_chunk_size=0
259
)
260
```
261
262
### Algorithm capability checking:
263
```python
264
# Check what features an algorithm supports
265
print(f"Supports threading: {contourpy.ThreadedContourGenerator.supports_threads()}")
266
print(f"Supports corner mask: {contourpy.Mpl2005ContourGenerator.supports_corner_mask()}")
267
print(f"Supports Z interp: {contourpy.SerialContourGenerator.supports_z_interp()}")
268
```
269
270
### Multi-level contour generation:
271
```python
272
# Generate multiple contour levels efficiently
273
levels = [1.0, 2.0, 3.0, 4.0, 5.0]
274
multi_lines = cont_gen.multi_lines(levels)
275
multi_filled = cont_gen.multi_filled(levels)
276
277
# Each result is a list with one entry per level
278
for i, level in enumerate(levels):
279
lines_at_level = multi_lines[i]
280
filled_at_level = multi_filled[i]
281
```