I/O helpers
patchworks.load_ome_zarr(store_path: Union[str, Path], channel: int | None = 0, level: int = 0, chunks: tuple[int, ...] | None = None) -> da.Array
Load one spatial array from an OME-ZARR store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store_path
|
Union[str, Path]
|
Path to the OME-ZARR store (.zarr directory). |
required |
channel
|
int | None
|
Channel index to select (axis is dropped). Pass |
0
|
level
|
int
|
Resolution pyramid level (0 = full resolution). |
0
|
chunks
|
tuple[int, ...] | None
|
Target chunk shape for the returned dask array. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Shape |
Examples:
Source code in src/patchworks/_io.py
Deciding which tiles hold signal
estimate_empty_tiles is a fast preview — it samples a centred window per
tile, so it can miss signal at a tile's edge. build_occupancy_map +
tile_occupancy are exact: a brick maximum exceeds the threshold exactly
when some voxel in that brick does. Use the latter pair when the result is
used as a skip list. See Skipping empty tiles.
patchworks.estimate_empty_tiles(image: Union[da.Array, str, Path], tile_shape: tuple[int, ...], threshold: float | None = None, channel: int | None = 0, level: int = 0, sample_window: tuple[int, ...] = (24, 256, 256)) -> dict[str, Any]
Fast preview of which tiles are background before processing.
For each tile, reads a small centred window (sample_window) and tests
whether its max exceeds threshold. Bounded I/O — runs in seconds to
minutes on terabyte arrays.
APPROXIMATE: only the tile centre is inspected. The actual tile_process
run always tests the whole tile inline. Use this only to pick a threshold
and gauge the empty fraction before committing to a full run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Union[Array, str, Path]
|
Dask array or OME-ZARR path. |
required |
tile_shape
|
tuple[int, ...]
|
Tile shape you plan to use, e.g. |
required |
threshold
|
float | None
|
Empty cutoff (signal <= threshold → empty). None → Otsu on samples. |
None
|
channel
|
int | None
|
Used only when image is a path. |
0
|
level
|
int | None
|
Used only when image is a path. |
0
|
sample_window
|
tuple[int, ...]
|
Size of the centred window read per tile. |
(24, 256, 256)
|
Returns:
| Type | Description |
|---|---|
dict with keys:
|
|
Examples:
>>> info = estimate_empty_tiles("image.zarr", (120, 697, 697))
>>> print(f"{info['empty_fraction']:.0%} of tiles are background")
>>> labels = tile_process("image.zarr", fn, tile_shape=(120, 697, 697),
... skip_empty=True, empty_threshold=info["threshold"])
Source code in src/patchworks/_io.py
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 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 | |
patchworks.build_occupancy_map(image_store: Union[str, Path], *, level: int = 0, block: tuple[int, ...] = DEFAULT_BLOCK, overwrite: bool = False) -> str
Max-pool every channel of an OME-ZARR level into a small summary array.
Reads the image once and writes <image_store>/occupancy/<level>, an
array of shape (n_channels, *ceil(spatial_shape / block)) holding the
maximum of each brick. Cheap to keep (~1/16384 of the image by default)
and reusable by every config that segments this image.
Idempotent: an existing map is reused unless overwrite is set, so
concurrent segmentation runs against one work_dir build it at most
once. The map is written to a temporary sibling and moved into place, so a
crash mid-build never leaves a partial map behind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_store
|
str or Path
|
OME-ZARR store to summarise. |
required |
level
|
int
|
Pyramid level to read (default 0, full resolution). |
0
|
block
|
tuple of int
|
Brick shape over the spatial axes (default |
DEFAULT_BLOCK
|
overwrite
|
bool
|
Rebuild even if a map already exists. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Path of the occupancy array. |
Examples:
Source code in src/patchworks/_occupancy.py
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 185 186 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 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 | |
patchworks.tile_occupancy(image_store: Union[str, Path], tile_shape: tuple[int, ...], *, channel: int = 0, threshold: float, level: int = 0) -> dict[str, Any]
Decide which tiles hold signal, using the whole tile, not a sample.
Reduces the occupancy map over each tile's full footprint and marks the tile occupied when any brick maximum exceeds threshold -- equivalent to testing every voxel, because a brick maximum exceeds the threshold exactly when some voxel in that brick does.
Blocks are only ever over-covered, never under-covered: when a tile edge falls inside a brick, that brick counts for both neighbours. A tile can therefore be occupied because of a neighbour's signal in a shared brick, which costs one extra segmentation job but can never drop a tile. Choosing a block that divides tile_shape (the default 128 divides 1024) avoids even that.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_store
|
str or Path
|
OME-ZARR store holding the occupancy map (see
:func: |
required |
tile_shape
|
tuple of int
|
Tile shape, in full-resolution voxels. |
required |
channel
|
int
|
Channel to test. |
0
|
threshold
|
float
|
Empty cutoff, derived from raw voxel values (signal <= threshold → empty). |
required |
level
|
int
|
Pyramid level the map was built from. |
0
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/patchworks/_occupancy.py
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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
patchworks.auto_empty_threshold(image: da.Array, channel: int | None, level: int) -> float
Pick an empty-tile threshold from a cheap bounded sample (Otsu).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Array
|
Image to sample. |
required |
channel
|
int or None
|
Channel hint (kept for signature symmetry). |
required |
level
|
int
|
Pyramid level hint (kept for signature symmetry). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Otsu threshold over a few small centred windows. |