monai
medical
File size: 1,965 Bytes
9984ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b64c65
9984ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import os

from monai.apps.pathology import LesionFROC


def full_path(dir: str, file: str):
    return os.path.normpath(os.path.join(dir, file))


def load_data(ground_truth_dir: str, eval_dir: str, level: int, spacing: float):
    # Get the list of probability map result files
    prob_files = os.listdir(eval_dir)

    # read the dataset and create an eval_dataset based on that.
    eval_dataset = []
    for prob_name in prob_files:
        if prob_name.endswith(".npy"):
            sample = {
                "tumor_mask": full_path(ground_truth_dir, prob_name.replace("npy", "tif")),
                "prob_map": full_path(eval_dir, prob_name),
                "level": level,
                "pixel_spacing": spacing,
            }

            eval_dataset.append(sample)

    return eval_dataset


def evaluate_froc(data, reader):
    lesion_froc = LesionFROC(data, image_reader_name=reader)
    score = lesion_froc.evaluate()
    return score


if __name__ == "__main__":
    # Parse command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--spacing", type=float, default=0.243, dest="spacing")
    parser.add_argument("-l", "--level", type=int, default=6, dest="level")
    parser.add_argument("-r", "--reader", type=str, default="cucim", dest="reader")
    parser.add_argument("-e", "--eval-dir", type=str, dest="eval_dir")
    parser.add_argument("-g", "--ground-truth-dir", type=str, dest="ground_truth_dir")
    args = parser.parse_args()

    # prepare FROC input data
    data = load_data(args.ground_truth_dir, args.eval_dir, args.level, args.spacing)
    if len(data) < 1:
        raise RuntimeError(f"No probability map result found in '{args.eval_dir}' with '.npy' extension.")

    # evaluate FROC
    score = evaluate_froc(data, args.reader)
    with open(full_path(args.eval_dir, "froc_score.txt"), "w") as f:
        f.write(f"FROC Score: {score}\n")
    print(f"FROC Score: {score}")