|
| 1 | +import os |
| 2 | +from glob import glob |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from flamingo_tools.validation import ( |
| 6 | + fetch_data_for_evaluation, parse_annotation_path, compute_scores_for_annotated_slice |
| 7 | +) |
| 8 | + |
| 9 | +ROOT = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/AnnotatedImageCrops/F1Validation" |
| 10 | +ANNOTATION_FOLDERS = ["AnnotationsEK", "AnnotationsAMD"] |
| 11 | + |
| 12 | + |
| 13 | +def run_evaluation(root, annotation_folders, result_file, cache_folder): |
| 14 | + # TODO load existing result file and initialize |
| 15 | + results = { |
| 16 | + "annotator": [], |
| 17 | + "cochlea": [], |
| 18 | + "slice": [], |
| 19 | + "tps": [], |
| 20 | + "fps": [], |
| 21 | + "fns": [], |
| 22 | + } |
| 23 | + |
| 24 | + if cache_folder is not None: |
| 25 | + os.makedirs(cache_folder, exist_ok=True) |
| 26 | + |
| 27 | + for folder in annotation_folders: |
| 28 | + annotator = folder[len("Annotations"):] |
| 29 | + annotations = sorted(glob(os.path.join(root, folder, "*.csv"))) |
| 30 | + for annotation_path in annotations: |
| 31 | + cochlea, slice_id = parse_annotation_path(annotation_path) |
| 32 | + # We don't have this cochlea in MoBIE yet |
| 33 | + if cochlea == "M_LR_000169_R": |
| 34 | + continue |
| 35 | + |
| 36 | + # TODO skip if this is already in the results |
| 37 | + print("Run evaluation for", annotator, cochlea, slice_id) |
| 38 | + segmentation, annotations = fetch_data_for_evaluation( |
| 39 | + annotation_path, components_for_postprocessing=[1], |
| 40 | + cache_path=None if cache_folder is None else os.path.join(cache_folder, f"{cochlea}_{slice_id}.tif") |
| 41 | + ) |
| 42 | + scores = compute_scores_for_annotated_slice(segmentation, annotations) |
| 43 | + results["annotator"].append(annotator) |
| 44 | + results["cochlea"].append(cochlea) |
| 45 | + results["slice"].append(slice_id) |
| 46 | + results["tps"].append(scores["tp"]) |
| 47 | + results["fps"].append(scores["fp"]) |
| 48 | + results["fns"].append(scores["fn"]) |
| 49 | + table = pd.DataFrame(results) |
| 50 | + table.to_csv(result_file, index=False) |
| 51 | + print(table) |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + import argparse |
| 56 | + parser = argparse.ArgumentParser() |
| 57 | + parser.add_argument("-i", "--input", default=ROOT) |
| 58 | + parser.add_argument("--folders", default=ANNOTATION_FOLDERS) |
| 59 | + parser.add_argument("--result_file", default="results.csv") |
| 60 | + parser.add_argument("--cache_folder") |
| 61 | + args = parser.parse_args() |
| 62 | + run_evaluation(args.input, args.folders, args.result_file, args.cache_folder) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
0 commit comments