Skip to content

Commit

Permalink
Fix the behavior of get.expanded.adjacency.matrices
Browse files Browse the repository at this point in the history
In commit e72eff8, the function
`get.expanded.adjacency.matrices` has been refactored. Unfortunately,
the author names have only be extracted from each network individually
instead of globally over all networks. This way, the functionality of
the function has been changed inadvertently. With this commit, we
follow-up on this and restore the previous behavior.

As `get.author.names.from.networks(networks)` now always returns a list,
accessing the first element is necessary (although this was not
necessary in previous implementations).

Moreover, this commit also fixes a wrong test with respect to
get.expanded.adjacency.matrices`.

Signed-off-by: Thomas Bock <bockthom@cs.uni-saarland.de>
  • Loading branch information
bockthom committed Apr 12, 2024
1 parent 29418f2 commit a53fab8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
39 changes: 20 additions & 19 deletions tests/test-util-networks-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##
## Copyright 2024 by Leo Sendelbach <s8lesend@stud.uni-saarland.de>
## Copyright 2024 by Thomas Bock <bockthom@cs.uni-saarland.de>
## All Rights Reserved.


Expand Down Expand Up @@ -576,19 +577,6 @@ test_that("getting a sparse adjacency matrix per network, two networks", {
to = c("Dieter", "Klaus", "Heinz")
)
network.in.one = igraph::graph.data.frame(edges, directed = FALSE, vertices = vertices)
authors.in.one = sort(c("Heinz", "Dieter", "Klaus"))

matrix.out.one = Matrix::sparseMatrix(i = c(), j = c(), x = 0, dims = c(length(authors.in.one),
length(authors.in.one)), repr = "T")
rownames(matrix.out.one) = authors.in.one
colnames(matrix.out.one) = authors.in.one

# order these statements so that the second arguments are ordered alphabetically
# or use the helper function as used below
matrix.out.one["Heinz", "Dieter"] = 1
matrix.out.one["Klaus", "Dieter"] = 1
matrix.out.one["Dieter", "Heinz"] = 1
matrix.out.one["Dieter", "Klaus"] = 1

vertices = data.frame(
name = c("Klaus", "Tobias"),
Expand All @@ -600,14 +588,27 @@ test_that("getting a sparse adjacency matrix per network, two networks", {
to = c("Tobias")
)
network.in.two = igraph::graph.data.frame(edges, directed = FALSE, vertices = vertices)
authors.in.two = sort(c("Klaus", "Tobias"))

matrix.out.two = Matrix::sparseMatrix(i = c(), j = c(), x = 0, dims = c(length(authors.in.two),
length(authors.in.two)), repr = "T")
rownames(matrix.out.two) = authors.in.two
colnames(matrix.out.two) = authors.in.two
all.authors = sort(c("Heinz", "Dieter", "Klaus", "Tobias"))

matrix.out.one = Matrix::sparseMatrix(i = c(), j = c(), x = 0, dims = c(length(all.authors),
length(all.authors)), repr = "T")
rownames(matrix.out.one) = all.authors
colnames(matrix.out.one) = all.authors

# order these statements so that the second arguments are ordered alphabetically
# order these statements so that the second arguments are ordered alphabetically
# or use the helper function as used below
matrix.out.one["Heinz", "Dieter"] = 1
matrix.out.one["Klaus", "Dieter"] = 1
matrix.out.one["Dieter", "Heinz"] = 1
matrix.out.one["Dieter", "Klaus"] = 1

matrix.out.two = Matrix::sparseMatrix(i = c(), j = c(), x = 0, dims = c(length(all.authors),
length(all.authors)), repr = "T")
rownames(matrix.out.two) = all.authors
colnames(matrix.out.two) = all.authors

# order these statements so that the second arguments are ordered alphabetically
# or use the helper function as used below
matrix.out.two["Tobias", "Klaus"] = 1
matrix.out.two["Klaus", "Tobias"] = 1
Expand Down
11 changes: 5 additions & 6 deletions util-networks-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## Copyright 2016-2017 by Sofie Kemper <kemperso@fim.uni-passau.de>
## Copyright 2016-2017 by Claus Hunsen <hunsen@fim.uni-passau.de>
## Copyright 2016-2018 by Thomas Bock <bockthom@fim.uni-passau.de>
## Copyright 2020, 2023 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2020, 2023-2024 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2017 by Angelika Schmid <schmidang@fim.uni-passau.de>
## Copyright 2019 by Jakob Kronawitter <kronawij@fim.uni-passau.de>
## Copyright 2019-2020 by Anselm Fehnker <anselm@muenster.de>
Expand Down Expand Up @@ -169,7 +169,7 @@ get.expanded.adjacency = function(network, authors, weighted = FALSE) {
}

#' Calculates a sparse adjacency matrix for each network in the list.
#' All adjacency matrices are expanded in such a way that the use the same set
#' All adjacency matrices are expanded in such a way that they use the same set
#' of authors derived from all networks in the list.
#'
#' @param networks list of networks
Expand All @@ -178,10 +178,9 @@ get.expanded.adjacency = function(network, authors, weighted = FALSE) {
#' @return the list of adjacency matrices
get.expanded.adjacency.matrices = function(networks, weighted = FALSE){

adjacency.matrices = parallel::mclapply(networks, function(network) {
active.authors = sort(igraph::V(network)$name)
return(get.expanded.adjacency(network = network, authors = active.authors, weighted = weighted))
})
authors = get.author.names.from.networks(networks)[[1]]

adjacency.matrices = parallel::mclapply(networks, get.expanded.adjacency, authors, weighted)

return(adjacency.matrices)
}
Expand Down

0 comments on commit a53fab8

Please sign in to comment.