Skip to content

Commit

Permalink
fix: fix the incorrect handling of the sample parameter in `sample_…
Browse files Browse the repository at this point in the history
…motifs()` and ensure that the default `sample.size` is integer (#1568)
  • Loading branch information
krlmlr authored Nov 7, 2024
2 parents 30518c2 + 751f593 commit fd1fd9b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
21 changes: 18 additions & 3 deletions R/motifs.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ count_motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
#' of the motif (the `size` argument). By default no cuts are made.
#' @param sample.size The number of vertices to use as a starting point for
#' finding motifs. Only used if the `sample` argument is `NULL`.
#' The default is `ceiling(vcount(graph) / 10)` .
#' @param sample If not `NULL` then it specifies the vertices to use as a
#' starting point for finding motifs.
#' @return A numeric scalar, an estimate for the total number of motifs in
Expand All @@ -215,8 +216,13 @@ count_motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
#' motifs(g, 3)
#' count_motifs(g, 3)
#' sample_motifs(g, 3)
sample_motifs <- function(graph, size = 3, cut.prob = rep(0, size),
sample.size = vcount(graph) / 10, sample = NULL) {
sample_motifs <- function(
graph,
size = 3,
cut.prob = rep(0, size),
sample.size = NULL,
sample = NULL
) {
ensure_igraph(graph)
cut.prob <- as.numeric(cut.prob)
if (length(cut.prob) != size) {
Expand All @@ -226,10 +232,19 @@ sample_motifs <- function(graph, size = 3, cut.prob = rep(0, size),
)
}

if (is.null(sample)) {
if (is.null(sample.size)) {
sample.size <- ceiling(vcount(graph) / 10)
}
} else {
sample <- as_igraph_vs(graph, sample) - 1
sample.size <- 0
}

on.exit(.Call(R_igraph_finalizer))
.Call(
R_igraph_motifs_randesu_estimate, graph, as.numeric(size),
as.numeric(cut.prob), as.numeric(sample.size), as.numeric(sample)
as.numeric(cut.prob), as.numeric(sample.size), sample
)
}

Expand Down
3 changes: 2 additions & 1 deletion man/graph.motifs.est.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/sample_motifs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-motifs.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ test_that("motif finding works", {

expect_equal(m5 / m, c(NA, NA, 0.439985332979302, NA, 0.440288166730411, 0.346938775510204, 0.44159753136382, 0.452054794520548, NaN, 0.323076923076923, NaN, 0.347826086956522, NaN, NaN, NaN, NaN))
})

test_that("sample_motifs works", {
withr::local_seed(20041103)

g <- make_graph(~ A-B-C-A-D-E-F-D-C-F)
n <- vcount(g)

motif_count <- sample_motifs(g)
expect_true(0 <= motif_count && motif_count <= n*(n-1)*(n-2) / 6)

motif_count_letters <- sample_motifs(g, sample = c("C", "D", "E", "F"))
expect_true(0 <= motif_count_letters && motif_count_letters <= n*(n-1)*(n-2) / 6)

motif_count_all <- sample_motifs(g, sample = V(g))
expect_true(0 <= motif_count_all && motif_count_all <= n*(n-1)*(n-2) / 6)
})
1 change: 1 addition & 0 deletions tests/testthat/test-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ test_that("basic plot test, spheres", {

test_that("rglplot() works", {
skip_if_not_installed("rgl")
skip_if(basename(commandArgs())[[1]] == "RDcsan")

# https://stackoverflow.com/a/46320771/5489251
withr::local_envvar(RGL_USE_NULL = TRUE)
Expand Down

0 comments on commit fd1fd9b

Please sign in to comment.