Skip to content

Commit

Permalink
refactor parameter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgigante committed Dec 17, 2019
1 parent d5038ea commit 1d40ffb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
39 changes: 12 additions & 27 deletions Rmagic/R/magic.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,32 +140,16 @@ magic.default <- function(
message("Argument alpha is deprecated. Using decay instead.")
decay <- alpha
}
knn <- as.integer(x = knn)
t.max <- as.integer(x = t.max)
n.jobs <- as.integer(x = n.jobs)
if (is.numeric(x = npca)) {
npca <- as.integer(x = npca)
} else if (!is.null(x = npca) && is.na(x = npca)) {
npca <- NULL
}
if (is.numeric(x = decay)) {
decay <- as.double(x = decay)
} else if (!is.null(x = decay) && is.na(x = decay)) {
decay <- NULL
}
if (is.numeric(x = t)) {
t <- as.integer(x = t)
} else if (is.null(x = t) || is.na(x = t)) {
t <- 'auto'
}
if (is.numeric(x = seed)) {
seed <- as.integer(x = seed)
} else if (!is.null(x = seed) && is.na(x = seed)) {
seed <- NULL
}
if (is.numeric(x = verbose)) {
verbose <- as.integer(x = verbose)
}
# validate parameters
knn <- check.int(x = knn)
t.max <- check.int(x = t.max)
n.jobs <- check.int(x = n.jobs)
npca <- check.int.or.null(npca)
knn.max <- check.int.or.null(knn.max)
seed <- check.int.or.null(seed)
verbose <- check.int.or.null(verbose)
decay <- check.double.or.null(decay)
t <- check.int.or.string(t, 'auto')
if (!methods::is(object = data, "Matrix")) {
data <- as.matrix(x = data)
}
Expand Down Expand Up @@ -216,7 +200,8 @@ magic.default <- function(
knn_dist = knn.dist.method,
n_jobs = n.jobs,
random_state = seed,
verbose = verbose
verbose = verbose,
...
)
}
}
Expand Down
35 changes: 35 additions & 0 deletions Rmagic/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,38 @@ pymagic <- NULL
py_config <- reticulate::py_discover_config(required_module = "magic")
load_pymagic()
}

######
# Parameter validation
######

check.int <- function(x) {
as.integer(x)
}

check.int.or.null <- function(x) {
if (is.numeric(x = x)) {
x <- as.integer(x = x)
} else if (!is.null(x = x) && is.na(x = x)) {
x <- NULL
}
x
}

check.double.or.null <- function(x) {
if (is.numeric(x = x)) {
x <- as.integer(x = x)
} else if (!is.null(x = x) && is.na(x = x)) {
x <- NULL
}
x
}

check.int.or.string <- function(x, str) {
if (is.numeric(x = x)) {
x <- as.integer(x = x)
} else if (is.null(x = x) || is.na(x = x)) {
x <- str
}
x
}

0 comments on commit 1d40ffb

Please sign in to comment.