Skip to content

Commit

Permalink
refactor: adapt to cut.prob's new handling of NULL in the C core
Browse files Browse the repository at this point in the history
Simpler default for the R interface
  • Loading branch information
maelle committed Nov 28, 2024
1 parent b2d09dd commit 99122be
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
43 changes: 26 additions & 17 deletions R/motifs.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ triad.census <- function(graph) { # nocov start
#' @inheritParams count_motifs
#' @keywords internal
#' @export
graph.motifs.no <- function(graph, size = 3, cut.prob = rep(0, size)) { # nocov start
graph.motifs.no <- function(graph, size = 3, cut.prob = NULL) { # nocov start
lifecycle::deprecate_soft("2.0.0", "graph.motifs.no()", "count_motifs()")
count_motifs(graph = graph, size = size, cut.prob = cut.prob)
} # nocov end
Expand All @@ -39,7 +39,7 @@ graph.motifs.no <- function(graph, size = 3, cut.prob = rep(0, size)) { # nocov
#' @inheritParams sample_motifs
#' @keywords internal
#' @export
graph.motifs.est <- function(graph, size = 3, cut.prob = rep(0, size), sample.size = vcount(graph) / 10, sample = NULL) { # nocov start
graph.motifs.est <- function(graph, size = 3, cut.prob = NULL, sample.size = vcount(graph) / 10, sample = NULL) { # nocov start
lifecycle::deprecate_soft("2.0.0", "graph.motifs.est()", "sample_motifs()")
sample_motifs(graph = graph, size = size, cut.prob = cut.prob, sample.size = sample.size, sample = sample)
} # nocov end
Expand All @@ -54,7 +54,7 @@ graph.motifs.est <- function(graph, size = 3, cut.prob = rep(0, size), sample.si
#' @inheritParams motifs
#' @keywords internal
#' @export
graph.motifs <- function(graph, size = 3, cut.prob = rep(0, size)) { # nocov start
graph.motifs <- function(graph, size = 3, cut.prob = NULL) { # nocov start
lifecycle::deprecate_soft("2.0.0", "graph.motifs()", "motifs()")
motifs(graph = graph, size = size, cut.prob = cut.prob)
} # nocov end
Expand Down Expand Up @@ -110,7 +110,8 @@ dyad.census <- function(graph) { # nocov start
#' directed graphs and sizes 3-6 in undirected graphs.
#' @param cut.prob Numeric vector giving the probabilities that the search
#' graph is cut at a certain level. Its length should be the same as the size
#' of the motif (the `size` argument). By default no cuts are made.
#' of the motif (the `size` argument).
#' If `NULL`, the default, no cuts are made.
#' @return `motifs()` returns a numeric vector, the number of occurrences of
#' each motif in the graph. The motifs are ordered by their isomorphism
#' classes. Note that for unconnected subgraphs, which are not considered to be
Expand All @@ -125,10 +126,12 @@ dyad.census <- function(graph) { # nocov start
#' motifs(g, 3)
#' count_motifs(g, 3)
#' sample_motifs(g, 3)
motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
motifs <- function(graph, size = 3, cut.prob = NULL) {
ensure_igraph(graph)
cut.prob <- as.numeric(cut.prob)
if (length(cut.prob) != size) {

if (!is.null(cut.prob)) cut.prob <- as.numeric(cut.prob)

if (!is.null(cut.prob) && length(cut.prob) != size) {
cut.prob <- c(
cut.prob[-length(cut.prob)],
rep(cut.prob[-length(cut.prob)], length(cut.prob) - 1)
Expand All @@ -138,7 +141,7 @@ motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
on.exit(.Call(R_igraph_finalizer))
res <- .Call(
R_igraph_motifs_randesu, graph, as.numeric(size),
as.numeric(cut.prob)
cut.prob
)
res[is.nan(res)] <- NA
res
Expand All @@ -156,7 +159,8 @@ motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
#' @param size The size of the motif.
#' @param cut.prob Numeric vector giving the probabilities that the search
#' graph is cut at a certain level. Its length should be the same as the size
#' of the motif (the `size` argument). By default no cuts are made.
#' of the motif (the `size` argument).
#' If `NULL`, the default, no cuts are made.
#' @return `count_motifs()` returns a numeric scalar.
#' @seealso [isomorphism_class()]
#'
Expand All @@ -168,10 +172,12 @@ motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
#' motifs(g, 3)
#' count_motifs(g, 3)
#' sample_motifs(g, 3)
count_motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
count_motifs <- function(graph, size = 3, cut.prob = NULL) {
ensure_igraph(graph)
cut.prob <- as.numeric(cut.prob)
if (length(cut.prob) != size) {

if (!is.null(cut.prob)) cut.prob <- as.numeric(cut.prob)

if (!is.null(cut.prob) && length(cut.prob) != size) {
cut.prob <- c(
cut.prob[-length(cut.prob)],
rep(cut.prob[-length(cut.prob)], length(cut.prob) - 1)
Expand All @@ -181,7 +187,7 @@ count_motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
on.exit(.Call(R_igraph_finalizer))
.Call(
R_igraph_motifs_randesu_no, graph, as.numeric(size),
as.numeric(cut.prob)
cut.prob
)
}

Expand All @@ -198,7 +204,8 @@ count_motifs <- function(graph, size = 3, cut.prob = rep(0, size)) {
#' in directed graphs and sizes 3-6 in undirected graphs.
#' @param cut.prob Numeric vector giving the probabilities that the search
#' graph is cut at a certain level. Its length should be the same as the size
#' of the motif (the `size` argument). By default no cuts are made.
#' of the motif (the `size` argument).
#' If `NULL`, the 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)` .
Expand All @@ -224,8 +231,10 @@ sample_motifs <- function(
sample = NULL
) {
ensure_igraph(graph)
cut.prob <- as.numeric(cut.prob)
if (length(cut.prob) != size) {

if (!is.null(cut.prob)) cut.prob <- as.numeric(cut.prob)

if (!is.null(cut.prob) && length(cut.prob) != size) {
cut.prob <- c(
cut.prob[-length(cut.prob)],
rep(cut.prob[-length(cut.prob)], length(cut.prob) - 1)
Expand All @@ -244,7 +253,7 @@ sample_motifs <- function(
on.exit(.Call(R_igraph_finalizer))
.Call(
R_igraph_motifs_randesu_estimate, graph, as.numeric(size),
as.numeric(cut.prob), as.numeric(sample.size), sample
cut.prob, as.numeric(sample.size), sample
)
}

Expand Down
5 changes: 3 additions & 2 deletions man/count_motifs.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/graph.motifs.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/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/graph.motifs.no.Rd

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

3 changes: 2 additions & 1 deletion man/sample_motifs.Rd

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

0 comments on commit 99122be

Please sign in to comment.