Skip to content

Commit

Permalink
one more check for clusters to ensure we fail if clusters is not a ve…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
sjspielman committed Sep 25, 2024
1 parent 9b90c0a commit c38ff0a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/rOpenScPCA/R/evaluate-clusters.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ calculate_stability <- function(

# check clusters and matrix compatibility
stopifnot(
"The provided clusters variable must be a vector" = is.factor(clusters) || is.vector(clusters),
"The number of rows in the matrix must equal the length of the clusters vector." =
nrow(pca_matrix) == length(clusters)
)
Expand All @@ -229,8 +230,11 @@ calculate_stability <- function(
dplyr::slice(1) |>
dplyr::select(!c("cell_id", "cluster")) |>
dplyr::mutate(
replicate = i, # define this variable here to ensure it's numeric
ari = ari
# define this variable here to ensure it's numeric
replicate = i,
ari = ari,
# ensure these columns come first
.before = "algorithm"
)

return(ari_df)
Expand Down
22 changes: 22 additions & 0 deletions packages/rOpenScPCA/tests/testthat/test-evaluate-clusters.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ test_that("calculate_stability errors as expected", {
})

expect_error({
# cluster_df not a vector
calculate_stability(test_mat, cluster_df)
})


# the next test ensures we fail even if the length of clusters
# is the same as the nrow in test_mat

# make it smaller for test
test_mat_small <- test_mat[1:5, ]

# make df with same length as rows in test_mat
bad_df <- data.frame(
cluster = sample(1:2, nrow(test_mat_small), replace = T),
cell_id = rownames(test_mat_small),
col1 = 1,
col2 = 1,
col3 = 1
)

expect_error({
# cluster_df not a vector, even though df length matches test mat
calculate_stability(test_mat_small, bad_df)
})
})

0 comments on commit c38ff0a

Please sign in to comment.