Skip to content

Commit

Permalink
Maintain both path and memory functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Ding committed Aug 4, 2021
1 parent 7cc1317 commit de1ff5c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
63 changes: 45 additions & 18 deletions deepcell_tracking/isbi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def trk_to_isbi(track, path=None):
"""
if path is not None:
print("UserWarning: The `path` argument is deprecated.")
text_file = open(path, 'w')

isbi = []
for label in track:
Expand All @@ -63,13 +64,22 @@ def trk_to_isbi(track, path=None):
parent_frames = track[parent]['frames']
if parent_frames[-1] != first_frame - 1:
parent = 0

line = {'Cell_ID': label,
'Start': first_frame,
'End': last_frame,
'Parent_ID': parent}

isbi.append(line)
if path is not None:
line = '{cell_id} {start} {end} {parent}\n'.format(
cell_id=label,
start=first_frame,
end=last_frame,
parent=parent
)
text_file.write(line)

isbi_dict = {'Cell_ID': label,
'Start': first_frame,
'End': last_frame,
'Parent_ID': parent}
isbi.append(isbi_dict)
if path is not None:
text_file.close()
return isbi


Expand Down Expand Up @@ -191,7 +201,7 @@ def match_nodes(gt, res):
return gtcells, rescells


def txt_to_graph(data, node_key=None):
def txt_to_graph(path=None, node_key=None, data=None):
"""Create a Graph from array of ISBI info.
Args:
Expand All @@ -204,7 +214,12 @@ def txt_to_graph(data, node_key=None):
Raises:
ValueError: If the Parent_ID is not in any previous frames.
"""
df = pd.DataFrame(data)
if path is not None:
print("UserWarning: The `path` argument is deprecated.")
names = ['Cell_ID', 'Start', 'End', 'Parent_ID']
df = pd.read_csv(path, header=None, sep=' ', names=names)
else:
df = pd.DataFrame(data)

if node_key is not None:
df[['Cell_ID', 'Parent_ID']] = df[['Cell_ID', 'Parent_ID']].replace(node_key)
Expand Down Expand Up @@ -343,13 +358,11 @@ def benchmark_division_performance(trk_gt, trk_res, path_gt=None, path_res=None)
path_gt (path): Desired destination path for the GT ISBI-style .txt
file (deprecated).
path_res (path): Desired destination path for the result ISBI-style
.txt file (depracated).
.txt file (deprecated).
Returns:
dict: Dictionary of all division statistics.
"""
if path_gt or path_res is not None:
print("UserWarning: The 'path_gt` and 'path_res' arguments are deprecated.")
# Identify nodes with parent attribute
# Load both .trk
trks = load_trks(trk_gt)
Expand All @@ -358,22 +371,36 @@ def benchmark_division_performance(trk_gt, trk_res, path_gt=None, path_res=None)
lineage_res, _, y_res = trks['lineages'][0], trks['X'], trks['y']

# Produce ISBI style array to work with
gt = trk_to_isbi(lineage_gt)
res = trk_to_isbi(lineage_res)
if path_gt or path_res is not None:
print("UserWarning: The 'path_gt` and 'path_res' arguments are deprecated.")
trk_to_isbi(lineage_gt, path_gt)
trk_to_isbi(lineage_res, path_res)
else:
gt = trk_to_isbi(lineage_gt)
res = trk_to_isbi(lineage_res)

# Match up labels in GT to Results to allow for direct comparisons
cells_gt, cells_res = match_nodes(y_gt, y_res)

if len(np.unique(cells_res)) < len(np.unique(cells_gt)):
node_key = {r: g for g, r in zip(cells_gt, cells_res)}
# node_key maps gt nodes onto resnodes so must be applied to gt
G_res = txt_to_graph(res, node_key=node_key)
G_gt = txt_to_graph(gt)
if path_gt or path_res is not None:
G_res = txt_to_graph(path=path_res, node_key=node_key)
G_gt = txt_to_graph(path=path_gt)
else:
G_res = txt_to_graph(data=res, node_key=node_key)
G_gt = txt_to_graph(data=gt)

div_results = classify_divisions(G_gt, G_res)
else:
node_key = {g: r for g, r in zip(cells_gt, cells_res)}
G_res = txt_to_graph(res)
G_gt = txt_to_graph(gt, node_key=node_key)
if path_gt or path_res is not None:
G_res = txt_to_graph(path=path_res)
G_gt = txt_to_graph(path=path_gt, node_key=node_key)
else:
G_res = txt_to_graph(data=res)
G_gt = txt_to_graph(data=gt, node_key=node_key)
div_results = classify_divisions(G_gt, G_res)

return div_results
2 changes: 1 addition & 1 deletion deepcell_tracking/isbi_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_txt_to_graph(self):
{'Cell_ID': 4, 'Start': 3, 'End': 3, 'Parent_ID': 2},
{'Cell_ID': 5, 'Start': 3, 'End': 3, 'Parent_ID': 4}]

G = isbi_utils.txt_to_graph(data)
G = isbi_utils.txt_to_graph(data=data)
for d in data:
node_ids = ['{}_{}'.format(d["Cell_ID"], t)
for t in range(d["Start"], d["End"] + 1)]
Expand Down

0 comments on commit de1ff5c

Please sign in to comment.