Skip to content

Commit

Permalink
Adjust adjacency matrix to recent Matrix package versions and fix bugs
Browse files Browse the repository at this point in the history
The previously used statements to initiate sparse matrices are deprecated or
disfunct in the recent versions of the Matrix package. Therefore, update the
way of how we initiate a 'dgTMatrix' (i.e., a matrix with numeric values in
triplet form). The new syntax requires, at least, Matrix version 1.3.0
(which has already been released in December 2020). Therefore, add a
corresponding warning to the install script if older versions of package
Matrix would be installed.

In addition, fix two bugs related to our expanded adjacency matrices:
- We have to call the 'which' function from the Matrix package (and not the
  'which' function of the base package).
- Replace '1:nrow' by 'seq_len' to corretly handle empty adjacency
  matrices.

Signed-off-by: Thomas Bock <bockthom@cs.uni-saarland.de>
  • Loading branch information
bockthom committed Apr 15, 2023
1 parent 50c68cb commit 573fab2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
8 changes: 6 additions & 2 deletions install.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
## Copyright 2015 by Wolfgang Mauerer <wolfgang.mauerer@oth-regensburg.de>
## Copyright 2015-2017 by Claus Hunsen <hunsen@fim.uni-passau.de>
## Copyright 2017 by Thomas Bock <bockthom@fim.uni-passau.de>
## Copyright 2022 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2020-2021 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2020-2023 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2019 by Anselm Fehnker <fehnker@fim.uni-passau.de>
## Copyright 2021 by Christian Hechtl <hechtl@cs.uni-saarland.de>
## All Rights Reserved.
Expand Down Expand Up @@ -69,4 +68,9 @@ if (length(p) > 0) {
if (compareVersion(igraph.version, "1.3.0") == -1) {
print("WARNING: igraph version 1.3.0 or higher is recommended for using coronet.")
}

Matrix.version = installed.packages()[rownames(installed.packages()) == "Matrix", "Version"]
if (compareVersion(Matrix.version, "1.3.0") == -1) {
print("WARNING: Matrix version 1.3.0 or higher is necessary for using coronet.")
}
}
17 changes: 9 additions & 8 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 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2020, 2023 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 @@ -104,7 +104,7 @@ get.author.names.from.data = function(data.ranges, data.sources = c("commits", "
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Adjacency matrices ----------------------------------------------------

#' Get a sparse expanded adjacency matrix for network.
#' Get a sparse expanded adjacency matrix (in triplet format) for a given network.
#'
#' The adjacency matrix is expanded as it may contain rows and columns for authors which are not part of the network
#' but given in the \code{authors} parameter. However, this also means that authors present in the network
Expand All @@ -117,9 +117,10 @@ get.author.names.from.data = function(data.ranges, data.sources = c("commits", "
#' @return the sparse adjacency matrix of the network
get.expanded.adjacency = function(network, authors, weighted = FALSE) {

## create an empty sparse matrix with the right size
matrix = Matrix::sparseMatrix(i = c(), j = c(), dims = c(length(authors), length(authors)), giveCsparse = FALSE)
matrix = as(matrix, "dgTMatrix")
## create an empty sparse matrix using the triplet form with the right size.
## x = 0 indicates that the matrix should contain numeric values (i.e., it is a 'dgTMatrix';
## without setting x = 0 it would be a binary 'ngTMatrix')
matrix = Matrix::sparseMatrix(i = c(), j = c(), x = 0, dims = c(length(authors), length(authors)), repr = "T")

## add row and column names
rownames(matrix) = authors
Expand Down Expand Up @@ -225,11 +226,11 @@ convert.adjacency.matrix.list.to.array = function(adjacency.list){
colnames(array) = colnames(adjacency.list[[1]])

## copy the activity values from the adjacency matrices in the list to the corresponding array slices
for (i in seq_along(adjacency.list)){
for (i in seq_along(adjacency.list)) {
adjacency = adjacency.list[[i]]
activity.indices = which(adjacency != 0, arr.ind = TRUE)
activity.indices = Matrix::which(adjacency != 0, arr.ind = TRUE)

for (j in 1:nrow(activity.indices)){
for (j in seq_len(nrow(activity.indices))) {
array[as.vector(activity.indices[j, 1]), as.vector(activity.indices[j, 2]), i] =
adjacency[as.vector(activity.indices[j, 1]), as.vector(activity.indices[j, 2])]
}
Expand Down

0 comments on commit 573fab2

Please sign in to comment.