Skip to content

Commit

Permalink
add checks to existingModel
Browse files Browse the repository at this point in the history
  • Loading branch information
egillax committed Jan 23, 2025
1 parent 24cd2bd commit 9feeee4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
7 changes: 6 additions & 1 deletion R/ExistingPython.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' This function lets users add an existing scikit learn that is saved as model.pkl
#' into PLP format. covariateMap is a mapping between standard covariateIds and the model column names
#' and order are required in addition to pythonModelLocation, the location of the model that must be saved
#' as model.pkl . The user also needs to specify the covariate settings and population settings as these
#' as `model.pkl`. The user also needs to specify the covariate settings and population settings as these
#' are used to determine the standard PLP model design.
#'
#' @param pythonModelLocation The location of the folder that contains the model as model.pkl
Expand Down Expand Up @@ -56,6 +56,11 @@ createSciKitLearnModel <- function(
populationSettings, # specify time at risk used to develop model
isPickle = TRUE) {
checkSklearn()
checkFileExists(pythonModelLocation)
checkIsClass(covariateMap, "data.frame")
checkIsClass(covariateSettings, "covariateSettings")
checkIsClass(populationSettings, "populationSettings")
checkBoolean(isPickle)
existingModel <- list(model = "existingPython")
class(existingModel) <- "modelSettings"

Check warning on line 65 in R/ExistingPython.R

View check run for this annotation

Codecov / codecov/patch

R/ExistingPython.R#L58-L65

Added lines #L58 - L65 were not covered by tests

Expand Down
13 changes: 1 addition & 12 deletions R/Logging.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ createLog <- function(
logName = "PLP Log",
saveDirectory = getwd(),
logFileName = paste0("plpLog", as.Date(Sys.Date(), "%Y%m%d"), ".txt")) {
checkFileExists(saveDirectory, createIfNot = TRUE)
checkDirExists(saveDirectory, createIfNot = TRUE)

logFileName <- gsub("[[:punct:]]", "", logFileName)

Expand All @@ -83,17 +83,6 @@ createLog <- function(
return(logger)
}

checkFileExists <- function(
saveDirectory,
createIfNot = TRUE) {
dirExists <- dir.exists(saveDirectory)
if (!dirExists && createIfNot) {
ParallelLogger::logInfo(paste0("Creating save directory at: ", saveDirectory))
dir.create(saveDirectory, recursive = TRUE)
}
return(invisible(dirExists))
}

closeLog <- function(logger) {
# stop logger
ParallelLogger::unregisterLogger(logger)
Expand Down
19 changes: 19 additions & 0 deletions R/ParamChecks.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,22 @@ checkFileType <- function(parameter, fileType) {
return(TRUE)

Check warning on line 118 in R/ParamChecks.R

View check run for this annotation

Codecov / codecov/patch

R/ParamChecks.R#L118

Added line #L118 was not covered by tests
}

checkDirExists <- function(
saveDirectory,
createIfNot = TRUE) {
dirExists <- dir.exists(saveDirectory)
if (!dirExists && createIfNot) {
ParallelLogger::logInfo(paste0("Creating save directory at: ", saveDirectory))
dir.create(saveDirectory, recursive = TRUE)
}
return(invisible(dirExists))
}

checkFileExists <- function(parameter) {
name <- deparse(substitute(parameter))
if (!file.exists(parameter)) {
ParallelLogger::logError(paste0(name, " does not exist"))
stop(paste0(name, " does not exist"))

Check warning on line 136 in R/ParamChecks.R

View check run for this annotation

Codecov / codecov/patch

R/ParamChecks.R#L133-L136

Added lines #L133 - L136 were not covered by tests
}
return(TRUE)

Check warning on line 138 in R/ParamChecks.R

View check run for this annotation

Codecov / codecov/patch

R/ParamChecks.R#L138

Added line #L138 was not covered by tests
}

0 comments on commit 9feeee4

Please sign in to comment.