-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
131 lines (100 loc) · 3.93 KB
/
server.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
# server-side script of MTS application
# get the working directory right
# setwd("./Google Drive/MTS/MTS project")
# INCLUDE THE SOURCE CODE
library(shiny)
source("MTS.R")
ls_datasets <- function(dataset){
# splits the dataset into normal and abnormal parts
# split the dataset into normal and abnormal
normal <- dataset[dataset$target == 1, ]
abnormal <- dataset[dataset$target==0, ]
# remove the unncessary columns
normal <- normal[, !(colnames(normal) %in% c("X", "id"))]
abnormal <- abnormal[, !(colnames(abnormal) %in% c("X", "id"))]
# get the necessary subsets of the variables
columns <- colnames(normal)
# exclude the target variables
columns_except_target <- columns[!(columns %in% c("target"))]
# normalize the dataset
normal[, columns_except_target] <- normalize(normal[, columns_except_target] , normal[, columns_except_target] )
abnormal[, columns_except_target] <- normalize(normal[, columns_except_target], abnormal[, columns_except_target])
# create variables selection options
vars <- columns_except_target[grep("var", columns_except_target)]
numeric_vars <- vars[-which(vars == "var7" | vars == "var8")]
geodem_vars <- columns_except_target[grep("geo", columns_except_target)]
weather_vars <- columns_except_target[grep("weather", columns_except_target)]
# variables to test upon
vars_to_test <- append(append(numeric_vars,geodem_vars), weather_vars)
normal <- normal[, vars_to_test]
abnormal <- abnormal[, vars_to_test]
return (list('normal'= normal, 'abnormal'= abnormal))
}
mahalanobis_process <- function(normal, abnormal){
# find the correlations
corr <- cor(normal, normal)
# estimate the mahalanobis distances
ref_group <- mahalanobis_dist(normal, corr)
outside_group <- mahalanobis_dist(abnormal, corr)
outside_group <- outside_group[-which(outside_group == max(outside_group))]
return (list('ref'=ref_group, 'outside'=outside_group))
}
readFile <- function(inFile){
if(is.null(inFile)){
return (NULL)
}
dataset <- read.csv(inFile$datapath)
return(dataset)
}
show_sn_ratio <- function(ortho_filename, normal, abnormal){
nVariables <- dim(abnormal)[2]
ortho_arr <- make_ortho_arr(ortho_filename, nVariables)
nCols <- seq(1, dim(ortho_arr)[2])
var_names <- colnames(normal[, nCols])
# comput the nosie-to-signal ratio
runs <- generate_runs(ortho_arr, normal[, nCols], abnormal[, nCols])
avr_sn_ratio <- avr_SN_ratio(runs, ortho_arr, var_names)
return (get_ordred_sn_ratio(avr_sn_ratio))
}
shinyServer(function(input, output, session) {
get_dataset <- reactive({readFile(input$dataset)})
nSelectedVariables <- reactive({(input$nVariables)})
output$data <- renderTable({
dataset <- get_dataset()
dataset[seq(1,20), ]
}
)
output$mts_first <- renderPlot({
dataset <- get_dataset()
ls_data <- ls_datasets(dataset)
normal <- ls_data[[1]]
abnormal <- ls_data[[2]]
ls_MD_groups <- mahalanobis_process(normal, abnormal)
ref_group <- ls_MD_groups[[1]]
outside_group <- ls_MD_groups[[2]]
plot_result(ref_group, outside_group)
})
output$sn_ratio <- renderTable({
dataset <- get_dataset()
ls_data <- ls_datasets(dataset)
normal <- ls_data[[1]]
abnormal <- ls_data[[2]]
ortho_filename <- "./data/L256.csv"
show_sn_ratio(ortho_filename, normal, abnormal)
})
output$mts_second <- renderPlot({
dataset <- get_dataset()
ls_data <- ls_datasets(dataset)
normal <- ls_data[[1]]
abnormal <- ls_data[[2]]
ortho_filename <- "./data/L256.csv"
ratio_ordered<-show_sn_ratio(ortho_filename, normal, abnormal)
normal <- dim_reduction(normal, ratio_ordered,nSelectedVariables)
abnormal <- dim_reduction(abnormal, ratio_ordered,nSelectedVariables)
ls_MD_groups <- mahalanobis_process(normal, abnormal)
ref_group <- ls_MD_groups[[1]]
outside_group <- ls_MD_groups[[2]]
plot_result(ref_group, outside_group)
})
}
)