Skip to content

Commit

Permalink
Merge pull request #23 from pteridin/master
Browse files Browse the repository at this point in the history
Fixed n_jobs errors for umap-learn
  • Loading branch information
tkonopka authored Apr 29, 2024
2 parents 96f0778 + 705a1a9 commit 9106cac
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: umap
Title: Uniform Manifold Approximation and Projection
Version: 0.2.10.0
Version: 0.2.11.0
Authors@R:
person("Tomasz", "Konopka", , "[email protected]", role = c("aut", "cre"))
Author: Tomasz Konopka [aut, cre]
Expand Down
4 changes: 3 additions & 1 deletion R/umap.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ umap <- function(d, config=umap.defaults,
method <- config$method <- match.arg(method)
config <- umap.prep.config(config, ...)
d <- umap.prep.input(d, config)
set.seed(config$random_state)
if(!is.na(config$random_state)) {
set.seed(config$random_state)
}

# perform the actual work with a specific umap implementation
if (nrow(d)<=2) {
Expand Down
41 changes: 34 additions & 7 deletions R/umap_checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
umap.prep.config <- function(config=umap.defaults, ...) {

umap.check.config.class(config)

# transfer values from arguments into the config object
arguments <- list(...)
for (onearg in names(arguments)) {
Expand Down Expand Up @@ -55,7 +55,7 @@ umap.prep.config <- function(config=umap.defaults, ...) {
umap.warning(
"parameter 'b' is set but 'a' is not;\n",
"value of 'b' will be ignored.\n",
"(Embedding will be controlled via 'min_dist' and 'spread')")
"(Embedding will be controlled via 'min_dist' and 'spread')")
}
if (!identical(config$a, NA) & !identical(config$b, NA)) {
abcontrol <- "(Embedding will be controlled via 'a' and 'b')"
Expand All @@ -76,13 +76,13 @@ umap.prep.config <- function(config=umap.defaults, ...) {
if (config$min_dist <=0) {
umap.error("setting 'min_dist' must be > 0")
}

# force some data types
for (x in c("n_epochs", "n_neighbors", "n_components",
"random_state", "negative_sample_rate", "transform_state")) {
config[[x]] <- as.integer(config[[x]])
}

# always give a metric name
if (is(config$metric, "function")) {
config$metric.function <- config$metric
Expand All @@ -106,10 +106,37 @@ umap.prep.config <- function(config=umap.defaults, ...) {
}
}

if (is.na(config$random_state)) {

## check n_jobs for method umap-learn
is_umap_learn <- "method" %in% names(config) &&
config$method == "umap-learn"
is_more_than_one_job <- F

if(is_umap_learn && "n_jobs" %in% names(config)) {
if(!is.numeric(config$n_jobs) | length(config$n_jobs) > 1)
umap.error("n_jobs must be one numeric value: ", config$n_jobs)

if(config$n_jobs < 1)
umap.error("n_jobs must be a positive integer: ", config$n_jobs)

if(!is.integer(config$n_jobs))
config$n_jobs <- as.integer(config$n_jobs)

if(config$n_jobs > 1L)
is_more_than_one_job <- T
}

if(is_umap_learn && !"n_jobs" %in% names(config)) {
config$n_jobs <- 1L
}

## set random state when NA and naive or n_jobs == 1
if (is.na(config$random_state) &&
(!is_umap_learn || !is_more_than_one_job)) {
config$random_state <- as.integer(runif(1, 0, 2^30))
}


config
}

Expand All @@ -133,15 +160,15 @@ umap.prep.input <- function(d, config) {
}
# ensure data is numeric (not integer or other data type)
d[, 1] <- as.numeric(d[, 1])

# perhaps adjust the data matrix
if (config$metric %in% c("pearson", "pearson2")) {
# for pearson correlation distance, center by-sample
# (this avoids computing means during correlations)
d <- t(d)
d <- t(d) - apply(d, 2, mean)
}

d
}

Expand Down

0 comments on commit 9106cac

Please sign in to comment.