-
Notifications
You must be signed in to change notification settings - Fork 12
/
count_segs.py
44 lines (39 loc) · 1.36 KB
/
count_segs.py
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
from argparse import ArgumentParser
import json
import numpy as np
import pandas as pd
from pycocotools import mask
from eval_constants import LOCALIZATION_TASKS
def main(args):
"""
For each pathology, count the number of CXRs with at least one segmentation.
"""
with open(args.seg_path) as f:
seg_dict = json.load(f)
cxr_ids = sorted(seg_dict.keys())
segmentation_label = {}
for task in sorted(LOCALIZATION_TASKS):
print(task)
has_seg = []
for cxr_id in cxr_ids:
seg_item = seg_dict[cxr_id][task]
seg_mask = mask.decode(seg_item)
if np.sum(seg_mask) == 0:
has_segmentation = 0
else:
has_segmentation = 1
has_seg.append(has_segmentation)
segmentation_label[task] = has_seg
df = pd.DataFrame.from_dict(segmentation_label)
n_cxr_per_pathology = df.sum()
print(n_cxr_per_pathology)
n_cxr_per_pathology.to_csv(f'{args.save_dir}/n_segs.csv')
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('--seg_path', type=str,
help='json file path where segmentations are saved \
(encoded)')
parser.add_argument('--save_dir', default='.',
help='where to save results')
args = parser.parse_args()
main(args)