Skip to content

Commit bf3ed4d

Browse files
committed
Additional test for DivisionRate
1 parent a9f088b commit bf3ed4d

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

pycellin/graph/features/tracking.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,17 @@ def compute( # type: ignore[override]
404404
If the cell or cycle is not in the lineage.
405405
"""
406406
if self.use_div_time:
407+
if noi not in lineage.nodes:
408+
if isinstance(lineage, CellLineage):
409+
lin_txt = "Cell"
410+
elif isinstance(lineage, CycleLineage):
411+
lin_txt = "Cycle"
412+
else:
413+
raise TypeError(
414+
f"Lineage must be of type CellLineage or CycleLineage, "
415+
f"not {type(lineage)}."
416+
)
417+
raise KeyError(f"{lin_txt} {noi} not in the lineage.")
407418
try:
408419
div_time = lineage.nodes[noi]["division_time"]
409420
except KeyError:

tests/graph/features/test_tracking.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,39 @@ def test_division_rate_gap(cell_lin, feat_cell_lin):
368368
calculator.compute(Data({}), cell_lin, noi=99)
369369

370370

371+
def test_division_rate_from_division_time(cell_lin, feat_cell_lin):
372+
"""Test DivisionRate from DivisionTime."""
373+
calculator = DivisionRate(feat_cell_lin, use_div_time=True)
374+
# Root.
375+
noi = 1
376+
cell_lin.nodes[noi]["division_time"] = 10
377+
assert calculator.compute(Data({}), cell_lin, noi) == 1 / 10
378+
# Divisions.
379+
noi = 2
380+
cell_lin.nodes[noi]["division_time"] = 20
381+
assert calculator.compute(Data({}), cell_lin, noi) == 1 / 20
382+
noi = 4
383+
cell_lin.nodes[noi]["division_time"] = 40
384+
assert calculator.compute(Data({}), cell_lin, noi) == 1 / 40
385+
noi = 14
386+
cell_lin.nodes[noi]["division_time"] = 140
387+
assert calculator.compute(Data({}), cell_lin, noi) == 1 / 140
388+
# Leaves.
389+
noi = 6
390+
cell_lin.nodes[noi]["division_time"] = 60
391+
assert calculator.compute(Data({}), cell_lin, noi) == 1 / 60
392+
noi = 10
393+
cell_lin.nodes[noi]["division_time"] = 100
394+
assert calculator.compute(Data({}), cell_lin, noi) == 1 / 100
395+
# Intermediate nodes.
396+
noi = 11
397+
cell_lin.nodes[noi]["division_time"] = 110
398+
assert calculator.compute(Data({}), cell_lin, noi) == 1 / 110
399+
# Non-existent node.
400+
with pytest.raises(KeyError, match="Cell 99 not in the lineage."):
401+
calculator.compute(Data({}), cell_lin, noi=99)
402+
403+
371404
def test_division_rate_cycle_lin_default_time_step(cell_lin, feat_cycle_lin):
372405
"""Test DivisionRate with CycleLineage and default time step."""
373406
calculator = DivisionRate(feat_cycle_lin)

0 commit comments

Comments
 (0)