Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/lineagetree/lineage_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def load(clf, fname: str):
)
if not hasattr(lT, "time_resolution"):
lT.time_resolution = 1
if not hasattr(lT, "spatial_resolution"):
lT.spatial_resolution = np.ones(3)

return lT

Expand Down Expand Up @@ -143,6 +145,7 @@ def __init__(
pos: dict[int, Iterable] | None = None,
name: str | None = None,
root_leaf_value: Sequence | None = None,
spatial_resolution: Sequence | None = None,
**kwargs,
):
"""Create a LineageTree object from minimal information, without reading from a file.
Expand Down Expand Up @@ -312,3 +315,21 @@ def __init__(
self._custom_property_list.append(name)
if not hasattr(self, "_comparisons"):
self._comparisons = {}

spatia_dimension = (
len(self.pos[next(iter(self.nodes))])
if self.nodes and self.pos
else 3
)
if self.nodes and spatial_resolution is not None:
if len(spatial_resolution) == spatia_dimension:
self.spatial_resolution = np.array(spatial_resolution)
else:
raise ValueError(
"The spatial resolution should have the same dimension as the one of the positions:\n"
f"{len(spatial_resolution)=}, spatial dimension={spatia_dimension}"
)
else:
self.spatial_resolution = spatial_resolution or np.ones(
spatia_dimension
)
2 changes: 1 addition & 1 deletion src/lineagetree/measure/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_idx3d(lT: LineageTree, t: int) -> tuple[KDTree, np.ndarray]:
data_corres = {}
data = []
for i, C in enumerate(to_check_lT):
data.append(tuple(lT.pos[C]))
data.append(tuple(lT.pos[C] * lT.spatial_resolution))
data_corres[i] = C
idx3d = KDTree(data)
lT.kdtrees[t] = idx3d
Expand Down
56 changes: 56 additions & 0 deletions tests/test_lineageTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,62 @@ def test_bad_leaf():
)


def test_spatial_resolution():
successor_dict = {
1: (2,),
2: (3, 100),
100: (101,),
0: (1,),
10: (0,),
5: (),
3: (),
4: (),
101: (),
}
test_lT = LineageTree(
successor=successor_dict,
pos=dict(
zip(
successor_dict.keys(),
np.random.random((len(successor_dict), 3)),
strict=True,
)
),
spatial_resolution=[1, 1, 1],
)
assert (test_lT.spatial_resolution == [1, 1, 1]).all()


def test_wrong_spatial_resolution():
successor_dict = {
1: (2,),
2: (3, 100),
100: (101,),
0: (1,),
10: (0,),
5: (),
3: (),
4: (),
101: (),
}
with pytest.raises(ValueError) as excinfo:
LineageTree(
successor=successor_dict,
pos=dict(
zip(
successor_dict.keys(),
np.random.random((len(successor_dict), 3)),
strict=True,
)
),
spatial_resolution=[1, 1],
)
assert (
str(excinfo.value)
== "The spatial resolution should have the same dimension as the one of the positions:\nlen(spatial_resolution)=2, spatial dimension=3"
)


def test_multiple_predecessors():
with pytest.raises(ValueError) as excinfo:
LineageTree(successor={2: (1,), 3: (2,), 4: (2,)})
Expand Down
Loading