From 67206617f111aa20fa8fce5e039e2e2a37545d0c Mon Sep 17 00:00:00 2001 From: Christian Hohenfeld <r@hohenfeld.is> Date: Wed, 4 Aug 2021 11:52:08 +0200 Subject: [PATCH] Add function for creating subgraphs. --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/to_subgraph.R | 23 +++++++++++++++++++++++ man/to_subgraph.Rd | 21 +++++++++++++++++++++ tests/testthat/test-to_subgraph.R | 9 +++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 R/to_subgraph.R create mode 100644 man/to_subgraph.Rd create mode 100644 tests/testthat/test-to_subgraph.R diff --git a/DESCRIPTION b/DESCRIPTION index e386038..3cf4ce8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rsAnalysis Title: High-Level Tools for rsfMRI analysis -Version: 0.4.1 +Version: 0.4.2 Authors@R: person(given = "Christian", family = "Hohenfeld", diff --git a/NAMESPACE b/NAMESPACE index 644fcb9..e87f8b3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ export(remove_loops) export(rs_pipeline) export(single_component_threshold) export(split_graph_list) +export(to_subgraph) importFrom(magrittr,"%>%") importFrom(rlang,":=") importFrom(rlang,.data) diff --git a/R/to_subgraph.R b/R/to_subgraph.R new file mode 100644 index 0000000..80715af --- /dev/null +++ b/R/to_subgraph.R @@ -0,0 +1,23 @@ +#' Take a list of graphs and return subgraphs. +#' +#' `to_subgraph` makes the assumption that all graphs share a common set of +#' nodes like it is common with graph analysis of the brain. +#' +#' @param graph_list A list of tbl_graphs +#' @param to_keep The names of the nodes to keep. +#' +#' @return A list of the graphs in `graph_list` reduced to the respective +#' subgraphs. +#' @export +to_subgraph <- function(graph_list, to_keep) { + all_nodes <- graph_list[[1]] %>% + tidygraph::activate("nodes") %>% + tibble::as_tibble() %>% + tibble::deframe() + + to_remove_net <- all_nodes[!all_nodes %in% to_keep] + graph_list <- lapply(graph_list, + function(x) igraph::delete.vertices(x, to_remove_net)) + graph_list <- lapply(graph_list, tidygraph::as_tbl_graph) + graph_list +} diff --git a/man/to_subgraph.Rd b/man/to_subgraph.Rd new file mode 100644 index 0000000..0c71cc4 --- /dev/null +++ b/man/to_subgraph.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/to_subgraph.R +\name{to_subgraph} +\alias{to_subgraph} +\title{Take a list of graphs and return subgraphs.} +\usage{ +to_subgraph(graph_list, to_keep) +} +\arguments{ +\item{graph_list}{A list of tbl_graphs} + +\item{to_keep}{The names of the nodes to keep.} +} +\value{ +A list of the graphs in `graph_list` reduced to the respective +subgraphs. +} +\description{ +`to_subgraph` makes the assumption that all graphs share a common set of +nodes like it is common with graph analysis of the brain. +} diff --git a/tests/testthat/test-to_subgraph.R b/tests/testthat/test-to_subgraph.R new file mode 100644 index 0000000..daecd32 --- /dev/null +++ b/tests/testthat/test-to_subgraph.R @@ -0,0 +1,9 @@ +describe("to_subgraph", { + data("graph_list") + it("creates a graph only containing the specified nodes", { + to_keep <- c("Precentral_L", "Precentral_R") + sub_list <- to_subgraph(graph_list, to_keep) + sub_order <- sapply(sub_list, igraph::gorder, USE.NAMES = FALSE) + expect_true(all(sub_order == length(to_keep))) + }) +}) -- GitLab