layout.extraction — cropping and rasterizing
tempura/layout/extraction.py holds the geometric steps that turn raw layout
polygons into device-aligned boolean gate masks: building an area of interest
(AOI), cropping polygons to it, and rasterizing them onto a regular grid. It
also provides the layout inspection plots.
Coordinate systems
Three coordinate systems pass through this module, and keeping them straight avoids most confusion:
| System | Set by | Units |
|---|---|---|
| Original layout | the source file / aoi_bbox |
layout units |
| Local AOI | crop_polygons_to_aoi(...) |
layout units, shifted so the lower-left corner is the origin |
| Internal grid | rasterization | grid cells (one cell = one unit) |
aoi_bbox is always given in the original layout coordinates;
make_aoi_bbox_from_ranges(...) builds one from two coordinate ranges. Cropping
clips every layer to that window and translates the result so its lower-left
corner sits at \((0, 0)\).
Rasterizing at cell centers
rasterize_gate_vertices(...) samples each polygon at cell centers and
returns boolean masks that share one \((n_y, n_x)\) shape and alignment. Sampling
at centers (rather than corners) means a cell is "on" when the gate covers its
center, which keeps adjacent gates from sharing edge cells. Masks are indexed
[row, col] = [y, x].
The grid count for a span is snapped with a small decimal guard
(_grid_count_from_span) so values such as 48.00000000000001 resolve to the
intended integer cell count instead of rounding up spuriously.
Inspection plots
plot_layout_layers(...), plot_gate_stencil_layers(...), and
plot_layout_interactive(...) draw the whole layout, the rasterized masks, or an
interactive view. Drawing polygon outlines rather than filled regions keeps the
overview fast on large layouts.
API
extraction
AOI extraction and mask generation helpers.
crop_polygons_to_aoi(polygons_by_layer, aoi_bbox, precision=1e-06)
Clip all layers to one AOI and return local-coordinate polygons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygons_by_layer
|
PolygonMap
|
Input polygons grouped by layer. |
required |
aoi_bbox
|
BBoxXY
|
Global AOI bounds as |
required |
precision
|
float
|
Boolean-operation precision passed to |
1e-06
|
Returns:
| Type | Description |
|---|---|
PolygonMap
|
New polygon mapping where each polygon is clipped to |
PolygonMap
|
translated so the AOI lower-left corner becomes |
Source code in tempura/layout/extraction.py
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 | |
make_aoi_bbox_from_ranges(x_range, y_range)
Build an AOI bbox from two two-point coordinate ranges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_range
|
Sequence[float]
|
Two x coordinates describing the lower and upper AOI bounds. |
required |
y_range
|
Sequence[float]
|
Two y coordinates describing the lower and upper AOI bounds. |
required |
Returns:
| Type | Description |
|---|---|
BBoxXY
|
Normalized |
Source code in tempura/layout/extraction.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
plot_gate_stencil_layers(gate_stencils, *, roi_size, layer_order=None, title='Rasterized gate masks by layer', figsize=None, coordinate_scale=1.0, axis_unit=None, layer_thicknesses=None)
Plot one panel per source layer with that layer's gates overlaid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gate_stencils
|
MasksByLayer
|
Rasterized masks grouped by source layer. |
required |
roi_size
|
Sequence[float]
|
ROI size as |
required |
layer_order
|
Sequence[str] | None
|
Optional explicit source-layer order. |
None
|
title
|
str
|
Figure title. |
'Rasterized gate masks by layer'
|
figsize
|
tuple[float, float] | None
|
Optional Matplotlib figure size. |
None
|
coordinate_scale
|
float
|
Multiplicative scale applied to displayed coordinates and optional thickness annotations. |
1.0
|
axis_unit
|
str | None
|
Optional axis-unit label appended to x/y labels. |
None
|
layer_thicknesses
|
Mapping[str, float] | None
|
Optional per-layer thickness values used in subplot titles. |
None
|
Returns:
| Type | Description |
|---|---|
'Figure'
|
Matplotlib figure containing one subplot per non-empty source layer. |
Source code in tempura/layout/extraction.py
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 | |
plot_layout_file_interactive(layout_path, *, layers=None, exclude_layers=None, title=None, filled=True)
Load a layout file and return an interactive figure for its polygons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout_path
|
str | Path
|
Filesystem path to the layout file. |
required |
layers
|
Sequence[str] | None
|
Optional subset of layers to include. |
None
|
exclude_layers
|
Sequence[str] | None
|
Optional subset of layers to exclude. |
None
|
title
|
str | None
|
Optional figure title. Defaults to |
None
|
filled
|
bool
|
Whether to render filled polygons or outlines only. |
True
|
Returns:
| Type | Description |
|---|---|
'go.Figure'
|
Plotly figure for the polygons loaded from |
Source code in tempura/layout/extraction.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
plot_layout_interactive(polygons_by_layer, layers=None, exclude_layers=None, title='Layout Layers (interactive)', *, filled=True)
Return an interactive Plotly figure for layer inspection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygons_by_layer
|
PolygonMap
|
Polygons grouped by layer name. |
required |
layers
|
Sequence[str] | None
|
Optional explicit layer order to render. |
None
|
exclude_layers
|
Sequence[str] | None
|
Optional layer names to omit from the figure. |
None
|
title
|
str
|
Figure title. |
'Layout Layers (interactive)'
|
filled
|
bool
|
Whether to render filled polygons or outlines only. |
True
|
Returns:
| Type | Description |
|---|---|
'go.Figure'
|
Plotly figure containing one trace per polygon with shared layer-based |
'go.Figure'
|
legend entries. |
Source code in tempura/layout/extraction.py
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 | |
plot_layout_layers(polygons_by_layer, *, layers=None, exclude_layers=None, title=None, filled=True, coordinate_scale=1.0, axis_unit=None)
Plot layout polygons with Matplotlib using one shared set of axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygons_by_layer
|
PolygonMap
|
Polygons grouped by layer name. |
required |
layers
|
Sequence[str] | None
|
Optional subset of layers to include. |
None
|
exclude_layers
|
Sequence[str] | None
|
Optional subset of layers to exclude. |
None
|
title
|
str | None
|
Optional axis title. |
None
|
filled
|
bool
|
Whether to render filled polygons or outlines only. |
True
|
coordinate_scale
|
float
|
Multiplicative scale applied to plotted coordinates. |
1.0
|
axis_unit
|
str | None
|
Optional axis-unit label appended to x/y labels. |
None
|
Returns:
| Type | Description |
|---|---|
'Figure'
|
Matplotlib figure containing all selected layers on one axis. |
Source code in tempura/layout/extraction.py
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
polygons_to_vertices(polygons_by_layer)
Convert polygons grouped by layer into nested Python lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygons_by_layer
|
PolygonMap
|
Polygons grouped by layer name. |
required |
Returns:
| Type | Description |
|---|---|
VerticesByLayer
|
Plain nested lists of vertex coordinates grouped the same way as the |
VerticesByLayer
|
input mapping. |
Source code in tempura/layout/extraction.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
rasterize_gate_vertices(gate_vertices_by_layer, *, dx, dy, roi_bbox)
Rasterize gate polygons into boolean masks on a regular grid.
All polygons are sampled on the same cell-centered ROI grid. The returned
masks use NumPy row-major ordering, so array indices are [y, x] while
polygon coordinates are (x, y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gate_vertices_by_layer
|
VerticesByLayer
|
Layer-to-polygons mapping where each polygon is
an ordered sequence of |
required |
dx
|
float
|
Positive cell spacing along x. |
required |
dy
|
float
|
Positive cell spacing along y. |
required |
roi_bbox
|
BBoxXY
|
Rasterization bounds as |
required |
Returns:
| Type | Description |
|---|---|
MasksByLayer
|
Mapping from layer name to one or more boolean masks. Each mask has |
MasksByLayer
|
shape |
MasksByLayer
|
polygon. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in tempura/layout/extraction.py
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 | |