SlideReader
Reader class for histological slide images.
Source code in histoslice/_reader.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 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 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 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 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 534 535 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 | |
backend_name
property
Name of the slide reader backend.
data_bounds
property
Data bounds defined by xywh-coordinates at level=0.
Some image formats (eg. .mrxs) define a bounding box where image data resides,
which may differ from the actual image dimensions. HistoPrep always uses the
full image dimensions, but other software (such as QuPath) uses the image
dimensions defined by this data bound.
dimensions
property
Image dimensions (height, width) at level=0.
level_count
property
Number of slide pyramid levels.
level_dimensions
property
Image dimensions (height, width) for each pyramid level.
level_downsamples
property
Image downsample factors (height, width) for each pyramid level.
mpp
property
Microns per pixel (mpp_x, mpp_y) at level 0.
Returns user-provided override if available, otherwise extracts from slide metadata. Returns None if not available.
name
property
Slide filename without an extension.
path
property
Full slide filepath.
suffix
property
Slide file-extension.
__init__(path, mpp=None)
Initialize SlideReader instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to slide image. |
required |
mpp
|
Optional[tuple[float, float]]
|
Override microns per pixel as (mpp_x, mpp_y). If None, attempts to extract from slide metadata. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
Path does not exist. |
Source code in histoslice/_reader.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
get_annotated_thumbnail(image, coordinates, linewidth=1)
Generate annotated thumbnail from coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ndarray
|
Input image. |
required |
coordinates
|
Iterator[tuple[int, int, int, int]]
|
Coordinates to annotate. |
required |
linewidth
|
int
|
Width of rectangle lines. |
1
|
Returns:
| Type | Description |
|---|---|
Image
|
Annotated thumbnail. |
Source code in histoslice/_reader.py
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 | |
get_mean_and_std(coordinates, *, level=0, max_samples=1000, num_workers=1, raise_exception=True)
Calculate mean and std for each image channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates
|
Iterator[tuple[int, int, int, int]]
|
|
required |
level
|
int
|
Slide pyramid level for reading tile images. Defaults to 0. |
0
|
max_samples
|
int
|
Maximum tiles to load. Defaults to 1000. |
1000
|
num_workers
|
int
|
Number of worker processes for yielding tiles. Defaults to 1. |
1
|
raise_exception
|
bool
|
Whether to raise an exception if there are problems with reading tile regions. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[tuple[float, ...], tuple[float, ...]]
|
Tuples of mean and std values for each image channel. |
Source code in histoslice/_reader.py
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 | |
get_spot_coordinates(tissue_mask, *, min_area_pixel=10, max_area_pixel=None, min_area_relative=0.2, max_area_relative=2.0)
Generate tissue microarray spot coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tissue_mask
|
ndarray
|
Tissue mask of the slide. It's recommended to increase |
required |
min_area_pixel
|
int
|
Minimum pixel area for contours. Defaults to 10. |
10
|
max_area_pixel
|
Optional[int]
|
Maximum pixel area for contours. Defaults to None. |
None
|
min_area_relative
|
float
|
Relative minimum contour area, calculated from the median
contour area after filtering contours with |
0.2
|
max_area_relative
|
Optional[float]
|
Relative maximum contour area, calculated from the median
contour area after filtering contours with |
2.0
|
Returns:
| Type | Description |
|---|---|
SpotCoordinates
|
|
Source code in histoslice/_reader.py
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 | |
get_tile_coordinates(tissue_mask, width, *, height=None, target_mpp=None, overlap=0.0, max_background=0.95, out_of_bounds=True)
Generate tile coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tissue_mask
|
Optional[ndarray]
|
Tissue mask for filtering tiles with too much background. If None, the filtering is disabled. |
required |
width
|
int
|
Width of a tile in pixels at target resolution. |
required |
height
|
Optional[int]
|
Height of a tile in pixels at target resolution. If None, will be
set to |
None
|
target_mpp
|
Optional[float]
|
Target microns per pixel for normalization. If specified, tiles
are extracted at the appropriate level to achieve this resolution. The
output tiles will be |
None
|
overlap
|
float
|
Overlap between neighbouring tiles. Defaults to 0.0. |
0.0
|
max_background
|
float
|
Maximum proportion of background in tiles. Ignored if
|
0.95
|
out_of_bounds
|
bool
|
Keep tiles which contain regions outside of the image. Defaults to True. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
|
ValueError
|
Height and/or width are smaller than 1. |
ValueError
|
Height and/or width is larger than dimensions. |
ValueError
|
Overlap is not in range [0, 1). |
Returns:
| Type | Description |
|---|---|
TileCoordinates
|
|
Source code in histoslice/_reader.py
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 | |
get_tissue_mask(*, level=None, threshold=None, multiplier=1.05, sigma=0.0)
Detect tissue from slide pyramid level image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Optional[int]
|
Slide pyramid level to use for tissue detection. If None, uses the
|
None
|
threshold
|
Optional[int]
|
Threshold for tissue detection. If set, will detect tissue by global thresholding. Otherwise Otsu's method is used to find a threshold. Defaults to None. |
None
|
multiplier
|
float
|
Otsu's method finds an optimal threshold by minimizing the
weighted within-class variance. This threshold is then multiplied with
|
1.05
|
sigma
|
float
|
Sigma for gaussian blurring. Defaults to 0.0. |
0.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Threshold not between 0 and 255. |
Returns:
| Type | Description |
|---|---|
tuple[int, ndarray]
|
Threshold and tissue mask. |
Source code in histoslice/_reader.py
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 | |
level_from_dimensions(dimensions)
Find pyramid level which is closest to dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
tuple[int, int]
|
Height and width. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Slide pyramid level. |
Source code in histoslice/_reader.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
level_from_max_dimension(max_dimension=4096)
Find pyramid level with both dimensions less or equal to max_dimension.
If one isn't found, return the last pyramid level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_dimension
|
int
|
Maximum dimension for the level. Defaults to 4096. |
4096
|
Returns:
| Type | Description |
|---|---|
int
|
Slide pyramid level. |
Source code in histoslice/_reader.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
read_level(level)
Read full pyramid level data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
Slide pyramid level to read. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
Invalid level argument. |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array containing image data from |
Source code in histoslice/_reader.py
113 114 115 116 117 118 119 120 121 122 123 124 125 | |
read_region(xywh, level=0)
Read region based on xywh-coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xywh
|
tuple[int, int, int, int]
|
Coordinates for the region. |
required |
level
|
int
|
Slide pyramid level to read from. Defaults to 0. |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Invalid |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array containing image data from |
Source code in histoslice/_reader.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
save_regions(parent_dir, coordinates, *, level=0, threshold=None, tissue_mask=None, overwrite=False, save_metrics=False, save_masks=False, save_thumbnails=True, thumbnail_level=None, image_format='jpeg', quality=80, num_workers=1, raise_exception=True, verbose=True)
Save regions from an iterable of xywh-coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_dir
|
Union[str, Path]
|
Parent directory for output. All output is saved to
|
required |
coordinates
|
Iterator[tuple[int, int, int, int]]
|
Iterator of xywh-coordinates. |
required |
level
|
int
|
Slide pyramid level for extracting xywh-regions. Defaults to 0. |
0
|
threshold
|
Optional[int]
|
Tissue detection threshold. Required when either |
None
|
overwrite
|
bool
|
Overwrite everything in |
False
|
save_metrics
|
bool
|
Save image metrics to metadata, requires that threshold is set. Defaults to False. |
False
|
save_masks
|
bool
|
Save tissue masks as |
False
|
save_thumbnails
|
bool
|
Save slide thumbnail with and without region annotations. Defaults to True. |
True
|
tissue_mask
|
Optional[ndarray]
|
Optional tissue mask to use for thumbnail visualization. If None and coordinates contains a tissue mask, that mask is used. |
None
|
thumbnail_level
|
Optional[int]
|
Slide pyramid level for thumbnail images. If None, uses the
|
None
|
image_format
|
str
|
File format for |
'jpeg'
|
quality
|
int
|
JPEG compression quality if |
80
|
num_workers
|
int
|
Number of data saving workers. Defaults to 1. |
1
|
raise_exception
|
bool
|
Whether to raise an exception if there are problems with reading tile regions. Defaults to True. |
True
|
verbose
|
bool
|
Enables |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Invalid |
ValueError
|
Threshold is not between 0 and 255. |
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, list[dict[str, object]]]
|
Tuple of (metadata dataframe, failure reports). |
Source code in histoslice/_reader.py
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 534 535 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 | |
yield_regions(coordinates, *, level=0, transform=None, num_workers=1, return_exception=False)
Yield tile images and corresponding xywh coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates
|
Iterator[tuple[int, int, int, int]]
|
List of xywh-coordinates. |
required |
level
|
int
|
Slide pyramid level for reading tile images. Defaults to 0. |
0
|
transform
|
Optional[Callable[[ndarray], Any]]
|
Transform function for tile image. Defaults to None. |
None
|
num_workers
|
int
|
Number of worker processes. Defaults to 1. |
1
|
return_exception
|
bool
|
Whether to return exception in case there is a failure to read region, instead of raising the exception. Defaults to False. |
False
|
Yields:
| Type | Description |
|---|---|
Union[ndarray, Exception, Any]
|
Tuple of (possibly transformed) tile image and corresponding |
tuple[int, int, int, int]
|
xywh-coordinate. |
Source code in histoslice/_reader.py
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 | |