Skip to content

Commit

Permalink
Fix merging of networks without vertices
Browse files Browse the repository at this point in the history
The function 'merge.networks' is not able to handle networks that have
not vertices and also (!) no vertex attributes: The final call to
'igraph::graph.data.frame' results in an error.
To fix this, the special case is identified and the list of vertices is
adjusted accordingly. A corresponding test is added to the test suite.

Additionally, the default case of merging only one network is handled by
returning the only network right away.

Signed-off-by: Claus Hunsen <hunsen@fim.uni-passau.de>
  • Loading branch information
clhunsen committed Mar 4, 2019
1 parent 641624b commit 0666f1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/test-networks.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ test_that("Merge networks", {
## Merge two empty networks (i.e., no vertices at all)
##

network.empty = igraph::graph.empty(0, directed = TRUE)
#create.empty.network(directed = FALSE)
expect_error(merge.networks(list(network.empty, network.empty)), NA) # expect that no error occurs
expect_true(igraph::vcount(merge.networks(list(network.empty, network.empty))) == 0) # vertices
expect_true(igraph::ecount(merge.networks(list(network.empty, network.empty))) == 0) # edges

##
## Merge two empty networks (i.e., no vertices at all, but with vertex attributes)
##

network.empty = create.empty.network(directed = FALSE)
expect_error(merge.networks(list(network.empty, network.empty)), NA) # expect that no error occurs
expect_true(igraph::vcount(merge.networks(list(network.empty, network.empty))) == 0) # vertices
Expand Down
10 changes: 10 additions & 0 deletions util-networks.R
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,11 @@ merge.network.data = function(vertex.data, edge.data) {
merge.networks = function(networks) {
logging::logdebug("merge.networks: starting.")

if (length(networks) == 1) {
logging::logdebug("Only one network given, so a merge is not necessary.")
return(networks[[1]])
}

## list with all vertex data frames
vertex.data = lapply(networks, function(network) {
return(igraph::as_data_frame(network, what = "vertices"))
Expand All @@ -1138,6 +1143,11 @@ merge.networks = function(networks) {
## merge all edge and vertex data frames
new.network.data = merge.network.data(vertex.data, edge.data)

## catch case where no vertices (and no vertex attributes) are given
if (ncol(new.network.data[["vertices"]]) == 0) {
new.network.data[["vertices"]] = NULL # igraph::graph.data.frame can handle this
}

## build whole network form edge and vertex data frame
whole.network = igraph::graph.data.frame(
new.network.data[["edges"]],
Expand Down

0 comments on commit 0666f1f

Please sign in to comment.