Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get.inducedSubgraph() method for networkLite #9

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ S3method(delete.vertex.attribute,networkLite)
S3method(delete.vertices,networkLite)
S3method(get.edge.attribute,networkLite)
S3method(get.edge.value,networkLite)
S3method(get.inducedSubgraph,networkLite)
S3method(get.network.attribute,networkLite)
S3method(get.vertex.attribute,networkLite)
S3method(is.na,networkLite)
Expand All @@ -32,6 +33,7 @@ S3method(network.naedgecount,networkLite)
S3method(networkLite,edgelist)
S3method(networkLite,matrix)
S3method(networkLite,numeric)
S3method(permute.vertexIDs,networkLite)
S3method(print,networkLite)
S3method(set.edge.attribute,networkLite)
S3method(set.edge.value,networkLite)
Expand Down
28 changes: 28 additions & 0 deletions R/add_vertices.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,31 @@

modify_in_place(x)
}

#' Permute vertices
#'
#' @param x,vids,... see [network::permute.vertexIDs()]
#' @export
permute.vertexIDs.networkLite <- function(x, vids, ...) {
#Sanity check: is this a permutation vector?
n<-network.size(x)

Check warning on line 80 in R/add_vertices.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_vertices.R,line=80,col=4,[infix_spaces_linter] Put spaces around all infix operators.
if((length(unique(vids))!=n)||any(range(vids)!=c(1,n)))

Check warning on line 81 in R/add_vertices.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_vertices.R,line=81,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 81 in R/add_vertices.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_vertices.R,line=81,col=27,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 81 in R/add_vertices.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_vertices.R,line=81,col=31,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 81 in R/add_vertices.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_vertices.R,line=81,col=48,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 81 in R/add_vertices.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_vertices.R,line=81,col=54,[commas_linter] Commas should always have a space after.
stop("Invalid permutation vector in permute.vertexIDs.")
if(is.bipartite(x)){ #If bipartite, enforce partitioning

Check warning on line 83 in R/add_vertices.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_vertices.R,line=83,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 83 in R/add_vertices.R

View workflow job for this annotation

GitHub Actions / lint

file=R/add_vertices.R,line=83,col=22,[brace_linter] There should be a space before an opening curly brace.
bpc<-get.network.attribute(x,"bipartite")
if(any(vids[0:bpc]>bpc)||any(vids[(bpc+1):n]<=bpc))
warning("Performing a cross-mode permutation in permute.vertexIDs. I hope you know what you're doing....")
}

# Remap the edge list and sort by new indices.
o <- order(vids)
x$el$.tail <- o[x$el$.tail]
x$el$.head <- o[x$el$.head]
if(!is.directed(x)) x$el[, c(".tail", ".head")] <- cbind(pmin(x$el$.tail, x$el$.head), pmax(x$el$.tail, x$el$.head))
x$el <- x$el[order(x$el$.tail, x$el$.head), , drop = FALSE]

# Permute the vertex attributes.
x$attr <- x$attr[vids, , drop = FALSE]

modify_in_place(x)
}
39 changes: 39 additions & 0 deletions R/delete_vertices.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,42 @@ delete.vertices.networkLite <- function(x, vid, ...) {

modify_in_place(x)
}


#' Return an induced subgraph
#'
#' @param x,v,alters,... see [network::get.inducedSubgraph()]
#' @export
get.inducedSubgraph.networkLite <- function(x, v, alters=NULL, ...){
#Do some reality checking
n<-network.size(x)

# do checks for v and alters
if((length(v)<1)||any(is.na(v))||any(v<1)||any(v>n))
stop("Illegal vertex selection in get.inducedSubgraph")
if(!is.null(alters)){
if((length(alters)<1)||any(is.na(alters))||any(alters<1)||any(alters>n)|| any(alters%in%v))
stop("Illegal vertex selection (alters) in get.inducedSubgraph")
}

#Start by making a copy of our target network (yes, this can be wasteful)
#TODO: in most cases, probably faster to create a new network and only copy over what is needed

#Now, strip out what is needed, and/or permute in the two-mode case
if(is.null(alters)){ #Simple case
delete.vertices(x,(1:n)[-v]) #Get rid of everyone else
}else{ #Really an edge cut, but w/vertices
nv<-length(v)
na<-length(alters)
newids<-sort(c(v,alters))
newv<-match(v,newids)
newalt<-match(alters,newids)
delete.vertices(x,(1:n)[-c(v,alters)]) #Get rid of everyone else
permute.vertexIDs(x,c(newv,newalt)) #Put the new vertices first
#Remove within-group edges
x$el <- x$el[(x$el$.tail <= nv) != (x$el$.head <= nv), , drop=FALSE]
x%n%"bipartite"<-nv #Set bipartite attribute
}

x
}
14 changes: 14 additions & 0 deletions man/get.inducedSubgraph.networkLite.Rd

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

14 changes: 14 additions & 0 deletions man/permute.vertexIDs.networkLite.Rd

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

37 changes: 36 additions & 1 deletion tests/testthat/test-networkLite.R
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,32 @@ test_that("network and networkLite produce identical matrices, edgelists, and ti
expect_identical(tbl,
tibble::as_tibble(nwL, attrname = attrname, na.rm = na.rm))
}
}

n <- network.size(nw)
for(into_bipartite in c(FALSE, TRUE)){
if(is.bipartite(nw)){
if(into_bipartite){
b <- nw %n% "bipartite"
v <- sample.int(b, round(b/2))
a <- b + round((n-b)/2)
}else{
v <- sort(sample.int(n, round(n/2)))
a <- NULL
}
}else{
v <- sample.int(n, round(n/2))
if(into_bipartite){
a <- sample(v, round(n/4))
v <- setdiff(v, a)
}else a <- NULL
}

nwS <- get.inducedSubgraph(nw, v, a)
nwLS <- get.inducedSubgraph(nwL, v, a)

expect_equiv_nets(as.networkLite(nwS), nwLS)
expect_equiv_nets(as.networkLite(nwS), as.networkLite(to_network_networkLite(nwLS)))
}
}
}
Expand Down Expand Up @@ -883,7 +909,7 @@ test_that("network and networkLite behave equivalently for basic access and muta
}
})

test_that("add.vertices and add.edges with irregular attribute arguments behave equivalently for network and networkLite", {
test_that("add.vertices and add.edges with irregular attribute arguments and vertex permutation behave equivalently for network and networkLite", {
net_size <- 100
bip_size <- 40
edges_target <- net_size
Expand Down Expand Up @@ -976,6 +1002,15 @@ test_that("add.vertices and add.edges with irregular attribute arguments behave

expect_equiv_nets(as.networkLite(nw), nwL)
expect_equiv_nets(as.networkLite(nw), as.networkLite(to_network_networkLite(nwL)))

P <- if(is.bipartite(nw)){
b <- nw %n% "bipartite"
c(sample.int(b), b + sample.int(network.size(nw) - b))
}else sample.int(network.size(nw))
nwP <- permute.vertexIDs((nw), P)
nwLP <- permute.vertexIDs((nwL), P)
expect_equiv_nets(as.networkLite(nwP), nwLP)
expect_equiv_nets(as.networkLite(nwP), as.networkLite(to_network_networkLite(nwLP)))
}
}
}
Expand Down
Loading