-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathalgorithm.r
75 lines (63 loc) · 2.29 KB
/
algorithm.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
#!/usr/bin/env Rscript
library(tsmp)
library(jsonlite)
`%or%` = function(a, b) {
cmp = function(a,b) if (
identical(a, FALSE) || is.null(a) ||
is.na(a) || is.nan(a) || length(a) == 0
) b else a
if (length(a) > 1)
mapply(cmp, a, b)
else
cmp(a, b)
}
args <- commandArgs(trailingOnly = TRUE)
if (is.null(args) || is.na(args) || is.nan(args) || length(args) == 0) {
stop("No arguments specified!")
}
config <- fromJSON(args)
input <- config$dataInput %or% "/data/dataset.csv"
output <- config$dataOutput %or% "/results/anomaly_scores.csv"
# ignore modelInput and modelOutput, because they are not needed
executionType <- config$executionType %or% "execute"
window_min <- config$customParameters$min_anomaly_window_size %or% 30
window_max <- config$customParameters$max_anomaly_window_size %or% 40
# window size must be at least 4
window_min <- max(window_min, 4)
window_max <- max(window_min + 1, window_max)
heap_size <- config$customParameters$heap_size %or% 50
exclusion_zone <- config$customParameters$exclusion_zone %or% 0.5
verbose <- config$customParameters$verbose %or% 1
random_state <- config$customParameters$random_state %or% 42
# Set random seed
set.seed(random_state)
if(verbose > 1) {
message("-- Configuration ------------")
message("executionType=", executionType)
message("window_min=", window_min)
message("window_max=", window_max)
message("heap_size=", heap_size)
message("exclusion_zone=", exclusion_zone)
message("verbose=", verbose)
message("-----------------------------")
}
if (executionType != "execute") {
message("Training not required. Finished!")
quit()
}
message("Reading data from ", input)
data <- read.csv(file=input)
message("number of dataset columns ", ncol(data))
result = NULL
for(i in seq(1, ncol(data)-2, 1)) {
message("running VALMOD for column ", i)
values = data[, 1+i]
valmod_mp <- valmod(values, window_min=window_min, window_max=window_max, heap_size=heap_size, exclusion_zone=exclusion_zone, verbose=verbose)
if(is.null(result)) {
result <- valmod_mp$mp[,1]
} else {
result <- result + valmod_mp$mp[,1]
}
}
message("Writing results to ", output)
write.table(result, file=output, sep=",", eol="\n", row.names = FALSE, col.names = FALSE, fileEncoding="UTF-8")