|
| 1 | +import os |
| 2 | +from typing import List |
| 3 | + |
| 4 | +import h5py |
| 5 | +import pandas as pd |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +from elf.evaluation.matching import _compute_scores, _compute_tps |
| 9 | +from elf.evaluation import dice_score |
| 10 | +from skimage.measure import label |
| 11 | +from tqdm import tqdm |
| 12 | + |
| 13 | + |
| 14 | +def _postprocess(data, apply_cc, min_component_size): |
| 15 | + if apply_cc: |
| 16 | + data = label(data) |
| 17 | + ids, sizes = np.unique(data, return_counts=True) |
| 18 | + filter_ids = ids[sizes < min_component_size] |
| 19 | + data[np.isin(data, filter_ids)] = 0 |
| 20 | + return data |
| 21 | + |
| 22 | + |
| 23 | +def _single_az_evaluation(seg, gt, apply_cc, min_component_size): |
| 24 | + assert seg.shape == gt.shape, f"{seg.shape}, {gt.shape}" |
| 25 | + seg = _postprocess(seg, apply_cc, min_component_size) |
| 26 | + gt = _postprocess(gt, apply_cc, min_component_size) |
| 27 | + |
| 28 | + dice = dice_score(seg > 0, gt > 0) |
| 29 | + |
| 30 | + n_true, n_matched, n_pred, scores = _compute_scores(seg, gt, criterion="iou", ignore_label=0) |
| 31 | + tp = _compute_tps(scores, n_matched, threshold=0.5) |
| 32 | + fp = n_pred - tp |
| 33 | + fn = n_true - tp |
| 34 | + |
| 35 | + return {"tp": tp, "fp": fp, "fn": fn, "dice": dice} |
| 36 | + |
| 37 | + |
| 38 | +# TODO further post-processing? |
| 39 | +def az_evaluation( |
| 40 | + seg_paths: List[str], |
| 41 | + gt_paths: List[str], |
| 42 | + seg_key: str, |
| 43 | + gt_key: str, |
| 44 | + apply_cc: bool = True, |
| 45 | + min_component_size: int = 100, # TODO |
| 46 | +) -> pd.DataFrame: |
| 47 | + """Evaluate active zone segmentations against ground-truth annotations. |
| 48 | +
|
| 49 | + Args: |
| 50 | + seg_paths: The filepaths to the segmentations, stored as hd5 files. |
| 51 | + gt_paths: The filepaths to the ground-truth annotatons, stored as hdf5 files. |
| 52 | + seg_key: The internal path to the data in the segmentation hdf5 file. |
| 53 | + gt_key: The internal path to the data in the ground-truth hdf5 file. |
| 54 | + apply_cc: Whether to apply connected components before evaluation. |
| 55 | + min_component_size: Minimum component size for filtering the segmentation and annotations before evaluation. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + A data frame with the evaluation results per tomogram. |
| 59 | + """ |
| 60 | + assert len(seg_paths) == len(gt_paths) |
| 61 | + |
| 62 | + results = { |
| 63 | + "tomo_name": [], |
| 64 | + "tp": [], |
| 65 | + "fp": [], |
| 66 | + "fn": [], |
| 67 | + "dice": [], |
| 68 | + } |
| 69 | + for seg_path, gt_path in tqdm(zip(seg_paths, gt_paths), total=len(seg_paths), desc="Run AZ Eval"): |
| 70 | + with h5py.File(seg_path, "r") as f: |
| 71 | + seg = f[seg_key][:] |
| 72 | + with h5py.File(gt_path, "r") as f: |
| 73 | + gt = f[gt_key][:] |
| 74 | + # TODO more post-processing params |
| 75 | + result = _single_az_evaluation(seg, gt, apply_cc, min_component_size) |
| 76 | + results["tomo_name"].append(os.path.basename(seg_path)) |
| 77 | + for res in ("tp", "fp", "fn", "dice"): |
| 78 | + results[res].append(result[res]) |
| 79 | + return pd.DataFrame(results) |
0 commit comments