[bf1564]: / count_tiles.py

Download this file

36 lines (28 with data), 1.3 kB

 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
import os
from argparse import ArgumentParser
import json
script_dir = os.path.dirname(os.path.realpath(__file__))
def main(config_path):
with open(config_path) as json_file:
config = json.load(json_file)
tile_path = config["output_path"]
total_tumor = 0
total_tiles = 0
slides = sorted([f for f in os.listdir(tile_path) if os.path.isdir(os.path.join(tile_path, f))])
for i in range(len(slides)):
slide = slides[i]
slide_path = os.path.join(tile_path, slide)
n_tumor = len(os.listdir(os.path.join(slide_path, 'tumor')))
n_other = len(os.listdir(os.path.join(slide_path, 'non_tumor')))
n_total = n_tumor + n_other
frac = n_tumor / n_total * 100
print("{}: {}/{}, {:.4f}% tumor tiles of total tiles, name: {}".format(i, n_tumor, n_total, frac, slide))
total_tumor += n_tumor
total_tiles += n_total
frac = total_tumor / total_tiles * 100
print("Total: {}/{}, {:.4f}% tumor tiles of total tiles".format(total_tumor, total_tiles, frac))
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--config_path", default=script_dir + "/resources/config.json")
args = parser.parse_args()
main(args.config_path)