Skip to content

Commit

Permalink
Enable to simplify multiple-relation edges to a single edge
Browse files Browse the repository at this point in the history
This can either be done manually when calling 'simplify.network' or
automatically at network creation via a new network configuration
parameter 'simplify.multiple.relations'.

Signed-off-by: Thomas Bock <bockthom@cs.uni-saarland.de>
  • Loading branch information
bockthom committed Feb 9, 2024
1 parent 03e0688 commit 2105ea8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ Updates to the parameters can be done by calling `NetworkConf$update.variables(.
- `simplify`
* Perform edge contraction to retrieve a simplified network
* [`TRUE`, *`FALSE`*]
- `simplify.multiple.relations`
* Whether the simplified network should contract edges of multiple relations into a single edge or not (if not, there will be one edge for each relation, resulting in possibly more than one edge between a pair of vertices)
* **Note** This parameter does not take effect if ``simplify = FALSE``!
* [`TRUE`, *`FALSE`*]
- `skip.threshold`
* The upper bound for total amount of edges to build for a subset of the data, i.e., not building any edges for the subset exceeding the limit
* any positive integer
Expand Down
8 changes: 7 additions & 1 deletion util-conf.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
## Copyright 2020-2021 by Christian Hechtl <hechtl@cs.uni-saarland.de>
## Copyright 2017 by Felix Prasse <prassefe@fim.uni-passau.de>
## Copyright 2017-2019 by Thomas Bock <bockthom@fim.uni-passau.de>
## Copyright 2021, 2023 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2021, 2023-2024 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2018 by Barbara Eckl <ecklbarb@fim.uni-passau.de>
## Copyright 2018-2019 by Jakob Kronawitter <kronawij@fim.uni-passau.de>
## Copyright 2019 by Anselm Fehnker <fehnker@fim.uni-passau.de>
Expand Down Expand Up @@ -863,6 +863,12 @@ NetworkConf = R6::R6Class("NetworkConf", inherit = Conf,
allowed = c(TRUE, FALSE),
allowed.number = 1
),
simplify.multiple.relations = list(
default = FALSE,
type = "logical",
allowed = c(TRUE, FALSE),
allowed.number = 1
),
skip.threshold = list(
default = Inf,
type = "numeric",
Expand Down
19 changes: 13 additions & 6 deletions util-networks.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
## Copyright 2017 by Raphael Nömmer <noemmer@fim.uni-passau.de>
## Copyright 2017-2018 by Christian Hechtl <hechtl@fim.uni-passau.de>
## Copyright 2017-2019 by Thomas Bock <bockthom@fim.uni-passau.de>
## Copyright 2021, 2023 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2021, 2023-2024 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2018 by Barbara Eckl <ecklbarb@fim.uni-passau.de>
## Copyright 2018-2019 by Jakob Kronawitter <kronawij@fim.uni-passau.de>
## Copyright 2020 by Anselm Fehnker <anselm@muenster.de>
Expand Down Expand Up @@ -1260,7 +1260,8 @@ construct.network.from.edge.list = function(vertices, edge.list, network.conf, d

## transform multiple edges to edge weights
if (network.conf$get.value("simplify")) {
net = simplify.network(net)
net = simplify.network(net,
simplify.multiple.relations = network.conf$get.value("simplify.multiple.relations"))
}

logging::logdebug("construct.network.from.edge.list: finished.")
Expand Down Expand Up @@ -1536,16 +1537,19 @@ add.attributes.to.network = function(network, type = c("vertex", "edge"), attrib
#' @param network the given network
#' @param remove.multiple whether to contract multiple edges between the same pair of vertices [default: TRUE]
#' @param remove.loops whether to remove loops [default: TRUE]
#' @param simplify.multiple.relations whether to combine edges of multiple relations into
#' one simplified edge [default: FALSE]
#'
#' @return the simplified network
simplify.network = function(network, remove.multiple = TRUE, remove.loops = TRUE) {
simplify.network = function(network, remove.multiple = TRUE, remove.loops = TRUE,
simplify.multiple.relations = FALSE) {
logging::logdebug("simplify.network: starting.")
logging::loginfo("Simplifying network.")

## save network attributes, otherwise they get lost
network.attributes = igraph::get.graph.attribute(network)

if (length(unique(igraph::get.edge.attribute(network, "relation"))) > 1) {
if (!simplify.multiple.relations && length(unique(igraph::get.edge.attribute(network, "relation"))) > 1) {
## data frame of the network
edge.data = igraph::as_data_frame(network, what = "edges")
vertex.data = igraph::as_data_frame(network, what = "vertices")
Expand Down Expand Up @@ -1587,17 +1591,20 @@ simplify.network = function(network, remove.multiple = TRUE, remove.loops = TRUE
#' @param networks the list of networks
#' @param remove.multiple whether to contract multiple edges between the same pair of vertices [default: TRUE]
#' @param remove.loops whether to remove loops [default: TRUE]
#' @param simplify.multiple.relations whether to combine edges of multiple relations into
#' one simplified edge [default: FALSE]
#'
#' @return the simplified networks
simplify.networks = function(networks, remove.multiple = TRUE, remove.loops = TRUE) {
simplify.networks = function(networks, remove.multiple = TRUE, remove.loops = TRUE,
simplify.multiple.relations = FALSE) {
logging::logdebug("simplify.networks: starting.")
logging::loginfo(
"Simplifying networks (names = [%s]).",
paste(names(networks), collapse = ", ")
)

nets = parallel::mclapply(networks, simplify.network, remove.multiple = remove.multiple,
remove.loops = remove.loops)
remove.loops = remove.loops, simplify.multiple.relations = simplify.multiple.relations)

logging::logdebug("simplify.networks: finished.")
return(nets)
Expand Down

0 comments on commit 2105ea8

Please sign in to comment.