Skip to content

Commit

Permalink
knn sometimes not unique
Browse files Browse the repository at this point in the history
  • Loading branch information
katosh committed Jun 3, 2024
1 parent 45c88c6 commit bea702c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
7 changes: 6 additions & 1 deletion R/extract_pairs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
11 changes: 1 addition & 10 deletions tests/testthat/test-extract_pairs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})

0 comments on commit bea702c

Please sign in to comment.