forked from se-sic/coronet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-tensor.R
199 lines (157 loc) · 7.42 KB
/
util-tensor.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
## This file is part of coronet, which is free software: you
## can redistribute it and/or modify it under the terms of the GNU General
## Public License as published by the Free Software Foundation, version 2.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##
## Copyright 2019-2020 by Anselm Fehnker <[email protected]>
## All Rights Reserved.
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Libraries ---------------------------------------------------------------
requireNamespace("R6") # for R6 classes
requireNamespace("logging") # for logging
requireNamespace("parallel") # for parallel computation
requireNamespace("igraph") # networks
requireNamespace("rTensor") # tensors
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## FourthOrderTensor class ------------------------------------------------
#' The class \code{FourthOrderTensor} creates an (author x relation x author x relation)
#' tensor from a list of networks. The tensor as well as the dimensions and lists
#' of the authors and relations are stored.
#'
FourthOrderTensor = R6::R6Class("FourthOrderTensor",
## * private ----------------------------------------------------------
private = list(
## * * data -------------------------------------------------------
dim = NULL,
relations = NULL,
authors = NULL,
tensor = NULL,
## * * tensor creation --------------------------------------------
#' Creates a fourth-order tensor from a list of networks using their
#' adjacency matrices.
#'
#' @param networks the list of networks
#' @param weighted decides if the tensor shall be weighted [default: FALSE]
#'
#' @return the created tensor
build.tensor.from.networks = function(networks, weighted = FALSE) {
## get adjacency matrices from networks
adjacency.matrices = parallel::mclapply(networks, get.expanded.adjacency, private$authors, weighted)
## create an array with the size of the fourth-order tensor that only contains zeros
array = array(0, dim = private$dim)
## transfer entries from adjacency matrices to array
for (l in seq_along(adjacency.matrices)) {
matrix = as(adjacency.matrices[[l]], "dgTMatrix")
for (entry in seq_along(matrix@x)) {
## Transfer the entries from the adjacency matrix to the tensor.
## Due to the property that the indexes saved in a sparse matrix start with 0,
## while the indexes of an array start with 1, the indexes need to be shifted.
array[matrix@i[entry] + 1, l, matrix@j[entry] + 1, l] = matrix@x[entry]
}
}
## convert array to tensor
tensor = rTensor::as.tensor(array)
return(tensor)
}
),
## * * public ----------------------------------------------------------
public = list(
#' Constructor of the class. Constructs a new fourth-order tensor instance
#' based on the given list of networks.
#'
#' @param networks the given list of networks
#' @param weighted decides if the tensor shall be weighted [default: FALSE]
initialize = function(networks, weighted = FALSE) {
private$relations = names(networks)
private$authors = get.author.names.from.networks(networks)
private$dim = c(length(private$authors), length(private$relations), length(private$authors), length(private$relations))
private$tensor = private$build.tensor.from.networks(networks, weighted)
},
#' Get the dimension of the tensor.
#'
#' @return the dimension
get.dim = function() {
return(private$dim)
},
#' Get the list of relations of the tensor.
#'
#' @return the list of relations
get.relations = function() {
return(private$relations)
},
#' Get the list of authors of the tensor.
#'
#' @return the list of authors
get.authors = function() {
return(private$authors)
},
#' Get the tensor data saved in the object.
#'
#' @return the tensor data
get.tensor = function() {
return(private$tensor)
}
)
)
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Get author networks -----------------------------------------------------
#' Get a list of author networks for each relation.
#' If a relation is not available for the current project, it is not added to the list.
#'
#' @param network.builder the network builder for the project
#' @param relations the relations of the wanted networks
#'
#' @return the list of networks
get.author.networks.for.multiple.relations = function(network.builder, relations) {
networks = list()
networks = lapply(relations, function(rel) {
## retrieve network for relation
network.builder$update.network.conf(updated.values = list(author.relation = rel))
retrieved.network = network.builder$get.author.network()
## check if network is not empty
if (igraph::vcount(retrieved.network) > 0){
logging::loginfo("Added %s data to list", rel)
return(retrieved.network)
} else {
logging::logwarn("There is no %s data available for the current project", rel)
return(NA)
}
})
## add names of the relations
names(networks) = relations
## removes empty networks
networks = networks[!is.na(networks)]
return(networks)
}
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Calculate centrality ----------------------------------------------------
#' Calculate EDCPTD centrality for a given fourth-order tensor.
#' EDCPTD centrality is based on the work "Identifying Key Nodes in Multilayer Networks based on Tensor Decomposition"
#' by Dingjie Wang, Haitao Wang, and Xiufen Zou, Chaos 27, 063108 (2017) [1].
#' [1] https://doi.org/10.1063/1.4985185
#'
#' @param fourth.order.tensor the given tensor
#'
#' @return data frame with EDCPTD score for every author
calculate.EDCPTD.centrality = function(fourth.order.tensor) {
## create data frame for results
results = data.frame(names = fourth.order.tensor$get.authors(), EDCPTD.score = 0)
## decompose tensor. 'num_components = 1' needed for EDCPTD centrality.
## 'max_iter' and 'tol' chosen from default in documentation.
decomposition = rTensor::cp(fourth.order.tensor$get.tensor(), num_components = 1, max_iter = 25, tol = 1e-05)
## calculate EDCPTD centrality
for (y in seq_along(fourth.order.tensor$get.relations())) {
results[["EDCPTD.score"]] = (results[["EDCPTD.score"]]
+ abs(decomposition[["U"]][[1]][,1] * decomposition[["U"]][[2]][,1][y])
+ abs(decomposition[["U"]][[3]][,1] * decomposition[["U"]][[4]][,1][y])) / 2
}
return(results)
}