From e36848dc2678bc63e50943478298b9f08490ce0b Mon Sep 17 00:00:00 2001 From: BadPrograms Date: Mon, 15 Dec 2025 13:33:23 +0100 Subject: [PATCH 1/4] fix_k_neighbours --- src/lineagetree/measure/spatial.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/lineagetree/measure/spatial.py b/src/lineagetree/measure/spatial.py index 417ac05..e8867b1 100644 --- a/src/lineagetree/measure/spatial.py +++ b/src/lineagetree/measure/spatial.py @@ -172,27 +172,34 @@ def compute_k_nearest_neighbours( dict mapping int to set of int dictionary that maps a node id to its `k` nearest neighbors + dict mapping int to set of float + dictionary that maps + a node id to the distances of its `k` nearest neighbors """ lT.kn_graph = {} - for t in set(lT._time.values()): - nodes = lT.time_nodes[t] + k = k + 1 + for t, nodes in lT.time_nodes.items(): if 1 < len(nodes): use_k = k if k < len(nodes) else len(nodes) idx3d, nodes = lT.get_idx3d(t) pos = [lT.pos[c] for c in nodes] - _, neighbs = idx3d.query(pos, use_k) + distances, neighbs = idx3d.query(pos, use_k) out = dict( zip( nodes, - map(set, nodes[neighbs]), + map(set, nodes[[neighb[1:] for neighb in neighbs]]), + strict=True, + ) + ) + out_distances = dict( + zip( + nodes, + map(set, [dist[1:] for dist in distances]), strict=True, ) ) lT.kn_graph.update(out) - else: - n = nodes.pop - lT.kn_graph.update({n: {n}}) - return lT.kn_graph + return lT.kn_graph, out_distances def compute_spatial_edges( From c329ec5dc21b98cfcab45de0803c9054091db455 Mon Sep 17 00:00:00 2001 From: BadPrograms Date: Mon, 15 Dec 2025 13:49:43 +0100 Subject: [PATCH 2/4] fix tests --- src/lineagetree/measure/spatial.py | 4 +++- tests/test_lineageTree.py | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lineagetree/measure/spatial.py b/src/lineagetree/measure/spatial.py index e8867b1..c6949bf 100644 --- a/src/lineagetree/measure/spatial.py +++ b/src/lineagetree/measure/spatial.py @@ -177,6 +177,7 @@ def compute_k_nearest_neighbours( a node id to the distances of its `k` nearest neighbors """ lT.kn_graph = {} + lT.kn_distances = {} k = k + 1 for t, nodes in lT.time_nodes.items(): if 1 < len(nodes): @@ -199,7 +200,8 @@ def compute_k_nearest_neighbours( ) ) lT.kn_graph.update(out) - return lT.kn_graph, out_distances + lT.kn_distances.update(out_distances) + return lT.kn_graph, lT.kn_distances def compute_spatial_edges( diff --git a/tests/test_lineageTree.py b/tests/test_lineageTree.py index bd0d54e..26594fd 100644 --- a/tests/test_lineageTree.py +++ b/tests/test_lineageTree.py @@ -623,16 +623,25 @@ def test_spatial_density(): def test_compute_k_nearest_neighbours(): - assert lT1.compute_k_nearest_neighbours()[169994] == { + assert lT1.compute_k_nearest_neighbours()[0][169994] == { 108588, 114722, 129276, 139163, 148361, 165681, - 169994, 178396, } + print(lT1.compute_k_nearest_neighbours()[1][169994]) + assert lT1.compute_k_nearest_neighbours()[1][169994] == { + np.float64(34.39062611344147), + np.float64(50.72494649405221), + np.float64(58.97813932274096), + np.float64(71.4891082395894), + np.float64(80.50930277501162), + np.float64(171.6539282020323), + np.float64(173.50879512331872), + } def test_compute_spatial_edges(): From a79066093c04e8b19fe6459a6683e02918783fa2 Mon Sep 17 00:00:00 2001 From: BadPrograms Date: Mon, 15 Dec 2025 13:57:20 +0100 Subject: [PATCH 3/4] remove print --- tests/test_lineageTree.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_lineageTree.py b/tests/test_lineageTree.py index 26594fd..16f9348 100644 --- a/tests/test_lineageTree.py +++ b/tests/test_lineageTree.py @@ -632,7 +632,6 @@ def test_compute_k_nearest_neighbours(): 165681, 178396, } - print(lT1.compute_k_nearest_neighbours()[1][169994]) assert lT1.compute_k_nearest_neighbours()[1][169994] == { np.float64(34.39062611344147), np.float64(50.72494649405221), From 8ad2633654d036595be880b3df62b1dd0d024ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Guignard?= Date: Tue, 16 Dec 2025 14:21:21 +0100 Subject: [PATCH 4/4] updating spatial distance so it makes sense --- src/lineagetree/measure/spatial.py | 4 ++-- tests/test_lineageTree.py | 34 ++++++++++++++---------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/lineagetree/measure/spatial.py b/src/lineagetree/measure/spatial.py index c6949bf..52406c9 100644 --- a/src/lineagetree/measure/spatial.py +++ b/src/lineagetree/measure/spatial.py @@ -188,14 +188,14 @@ def compute_k_nearest_neighbours( out = dict( zip( nodes, - map(set, nodes[[neighb[1:] for neighb in neighbs]]), + nodes[neighbs[:, 1:]], strict=True, ) ) out_distances = dict( zip( nodes, - map(set, [dist[1:] for dist in distances]), + distances[:, 1:], strict=True, ) ) diff --git a/tests/test_lineageTree.py b/tests/test_lineageTree.py index 16f9348..c7eb6f1 100644 --- a/tests/test_lineageTree.py +++ b/tests/test_lineageTree.py @@ -623,24 +623,22 @@ def test_spatial_density(): def test_compute_k_nearest_neighbours(): - assert lT1.compute_k_nearest_neighbours()[0][169994] == { - 108588, - 114722, - 129276, - 139163, - 148361, - 165681, - 178396, - } - assert lT1.compute_k_nearest_neighbours()[1][169994] == { - np.float64(34.39062611344147), - np.float64(50.72494649405221), - np.float64(58.97813932274096), - np.float64(71.4891082395894), - np.float64(80.50930277501162), - np.float64(171.6539282020323), - np.float64(173.50879512331872), - } + assert ( + lT1.compute_k_nearest_neighbours()[0][169994] + == [178396, 139163, 165681, 148361, 129276, 114722, 108588] + ).all() + assert np.allclose( + lT1.compute_k_nearest_neighbours()[1][169994], + [ + 34.39062611, + 50.72494649, + 58.97813932, + 71.48910824, + 80.50930278, + 171.6539282, + 173.50879512, + ], + ) def test_compute_spatial_edges():