diff --git a/R/MainFunctions.R b/R/MainFunctions.R index 1ac9108..17ff331 100755 --- a/R/MainFunctions.R +++ b/R/MainFunctions.R @@ -226,8 +226,8 @@ trainExplore <- function(train_data = NULL, results <- list("model" = rule_string, - "candidate_models" = candidate_models, - "cutoff_sets" = cutoff_sets) + "candidate_models" = candidate_models, + "cutoff_sets" = cutoff_sets) result <- results[resultType] @@ -379,6 +379,38 @@ predictExplore <- function(model, test_data) { return(predictions) } +#' Return a set of results from EXPLORE output file +#' @param outputFile outputfile = paste0(output_path, file_name, ".result") +#' +#' @export +resultsExplore <- function(outputFile) { + + # Read in results file + results <- paste(readLines(outputFile), collapse="\n") + results_lines <- strsplit(results, "\n") %>% unlist() + + result <- list() + + for (line in results_lines) { + # line <- "Candidate model: '198124209' = \"0\"" + if (grepl(":", line)) { + if (grepl("Candidate model", line)) { + split_line <- strsplit(line, ":")[[1]] + key <- trimws(split_line[1]) %>% tolower() %>% gsub(" ", "_", .) + value <- trimws(split_line[2]) + result[[key]] <- c(result[[key]], value) + } else { + split_line <- strsplit(line, ":")[[1]] + key <- trimws(split_line[1]) %>% tolower() %>% gsub(" ", "_", .) + value <- trimws(split_line[2]) + result[[key]] <- value + } + } + } + + return(result) +} + #' Return the number of candidate rules for EXPLORE #' @param OutputFile output file = paste0(output_path, file_name, ".result") #' diff --git a/tests/testthat/test-HelperFunctions.R b/tests/testthat/test-HelperFunctions.R index f68a96a..3d27d8d 100644 --- a/tests/testthat/test-HelperFunctions.R +++ b/tests/testthat/test-HelperFunctions.R @@ -7,11 +7,7 @@ test_that("Convert logical to 0/1", { `316139209` = c(FALSE,FALSE,FALSE, FALSE,FALSE,FALSE), `316139210` = c(FALSE,FALSE,FALSE, - FALSE,FALSE,FALSE) - ) - + FALSE,FALSE,FALSE)) train_data <- convert_logical(train_data) - expect_true(all(sapply(train_data, function(col) all(col %in% c(0, 1))))) - }) diff --git a/tests/testthat/test-MainFunctions.R b/tests/testthat/test-MainFunctions.R index 02dce8b..98f3ddf 100644 --- a/tests/testthat/test-MainFunctions.R +++ b/tests/testthat/test-MainFunctions.R @@ -166,4 +166,37 @@ test_that("balanced accuracy constraint ", { expect_equal(num_without, 1940) expect_equal(num_with, 36) -}) \ No newline at end of file +}) + +test_that("Results Explore", { + + dataset <- "binary_3" + config <- getDataSetPath(dataset = dataset) + train_data <- farff::readARFF(config$data_path) + output_path <- paste0(tempdir(), "/", glue::glue("{getRandomId()}"), "/") + file_name <- paste0(dataset, "_train_data") + dir.create(output_path) + if (.Platform$OS.type == "windows") { + output_path <- gsub("\\\\", "/", output_path) + } + + result <- trainExplore(train_data = train_data, + settings_path = NULL, + output_path = output_path, + file_name = file_name, + StartRulelength = 1, + EndRulelength = 2, + CutoffMethod = "RVAC", + ClassFeature = "'outcomeCount'", + PositiveClass = "\"1\"", + Maximize = "ACCURACY", + PrintPerformance = TRUE, + Subsumption = TRUE) + + + outputFile <- paste0(output_path, file_name, ".result") + results_list <- resultsExplore(outputFile = outputFile) + expect_equal(results_list$total_count_cutoff_sets, "16") + expect_length(results_list$candidate_model, 32) + +}) diff --git a/tests/testthat/test-testExplore.R b/tests/testthat/test-testExplore.R index cb0dd91..69dc096 100644 --- a/tests/testthat/test-testExplore.R +++ b/tests/testthat/test-testExplore.R @@ -1,3 +1,48 @@ +test_that("binary_3 trainExplore resultsExplore", { + + dataset <- "binary_3" + config <- getDataSetPath(dataset = dataset) + train_data <- farff::readARFF(config$data_path) + output_path <- paste0(tempdir(), "/", glue::glue("{getRandomId()}"), "/") + file_name <- paste0(dataset, "_train_data") + dir.create(output_path) + if (.Platform$OS.type == "windows") { + output_path <- gsub("\\\\", "/", output_path) + } + + result <- trainExplore(train_data = train_data, + settings_path = NULL, + output_path = output_path, + file_name = file_name, + OutputFile = NULL, + StartRulelength = 1, + EndRulelength = 1, + OperatorMethod = "MEDIAN", + CutoffMethod = "RVAC", + ClassFeature = config$class_feature, + PositiveClass = config$positive_class, + FeatureInclude = "", + Maximize = "ACCURACY", + Accuracy = 0, + BalancedAccuracy = 0, + Specificity = 0, + PrintSettings = TRUE, + PrintPerformance = TRUE, + Subsumption = TRUE, + BranchBound = TRUE, + Parallel = FALSE, + PrintCutoffSets = TRUE, + Sorted = "none", + OutputMethod = "EVERY", + BinaryReduction = FALSE) + + outputFile <- paste0(output_path, file_name, ".result") + results_list <- resultsExplore(outputFile = outputFile) + expect_length(results_list$candidate_model, 6) + unlink(output_path, recursive = TRUE) + +}) + test_that("Test binary_3", { # Binary reduction FALSE