Skip to content

Commit 45c96ca

Browse files
committed
added random landmarking precomputed distance test
1 parent 2d65034 commit 45c96ca

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/test_random_landmarking.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,48 @@ def test_random_landmarking_with_precomputed_affinity():
445445
assert G.landmark_op.shape == (n_landmark, n_landmark)
446446

447447

448+
def test_random_landmarking_with_precomputed_distance():
449+
"""Random landmarking should work with precomputed distance matrices"""
450+
dist = np.array(
451+
[
452+
[0, 1, 4, 4, 4, 4],
453+
[1, 0, 4, 4, 4, 4],
454+
[4, 4, 0, 1, 4, 4],
455+
[4, 4, 1, 0, 4, 4],
456+
[4, 4, 4, 4, 0, 1],
457+
[4, 4, 4, 4, 1, 0],
458+
]
459+
)
460+
461+
n_landmark = 3
462+
random_state = 42
463+
464+
G = graphtools.Graph(
465+
dist,
466+
precomputed="distance",
467+
n_landmark=n_landmark,
468+
random_landmarking=True,
469+
random_state=random_state,
470+
bandwidth=1, # deterministic affinity: exp(-dist)
471+
decay=1,
472+
thresh=0,
473+
knn=3,
474+
)
475+
476+
# Trigger landmark construction
477+
_ = G.landmark_op
478+
479+
rng = np.random.default_rng(random_state)
480+
landmark_indices = rng.choice(dist.shape[0], n_landmark, replace=False)
481+
expected_clusters = np.asarray(
482+
G.kernel[:, landmark_indices].argmax(axis=1)
483+
).reshape(-1)
484+
485+
assert np.array_equal(G.clusters, expected_clusters)
486+
assert G.transitions.shape == (dist.shape[0], n_landmark)
487+
assert G.landmark_op.shape == (n_landmark, n_landmark)
488+
489+
448490
#############
449491
# Test API
450492
#############

0 commit comments

Comments
 (0)