Skip to content

Commit

Permalink
Add scripts for evaluating automatic tracking results
Browse files Browse the repository at this point in the history
  • Loading branch information
anwai98 committed Aug 27, 2024
1 parent 1a7a818 commit 7fc593b
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions scripts/measure_for_automatic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import os

import json
import h5py
import numpy as np
import pandas as pd
import imageio.v3 as imageio

from deepcell_tracking import isbi_utils

from traccuracy import run_metrics
from traccuracy.matchers import CTCMatcher
from traccuracy._tracking_graph import TrackingGraph
from traccuracy.metrics import CTCMetrics, DivisionMetrics
from traccuracy.loaders._ctc import _get_node_attributes, ctc_to_graph, _check_ctc


ROOT = "/scratch/share/cidas/cca/experiments/results_for_automatic_tracking"


def _get_tracks_to_isbi():
with open(os.path.join(ROOT, "tracking_result.json"), "r") as f:
data = json.load(f)

segmentation = imageio.imread(os.path.join(ROOT, "tracking_result.tif"))

# we convert the parent ids to frames
track_info = {}
for parent_id, daughter_ids in data.items():
parent_id = int(parent_id)
frames = np.where(segmentation == parent_id)[0]

# store the parent's track information
track_info[parent_id] = {
'frames': list(np.unique(frames)),
'daughters': list(daughter_ids),
'frame_div': frames.max() + 1,
'parent': None,
'label': parent_id,
}

# next, store the daughter's track information
for daughter_id in daughter_ids:
frames = np.where(segmentation == daughter_id)[0]
track_info[daughter_id] = {
'frames': list(np.unique(frames)),
'daughters': [],
'frame_div': None,
'parent': parent_id,
'label': daughter_id
}

track_df = isbi_utils.trk_to_isbi(track_info)
return segmentation, track_df


def _get_metrics_for_autotrack(segmentation, seg_df):
# NOTE: for ground-truth
with h5py.File(os.path.join(ROOT, "tracking_micro_sam.h5")) as f:
gt = f['labels'][:]

gt_nodes = _get_node_attributes(gt)
gt_df = pd.read_csv(os.path.join(ROOT, "gt_tracks.csv"))
gt_G = ctc_to_graph(gt_df, gt_nodes)
_check_ctc(gt_df, gt_nodes, gt)
gt_T = TrackingGraph(gt_G, segmentation=gt, name="DynamicNuclearNet-GT")

# NOTE: for segmentation results
# calculate node attributes for each detection
seg_nodes = _get_node_attributes(segmentation)
seg_G = ctc_to_graph(seg_df, seg_nodes)
seg_T = TrackingGraph(seg_G, segmentation=segmentation, name="DynamicNuclearNet-autotracking")

ctc_results = run_metrics(
gt_data=gt_T,
pred_data=seg_T,
matcher=CTCMatcher(),
metrics=[CTCMetrics(), DivisionMetrics(max_frame_buffer=0)]
)
print(ctc_results)


def main():
segmentation, seg_df = _get_tracks_to_isbi()
_get_metrics_for_autotrack(segmentation, seg_df)


main()

0 comments on commit 7fc593b

Please sign in to comment.