Skip to content

Commit

Permalink
Add CTC fiji plugin export
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinpape committed Aug 27, 2024
1 parent 86f2ff5 commit 8471613
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions scripts/measure_for_automatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,79 @@ def _get_tracks_to_isbi():
def _get_tracks_df():
tracks_path = "automatic_tracks.csv"
segmentation = imageio.imread(os.path.join(ROOT, "tracking_result.tif"))

if os.path.exists(tracks_path):
track_df = pd.read_csv(tracks_path)
else:
track_df = _get_tracks_to_isbi()
return segmentation, track_df


def check_consecutive(instances):
instance_ids = np.unique(instances)[1:]

id_list = []
for idx in instance_ids:
frames = np.unique(np.where(instances == idx)[0])
consistent_instance = (sorted(frames) == list(range(min(frames), max(frames) + 1)))
if not consistent_instance:
id_list.append(idx)

return id_list


def fix_gt(gt, gt_df):
if os.path.exists("./tracking_gt_corrected.tif"):
return imageio.imread("./tracking_gt_corrected.tif")

broken_ids = check_consecutive(gt)
for idd in broken_ids:
print(gt_df[gt_df["Cell_ID"] == idd])

import napari
v = napari.Viewer()
v.add_labels(gt)
napari.run()


# Code to export for compatibility with the Fiji CTC plugin.
# Should refactor this to somewhere as it might prove useful.
def export_gt_for_ctc(seg, df, folder):
tra_folder = os.path.join(folder, "TRA")
os.makedirs(tra_folder, exist_ok=True)
seg_folder = os.path.join(folder, "SEG")
os.makedirs(seg_folder, exist_ok=True)

df = df[["Cell_ID", "Start", "End", "Parent_ID"]]
df = df.to_csv(os.path.join(tra_folder, "man_track.txt"), index=False, header=False, sep=" ")

for i, frame in enumerate(seg):
imageio.imwrite(os.path.join(tra_folder, f"man_track{i:03}.tif"), frame.astype("uint16"))
imageio.imwrite(os.path.join(seg_folder, f"man_seg{i:03}.tif"), frame.astype("uint16"))


def export_for_ctc(seg, df, folder):
os.makedirs(folder, exist_ok=True)

df = df[["Cell_ID", "Start", "End", "Parent_ID"]]
df = df.to_csv(os.path.join(folder, "res_track.txt"), index=False, header=False, sep=" ")

for i, frame in enumerate(seg):
imageio.imwrite(os.path.join(folder, f"mask{i:03}.tif"), frame.astype("uint16"))


def _get_metrics_for_autotrack(segmentation, seg_df):
# NOTE: for ground-truth
gt = imageio.imread(os.path.join(ROOT, "tracking_gt_corrected.tif"))
gt_nodes = _get_node_attributes(gt)

gt_df = pd.read_csv("gt_tracks.csv")

# export_for_ctc(segmentation, seg_df, "seg")
# export_gt_for_ctc(gt, gt_df, "gt")
# return

gt_nodes = _get_node_attributes(gt)
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")
Expand Down

0 comments on commit 8471613

Please sign in to comment.