-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.R
More file actions
153 lines (122 loc) · 4.42 KB
/
server.R
File metadata and controls
153 lines (122 loc) · 4.42 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
source('active_learning.R')
source('validate_classifier.R')
source('installPackages.R')
source('detectOutliers.R')
#data_sets <- c("iris")
createLink <- function(val){
sprintf('<a href="%s/%s" target="_blank" class="btn btn-primary">%s</a>',getwd(),val, val)
}
#Used for creating global variables
values = reactiveValues()
#Create a global dataframe
values$out <- data.frame(matrix(nrow = 1, ncol = 14))
#Used so that the reactive function is called only once
values$first <- TRUE
init <- reactive({
colnames(values$out) <- c('DataSet', 'Budget', 'Classifier', 'LOF', 'Mahalanobis', 'kMeans', 'ChiSq', 'BoxPlot', 'MAD', 'threeSigma', 'ActiveLearning', 'fmeasure', 'Time', 'Output')
values$out <- values$out[-1,]
})
server <- function(input, output, session) {
#Validation
output$err <- renderText({input$btn
validate(
need(input$infile, 'Data file required'),
need(!is.na(input$budget),'Budget cannot be null'),
need(!is.na(as.numeric(input$budget)),'The budget should be numeric'),
need(validate_classifier(input$classifier), 'Invalid Classifier')
)
#Load dataset
#datasetPath <- sprintf('data/sources/%s.data', input$dataset)
#dat <- read.csv(datasetPath, header = FALSE)
dat <- read.csv(input$infile$datapath, header = FALSE)
#print(dat)
#Create an oracle with random values
#print(input$infile$name)
fname <- sub("^([^.]*).*", "\\1", input$infile$name)
name_oracle <- sprintf("data/oracle/oracle_%s.rds",fname)
oracle <- dat
oracle$is_outlier <- NA
for(i in 1:nrow(dat)){
oracle[i, 'is_outlier'] <- sample(c('no', 'yes'), 1)
}
saveRDS(oracle, name_oracle)
validate(
need(input$file1$datapath, 'Please enter the correct oracle!')
#need(batch < budget/3,batch_range)
)
#The oracle file uploaded
oracle <- readRDS(input$file1$datapath)
row <- as.numeric(nrow(dat))
budget <- as.numeric(input$budget)
#batch <- as.numeric(input$batch)
range = sprintf("Budget should be between %s to %s in this case", as.integer(row/10), as.integer(row/3))
#batch_range = sprintf("Batch should be less than %s", as.integer(row/30))
kerror = sprintf("Knn and Kmn should be less than %s", row)
knn <- as.numeric(input$knn)
kmn <- as.numeric(input$kmn)
validate(
need(budget <= as.integer(row/3) && budget >= as.integer(row/10),range),
need(knn < row && kmn < row, kerror),
need(nrow(oracle) == row, 'Please enter the correct oracle!')
#need(batch < budget/3,batch_range)
)
print(input$knn)
print(input$kmn)
#print(input$infile$datapath)
detectOutliers(input$infile$datapath, input$infile$name,row/3, knn, kmn)
})
#When the button is clicked
observeEvent(input$btn, {
if(is.na(as.numeric(input$budget))){
return()
}
#datasetPath <- sprintf('data/sources/%s.data', input$dataset)
dat <- read.csv(input$infile$datapath, header = FALSE)
valid <- validate_classifier(input$classifier)
row <- as.numeric(nrow(dat))
budget <- as.numeric(input$budget)
#batch <- as.numeric(input$batch)
classifier <- as.character(input$classifier)
total_time <- 0
#Validations
if(budget >= as.integer(row/3) && budget <= as.integer(row/10)){
print(1)
return()
}
if(!valid){
print(2)
return()
}
inFile <- input$file1
if (is.null(inFile)){
print(3)
return()
}
if(values$first == TRUE){
init()
values$first = FALSE
}
if(as.numeric(input$knn) > row && as.numeric(input$kmn) > row){
print(4)
return()
}
#Get oracle and save it as a label in training matrix
oracle <- readRDS(input$file1$datapath)
if(nrow(oracle) != row){
return()
}
#Pass variables to active learning function
aloutput <- active_learning(input$infile$datapath, input$infile$name, input$file1$datapath, budget, classifier)
#print(final_analysis)
out <- values$out
out[nrow(out) + 1,] <- aloutput$data_frame[1,]
values$out[nrow(values$out) + 1, ] <- aloutput$data_frame[1,]
print(out)
#print(createLink(out$output))
output$final_analysis <- renderDataTable({
#out$Output <- createLink(out$Output)
return(out)
}, escape = FALSE, options = list(autoWidth = FALSE, columnDefs = list(list(sWidth = '10px', targets = "_all"))
))
})
}