svs_to_random_tiles.py 1.54 KB
Newer Older
Alessia Marcolini's avatar
Alessia Marcolini committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# %% 
import gc
import os
import sys
from pathlib import Path

import numpy as np
import pandas as pd

# custom Histology library
from histo_lib import wsi
from inf.config import SVS_DIR

# %%
#PATH = Path('..')

SIZE = 512
SIZE_NO_OVERLAP = 1024

LEVEL = 0

N_TILES_MAX = 100

OUT_DIR = SVS_DIR.parents[0] / f'tiles_level_{0}' # folder in the same parent folder of SVS_DIR
os.makedirs(OUT_DIR, exist_ok=True)

# %%
svs_files = os.listdir(SVS_DIR)
# %%
wrong_svs = []
for index, SVS in enumerate(svs_files): #for each svs file

    #sys.stdout.flush()
    sys.stdout.write(f'\r Processing svs: {index+1}/{len(svs_files)} : {SVS}')

    try:
        img = wsi.open_wsi(f'{SVS_DIR}/{SVS}') #open the image

        prefix = f'{OUT_DIR}/{SVS}_' 
        # prefix =  f'{OUTDIR}/{SVS}/{SVS}' if you want to create a subfolder for each wsi

        # get tissue box
        box_tissue = wsi.get_tissue_region(img) # get the tissue box
        w = box_tissue[2] - box_tissue[0] #get height of the tissue box
        h = box_tissue[3] - box_tissue[1] #get width of the tissue box

        #compute the maximum number of tiles that can be extracted
        min_size = min(h,w)
        max_tiles = (min_size // SIZE_NO_OVERLAP)**2

        # generate random tiles
        n_tiles = min(N_TILES_MAX, max_tiles)
        wsi.wsi2random_tiles(img, SIZE, LEVEL, n_tiles, check_tissue=True, prefix=prefix, max_iter=2*N_TILES_MAX)

        #clean workspace
        del img
        gc.collect()
        
        
    except Exception as e:
        wrong_svs.append(SVS)
        print(e)


# %%