Skip to content

Commit

Permalink
is_valid_lineage should check if the daughter ID is valid. (#70)
Browse files Browse the repository at this point in the history
* Check for invalid daughters (KeyError)

* Add simple test for invalid daughter ID.

* Bump version to 0.4.5
  • Loading branch information
willgraf committed Aug 18, 2021
1 parent fe53ddc commit 526b65b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
16 changes: 11 additions & 5 deletions deepcell_tracking/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import re
import tarfile
import tempfile
import warnings

import numpy as np

Expand Down Expand Up @@ -481,13 +482,18 @@ def is_valid_lineage(lineage):
Returns:
bool: Whether or not the lineage is valid.
"""
for cell_lineage in lineage.values():
for cell_label, cell_lineage in lineage.items():
# Get last frame of parent
last_parent_frame = cell_lineage['frames'][-1]

for daughter in cell_lineage['daughters']:
# get first frame of daughter
first_daughter_frame = lineage[daughter]['frames'][0]
try:
# get first frame of daughter
first_daughter_frame = lineage[daughter]['frames'][0]
except KeyError:
warnings.warn('lineage {} has invalid daughters: {}'.format(
cell_label, cell_lineage['daughters']))
return False

# Check that daughter's start frame is one larger than parent end frame
if first_daughter_frame - last_parent_frame != 1:
Expand Down Expand Up @@ -660,13 +666,13 @@ def __init__(self, path=None, tracked_data=None,

def _correct_lineages(self):
"""Ensure sequential labels for all batches"""
new_lineages = {}
new_lineages = []
for batch in range(self.y.shape[0]):

y_relabel, new_lineage = relabel_sequential_lineage(
self.y[batch], self.lineages[batch])

new_lineages[batch] = new_lineage
new_lineages.append(new_lineage)
self.y[batch] = y_relabel

self.lineages = new_lineages
Expand Down
5 changes: 5 additions & 0 deletions deepcell_tracking/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ def test_is_valid_lineage(self):
bad_lineage[2]['frames'] = [2]
assert not utils.is_valid_lineage(bad_lineage)

# change one of cell 0's daughters to an invalid ID.
bad_lineage = copy.copy(lineage)
bad_lineage[0]['daughters'][0] = 3
assert not utils.is_valid_lineage(bad_lineage)

def test_get_image_features(self):
num_labels = 3
y = get_annotated_image(num_labels=num_labels, sequential=True)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
readme = f.read()


VERSION = '0.4.4'
VERSION = '0.4.5'
NAME = 'DeepCell_Tracking'
DESCRIPTION = 'Tracking cells and lineage with deep learning.'
LICENSE = 'LICENSE'
Expand Down

0 comments on commit 526b65b

Please sign in to comment.