Verifies inputs, extracts patches from WSI image and generates metadata. Args: wsi: a whole slide image object loaded from a file or a list of such objects. location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0,
(
self,
wsi,
location: tuple[int, int] = (0, 0),
size: tuple[int, int] | None = None,
level: int | None = None,
mpp: float | tuple[float, float] | None = None,
power: int | None = None,
mode: str | None = None,
)
| 320 | return metadata |
| 321 | |
| 322 | def get_data( |
| 323 | self, |
| 324 | wsi, |
| 325 | location: tuple[int, int] = (0, 0), |
| 326 | size: tuple[int, int] | None = None, |
| 327 | level: int | None = None, |
| 328 | mpp: float | tuple[float, float] | None = None, |
| 329 | power: int | None = None, |
| 330 | mode: str | None = None, |
| 331 | ) -> tuple[np.ndarray, dict]: |
| 332 | """ |
| 333 | Verifies inputs, extracts patches from WSI image and generates metadata. |
| 334 | |
| 335 | Args: |
| 336 | wsi: a whole slide image object loaded from a file or a list of such objects. |
| 337 | location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0, 0). |
| 338 | size: (height, width) tuple giving the patch size at the given level (`level`). |
| 339 | If not provided or None, it is set to the full image size at the given level. |
| 340 | level: the whole slide image level at which the patches are extracted. |
| 341 | mpp: the resolution in micron per pixel at which the patches are extracted. |
| 342 | power: the objective power at which the patches are extracted. |
| 343 | dtype: the data type of output image. |
| 344 | mode: the output image mode, 'RGB' or 'RGBA'. |
| 345 | |
| 346 | Returns: |
| 347 | a tuples, where the first element is an image patch [CxHxW] or stack of patches, |
| 348 | and second element is a dictionary of metadata. |
| 349 | |
| 350 | Notes: |
| 351 | Only one of resolution parameters, `level`, `mpp`, or `power`, should be provided. |
| 352 | If none of them are provided, it uses the defaults that are set during class instantiation. |
| 353 | If none of them are set here or during class instantiation, `level=0` will be used. |
| 354 | """ |
| 355 | if mode is None: |
| 356 | mode = self.mode |
| 357 | patch_list: list = [] |
| 358 | metadata_list: list = [] |
| 359 | |
| 360 | # CuImage object is iterable, so ensure_tuple won't work on single object |
| 361 | if not isinstance(wsi, list | tuple): |
| 362 | wsi = (wsi,) |
| 363 | for each_wsi in ensure_tuple(wsi): |
| 364 | # get the valid level based on resolution info |
| 365 | level = self.get_valid_level(each_wsi, level, mpp, power) |
| 366 | |
| 367 | # Verify location |
| 368 | if location is None: |
| 369 | location = (0, 0) |
| 370 | wsi_size = self.get_size(each_wsi, 0) |
| 371 | if location[0] > wsi_size[0] or location[1] > wsi_size[1]: |
| 372 | raise ValueError(f"Location is outside of the image: location={location}, image size={wsi_size}") |
| 373 | |
| 374 | # Verify size |
| 375 | if size is None: |
| 376 | if location != (0, 0): |
| 377 | raise ValueError("Patch size should be defined to extract patches.") |
| 378 | size = self.get_size(each_wsi, level) |
| 379 | else: |
nothing calls this directly
no test coverage detected