R/cluster.R

Defines functions cluster_phate

Documented in cluster_phate

#' KMeans on the PHATE potential

#' Clustering on the PHATE operator as introduced in Moon et al.
#' This is similar to spectral clustering.
#' 
#' @param phate `phate()` output
#' @param k Number of clusters (default: 8)
#' @param seed Random seed for kmeans (default: NULL)
#' 
#' @return clusters Integer vector of cluster assignments
#' 
#' @examples
#' if (reticulate::py_module_available("phate")) {
#'
#' # Load data
#' # data(tree.data)
#' # We use a smaller tree to make examples run faster
#' data(tree.data.small)
#'
#' # Run PHATE
#' phate.tree <- phate(tree.data.small$data)
#' 
#' # Clustering
#' cluster_phate(phate.tree)
#' }
#' @export
cluster_phate <- function(phate, k=8, seed=NULL) {
  # check installation
  if (!reticulate::py_module_available(module = "phate") || (is.null(pyphate))) load_pyphate()
  if (!methods::is(phate, "phate")) {
    stop(paste0("Expected phate_op to be a phate object. Got a ", class(phate)))
  }
  k <- as.integer(k)
  if (is.numeric(seed)) {
    seed <- as.integer(seed)
  } else if (!is.null(seed) && is.na(seed)) {
    seed <- NULL
  }
  pyphate()$cluster$kmeans(phate$operator, n_clusters=k, random_state=seed)
}

Try the phateR package in your browser

Any scripts or data that you put into this service are public.

phateR documentation built on Feb. 12, 2021, 5:12 p.m.