NKI-AI / dlup

Dlup are the Deep Learning Utilities for Pathology developed at the Netherlands Cancer Institute
Apache License 2.0
25 stars 7 forks source link

Incorrect grid order of grid_local_coordinates in TiledWsiDataset #229

Closed VanessaBotha closed 4 days ago

VanessaBotha commented 5 months ago

Describe the bug

  1. When grid_order changes from C to F, grid_local_coordinates do not change accordingly.
  2. Furthermore, grid_local_coordinates do not match the order of the sample coordinates

image

To Reproduce

TCGA/images/gdc_manifest.2021-11-01_diagnostic_breast.txt/fffb83a0-dab6-41be-9d8b-f4fb6c2584ab/TCGA-BH-A0HW-01Z-00-DX1.44DCCE00-133F-4469-A4DA-5057C011B4AC.svs

grid_order = "F" # or grid_order = "C"
print(grid_order)

slide_dataset = TiledWsiDataset.from_standard_tiling(
                path=slide_path,
                mpp=15,
                tile_size=(224, 224),
                tile_overlap=(0, 0),
                grid_order=grid_order)

print("grid size:", slide_dataset.grids[0][0].size)
for idx, sample in enumerate(slide_dataset):
    print("coordinates:", sample["coordinates"], "grid_local_coordinates:", sample["grid_local_coordinates"])
    if idx==10:
        break

Expected behavior The grid_local_coordinates should always match the order of the coordinates, example: coordinates: (0, 0) grid_local_coordinates: (0, 0) coordinates: (224, 0) grid_local_coordinates: (1, 0) coordinates: (448, 0) grid_local_coordinates: (2, 0) etc.

Environment dlup version: current main branch at commit c1f58b8, but also earlier versions like dlup==0.3.38 How installed: pip install -e ".[dev]" Python version: 3.10 Operating System: Ubuntu 22.04.4 LTS

Proposed solution

replace grid_local_coordinates = np.unravel_index(grid_index, self.grids[starting_index][0].size) by: grid_local_coordinates = np.unravel_index(grid_index, self.grids[starting_index][0].size[::-1], order=self.grids[starting_index][0].order)[::-1]

Explanation

Problem 1 is solved by passing the grid order to np.unravel_index (default of this function in order="C") in def _process_tile_sample: So: grid_local_coordinates = np.unravel_index(grid_index, self.grids[starting_index][0].size, order=self.grids[starting_index][0].order) Now the order of grid_local_coordinates changes, when the grid_order changes image

Problem 2: However, grid_local_coordinates is still not matching the order of coordinates.

numpy.unravel_index(indices, shape, order) where shape is (h, w). In dlup Grid.size is (w, h). To verify: grid_size of this image is (9, 7) and the image looks like: image

so width should be 9 (longer) and height 7 (shorter)

So the Grid size needs to be reversed:

grid_local_coordinates = np.unravel_index(grid_index, self.grids[starting_index][0].size[::-1], order=self.grids[starting_index][0].order)

image

The output of np.unravel_index is also (h, w), so need to reverse these again to obtain the matching grid_local_coordinates

grid_local_coordinates = np.unravel_index(grid_index, self.grids[starting_index][0].size[::-1], order=self.grids[starting_index][0].order)[::-1]

image

`

github-actions[bot] commented 3 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days