forked from friedhelmvictor/alphacore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.R
More file actions
175 lines (154 loc) · 9.7 KB
/
utils.R
File metadata and controls
175 lines (154 loc) · 9.7 KB
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
library(data.table)
library(RSQLite)
source("algorithms/wrappers.R")
getTokenAddresses <- function(dbLocation) {
sqlitecon = dbConnect(RSQLite::SQLite(), dbname=dbLocation)
tokenNetworks <- dbGetQuery(sqlitecon, "SELECT DISTINCT token_address AS address FROM token_transfers")$address
dbDisconnect(sqlitecon)
return(tokenNetworks)
}
# function to retrieve a token network as an igraph object
getGraph <- function(networkAddress, dbLocation) {
con = dbConnect(RSQLite::SQLite(), dbname=dbLocation)
print(paste("Fetching transfers for token network", networkAddress))
transfers <- as.data.table(dbGetQuery(con, paste0("SELECT from_address, to_address, value, block_number FROM token_transfers WHERE token_address='",networkAddress,"';"))
)[,list(source = from_address, target = to_address, amount = as.numeric(value), blockNumber = as.numeric(block_number))]
transfers <- transfers[amount > 0]
transfers <- transfers[amount >= max(transfers$amount)/(10^10), list(source, target, weight=amount, blockNumber)]
graph <- graph_from_data_frame(transfers)
dbDisconnect(con)
return(graph)
}
#' Evaluate an algorithm's result
#'
#' @param nodeRanking A named vector of numerics, where the number corresponds to the rank, and name to the node.
#' @param nodesOfInterest A vector consisting of nodes of interest (should all be in node column of nodeRanking)
#' @param k A vector of integers, corresponding to the scores at the top k results
#' @param dataName A name for this dataset / graph
#' @param algoName A name for describing the algorithm that produced the ranking
#' @return A DataDable \code{x} and \code{y}.
#' @examples
#' add(1, 1)
#' add(10, 1)
scoreResult <- function(nodeRanking, nodesOfInterest, k = c(10,20,50,100), dataName = "newData", algoName = "newAlgo") {
sortedRankingDF <- data.table(node=names(nodeRanking), k=nodeRanking)[order(-k)]
vCount <- nrow(sortedRankingDF)
nodesOfInterestInGraph = length(intersect(nodesOfInterest, sortedRankingDF$node))
res <- do.call(rbind, lapply(k, function(x) {
data.table(dataName = dataName,
vCount = vCount,
nodesOfInterestInGraph = nodesOfInterestInGraph,
algorithmName = algoName,
found = length(intersect(head(sortedRankingDF, x)$node, nodesOfInterest)),
k=x)
}))
return(res)
}
evaluateAlphaCoreCombinations <- function(graph, dataName, features, startEpsilon, stepSize) {
getCombinations <- function(features) { # https://stackoverflow.com/a/18718066
n <- length(features)
masks <- 2^(1:n-1)
lapply( 2:2^n-1, function(u) features[ bitwAnd(u, masks) != 0 ] )
}
combs <- getCombinations(features)
res <- do.call(rbind, lapply(combs, function(x) {scoreResult(alphaCore_wrapped(graph, x), top100subreddits, dataName = dataName, algoName = paste0("alphaCore(",paste0(x, collapse = ","),")"))}))
return(res)
}
evaluateAlphaCore <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
scoreResult(alphaCore_wrapped(graph, c("inneighborhoodsize", "outneighborhoodsize")), nodesOfInterest, dataName = dataName, algoName = "alphaCore(inneighborhoodsize,outneighborhoodsize)"),
scoreResult(alphaCore_wrapped(graph, c("outneighborhoodsize")), nodesOfInterest, dataName = dataName, algoName = "alphaCore(outneighborhoodsize)"),
scoreResult(alphaCore_wrapped(graph, c("outneighborhoodsize", "instrength", "outstrength")), nodesOfInterest, dataName = dataName, algoName = "alphaCore(outneighborhoodsize,instrength,outstrength)"),
scoreResult(alphaCore_wrapped(graph, c("inneighborhoodsize", "outneighborhoodsize", "instrength", "outstrength")), nodesOfInterest, dataName = dataName, algoName = "alphaCore(inneighborhoodsize,outneighborhoodsize,instrength,outstrength)"),
scoreResult(alphaCore_wrapped(graph, c("inneighborhoodsize")), nodesOfInterest, dataName = dataName, algoName = "alphaCore(inneighborhoodsize)"),
scoreResult(alphaCore_wrapped(graph, c("inneighborhoodsize", "instrength")), nodesOfInterest, dataName = dataName, algoName = "alphaCore(inneighborhoodsize,instrength)")
)
}
evaluateKCore <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
scoreResult(coreness(simplify(graph), "all"), nodesOfInterest, dataName = dataName, algoName = "k-core(simplified,all)"),
scoreResult(coreness(simplify(graph), "all"), nodesOfInterest, dataName = dataName, algoName = "k-core(simplified,all)"),
scoreResult(coreness(simplify(graph), "out"), nodesOfInterest, dataName = dataName, algoName = "k-core(simplified,out)"),
scoreResult(coreness(simplify(graph), "in"), nodesOfInterest, dataName = dataName, algoName = "k-core(simplified,in)")
)
}
evaluateWKCore <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
scoreResult(wkCore(graph, "all"), nodesOfInterest, dataName = dataName, algoName = "wk-core(neighborhoodsize,strength)"),
scoreResult(wkCore(graph, "in"), nodesOfInterest, dataName = dataName, algoName = "wk-core(inneighborhoodsize,instrength)"),
scoreResult(wkCore(graph, "out"), nodesOfInterest, dataName = dataName, algoName = "wk-core(outneighborhoodsize,outstrength)"),
scoreResult(inOutSizeCore(graph), nodesOfInterest, dataName = dataName, algoName = "wk-core(outneighborhoodsize,inneighborhoodsize)")
)
}
evaluatePageRank <- function(graph, nodesOfInterest, dataName = "newData") {
res <- scoreResult(page_rank_wrapped(graph), nodesOfInterest, dataName = dataName, algoName = "Pagerank")
return(res)
}
evaluateDegCentr <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
scoreResult(degree_centrality_wrapped(graph, "in"), nodesOfInterest, dataName = dataName, algoName = "InDegree Centrality"),
scoreResult(degree_centrality_wrapped(graph, "out"), nodesOfInterest, dataName = dataName, algoName = "OutDegree Centrality")
)
}
evaluateWDegCentr <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
scoreResult(w_degree_centrality_wrapped(graph, "in"), nodesOfInterest, dataName = dataName, algoName = "Weighted InDegree Centrality"),
scoreResult(w_degree_centrality_wrapped(graph, "out"), nodesOfInterest, dataName = dataName, algoName = "Weighted OutDegree Centrality")
)
}
evaluateBetwCentr <- function(graph, nodesOfInterest, dataName = "newData") {
scoreResult(betweenness_centrality_wrapped(graph), nodesOfInterest, dataName = dataName, algoName = "Betweenness Centrality")
}
evaluateWBetwCentr <- function(graph, nodesOfInterest, dataName = "newData") {
scoreResult(w_betweenness_centrality_wrapped(graph), nodesOfInterest, dataName = dataName, algoName = "Weighted Betweenness Centrality")
}
evaluateCloCentr <- function(graph, nodesOfInterest, dataName = "newData") {
scoreResult(closeness_centrality_wrapped(graph), nodesOfInterest, dataName = dataName, algoName = "Closeness Centrality")
}
evaluateWCloCentr <- function(graph, nodesOfInterest, dataName = "newData") {
scoreResult(w_closeness_centrality_wrapped(graph), nodesOfInterest, dataName = dataName, algoName = "Weighted Closeness Centrality")
}
evaluateDataDepth <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
scoreResult(only_data_depth_wrapped(graph, c("inneighborhoodsize", "outneighborhoodsize")), nodesOfInterest, dataName = dataName, algoName = "dataDepth(inneighborhoodsize,outneighborhoodsize)"),
scoreResult(only_data_depth_wrapped(graph, c("outneighborhoodsize")), nodesOfInterest, dataName = dataName, algoName = "dataDepth(outneighborhoodsize)"),
scoreResult(only_data_depth_wrapped(graph, c("outneighborhoodsize", "instrength", "outstrength")), nodesOfInterest, dataName = dataName, algoName = "dataDepth(outneighborhoodsize,instrength,outstrength)"),
scoreResult(only_data_depth_wrapped(graph, c("inneighborhoodsize", "outneighborhoodsize", "instrength", "outstrength")), nodesOfInterest, dataName = dataName, algoName = "dataDepth(inneighborhoodsize,outneighborhoodsize,instrength,outstrength)"),
scoreResult(only_data_depth_wrapped(graph, c("inneighborhoodsize")), nodesOfInterest, dataName = dataName, algoName = "dataDepth(inneighborhoodsize)"),
scoreResult(only_data_depth_wrapped(graph, c("inneighborhoodsize", "instrength")), nodesOfInterest, dataName = dataName, algoName = "dataDepth(inneighborhoodsize,instrength)")
)
}
evaluateAll <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
evaluateAlphaCore(graph, nodesOfInterest, dataName),
evaluateKCore(graph, nodesOfInterest, dataName),
evaluateWKCore(graph, nodesOfInterest, dataName),
evaluatePageRank(graph, nodesOfInterest, dataName),
evaluateDegCentr(graph, nodesOfInterest, dataName),
evaluateWDegCentr(graph, nodesOfInterest, dataName),
evaluateBetwCentr(graph, nodesOfInterest, dataName),
evaluateWBetwCentr(graph, nodesOfInterest, dataName),
evaluateCloCentr(graph, nodesOfInterest, dataName),
evaluateWCloCentr(graph, nodesOfInterest, dataName),
evaluateDataDepth(graph, nodesOfInterest, dataName)
)
}
evaluateFastPart <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
evaluateAlphaCore(graph, nodesOfInterest, dataName),
evaluateKCore(graph, nodesOfInterest, dataName),
evaluatePageRank(graph, nodesOfInterest, dataName),
evaluateDegCentr(graph, nodesOfInterest, dataName),
evaluateWDegCentr(graph, nodesOfInterest, dataName),
evaluateDataDepth(graph, nodesOfInterest, dataName)
)
}
evaluateSlowPart <- function(graph, nodesOfInterest, dataName = "newData") {
res <- rbind(
evaluateWKCore(graph, nodesOfInterest, dataName),
evaluateBetwCentr(graph, nodesOfInterest, dataName),
evaluateWBetwCentr(graph, nodesOfInterest, dataName),
evaluateCloCentr(graph, nodesOfInterest, dataName),
evaluateWCloCentr(graph, nodesOfInterest, dataName)
)
}