Skip to content

Commit

Permalink
Report the names of the references in the combineRecomputedResults ou…
Browse files Browse the repository at this point in the history
…tput.
  • Loading branch information
LTLA committed Sep 6, 2024
1 parent 2e2f979 commit 4dd3c2b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions R/classifySingleR.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ classifySingleR <- function(
}

results <- vector("list", length(trained))
names(results) <- names(trained)
for (l in seq_along(results)) {
results[[l]] <- .classify_internals(
test=test,
Expand Down
6 changes: 2 additions & 4 deletions R/combineRecomputedResults.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#' For any given cell, entries of this matrix are only non-\code{NA} for the assigned label in each reference;
#' scores are not recomputed for the other labels.
#' \item \code{labels}, a character vector containing the per-cell combined label across references.
#' \item \code{references}, an integer vector specifying the reference from which the combined label was derived.
#' \item \code{reference}, an integer vector specifying the reference from which the combined label was derived.
#' \item \code{orig.results}, a DataFrame containing \code{results}.
#' }
#' It may also contain \code{pruned.labels} if these were also present in \code{results}.
Expand Down Expand Up @@ -206,15 +206,13 @@ combineRecomputedResults <- function(
}

output <- DataFrame(labels=chosen.label, row.names=rownames(results[[1]]))

if (has.pruned) {
output$pruned.labels <- chosen.pruned
}

output$reference <- chosen

if (is.null(names(results))) {
names(results) <- sprintf("ref%i", seq_along(results))
names(results) <- sprintf("ref%s", seq_along(results))
}
output$orig.results <- do.call(DataFrame, lapply(results, I))

Expand Down
1 change: 1 addition & 0 deletions R/trainSingleR.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ trainSingleR <- function(
}

output <- vector("list", length(ref))
names(output) <- names(ref)
for (l in seq_along(ref)) {
curref <- .to_clean_matrix(ref[[l]], assay.type, check.missing, msg="ref", BPPARAM=BPPARAM)

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,18 @@ test_that("classifySingleR works with multiple references", {

mtrain <- trainSingleR(list(training1, training2), list(training1$label, training2$label))
out <- classifySingleR(test, mtrain)
expect_identical(names(out$orig.results), c("ref1", "ref2"))
expect_true(all(out$reference %in% 1:2))

ref1 <- classifySingleR(test, mtrain[[1]])
ref2 <- classifySingleR(test, mtrain[[2]])
expect_identical(out, combineRecomputedResults(list(ref1, ref2), test, mtrain))

# Preserves names of the references themselves.
mtrain <- trainSingleR(list(foo=training1, bar=training2), list(training1$label, training2$label))
out <- classifySingleR(test, mtrain)
expect_identical(names(out$orig.results), c("foo", "bar"))
expect_true(all(out$reference %in% 1:2))
})

test_that("classifySingleR behaves with silly inputs", {
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-train.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,17 @@ test_that("trainSingleR behaves with multiple references, plus recomputation", {
training1 <- training1[sample(nrow(training1)),]
rownames(training1) <- rownames(training)

set.seed(1000)
ref1 <- trainSingleR(training1, training1$label)
ref2 <- trainSingleR(training2, training2$label)

set.seed(1000)
out <- trainSingleR(list(training1, training2), list(training1$label, training2$label), recompute=TRUE)
out <- trainSingleR(list(training1, training2), list(training1$label, training2$label))

except.built <- setdiff(names(ref1), "built")
expect_identical(ref1[except.built], out[[1]][except.built])
expect_identical(ref2[except.built], out[[2]][except.built])

# Same result with names.
out <- trainSingleR(list(foo=training1, bar=training2), list(training1$label, training2$label))
expect_identical(names(out), c("foo", "bar"))
})

test_that("trainSingleR behaves with aggregation turned on", {
Expand Down

0 comments on commit 4dd3c2b

Please sign in to comment.