|
a |
|
b/count_tiles.py |
|
|
1 |
import os |
|
|
2 |
from argparse import ArgumentParser |
|
|
3 |
import json |
|
|
4 |
|
|
|
5 |
script_dir = os.path.dirname(os.path.realpath(__file__)) |
|
|
6 |
|
|
|
7 |
|
|
|
8 |
def main(config_path): |
|
|
9 |
with open(config_path) as json_file: |
|
|
10 |
config = json.load(json_file) |
|
|
11 |
tile_path = config["output_path"] |
|
|
12 |
|
|
|
13 |
total_tumor = 0 |
|
|
14 |
total_tiles = 0 |
|
|
15 |
slides = sorted([f for f in os.listdir(tile_path) if os.path.isdir(os.path.join(tile_path, f))]) |
|
|
16 |
for i in range(len(slides)): |
|
|
17 |
slide = slides[i] |
|
|
18 |
slide_path = os.path.join(tile_path, slide) |
|
|
19 |
n_tumor = len(os.listdir(os.path.join(slide_path, 'tumor'))) |
|
|
20 |
n_other = len(os.listdir(os.path.join(slide_path, 'non_tumor'))) |
|
|
21 |
n_total = n_tumor + n_other |
|
|
22 |
frac = n_tumor / n_total * 100 |
|
|
23 |
print("{}: {}/{}, {:.4f}% tumor tiles of total tiles, name: {}".format(i, n_tumor, n_total, frac, slide)) |
|
|
24 |
total_tumor += n_tumor |
|
|
25 |
total_tiles += n_total |
|
|
26 |
frac = total_tumor / total_tiles * 100 |
|
|
27 |
print("Total: {}/{}, {:.4f}% tumor tiles of total tiles".format(total_tumor, total_tiles, frac)) |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
if __name__ == "__main__": |
|
|
31 |
parser = ArgumentParser() |
|
|
32 |
parser.add_argument("--config_path", default=script_dir + "/resources/config.json") |
|
|
33 |
args = parser.parse_args() |
|
|
34 |
|
|
|
35 |
main(args.config_path) |