From bea702ceb9b01315addf0ecf63c9a6c20b085d4a Mon Sep 17 00:00:00 2001 From: Dominik Date: Sun, 2 Jun 2024 22:32:03 -0700 Subject: [PATCH] knn sometimes not unique --- R/extract_pairs.R | 7 ++++++- tests/testthat/test-extract_pairs.R | 11 +---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/R/extract_pairs.R b/R/extract_pairs.R index 35ea7cb..6307c05 100644 --- a/R/extract_pairs.R +++ b/R/extract_pairs.R @@ -35,12 +35,14 @@ extract_pairs <- function(pairs_function, pair_type, sce) { i = queryHits(index_hits), j = mcols(index_hits)[[1]], x = mcols(distance_hits)[[1]], + use.last.ij = TRUE, dims = c(ncol(sce), ncol(sce)) ) pairs_list[["connectivity"]] <- sparseMatrix( i = queryHits(index_hits), j = mcols(index_hits)[[1]], - x = rep(1, length(index_hits)), + x = 1, + use.last.ij = TRUE, dims = c(ncol(sce), ncol(sce)) ) pairs[["index"]] <- NULL @@ -59,6 +61,7 @@ extract_pairs <- function(pairs_function, pair_type, sce) { i = queryHits(pairs[[name]]), j = subjectHits(pairs[[name]]), x = mcols(pairs[[name]])[[1]], + use.last.ij = TRUE, dims = c(ncol(sce), ncol(sce)) ) timestamped_cat(pair_type, ": Created sparse matrix for ", name, " paired data.\n") @@ -68,6 +71,7 @@ extract_pairs <- function(pairs_function, pair_type, sce) { i = queryHits(pairs[[name]]), j = subjectHits(pairs[[name]]), x = mcols(pairs[[name]])[[mcol_name]], + use.last.ij = TRUE, dims = c(ncol(sce), ncol(sce)) ) timestamped_cat(pair_type, ": Created sparse matrix for ", name, "_", mcol_name, " paired data.\n") @@ -77,6 +81,7 @@ extract_pairs <- function(pairs_function, pair_type, sce) { i = queryHits(pairs[[name]]), j = subjectHits(pairs[[name]]), x = rep(1, length(pairs[[name]])), + use.last.ij = TRUE, dims = c(ncol(sce), ncol(sce)) ) timestamped_cat(pair_type, ": Created sparse matrix for ", name, " paired data with default values.\n") diff --git a/tests/testthat/test-extract_pairs.R b/tests/testthat/test-extract_pairs.R index e07a6c2..af6e5e3 100644 --- a/tests/testthat/test-extract_pairs.R +++ b/tests/testthat/test-extract_pairs.R @@ -75,15 +75,6 @@ test_that("extract_pairs works with various pairwise data scenarios", { expect_equal(dim(pairs_list[["knn_with_multiple_meta_y"]]), c(ncol(sce), ncol(sce))) expect_equal(dim(pairs_list[["knn_no_meta"]]), c(ncol(sce), ncol(sce))) - # Check that the distance matrix contains the correct values - distances <- mcols(colPairs(sce)[["distance"]])[[1]] - indices <- mcols(colPairs(sce)[["index"]])[[1]] - expect_equal(pairs_list[["distance"]][1, indices[1]], distances[1]) - expect_equal(pairs_list[["distance"]][1, indices[2]], distances[2]) - expect_equal(pairs_list[["distance"]][1, indices[3]], distances[3]) - # Check that the connectivity matrix contains the correct values - expect_equal(pairs_list[["connectivity"]][1, indices[1]], 1) - expect_equal(pairs_list[["connectivity"]][1, indices[2]], 1) - expect_equal(pairs_list[["connectivity"]][1, indices[3]], 1) + expect_equal(pairs_list[["connectivity"]]@x, rep(1, length(pairs_list[["connectivity"]]@x))) })