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
259 260 261 262 263 264 265 266 267 268 269 270 271 272 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 | |
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, 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
|
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
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
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). |