Skip to content

Commit

Permalink
Try some better (maybe) c-iNMF factor selection method
Browse files Browse the repository at this point in the history
  • Loading branch information
mvfki committed Mar 10, 2024
1 parent 9c7607d commit c4410d9
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 196 deletions.
4 changes: 0 additions & 4 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ colNormalize_dense_cpp <- function(x, L) {
.Call(`_rliger2_colNormalize_dense_cpp`, x, L)
}

select_factor_cpp <- function(all_data, knn, threshold) {
.Call(`_rliger2_select_factor_cpp`, all_data, knn, threshold)
}

colAggregateMedian_dense_cpp <- function(x, group, n) {
.Call(`_rliger2_colAggregateMedian_dense_cpp`, x, group, n)
}
Expand Down
383 changes: 274 additions & 109 deletions R/cINMF.R

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions R/clustering.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ runCluster <- function(
niter = nIterations, nrep = nRandomStarts
)
} else {
edgeOutPath <- paste0("edge_", sub("\\s", "_", Sys.time()), '.txt')
edgeOutPath <- gsub("-", "", edgeOutPath)
edgeOutPath <- gsub(":", "", edgeOutPath)
edgeOutPath <- tempfile(pattern = "edge_", fileext = ".txt")
WriteEdgeFile(snn, edgeOutPath, display_progress = FALSE)
clusts <- RunModularityClusteringCpp(
snn,
Expand Down
32 changes: 23 additions & 9 deletions R/integration.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ runIntegration.Seurat <- function(
#' each element is the initial \eqn{V} matrix of each dataset. Default
#' \code{NULL}.
#' @param seed Random seed to allow reproducible results. Default \code{1}.
#' @param nCores The number of parallel tasks to speed up the computation.
#' Default \code{2L}. Only supported for platform with OpenMP support.
#' @param verbose Logical. Whether to show information of the progress. Default
#' \code{getOption("ligerVerbose")} or \code{TRUE} if users have not set.
#' @param ... Arguments passed to methods.
Expand Down Expand Up @@ -235,6 +237,7 @@ runINMF.liger <- function(
WInit = NULL,
VInit = NULL,
seed = 1,
nCores = 2L,
verbose = getOption("ligerVerbose", TRUE),
...
) {
Expand All @@ -260,6 +263,7 @@ runINMF.liger <- function(
WInit = WInit,
VInit = VInit,
seed = seed,
nCores = nCores,
verbose = verbose
)

Expand Down Expand Up @@ -305,6 +309,7 @@ runINMF.Seurat <- function(
WInit = NULL,
VInit = NULL,
seed = 1,
nCores = 2L,
verbose = getOption("ligerVerbose", TRUE),
...
) {
Expand Down Expand Up @@ -337,6 +342,7 @@ runINMF.Seurat <- function(
WInit = WInit,
VInit = VInit,
seed = seed,
nCores = nCores,
verbose = verbose
)
Hconcat <- t(Reduce(cbind, res$H))
Expand Down Expand Up @@ -371,6 +377,7 @@ runINMF.Seurat <- function(
WInit = NULL,
VInit = NULL,
seed = 1,
nCores = 2L,
verbose = getOption("ligerVerbose", TRUE)
) {
if (!requireNamespace("RcppPlanc", quietly = TRUE)) # nocov start
Expand All @@ -393,7 +400,7 @@ runINMF.Seurat <- function(
set.seed(seed = seed + i - 1)
out <- RcppPlanc::inmf(objectList = object, k = k, lambda = lambda,
niter = nIteration, Hinit = HInit,
Vinit = VInit, Winit = WInit,
Vinit = VInit, Winit = WInit, nCores = nCores,
verbose = verbose)
if (out$objErr < bestObj) {
bestResult <- out
Expand Down Expand Up @@ -591,6 +598,8 @@ optimizeALS <- function( # nocov start
#' @param minibatchSize Total number of cells in each minibatch. See detail.
#' Default \code{5000}.
#' @param seed Random seed to allow reproducible results. Default \code{1}.
#' @param nCores The number of parallel tasks to speed up the computation.
#' Default \code{2L}. Only supported for platform with OpenMP support.
#' @param verbose Logical. Whether to show information of the progress. Default
#' \code{getOption("ligerVerbose")} or \code{TRUE} if users have not set.
#' @param ... Arguments passed to other S3 methods of this function.
Expand Down Expand Up @@ -668,6 +677,7 @@ runOnlineINMF.liger <- function(
AInit = NULL,
BInit = NULL,
seed = 1,
nCores = 2L,
verbose = getOption("ligerVerbose", TRUE),
...
) {
Expand Down Expand Up @@ -748,7 +758,7 @@ runOnlineINMF.liger <- function(
minibatchSize = minibatchSize,
HALSiter = HALSiter, verbose = verbose,
WInit = WInit, VInit = VInit, AInit = AInit,
BInit = BInit, seed = seed)
BInit = BInit, seed = seed, nCores = nCores)
if (!isTRUE(projection)) {
# Scenario 1&2, everything updated
for (i in seq_along(object)) {
Expand Down Expand Up @@ -797,6 +807,7 @@ runOnlineINMF.liger <- function(
HALSiter = 1,
minibatchSize = 5000,
seed = 1,
nCores = 2L,
verbose = getOption("ligerVerbose", TRUE),
...
) {
Expand All @@ -819,7 +830,7 @@ runOnlineINMF.liger <- function(
minibatchSize = minibatchSize,
maxHALSIter = HALSiter, Vinit = VInit,
Winit = WInit, Ainit = AInit, Binit = BInit,
verbose = verbose)
nCores = nCores, verbose = verbose)
factorNames <- paste0("Factor_", seq(k))
if (isTRUE(projection)) {
# Scenario 3 only got H for new datasets
Expand Down Expand Up @@ -869,6 +880,7 @@ runOnlineINMF.Seurat <- function(
HALSiter = 1,
minibatchSize = 5000,
seed = 1,
nCores = 2L,
verbose = getOption("ligerVerbose", TRUE),
...
) {
Expand Down Expand Up @@ -896,6 +908,7 @@ runOnlineINMF.Seurat <- function(
newDatasets = NULL, projection = FALSE,
maxEpochs = maxEpochs, HALSiter = HALSiter,
minibatchSize = minibatchSize, seed = seed, verbose = verbose,
nCores = nCores,
WInit = NULL, VInit = NULL, AInit = NULL, BInit = NULL,
)
Hconcat <- t(Reduce(cbind, res$H))
Expand Down Expand Up @@ -1062,6 +1075,8 @@ online_iNMF <- function( # nocov start
#' the random seed by 1 for each consecutive restart, so future factorization
#' of the same dataset can be run with one rep if necessary. Default \code{1}.
#' @param seed Random seed to allow reproducible results. Default \code{1}.
#' @param nCores The number of parallel tasks to speed up the computation.
#' Default \code{2L}. Only supported for platform with OpenMP support.
#' @param verbose Logical. Whether to show information of the progress. Default
#' \code{getOption("ligerVerbose")} or \code{TRUE} if users have not set.
#' @param ... Arguments passed to other methods and wrapped functions.
Expand Down Expand Up @@ -1103,10 +1118,6 @@ runUINMF <- function(
object,
k = 20,
lambda = 5,
nIteration = 30,
nRandomStarts = 1,
seed = 1,
verbose = getOption("ligerVerbose", TRUE),
...
) {
UseMethod("runUINMF", object)
Expand All @@ -1122,6 +1133,7 @@ runUINMF.liger <- function(
nIteration = 30,
nRandomStarts = 1,
seed = 1,
nCores = 2L,
verbose = getOption("ligerVerbose", TRUE),
...
) {
Expand All @@ -1144,7 +1156,7 @@ runUINMF.liger <- function(
}
res <- .runUINMF.list(Elist, Ulist, k = k, lambda = lambda,
nIteration = nIteration,
nRandomStarts = nRandomStarts,
nRandomStarts = nRandomStarts, nCores = nCores,
seed = seed, verbose = verbose, ...)
for (d in names(object)) {
ld <- dataset(object, d)
Expand All @@ -1170,6 +1182,7 @@ runUINMF.liger <- function(
nIteration = 30,
nRandomStarts = 1,
seed = 1,
nCores = 2L,
verbose = getOption("ligerVerbose", TRUE)
) {
if (!requireNamespace("RcppPlanc", quietly = TRUE)) # nocov start
Expand All @@ -1189,7 +1202,8 @@ runUINMF.liger <- function(
seed <- seed + i - 1
set.seed(seed)
res <- RcppPlanc::uinmf(object, unsharedList, k = k, lambda = lambda,
niter = nIteration, verbose = verbose)
niter = nIteration, nCores = nCores,
verbose = verbose)
if (res$objErr < bestObj) {
bestRes <- res
bestObj <- res$objErr
Expand Down
29 changes: 18 additions & 11 deletions man/runCINMF.Rd

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

5 changes: 5 additions & 0 deletions man/runINMF.Rd

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

5 changes: 5 additions & 0 deletions man/runOnlineINMF.Rd

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

19 changes: 7 additions & 12 deletions man/runUINMF.Rd

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

Loading

0 comments on commit c4410d9

Please sign in to comment.