manuel-munoz-aguirre / PyHIST

A pipeline to segment tissue from the background in histological images
GNU General Public License v3.0
56 stars 13 forks source link

Tile coordinates #22

Open gabemarx opened 3 years ago

gabemarx commented 3 years ago

Hello,

I was wondering if there is any feature that would enable me to get information on the coordinates of each tile so I can relate it back to the WSI in analysis. The output csv currently just tells me the relative row and column but I am finding it difficult to use that in a meaningful way.

Best, Gabe

manuel-munoz-aguirre commented 3 years ago

Hi Gabe, Currently, there is no built-in feature to save the pixel tile coordinates in the output file. But since the tile sizes and row/column coordinates are included in the output csv, you can calculate the tile position (upper left corner of each tile) from the output file (at the level of output-downsample):

import pandas as pd
import numpy as np

tiles = pd.read_csv("tile_selection.tsv", sep = "\t")

# Calculate width position in output
tiles['width_position'] = pd.Series(tiles.groupby('Row').apply(lambda x: np.cumsum(x['Width']).shift(1, fill_value = 0)).values)

# Calculate height position in output
heights = [0]
h = 0
for i in range(1, tiles.shape[0]):
    if tiles['Row'][i] != tiles['Row'][i-1]:
        h += tiles['Height'][i-1]
    heights.append(h)
tiles['height_position'] = pd.Series(heights)

You can (approximately) relate these to the original WSI pixel coordinates through the downsampling factor used (the exact value can be seen when running PyHIST with --info verbose).

franfcunha commented 3 years ago

You could also change __create_tiles function, namely the value of imgtile_out to a string format that could be more easily interpreted afterwards. I did it by passing the counter i to the np.unravel_index() function and then added the vertical and horizontal subscript in the tile name. In my case I am writing tiles from the base level (downsample=1), if it is not your case you could also add some integer value to the tile_name to get a reference to the level from which it was sampled...

swvanderlaan commented 2 years ago

@franfcunha could you potentially share your code on how you did this?