Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AniekMarkus committed Mar 6, 2023
1 parent 0cff836 commit 290e618
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 248 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ vignettes/*.pdf
# R Environment Variables
.Renviron

# Documetns
# Documents and results
docs/
output/

# Mac files
.DS_STORE
Expand All @@ -41,7 +42,7 @@ docs/
*.a
*.lib

# C++ files
# Other C++ files
**/__history/

# EXPLORE result files
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(aucrocExplore)
export(aurocEXPLORE)
export(changeSetting)
export(getSetting)
export(predictExplore)
Expand Down
9 changes: 5 additions & 4 deletions R/MainFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ predictExplore <- function(model, test_data) {
#' @export
#'
#' @examples
aucrocExplore <- function(output_path, train_data, settings_path, file_name, ...) {
aurocEXPLORE <- function(output_path, train_data, settings_path, file_name, ...) {
# TODO: check with latest implementation in PLP

# Range of specificities to check
specificities <- seq(from = 0.01, to = 0.99, by = 0.02)
Expand All @@ -213,8 +214,8 @@ aucrocExplore <- function(output_path, train_data, settings_path, file_name, ...
sensitivities[s] <- as.numeric(sensitivity)
}

aucroc <- simple_auc(TPR = rev(sensitivities), FPR = rev(1 - specificities))
plot(1-specificities, sensitivities)
auroc <- simple_auc(TPR = rev(sensitivities), FPR = rev(1 - specificities))
# plot(1-specificities, sensitivities)

return(aucroc)
return(auroc)
}
4 changes: 2 additions & 2 deletions inst/examples/iris.project
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ NumberofPartitions=1
OperatorMethod=EXHAUSTIVE
CutoffMethod=RVAC
CutoffFile=/Users/aniekmarkus/Documents/Git/Explore/inst/examples/iris.cutoff
ClassFeature='class'
PositiveClass="Iris-versicolor"
ClassFeature=class
PositiveClass=Iris-versicolor
[Restrictions]
Rule=
FeatureInclude=
Expand Down
157 changes: 0 additions & 157 deletions inst/examples/train_data.arff

This file was deleted.

1 change: 0 additions & 1 deletion inst/examples/train_data.cutoff

This file was deleted.

48 changes: 0 additions & 48 deletions inst/examples/train_data.project

This file was deleted.

6 changes: 3 additions & 3 deletions man/aucrocExplore.Rd → man/aurocEXPLORE.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 23 additions & 30 deletions tests/testthat/test-package_tests.R
Original file line number Diff line number Diff line change
@@ -1,59 +1,52 @@
library(testthat)
library(Explore)

### Tests for EXPLORE using iris dataset
data <- farff::readARFF(paste0(getwd(),"/tests/testthat/examples/iris.arff"))
output_path <- paste0(getwd(),"/tests/testthat/examples/")

for (file_name in c("iris", "train_data")) {
settings_path <- paste0(output_path, file_name,".project")
settings <- paste0(paste(readLines(settings_path), collapse="\n"),"\n")

settings <- changeSetting(settings, parameter = "OutputFile", input = paste0(gsub("examples/", "results/" , output_path), file_name, ".result"), default_setting = NA)
settings <- changeSetting(settings, parameter = "DataFile", input = paste0(output_path, file_name, ".arff"), default_setting = NA)
settings <- changeSetting(settings, parameter = "CutoffFile", input = paste0(output_path, file_name, ".cutoff"), default_setting = NA)

write.table(settings,
file=settings_path,
quote = FALSE,
col.names = FALSE,
row.names = FALSE)
}
# TODO: check differences variable names with / without ''

### Tests for EXPLORE using iris dataset
root_folder <- "../.."

data_path <- file.path(root_folder, "inst/examples/iris.arff")
settings_path <- file.path(root_folder, "inst/examples/iris.project")
output_path <- file.path(root_folder, "inst/examples/output/")

data <- farff::readARFF(data_path)

test_that("option 1: fully pre-specified settings file works", {
model <- Explore::trainExplore(output_path = gsub("examples/", "results/" , output_path), file_name = "iris", settings_path = paste0(output_path, "iris.project"))
model <- Explore::trainExplore(output_path = output_path, file_name = "iris", settings_path = settings_path)

expect_equal(class(model), "character")
expect_false(model == "NA", info = "Test failed because model is NA")
expect_equal(model, "petallength>2.45ANDpetallength<=4.95ANDpetalwidth<=1.65")
})

test_that("option 2: pre-specified settings file with input data works", {
model <- Explore::trainExplore(output_path = gsub("examples/", "results/" , output_path), train_data = data, settings_path = paste0(output_path, "train_data.project"), ClassFeature = "'class'", PositiveClass = '"Iris-versicolor"', PrintSettings = "no", PrintPerformance = "no")
model <- Explore::trainExplore(output_path = output_path, train_data = data, settings_path = settings_path, ClassFeature = "'class'", PositiveClass = '"Iris-versicolor"')
expect_equal(class(model), "character")
expect_false(model == "NA", info = "Test failed because model is NA")
expect_equal(model, "'petallength'>2.45AND'petallength'<=4.95AND'petalwidth'<=1.65") # "'sepallength'>4.85"
expect_equal(model, "'petallength'>2.45AND'petallength'<=4.95AND'petalwidth'<=1.65") # OR "'sepallength'>4.85" ?
})

test_that("option 3: only input parameters (no settings file) works", {
model <- Explore::trainExplore(output_path = gsub("examples/", "results/" , output_path), file_name = "iris", settings_path = paste0(output_path, "iris.project"))
model <- Explore::trainExplore(output_path = output_path, file_name = "iris", train_data = data, ClassFeature = "'class'", PositiveClass = '"Iris-versicolor"')
expect_equal(class(model), "character")
expect_false(model == "NA", info = "Test failed because model is NA")
expect_equal(model, "petallength>2.45ANDpetallength<=4.95ANDpetalwidth<=1.65")
expect_equal(model, "'petallength'>2.45AND'petallength'<=4.95AND'petalwidth'<=1.65")
})

test_that("prediction (with model option 2)", {
model <- Explore::trainExplore(output_path = gsub("examples/", "results/" , output_path), train_data = data, settings_path = paste0(output_path, "train_data.project"), ClassFeature = "'class'", PositiveClass = '"Iris-versicolor"', PrintSettings = "no", PrintPerformance = "no")
test_that("prediction", {
model <- Explore::trainExplore(output_path = output_path, train_data = data, settings_path = settings_path, ClassFeature = "'class'", PositiveClass = '"Iris-versicolor"')
prediction <- Explore::predictExplore(model, test_data = data)

expect_equal(class(prediction), "integer")
expect_equal(length(prediction), nrow(data))
expect_equal(sum(prediction %in% c(0,1) == FALSE),0)
})

test_that("compute AUC", {
aucroc <- Explore::aucrocExplore(output_path = gsub("examples/", "results/" , output_path), train_data = data, OutputFile = paste0(gsub("examples/", "results/" , output_path), "train_data.result"), settings_path = paste0(output_path, "train_data.project"), ClassFeature = "'class'", PositiveClass = '"Iris-versicolor"')
auroc <- Explore::aurocEXPLORE(output_path = output_path, train_data = data, settings_path = settings_path, ClassFeature = "'class'", PositiveClass = '"Iris-versicolor"')

expect_equal(class(aucroc), "numeric")
expect_true(aucroc < 100)
expect_true(aucroc > 0)
expect_equal(class(auroc), "numeric")
expect_true(auroc < 100)
expect_true(auroc > 0)
})

0 comments on commit 290e618

Please sign in to comment.