diff --git a/DESCRIPTION b/DESCRIPTION index c6899a5cf0a2964fa0c48450f7c21ad01791782e..e3860381277040f83d38f8eb9c4d0b2d42f59576 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rsAnalysis Title: High-Level Tools for rsfMRI analysis -Version: 0.4 +Version: 0.4.1 Authors@R: person(given = "Christian", family = "Hohenfeld", diff --git a/R/make_random_graphs.R b/R/make_random_graphs.R index 44b0173ddedd4326abfcecda228e7a61095a56dd..dfc78a62a51c1ae8ba8da28ef24461374df6e9fe 100644 --- a/R/make_random_graphs.R +++ b/R/make_random_graphs.R @@ -6,28 +6,24 @@ #' @param calc_mean_dist (logical) If TRUE calculates mean_distance for all #' graphs. #' -#' If all reference graphs are of the same size, the random graphs will -#' have a size ranging between size / 4 and size / 3. -#' #' @return A list of tbl_graph objects of length `n`. #' @export make_random_graphs <- function(reference_graphs, n, calc_mean_dist = FALSE) { - sizes <- vapply(reference_graphs, igraph::gsize, - numeric(1), USE.NAMES = FALSE) + degree <- vapply(reference_graphs, function(x) mean(igraph::degree(x)), + numeric(1), USE.NAMES = FALSE) + degree <- round(mean(degree), 0) order <- vapply(reference_graphs, igraph::gorder, numeric(1), USE.NAMES = FALSE) order <- round(mean(order), 0) - - min_max_equal <- min(sizes) == max(sizes) + # use https://igraph.org/r/doc/sample_k_regular.html random_list <- lapply(1:n, function(x) - tidygraph::play_erdos_renyi( - n = order, - m = ifelse(min_max_equal, - sample((sizes[1] / 4):(sizes[1] / 3), 1), - sample(min(sizes):max(sizes), 1)), - directed = FALSE - ) + igraph::sample_k_regular( + no.of.nodes = order, + k = degree, + directed = FALSE, + multiple = FALSE + ) %>% tidygraph::as_tbl_graph() ) if (calc_mean_dist) { diff --git a/man/make_random_graphs.Rd b/man/make_random_graphs.Rd index d0ed7d581a3e327ed2f0e29cd99b32840c8a36b9..66d8bebbdf1647fa14d47b4e2440bd4d4e002108 100644 --- a/man/make_random_graphs.Rd +++ b/man/make_random_graphs.Rd @@ -13,10 +13,7 @@ determining the size of the random graphs.} \item{n}{(int) The amount of graphs to generate.} \item{calc_mean_dist}{(logical) If TRUE calculates mean_distance for all -graphs. - -If all reference graphs are of the same size, the random graphs will -have a size ranging between size / 4 and size / 3.} +graphs.} } \value{ A list of tbl_graph objects of length `n`. diff --git a/tests/testthat/test-make_random_graphs.R b/tests/testthat/test-make_random_graphs.R index 02b8bc512bc8841d45ef9d09e098b9df55280787..e71dc678d5f0b0b6096a56d5c25320f53b325807 100644 --- a/tests/testthat/test-make_random_graphs.R +++ b/tests/testthat/test-make_random_graphs.R @@ -1,16 +1,21 @@ describe("make_random_graphs", { data("graph_list") - gl <- lapply(graph_list, function(x) binarise_graph_fixed(x, weight, 0.05)) + gl <- lapply(graph_list, function(x) { + x %>% + remove_loops() %>% + remove_duplicate_edges() %>% + binarise_graph_prop(weight, 0.5) + }) it("creates a list of graphs with length n", { rand <- make_random_graphs(gl, 10) expect_length(rand, 10) }) - it("creates graphs of varying size, even if input has all equal sizes", { - rand <- make_random_graphs(graph_list, 10) + it("creates graphs of equal size", { + rand <- make_random_graphs(gl, 10) sizes <- sapply(rand, igraph::gsize) - expect_gt(length(unique(sizes)), 1) + expect_equal(length(unique(sizes)), 1) }) it("calculates mean distance if requested", {