-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest-ml.preprocess.R
149 lines (129 loc) · 4.4 KB
/
test-ml.preprocess.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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#library (R4ML)
context("Testing r4ml.pre.processing\n")
test_that("r4ml.impute", {
data("airquality")
airq_hf <- as.r4ml.frame(as.data.frame(airquality))
airq_hm <- as.r4ml.matrix(airq_hf)
airq_hm_n <- r4ml.impute(airq_hm, list("Ozone"="mean"))
})
# begin r4ml.ml.preprocess
test_that("r4ml.ml.preprocess", {
data("iris")
iris_hf <- as.r4ml.frame(as.data.frame(iris))
iris_transform <- r4ml.ml.preprocess(
iris_hf, transformPath = tempdir(),
dummycodeAttrs = "Species",
binningAttrs = c("Sepal_Length", "Sepal_Width"),
numBins=4,
missingAttrs = c("Petal_Length", "Sepal_Width"),
imputationMethod = c("global_mean", "constant"),
imputationValues = list("Sepal_Width" = 40),
omit.na="Petal_Width",
recodeAttrs=c("Species"),
scalingAttrs=c("Petal_Length")
)
showDF(iris_transform$data, n = 154)
})
test_that("r4ml.ml.preprocess omit.na", {
iris_hf <- iris
iris_hf$Petal.Width[5] <- NA
iris_hf <- as.r4ml.frame(iris_hf)
# with omit.na col specified
iris_transform <- r4ml.ml.preprocess(
data = iris_hf,
transformPath = tempdir(),
omit.na = c("Petal_Width")
)
expect_equal(nrow(iris_transform$data), 149)
# with no cols specified (should default to all columns)
iris_transform <- r4ml.ml.preprocess(
data = iris_hf,
transformPath = tempdir()
)
expect_equal(nrow(iris_transform$data), 149)
# with no cols spefied
iris_transform <- r4ml.ml.preprocess(
data = iris_hf,
transformPath = tempdir(),
omit.na = c()
)
expect_equal(nrow(iris_transform$data), 150)
})
#Execute ml.preprocess without transformPath parameter
test_that("r4ml.ml.preprocess excludetransformPath", {
data("iris")
iris_hf <- as.r4ml.frame(as.data.frame(iris))
iris_transform <- r4ml.ml.preprocess(
iris_hf, dummycodeAttrs = "Species",
binningAttrs = c("Sepal_Length", "Sepal_Width"),
numBins=4,
missingAttrs = c("Petal_Length", "Sepal_Width"),
imputationMethod = c("global_mean", "constant"),
imputationValues = list("Sepal_Width" = 40),
omit.na="Petal_Width",
recodeAttrs=c("Species"),
scalingAttrs=c("Petal_Length")
)
showDF(iris_transform$data, n = 154)
})
test_that("r4ml.sysml.transform", {
data("iris")
iris_hf <- as.r4ml.frame(as.data.frame(iris))
iris_transform <- r4ml.sysml.transform(
iris_hf,
dummycodeAttrs = "Species",
binningAttrs = c("Sepal_Length", "Sepal_Width"),
numBins=4,
missingAttrs = c("Petal_Length", "Sepal_Width"),
imputationMethod = c("global_mean", "constant"),
imputationValues = list("Sepal_Width" = 40),
omit.na="Petal_Width",
recodeAttrs=c("Species"),
scalingAttrs=c("Petal_Length")
)
expect_true(class(iris_transform$data) == "r4ml.matrix")
})
test_that("r4ml.sysml.transform run all na dataframe", {
result <- tryCatch({
na_df <- data.frame(c(NA,NA,NA), c(10,20,30))
names(na_df) <- c("c1", "c2")
na_hf <- as.r4ml.frame(na_df)
na_transform <- r4ml.sysml.transform(
na_hf,
dummycodeAttrs = "c2",
omit.na=c("c1"),
recodeAttrs=c("c2")
)
return(FALSE) # this should fail
}, warning = function(war) {
print(paste("Got Warning as expected : ",war))
ok=grep("After na omission, there are no records left", war)
if (length(ok) > 0) {
return(TRUE) # we should get warning first before anything
}else {
return(FALSE) # we didn't get the warning
}
}, error = function(err) {
print(paste("Got Error as expected ",err))
return(TRUE) # if we got err without warn, that's not right
}, finally = {
print("done running all na dataframe")
}) # END tryCatch
expect_true(result)
})