Tile sizing
patchworks.auto_tile_shape(shape: tuple[int, ...], dtype: Any, target_bytes: int = 64 * 1024 ** 2, use_gpu: bool = False, gpu_memory: int | None = None, available_memory: int | None = None, n_workers: int | None = None, n_channels: int = 1, verbose: bool = False) -> tuple[int, ...]
Balanced tile shape for general-purpose 3-D processing.
Sizes the last three axes (spatial) to stay within the memory budget while keeping the shape as cubic as possible. Leading axes (t, c) are always 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, ...]
|
Full array shape, e.g. |
required |
dtype
|
Any
|
Array dtype. |
required |
target_bytes
|
int
|
Memory ceiling per tile. Default 64 MiB. |
64 * 1024 ** 2
|
use_gpu
|
bool
|
Size tiles against GPU VRAM rather than host RAM. Still capped by
host RAM too -- see |
False
|
gpu_memory
|
int | None
|
Available GPU VRAM in bytes; auto-queried when None. |
None
|
available_memory
|
int | None
|
Available host RAM in bytes; auto-queried when None. Consulted even
when |
None
|
n_workers
|
int | None
|
Number of parallel workers (divides the RAM budget). |
None
|
n_channels
|
int
|
Channels each tile carries (default 1). Above 1 the per-voxel cost
scales with it, so the tile shrinks accordingly -- e.g. the workflow's
|
1
|
verbose
|
bool
|
Log the chosen shape and estimated tile size. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[int, ...]
|
Tile shape with the same number of dimensions as shape. |
Examples:
Source code in src/patchworks/_chunks.py
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 479 480 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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
patchworks.auto_tile_shape_cellpose(shape: tuple[int, ...], dtype: Any, diameter: float | None = None, do_3D: bool = False, use_gpu: bool = False, gpu_memory: int | None = None, available_memory: int | None = None, n_workers: int | None = None, model_memory_bytes: int = 2 * 1024 ** 3, cellpose_memory_factor: int = 20, n_channels: int = 1, anisotropy: float | None = None, verbose: bool = False) -> tuple[int, ...]
Cellpose-optimised tile shape.
Cellpose is fundamentally 2-D: even in 3-D mode it runs 2-D segmentation on orthogonal planes and takes a consensus.
do_3D=False (default)
z is set to 1. Each tile is one 2-D (y, x) slice.
do_3D=True z is kept at its full extent per tile. y and x are tiled based on the available memory, accounting for the 3× overhead of three plane orientations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, ...]
|
Spatial shape, e.g. |
required |
dtype
|
Any
|
Array dtype. |
required |
diameter
|
float | None
|
Expected cell diameter in pixels. Tile will be at least |
None
|
do_3D
|
bool
|
Whether Cellpose will run in 3-D mode. |
False
|
use_gpu
|
bool
|
Size tiles for GPU VRAM. Still capped by host RAM too -- see
|
False
|
gpu_memory
|
int | None
|
Memory parameters (auto-queried when None). |
None
|
available_memory
|
int | None
|
Memory parameters (auto-queried when None). |
None
|
n_workers
|
int | None
|
Memory parameters (auto-queried when None). |
None
|
model_memory_bytes
|
int
|
Memory consumed by the Cellpose model weights (default 2 GiB). |
2 * 1024 ** 3
|
cellpose_memory_factor
|
int
|
Cellpose allocates roughly this multiple of raw input bytes (default
20×). Applied against whichever of VRAM/host RAM is tighter --
|
20
|
n_channels
|
int
|
Channels each tile carries (default 1). Above 1 the per-voxel cost
scales with it, so the tile shrinks accordingly -- e.g. the workflow's
|
1
|
anisotropy
|
float | None
|
The |
None
|
verbose
|
bool
|
Log the chosen shape and memory estimates. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[int, ...]
|
Tile shape with the same number of dimensions as shape. |
Examples:
>>> tile = auto_tile_shape_cellpose((128, 2048, 2048), "uint16", diameter=30)
>>> tile
(1, 2048, 2048)
Source code in src/patchworks/_chunks.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | |
patchworks.auto_overlap(diameter: float, safety: float = 1.0, voxel_size: Union[Sequence[float], None] = None) -> Union[int, tuple[int, ...]]
Recommended overlap (halo) for a given cell diameter.
Rule: overlap >= diameter so the segmentation function always sees at least one full cell's worth of context on every tile edge. Cells near tile boundaries are then segmented correctly and only genuinely split cells produce touching labels at the boundary → correct merge.
With voxel_size the halo is returned per axis instead of as one number. That matters on anisotropic stacks: a halo big enough laterally is far more than one cell deep in z, and the extra planes are read and segmented only to be trimmed away again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diameter
|
float
|
Expected cell diameter in lateral pixels (same unit as your image's x/y). |
required |
safety
|
float
|
Multiplier on top of diameter. Default 1.0 (= one cell width). Use 1.5–2.0 for elongated or irregularly-shaped cells. |
1.0
|
voxel_size
|
Union[Sequence[float], None]
|
Physical size per axis, in any single unit (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
int or tuple of int
|
Overlap depth to pass to |
Examples:
>>> from patchworks import auto_overlap, tile_process
>>> from patchworks.plugins.cellpose import cellpose_fn
>>>
>>> fn = cellpose_fn("cyto3", gpu=True, diameter=30)
>>> result = tile_process("image.zarr", fn,
... tile_shape=(1, 2048, 2048),
... overlap=auto_overlap(30))
>>> auto_overlap(15, voxel_size=(2.0, 0.1, 0.1))
(1, 15, 15)
Source code in src/patchworks/_chunks.py
patchworks.normalize_overlap(overlap: Overlap, ndim: int, tile_shape: 'Sequence[int] | None' = None) -> tuple[int, ...]
Expand an overlap spec to one halo width per axis.
A scalar applies the same halo to every axis (the historical behaviour).
A sequence gives the halo per axis, which matters for anisotropic tiles:
a (16, 1024, 1024) tile with a scalar overlap of 30 reads
76 x 1084 x 1084 to keep 16 x 1024 x 1024 -- 5.3x more voxels than
it uses, nearly all of it in z.
With tile_shape, an axis only one voxel thick gets no halo. There is
no context to gather along an axis the tile does not span, and a 2-D
method handed the extra planes would read them as channels. This is the
tile_shape: "auto" + do_3D: false case, where tiles come out one
plane thick: a z-overlap of 4 would otherwise read 9 planes per tile to
keep 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overlap
|
int or sequence of int
|
Halo width, shared or per-axis. |
required |
ndim
|
int
|
Number of axes the halo is applied to. |
required |
tile_shape
|
sequence of int
|
Tile extent per axis. Used to drop halos an axis has no room for. |
None
|
Returns:
| Type | Description |
|---|---|
tuple of int
|
One non-negative halo width per axis. |
Source code in src/patchworks/_distributed.py
Cluster resource detection
On a shared node the machine's core count and free RAM say nothing about what this job was granted. These read the allocation instead, and everything that sizes a worker pool goes through them.
patchworks.cpu_allocation() -> int
Return the number of CPUs this process may actually use.
os.cpu_count() reports the machine's cores, which on a shared cluster
node is wildly more than a job was granted -- a 4-core allocation on a
128-core node would size itself for 128. Prefer what the scheduler says,
then the process' CPU affinity mask, and only then the machine.
Returns:
| Type | Description |
|---|---|
int
|
Usable CPU count (always >= 1). |
Source code in src/patchworks/_chunks.py
patchworks.safe_worker_count(tile_nbytes: int, *, use_gpu: bool = False, fn_overhead: int = 4, ram_fraction: float = 0.8) -> int
Concurrent tiles that fit the machine without OOM or a CPU freeze.
Bounds the threaded scheduler by two limits and takes the smaller:
- CPU — leaves at least one core free so the box stays responsive (never pins every core).
- RAM — at most
ram_fractionof available memory, assuming each in-flight tile needsfn_overheadcopies (halo + output + temporaries).
On GPU the answer is always 1: one evaluation at a time so concurrent
tiles can never exhaust VRAM. Without psutil it returns a conservative
default rather than guessing high.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tile_nbytes
|
int
|
Size of one tile in bytes ( |
required |
use_gpu
|
bool
|
Whether tiles are processed on the GPU. |
False
|
fn_overhead
|
int
|
Assumed peak number of tile-sized buffers alive per worker. |
4
|
ram_fraction
|
float
|
Fraction of available RAM the staging step may use. |
0.8
|
Returns:
| Type | Description |
|---|---|
int
|
Worker-thread count (always >= 1). |