Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add seed to netEmbedding function #1

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions R/analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,15 @@ computeNetSimilarityPairwise <- function(object, slot.name = "netP", type = c("f
#' @param min_dist This controls how tightly the embedding is allowed compress points together.
#' Larger values ensure embedded points are moreevenly distributed, while smaller values allow the
#' algorithm to optimise more accurately with regard to local structure. Sensible values are in the range 0.001 to 0.5.
#' @param seed An integer value indicating the random seed passed to \code{\link[base]{set.seed}}, use `NULL` to re-initializes seed. Defaults to 42.
#' @param ... Parameters passing to umap
#' @importFrom methods slot
#' @return
#' @export
#'
#' @examples
netEmbedding <- function(object, slot.name = "netP", type = c("functional","structural"), comparison = NULL, pathway.remove = NULL,
umap.method = c("umap-learn", "uwot"), n_neighbors = NULL,min_dist = 0.3,...) {
umap.method = c("umap-learn", "uwot"), n_neighbors = NULL, min_dist = 0.3, seed = 42,...) {
umap.method <- match.arg(umap.method)
if (object@options$mode == "single") {
comparison <- "single"
Expand All @@ -674,10 +675,16 @@ netEmbedding <- function(object, slot.name = "netP", type = c("functional","stru
n_neighbors <- ceiling(sqrt(dim(Similarity)[1])) + 1
}
options(warn = -1)

if (is.numeric(seed)) {
seed = as.integer(seed)
}

# dimension reduction
if (umap.method == "umap-learn") {
Y <- runUMAP(Similarity, min_dist = min_dist, n_neighbors = n_neighbors,...)
Y <- runUMAP(Similarity, min_dist = min_dist, n_neighbors = n_neighbors, seed.use = seed,...)
} else if (umap.method == "uwot") {
set.seed(seed)
Y <- uwot::umap(Similarity, min_dist = min_dist, n_neighbors = n_neighbors,...)
colnames(Y) <- paste0('UMAP', 1:ncol(Y))
rownames(Y) <- colnames(Similarity)
Expand Down
7 changes: 7 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,13 @@ runUMAP <- function(
stop("Cannot find UMAP, please install through pip (e.g. pip install umap-learn or reticulate::py_install(packages = 'umap-learn')).")
}
set.seed(seed.use)

# Generate random seed for Python when NULL
if (is.null(seed.use)) {
seed.use <- sample.int(.Machine$integer.max, 1L)
}
reticulate::py_set_seed(seed.use)

umap_import <- reticulate::import(module = "umap", delay_load = TRUE)
umap <- umap_import$UMAP(
n_neighbors = as.integer(n_neighbors),
Expand All @@ -860,6 +866,7 @@ runUMAP <- function(
local_connectivity = local_connectivity,
repulsion_strength = repulsion_strength,
negative_sample_rate = negative_sample_rate,
random_state = seed.use,
a = a,
b = b,
metric_kwds = metric_kwds,
Expand Down
3 changes: 3 additions & 0 deletions man/netEmbedding.Rd

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