---
title: "Synthetic experiments for Mercat"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
source('../R/mercat.R', echo=F, chdir=T)
source('../R/mercat_exp_metrics.R', echo=F, chdir=T)

library(umap)
library(Rtsne)
library(densvis)
library(rtracklayer)
library(plyranges)
library(gridExtra)
library(rjson)

library(reticulate)
ncvis <- reticulate::import('ncvis')
ncvis_obj <- ncvis$NCVis(d=as.integer(2), n_neighbors=as.integer(15))
```

## Simulated data examples

```{r toy examples}
## k is number of clusters
## n is number of samples per cluster (single number) or vectors of cluster sizes
## d is number of dimensions to embed the clusters in
## S_var is vector of cluster-specific variances (so, has k entries) (default: NULL)
gen_clusters_mvrnorm <- function(k, n, d, S_var=NULL)
{
  if (length(n) == 1)
  {
    n <- rep(n, k)
  }
  dat <- do.call(rbind,lapply(1:k, function(i)
  {
    mu <- runif(d, min=-10, max=10)
    if (is.null(S_var))
    {
      S <- diag(runif(d, min=.5, max=2))
    } else {
      S <- diag(rep(S_var[i], d))
    }
    mvrnorm(n[i], mu, Sigma = S)
  }))
  return(dat)
}
# Same arguments as above, generate uniform data in a hypercube (S_var is used as scaling factor for edge length)
gen_clusters_unif <- function(k, n, d, S_var=NULL)
{
  if (length(n) == 1)
  {
    n <- rep(n, k)
  }
  if (is.null(S_var))
  {
    S <- rep(1, k)
  }
  dat <- do.call(rbind,lapply(1:k, function(i)
  {
    mu <- runif(d, min=-10, max=10)
    sweep(do.call(cbind, lapply(1:d, function(dim_idx)
    {
      runif(n[i], 0, S[i])
    })), 2, mu)
  }))
  return(dat)
}


iter <- 1000
stepsize <- c(350,1200)
lr <- 0.01
nrep <- 3

rep_res_eval <- lapply(1:nrep, function (seed)
{
  set.seed(seed)
  
  ## Example 0:5 clusters of equal (uniform) distribution with different center
  XU <- gen_clusters_unif(5, 100, 50)
  clusterlabels <- rep(1:5, each=100)
  shuffle <- sample(nrow(XU), nrow(XU))
  XU <- XU[shuffle,]
  clusterlabels <- clusterlabels[shuffle]
  
  start.time <- Sys.time()
  mercat_res <- mercat(XU, iter=iter, lr=lr, stepsize = stepsize, batch_size = 64, angle_subsample = 64)
  end.time <- Sys.time()
  mercat_time <- end.time - start.time

  
  consideriter <- seq(1,length(mercat_res$Y), 3)
  all_Y <- as.data.frame(do.call(rbind, lapply(consideriter, function(l){mercat_res$Y[[l]]})))
  all_Y <- rbind(mercat_res$Y_init, all_Y)
  colnames(all_Y) <- c("Y1", "Y2", "Y3")
  all_Y$iteration <- rep(c(0, consideriter), each=nrow(mercat_res$Y[[1]]))
  all_Y$cluster <- rep(clusterlabels, length(consideriter)+1)
  
  print(plot_ly(x=all_Y$Y1, y=all_Y$Y2, z=all_Y$Y3, mode="markers", color=all_Y$cluster, size=.5,
          frame=all_Y$iteration))
  
  Y_rot <- optimal_rot_vis(mercat_res$Y[[iter]])
  print(plot_mercat_2D(YtoR_plain(Y_rot), clusterlabels))
  print(plot_moll_2D(YtoR_plain(Y_rot), clusterlabels))
  
  start.time <- Sys.time()
  umap_XU <- umap::umap(XU)$layout
  end.time <- Sys.time()
  umap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(umap1=umap_XU[,1],
                          umap2=umap_XU[,2],
                          cluster=clusterlabels),
               aes(x=umap1, y=umap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  tsne_XU <- Rtsne(XU)$Y
  end.time <- Sys.time()
  tsne_time <- end.time - start.time
  
  gg <- ggplot(data.frame(tsne1=tsne_XU[,1],
                          tsne2=tsne_XU[,2],
                          cluster=clusterlabels),
               aes(x=tsne1, y=tsne2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  ncvis_XU <- ncvis_obj$fit_transform(XU)
  end.time <- Sys.time()
  ncvis_time <- end.time - start.time
  
  gg <- ggplot(data.frame(ncvis1=ncvis_XU[,1],
                          ncvis2=ncvis_XU[,2],
                          cluster=clusterlabels),
               aes(x=ncvis1, y=ncvis2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  densmap_XU <- densmap(XU, n_components=2L, init='spectral', random_state=as.integer(seed))
  end.time <- Sys.time()
  densmap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(densmap1=densmap_XU[,1],
                          densmap2=densmap_XU[,2],
                          cluster=clusterlabels),
               aes(x=densmap1, y=densmap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  
  ar_mercat <- angle_cor_eval(XU, mercat_res$Y[[length(mercat_res$Y)]], geodesic=T)
  dr_mercat <- sphere_dist_rank_eval(XU, mercat_res$Y[[length(mercat_res$Y)]])
  kn_mercat <- ngbh_eval(XU, mercat_res$Y[[length(mercat_res$Y)]])
  dp_mercat <- density_eval(XU, mercat_res$Y[[length(mercat_res$Y)]])
  
  ar_umap <- angle_cor_eval(XU, umap_XU)
  dr_umap <- eucl_dist_rank_eval(XU, umap_XU)
  kn_umap <- ngbh_eval(XU, umap_XU)
  dp_umap <- density_eval(XU, umap_XU)
  
  ar_tsne <- angle_cor_eval(XU, tsne_XU)
  dr_tsne <- eucl_dist_rank_eval(XU, tsne_XU)
  kn_tsne <- ngbh_eval(XU, tsne_XU)
  dp_tsne <- density_eval(XU, tsne_XU)
  
  ar_ncvis <- angle_cor_eval(XU, ncvis_XU)
  dr_ncvis <- eucl_dist_rank_eval(XU, ncvis_XU)
  kn_ncvis <- ngbh_eval(XU, ncvis_XU)
  dp_ncvis <- density_eval(XU, ncvis_XU)
  
  ar_densmap <- angle_cor_eval(XU, densmap_XU)
  dr_densmap <- eucl_dist_rank_eval(XU, densmap_XU)
  kn_densmap <- ngbh_eval(XU, densmap_XU)
  dp_densmap <- density_eval(XU, densmap_XU)
  
  metric_df_XU <- data.frame(Mercat=c(ar_mercat, dr_mercat, kn_mercat, dp_mercat, mercat_time),
                          UMAP=c(ar_umap, dr_umap, kn_umap, dp_umap, umap_time),
                          tSNE=c(ar_tsne, dr_tsne, kn_tsne, dp_tsne, tsne_time),
                          NCVis=c(ar_ncvis, dr_ncvis, kn_ncvis, dp_ncvis, ncvis_time),
                          DensMAP=c(ar_densmap, dr_densmap, kn_densmap, dp_densmap, densmap_time)
  )
  rownames(metric_df_XU) <- c("Angle", "Distance", "Neighborhood", "Density", "Runtime")
  plot(tableGrob(metric_df_XU))
  

  ## Example 1: 5 clusters of equal (gaussian) distribution with different center
  X1 <- gen_clusters_mvrnorm(5, 100, 50)
  clusterlabels <- rep(1:5, each=100)
  shuffle <- sample(nrow(X1), nrow(X1))
  X1 <- X1[shuffle,]
  clusterlabels <- clusterlabels[shuffle]
  
  start.time <- Sys.time()
  mercat_res <- mercat(X1, iter=iter, lr=lr, stepsize = stepsize, batch_size = 64, angle_subsample = 64)
  end.time <- Sys.time()
  mercat_time <- end.time - start.time
  
  consideriter <- seq(1,length(mercat_res$Y), 3)
  all_Y <- as.data.frame(do.call(rbind, lapply(consideriter, function(l){mercat_res$Y[[l]]})))
  all_Y <- rbind(mercat_res$Y_init, all_Y)
  colnames(all_Y) <- c("Y1", "Y2", "Y3")
  all_Y$iteration <- rep(c(0, consideriter), each=nrow(mercat_res$Y[[1]]))
  all_Y$cluster <- rep(clusterlabels, length(consideriter)+1)
  
  print(plot_ly(x=all_Y$Y1, y=all_Y$Y2, z=all_Y$Y3, mode="markers", color=all_Y$cluster, size=.5,
          frame=all_Y$iteration))
  
  Y_rot <- optimal_rot_vis(mercat_res$Y[[iter]])
  print(plot_mercat_2D(YtoR_plain(Y_rot), clusterlabels))
  print(plot_moll_2D(YtoR_plain(Y_rot), clusterlabels))
  
  start.time <- Sys.time()
  umap_X1 <- umap::umap(X1)$layout
  end.time <- Sys.time()
  umap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(umap1=umap_X1[,1],
                          umap2=umap_X1[,2],
                          cluster=clusterlabels),
               aes(x=umap1, y=umap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  tsne_X1 <- Rtsne(X1)$Y
  end.time <- Sys.time()
  tsne_time <- end.time - start.time
  
  gg <- ggplot(data.frame(tsne1=tsne_X1[,1],
                          tsne2=tsne_X1[,2],
                          cluster=clusterlabels),
               aes(x=tsne1, y=tsne2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  ncvis_X1 <- ncvis_obj$fit_transform(X1)
  end.time <- Sys.time()
  ncvis_time <- end.time - start.time
  
  gg <- ggplot(data.frame(ncvis1=ncvis_X1[,1],
                          ncvis2=ncvis_X1[,2],
                          cluster=clusterlabels),
               aes(x=ncvis1, y=ncvis2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  densmap_X1 <- densmap(X1, n_components=2L, init='spectral', random_state=as.integer(seed))
  end.time <- Sys.time()
  densmap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(densmap1=densmap_X1[,1],
                          densmap2=densmap_X1[,2],
                          cluster=clusterlabels),
               aes(x=densmap1, y=densmap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  
  ar_mercat <- angle_cor_eval(X1, mercat_res$Y[[length(mercat_res$Y)]], geodesic=T)
  dr_mercat <- sphere_dist_rank_eval(X1, mercat_res$Y[[length(mercat_res$Y)]])
  kn_mercat <- ngbh_eval(X1, mercat_res$Y[[length(mercat_res$Y)]])
  dp_mercat <- density_eval(X1, mercat_res$Y[[length(mercat_res$Y)]])
  
  ar_umap <- angle_cor_eval(X1, umap_X1)
  dr_umap <- eucl_dist_rank_eval(X1, umap_X1)
  kn_umap <- ngbh_eval(X1, umap_X1)
  dp_umap <- density_eval(X1, umap_X1)
  
  ar_tsne <- angle_cor_eval(X1, tsne_X1)
  dr_tsne <- eucl_dist_rank_eval(X1, tsne_X1)
  kn_tsne <- ngbh_eval(X1, tsne_X1)
  dp_tsne <- density_eval(X1, tsne_X1)
  
  ar_ncvis <- angle_cor_eval(X1, ncvis_X1)
  dr_ncvis <- eucl_dist_rank_eval(X1, ncvis_X1)
  kn_ncvis <- ngbh_eval(X1, ncvis_X1)
  dp_ncvis <- density_eval(X1, ncvis_X1)
  
  ar_densmap <- angle_cor_eval(X1, densmap_X1)
  dr_densmap <- eucl_dist_rank_eval(X1, densmap_X1)
  kn_densmap <- ngbh_eval(X1, densmap_X1)
  dp_densmap <- density_eval(X1, densmap_X1)
  
  metric_df_X1 <- data.frame(Mercat=c(ar_mercat, dr_mercat, kn_mercat, dp_mercat, mercat_time),
                          UMAP=c(ar_umap, dr_umap, kn_umap, dp_umap, umap_time),
                          tSNE=c(ar_tsne, dr_tsne, kn_tsne, dp_tsne, tsne_time),
                          NCVis=c(ar_ncvis, dr_ncvis, kn_ncvis, dp_ncvis, ncvis_time),
                          DensMAP=c(ar_densmap, dr_densmap, kn_densmap, dp_densmap, densmap_time)
  )
  rownames(metric_df_X1) <- c("Angle", "Distance", "Neighborhood", "Density", "Runtime")
  plot(tableGrob(metric_df_X1))
  
  
  ## Example 2: 10 clusters of equal (gaussian) distribution with different center
  X2 <- gen_clusters_mvrnorm(10, 100, 50)
  clusterlabels <- rep(1:10, each=100)
  shuffle <- sample(nrow(X2), nrow(X2))
  X2 <- X2[shuffle,]
  clusterlabels <- clusterlabels[shuffle]
  
  start.time <- Sys.time()
  mercat_res <- mercat(X2, iter=iter, lr=lr, stepsize = stepsize, batch_size = 64, angle_subsample = 64)
  end.time <- Sys.time()
  mercat_time <- end.time - start.time
  
  consideriter <- seq(1,length(mercat_res$Y), 3)
  all_Y <- as.data.frame(do.call(rbind, lapply(consideriter, function(l){mercat_res$Y[[l]]})))
  all_Y <- rbind(mercat_res$Y_init, all_Y)
  colnames(all_Y) <- c("Y1", "Y2", "Y3")
  all_Y$iteration <- rep(c(0, consideriter), each=nrow(mercat_res$Y[[1]]))
  all_Y$cluster <- rep(clusterlabels, length(consideriter)+1)
  
  print(plot_ly(x=all_Y$Y1, y=all_Y$Y2, z=all_Y$Y3, mode="markers", color=all_Y$cluster, size=.5,
          frame=all_Y$iteration))
  
  Y_rot <- optimal_rot_vis(mercat_res$Y[[iter]])
  print(plot_mercat_2D(YtoR_plain(Y_rot), clusterlabels))
  print(plot_moll_2D(YtoR_plain(Y_rot), clusterlabels))
  
  start.time <- Sys.time()
  umap_X2 <- umap::umap(X2)$layout
  end.time <- Sys.time()
  umap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(umap1=umap_X2[,1],
                          umap2=umap_X2[,2],
                          cluster=clusterlabels),
               aes(x=umap1, y=umap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  tsne_X2 <- Rtsne(X2)$Y
  end.time <- Sys.time()
  tsne_time <- end.time - start.time
  
  gg <- ggplot(data.frame(tsne1=tsne_X2[,1],
                          tsne2=tsne_X2[,2],
                          cluster=clusterlabels),
               aes(x=tsne1, y=tsne2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  ncvis_X2 <- ncvis_obj$fit_transform(X2)
  end.time <- Sys.time()
  ncvis_time <- end.time - start.time
  
  gg <- ggplot(data.frame(ncvis1=ncvis_X2[,1],
                          ncvis2=ncvis_X2[,2],
                          cluster=clusterlabels),
               aes(x=ncvis1, y=ncvis2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  densmap_X2 <- densmap(X2, n_components=2L, init='spectral', random_state=as.integer(seed))
  end.time <- Sys.time()
  densmap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(densmap1=densmap_X2[,1],
                          densmap2=densmap_X2[,2],
                          cluster=clusterlabels),
               aes(x=densmap1, y=densmap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  ar_mercat <- angle_cor_eval(X2, mercat_res$Y[[length(mercat_res$Y)]], geodesic=T)
  dr_mercat <- sphere_dist_rank_eval(X2, mercat_res$Y[[length(mercat_res$Y)]])
  kn_mercat <- ngbh_eval(X2, mercat_res$Y[[length(mercat_res$Y)]])
  dp_mercat <- density_eval(X2, mercat_res$Y[[length(mercat_res$Y)]])
  
  ar_umap <- angle_cor_eval(X2, umap_X2)
  dr_umap <- eucl_dist_rank_eval(X2, umap_X2)
  kn_umap <- ngbh_eval(X2, umap_X2)
  dp_umap <- density_eval(X2, umap_X2)
  
  ar_tsne <- angle_cor_eval(X2, tsne_X2)
  dr_tsne <- eucl_dist_rank_eval(X2, tsne_X2)
  kn_tsne <- ngbh_eval(X2, tsne_X2)
  dp_tsne <- density_eval(X2, tsne_X2)
  
  ar_ncvis <- angle_cor_eval(X2, ncvis_X2)
  dr_ncvis <- eucl_dist_rank_eval(X2, ncvis_X2)
  kn_ncvis <- ngbh_eval(X2, ncvis_X2)
  dp_ncvis <- density_eval(X2, ncvis_X2)
  
  ar_densmap <- angle_cor_eval(X2, densmap_X2)
  dr_densmap <- eucl_dist_rank_eval(X2, densmap_X2)
  kn_densmap <- ngbh_eval(X2, densmap_X2)
  dp_densmap <- density_eval(X2, densmap_X2)
  
  metric_df_X2 <- data.frame(Mercat=c(ar_mercat, dr_mercat, kn_mercat, dp_mercat, mercat_time),
                          UMAP=c(ar_umap, dr_umap, kn_umap, dp_umap, umap_time),
                          tSNE=c(ar_tsne, dr_tsne, kn_tsne, dp_tsne, tsne_time),
                          NCVis=c(ar_ncvis, dr_ncvis, kn_ncvis, dp_ncvis, ncvis_time),
                          DensMAP=c(ar_densmap, dr_densmap, kn_densmap, dp_densmap, densmap_time)
  )
  rownames(metric_df_X2) <- c("Angle", "Distance", "Neighborhood", "Density", "Runtime")
  plot(tableGrob(metric_df_X2))
  
  
  ## Example 3: 5 clusters of equal (gaussian) distribution with different center and different numbers of samples each
  clustersizes <- c(50,100,150,200,250)
  X3 <- gen_clusters_mvrnorm(5, clustersizes, 50)
  clusterlabels <- do.call(c, sapply(1:5, function(i){rep(i,clustersizes[i])}))
  shuffle <- sample(nrow(X3), nrow(X3))
  X3 <- X3[shuffle,]
  clusterlabels <- clusterlabels[shuffle]
  
  start.time <- Sys.time()
  mercat_res <- mercat(X3, iter=iter, lr=lr, stepsize = stepsize, batch_size = 64, angle_subsample = 64)
  end.time <- Sys.time()
  mercat_time <- end.time - start.time
  
  consideriter <- seq(1,length(mercat_res$Y), 3)
  all_Y <- as.data.frame(do.call(rbind, lapply(consideriter, function(l){mercat_res$Y[[l]]})))
  all_Y <- rbind(mercat_res$Y_init, all_Y)
  colnames(all_Y) <- c("Y1", "Y2", "Y3")
  all_Y$iteration <- rep(c(0, consideriter), each=nrow(mercat_res$Y[[1]]))
  all_Y$cluster <- rep(clusterlabels, length(consideriter)+1)
  
  print(plot_ly(x=all_Y$Y1, y=all_Y$Y2, z=all_Y$Y3, mode="markers", color=all_Y$cluster, size=.5,
          frame=all_Y$iteration))
  
  Y_rot <- optimal_rot_vis(mercat_res$Y[[iter]])
  print(plot_mercat_2D(YtoR_plain(Y_rot), clusterlabels))
  print(plot_moll_2D(YtoR_plain(Y_rot), clusterlabels))
  
  start.time <- Sys.time()
  umap_X3 <- umap::umap(X3)$layout
  end.time <- Sys.time()
  umap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(umap1=umap_X3[,1],
                          umap2=umap_X3[,2],
                          cluster=clusterlabels),
               aes(x=umap1, y=umap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  tsne_X3 <- Rtsne(X3)$Y
  end.time <- Sys.time()
  tsne_time <- end.time - start.time
  
  gg <- ggplot(data.frame(tsne1=tsne_X3[,1],
                          tsne2=tsne_X3[,2],
                          cluster=clusterlabels),
               aes(x=tsne1, y=tsne2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  ncvis_X3 <- ncvis_obj$fit_transform(X3)
  end.time <- Sys.time()
  ncvis_time <- end.time - start.time
  
  gg <- ggplot(data.frame(ncvis1=ncvis_X3[,1],
                          ncvis2=ncvis_X3[,2],
                          cluster=clusterlabels),
               aes(x=ncvis1, y=ncvis2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  densmap_X3 <- densmap(X3, n_components=2L, init='spectral', random_state=as.integer(seed))
  end.time <- Sys.time()
  densmap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(densmap1=densmap_X3[,1],
                          densmap2=densmap_X3[,2],
                          cluster=clusterlabels),
               aes(x=densmap1, y=densmap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  ar_mercat <- angle_cor_eval(X3, mercat_res$Y[[length(mercat_res$Y)]], geodesic=T)
  dr_mercat <- sphere_dist_rank_eval(X3, mercat_res$Y[[length(mercat_res$Y)]])
  kn_mercat <- ngbh_eval(X3, mercat_res$Y[[length(mercat_res$Y)]])
  dp_mercat <- density_eval(X3, mercat_res$Y[[length(mercat_res$Y)]])
  
  ar_umap <- angle_cor_eval(X3, umap_X3)
  dr_umap <- eucl_dist_rank_eval(X3, umap_X3)
  kn_umap <- ngbh_eval(X3, umap_X3)
  dp_umap <- density_eval(X3, umap_X3)
  
  ar_tsne <- angle_cor_eval(X3, tsne_X3)
  dr_tsne <- eucl_dist_rank_eval(X3, tsne_X3)
  kn_tsne <- ngbh_eval(X3, tsne_X3)
  dp_tsne <- density_eval(X3, tsne_X3)
  
  ar_ncvis <- angle_cor_eval(X3, ncvis_X3)
  dr_ncvis <- eucl_dist_rank_eval(X3, ncvis_X3)
  kn_ncvis <- ngbh_eval(X3, ncvis_X3)
  dp_ncvis <- density_eval(X3, ncvis_X3)
  
  ar_densmap <- angle_cor_eval(X3, densmap_X3)
  dr_densmap <- eucl_dist_rank_eval(X3, densmap_X3)
  kn_densmap <- ngbh_eval(X3, densmap_X3)
  dp_densmap <- density_eval(X3, densmap_X3)
  
  metric_df_X3 <- data.frame(Mercat=c(ar_mercat, dr_mercat, kn_mercat, dp_mercat, mercat_time),
                          UMAP=c(ar_umap, dr_umap, kn_umap, dp_umap, umap_time),
                          tSNE=c(ar_tsne, dr_tsne, kn_tsne, dp_tsne, tsne_time),
                          NCVis=c(ar_ncvis, dr_ncvis, kn_ncvis, dp_ncvis, ncvis_time),
                          DensMAP=c(ar_densmap, dr_densmap, kn_densmap, dp_densmap, densmap_time)
  )
  rownames(metric_df_X3) <- c("Angle", "Distance", "Neighborhood", "Density", "Runtime")
  plot(tableGrob(metric_df_X3))
  
  
  ## Example 4: 5 clusters of (gaussian) distribution with different center and different scale
  S_var <- 1:5
  X4 <- gen_clusters_mvrnorm(5, 100, 50, S_var=S_var)
  clusterlabels <- rep(1:5, each=100)
  shuffle <- sample(nrow(X4), nrow(X4))
  X4 <- X4[shuffle,]
  clusterlabels <- clusterlabels[shuffle]
  
  start.time <- Sys.time()
  mercat_res <- mercat(X4, iter=iter, lr=lr, stepsize = stepsize, batch_size = 64, angle_subsample = 64)
  end.time <- Sys.time()
  mercat_time <- end.time - start.time
  
  consideriter <- seq(1,length(mercat_res$Y), 3)
  all_Y <- as.data.frame(do.call(rbind, lapply(consideriter, function(l){mercat_res$Y[[l]]})))
  all_Y <- rbind(mercat_res$Y_init, all_Y)
  colnames(all_Y) <- c("Y1", "Y2", "Y3")
  all_Y$iteration <- rep(c(0, consideriter), each=nrow(mercat_res$Y[[1]]))
  all_Y$cluster <- rep(clusterlabels, length(consideriter)+1)
  
  print(plot_ly(x=all_Y$Y1, y=all_Y$Y2, z=all_Y$Y3, mode="markers", color=all_Y$cluster, size=.5,
          frame=all_Y$iteration))
  
  # print(plot_mercat_2D(mercat_res$R[[iter]], clusterlabels))
  # print(plot_moll_2D(mercat_res$R[[iter]], clusterlabels))
  Y_rot <- optimal_rot_vis(mercat_res$Y[[iter]])
  print(plot_mercat_2D(YtoR_plain(Y_rot), clusterlabels))
  print(plot_moll_2D(YtoR_plain(Y_rot), clusterlabels))
  
  start.time <- Sys.time()
  umap_X4 <- umap::umap(X4)$layout
  end.time <- Sys.time()
  umap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(umap1=umap_X4[,1],
                          umap2=umap_X4[,2],
                          cluster=clusterlabels),
               aes(x=umap1, y=umap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  tsne_X4 <- Rtsne(X4)$Y
  end.time <- Sys.time()
  tsne_time <- end.time - start.time
  
  gg <- ggplot(data.frame(tsne1=tsne_X4[,1],
                          tsne2=tsne_X4[,2],
                          cluster=clusterlabels),
               aes(x=tsne1, y=tsne2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  ncvis_X4 <- ncvis_obj$fit_transform(X4)
  end.time <- Sys.time()
  ncvis_time <- end.time - start.time
  
  gg <- ggplot(data.frame(ncvis1=ncvis_X4[,1],
                          ncvis2=ncvis_X4[,2],
                          cluster=clusterlabels),
               aes(x=ncvis1, y=ncvis2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  start.time <- Sys.time()
  densmap_X4 <- densmap(X4, n_components=2L, init='spectral', random_state=as.integer(seed))
  end.time <- Sys.time()
  densmap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(densmap1=densmap_X4[,1],
                          densmap2=densmap_X4[,2],
                          cluster=clusterlabels),
               aes(x=densmap1, y=densmap2, color=cluster))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis() + theme(legend.position = "none")
  print(gg)
  
  ar_mercat <- angle_cor_eval(X4, mercat_res$Y[[length(mercat_res$Y)]], geodesic=T)
  dr_mercat <- sphere_dist_rank_eval(X4, mercat_res$Y[[length(mercat_res$Y)]])
  kn_mercat <- ngbh_eval(X4, mercat_res$Y[[length(mercat_res$Y)]])
  dp_mercat <- density_eval(X4, mercat_res$Y[[length(mercat_res$Y)]])
  
  ar_umap <- angle_cor_eval(X4, umap_X4)
  dr_umap <- eucl_dist_rank_eval(X4, umap_X4)
  kn_umap <- ngbh_eval(X4, umap_X4)
  dp_umap <- density_eval(X4, umap_X4)
  
  ar_tsne <- angle_cor_eval(X4, tsne_X4)
  dr_tsne <- eucl_dist_rank_eval(X4, tsne_X4)
  kn_tsne <- ngbh_eval(X4, tsne_X4)
  dp_tsne <- density_eval(X4, tsne_X4)
  
  ar_ncvis <- angle_cor_eval(X4, ncvis_X4)
  dr_ncvis <- eucl_dist_rank_eval(X4, ncvis_X4)
  kn_ncvis <- ngbh_eval(X4, ncvis_X4)
  dp_ncvis <- density_eval(X4, ncvis_X4)
  
  ar_densmap <- angle_cor_eval(X4, densmap_X4)
  dr_densmap <- eucl_dist_rank_eval(X4, densmap_X4)
  kn_densmap <- ngbh_eval(X4, densmap_X4)
  dp_densmap <- density_eval(X4, densmap_X4)
  
  metric_df_X4 <- data.frame(Mercat=c(ar_mercat, dr_mercat, kn_mercat, dp_mercat, mercat_time),
                          UMAP=c(ar_umap, dr_umap, kn_umap, dp_umap, umap_time),
                          tSNE=c(ar_tsne, dr_tsne, kn_tsne, dp_tsne, tsne_time),
                          NCVis=c(ar_ncvis, dr_ncvis, kn_ncvis, dp_ncvis, ncvis_time),
                          DensMAP=c(ar_densmap, dr_densmap, kn_densmap, dp_densmap, densmap_time)
  )
  rownames(metric_df_X4) <- c("Angle", "Distance", "Neighborhood", "Density", "Runtime")
  plot(tableGrob(metric_df_X4))
  
  
  
  ## Example of circle data
  a_sample <- runif(900, 0, 2*pi)
  r <- 3
  X_circle <- cbind(r*cos(a_sample), r*sin(a_sample))
  X_circle <- X_circle + cbind(rnorm(900, 0, 0.01), rnorm(900, 0, 0.01))
  
  gg <- ggplot(data.frame(X=X_circle[,1], Y=X_circle[,2]), aes(x=X,y=Y))
  gg <- gg + theme_void() + geom_point(size=.5)
  print(gg)
  
  start.time <- Sys.time()
  mercat_res <- mercat(X_circle, iter=iter, lr=lr, stepsize = stepsize, batch_size = 64, angle_subsample = 64)
  end.time <- Sys.time()
  mercat_time <- end.time - start.time
  
  
  consideriter <- seq(1,length(mercat_res$Y), 3)
  all_Y <- as.data.frame(do.call(rbind, lapply(consideriter, function(l){mercat_res$Y[[l]]})))
  all_Y <- rbind(mercat_res$Y_init, all_Y)
  colnames(all_Y) <- c("Y1", "Y2", "Y3")
  all_Y$iteration <- rep(c(0, consideriter), each=nrow(mercat_res$Y[[1]]))
  
  print(plot_ly(x=all_Y$Y1, y=all_Y$Y2, z=all_Y$Y3, mode="markers", size=.5,
          frame=all_Y$iteration))
  
  Y_rot <- optimal_rot_vis(mercat_res$Y[[iter]])
  print(plot_mercat_2D(YtoR_plain(Y_rot)))
  print(plot_moll_2D(YtoR_plain(Y_rot)))
  
  
  start.time <- Sys.time()
  umap_X_circle <- umap::umap(X_circle)$layout
  end.time <- Sys.time()
  umap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(umap1=umap_X_circle[,1],
                          umap2=umap_X_circle[,2]),
               aes(x=umap1, y=umap2))
  gg <- gg + theme_void() + geom_point(alpha=.7)
  print(gg)
  
  start.time <- Sys.time()
  tsne_X_circle <- Rtsne(X_circle)$Y
  end.time <- Sys.time()
  tsne_time <- end.time - start.time
  
  gg <- ggplot(data.frame(tsne1=tsne_X_circle[,1],
                          tsne2=tsne_X_circle[,2]),
               aes(x=tsne1, y=tsne2))
  gg <- gg + theme_void() + geom_point(alpha=.7)
  print(gg)
  
  start.time <- Sys.time()
  ncvis_X_circle <- ncvis_obj$fit_transform(X_circle)
  end.time <- Sys.time()
  ncvis_time <- end.time - start.time
  
  gg <- ggplot(data.frame(ncvis1=ncvis_X_circle[,1],
                          ncvis2=ncvis_X_circle[,2]),
               aes(x=ncvis1, y=ncvis2))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis()
  print(gg)
  
  start.time <- Sys.time()
  densmap_X_circle <- densmap(X_circle, n_components=2L, init='spectral', random_state=as.integer(seed))
  end.time <- Sys.time()
  densmap_time <- end.time - start.time
  
  gg <- ggplot(data.frame(densmap1=densmap_X_circle[,1],
                          densmap2=densmap_X_circle[,2]),
               aes(x=densmap1, y=densmap2))
  gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis()
  print(gg)
  
  ar_mercat <- angle_cor_eval(X_circle, mercat_res$Y[[length(mercat_res$Y)]], geodesic=T)
  dr_mercat <- sphere_dist_rank_eval(X_circle, mercat_res$Y[[length(mercat_res$Y)]])
  kn_mercat <- ngbh_eval(X_circle, mercat_res$Y[[length(mercat_res$Y)]])
  dp_mercat <- density_eval(X_circle, mercat_res$Y[[length(mercat_res$Y)]])
  
  ar_umap <- angle_cor_eval(X_circle, umap_X_circle)
  dr_umap <- eucl_dist_rank_eval(X_circle, umap_X_circle)
  kn_umap <- ngbh_eval(X_circle, umap_X_circle)
  dp_umap <- density_eval(X_circle, umap_X_circle)
  
  ar_tsne <- angle_cor_eval(X_circle, tsne_X_circle)
  dr_tsne <- eucl_dist_rank_eval(X_circle, tsne_X_circle)
  kn_tsne <- ngbh_eval(X_circle, tsne_X_circle)
  dp_tsne <- density_eval(X_circle, tsne_X_circle)
  
  ar_ncvis <- angle_cor_eval(X_circle, ncvis_X_circle)
  dr_ncvis <- eucl_dist_rank_eval(X_circle, ncvis_X_circle)
  kn_ncvis <- ngbh_eval(X_circle, ncvis_X_circle)
  dp_ncvis <- density_eval(X_circle, ncvis_X_circle)
  
  ar_densmap <- angle_cor_eval(X_circle, densmap_X_circle)
  dr_densmap <- eucl_dist_rank_eval(X_circle, densmap_X_circle)
  kn_densmap <- ngbh_eval(X_circle, densmap_X_circle)
  dp_densmap <- density_eval(X_circle, densmap_X_circle)
  
  metric_df_X_circle <- data.frame(Mercat=c(ar_mercat, dr_mercat, kn_mercat, dp_mercat, mercat_time),
                          UMAP=c(ar_umap, dr_umap, kn_umap, dp_umap, umap_time),
                          tSNE=c(ar_tsne, dr_tsne, kn_tsne, dp_tsne, tsne_time),
                          NCVis=c(ar_ncvis, dr_ncvis, kn_ncvis, dp_ncvis, ncvis_time),
                          DensMAP=c(ar_densmap, dr_densmap, kn_densmap, dp_densmap, densmap_time)
  )
  rownames(metric_df_X_circle) <- c("Angle", "Distance", "Neighborhood", "Density", "Runtime")
  plot(tableGrob(metric_df_X_circle))

  
  return(list(XU=metric_df_XU,
              X1=metric_df_X1,
              X2=metric_df_X2,
              X3=metric_df_X3,
              X4=metric_df_X4,
              X_circle=metric_df_X_circle))
})

XU_info <- do.call(cbind, lapply(1:ncol(rep_res_eval[[1]]$XU), function(colIdx)
{
  do.call(rbind, lapply(1:nrow(rep_res_eval[[1]]$XU), function(rowIdx)
  {
    paste0(as.character(round(mean(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$XU[rowIdx,colIdx]}), na.rm=T), 3)),
           '±',
           as.character(round(sd(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$XU[rowIdx,colIdx]}), na.rm=T), 3)))
  }))
}))
colnames(XU_info) <- colnames(rep_res_eval[[1]]$XU)
rownames(XU_info) <- rownames(rep_res_eval[[1]]$XU)
plot(tableGrob(as.data.frame(XU_info)))

X1_info <- do.call(cbind, lapply(1:ncol(rep_res_eval[[1]]$X1), function(colIdx)
{
  do.call(rbind, lapply(1:nrow(rep_res_eval[[1]]$X1), function(rowIdx)
  {
    paste0(as.character(round(mean(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X1[rowIdx,colIdx]})), 3)),
           '±',
           as.character(round(sd(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X1[rowIdx,colIdx]})), 3)))
  }))
}))
colnames(X1_info) <- colnames(rep_res_eval[[1]]$X1)
rownames(X1_info) <- rownames(rep_res_eval[[1]]$X1)
plot(tableGrob(as.data.frame(X1_info)))

X2_info <- do.call(cbind, lapply(1:ncol(rep_res_eval[[1]]$X2), function(colIdx)
{
  do.call(rbind, lapply(1:nrow(rep_res_eval[[1]]$X2), function(rowIdx)
  {
    paste0(as.character(round(mean(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X2[rowIdx,colIdx]})), 3)),
           '±',
           as.character(round(sd(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X2[rowIdx,colIdx]})), 3)))
  }))
}))
colnames(X2_info) <- colnames(rep_res_eval[[1]]$X2)
rownames(X2_info) <- rownames(rep_res_eval[[1]]$X2)
plot(tableGrob(as.data.frame(X2_info)))

X3_info <- do.call(cbind, lapply(1:ncol(rep_res_eval[[1]]$X3), function(colIdx)
{
  do.call(rbind, lapply(1:nrow(rep_res_eval[[1]]$X3), function(rowIdx)
  {
    paste0(as.character(round(mean(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X3[rowIdx,colIdx]})), 3)),
           '±',
           as.character(round(sd(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X3[rowIdx,colIdx]})), 3)))
  }))
}))
colnames(X3_info) <- colnames(rep_res_eval[[1]]$X3)
rownames(X3_info) <- rownames(rep_res_eval[[1]]$X3)
plot(tableGrob(as.data.frame(X3_info)))

X4_info <- do.call(cbind, lapply(1:ncol(rep_res_eval[[1]]$X4), function(colIdx)
{
  do.call(rbind, lapply(1:nrow(rep_res_eval[[1]]$X4), function(rowIdx)
  {
    paste0(as.character(round(mean(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X4[rowIdx,colIdx]})), 3)),
           '±',
           as.character(round(sd(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X4[rowIdx,colIdx]})), 3)))
  }))
}))
colnames(X4_info) <- colnames(rep_res_eval[[1]]$X4)
rownames(X4_info) <- rownames(rep_res_eval[[1]]$X4)
plot(tableGrob(as.data.frame(X4_info)))

X_circle_info <- do.call(cbind, lapply(1:ncol(rep_res_eval[[1]]$X_circle), function(colIdx)
{
  do.call(rbind, lapply(1:nrow(rep_res_eval[[1]]$X3), function(rowIdx)
  {
    paste0(as.character(round(mean(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X_circle[rowIdx,colIdx]})), 3)),
           '±',
           as.character(round(sd(sapply(1:length(rep_res_eval), function(i){rep_res_eval[[i]]$X_circle[rowIdx,colIdx]})), 3)))
  }))
}))
colnames(X_circle_info) <- colnames(rep_res_eval[[1]]$X_circle)
rownames(X_circle_info) <- rownames(rep_res_eval[[1]]$X_circle)
plot(tableGrob(as.data.frame(X_circle_info)))

```

## More involved simulated manifolds

```{r toy manifold examples}
## n is number of samples
gen_smiley <- function(n)
{
  eyes.r = 0.1*sqrt(runif(n/4, 0, 1))
  eyes.theta = runif(n/4,0, 2*pi)
  eyes.sample = cbind(eyes.r*sin(eyes.theta),eyes.r*cos(eyes.theta))
  eyes.sample[1:(n/8),]= eyes.sample[1:(n/8),] + c(0.25,0.25)
  eyes.sample[-(1:(n/8)),1]= eyes.sample[-(1:(n/8)),1] - 0.25
  eyes.sample[-(1:(n/8)),2]= eyes.sample[-(1:(n/8)),2] + 0.25
  plot(eyes.sample)
  
  face.r = sqrt(runif(n/2, 0.9^2, 1))
  face.theta = runif(n/2,0, 2*pi)
  face.sample = cbind(face.r*sin(face.theta),face.r*cos(face.theta))
  plot(face.sample)
  
  mouth.r = sqrt(runif(n/4, 0.45^2, 0.55^2))
  mouth.theta = runif(n/4,0, pi)
  mouth.sample = cbind(mouth.r*cos(mouth.theta), -mouth.r*sin(mouth.theta))
  plot(mouth.sample)
  
  data=rbind(eyes.sample, face.sample, mouth.sample)
  data=data*2
  r=2
  return(data)
}



iter <- 1000
stepsize <- c(350,1200)
lr <- 0.01

## smiley data

X_smiley <- gen_smiley(3000)

gg <- ggplot(data.frame(X=X_smiley[,1], Y=X_smiley[,2]), aes(x=X,y=Y))
gg <- gg + theme_void() + geom_point(size=.5)
print(gg)

mercat_res <- mercat(X_smiley, iter=iter, lr=lr, stepsize = stepsize, batch_size = 64, angle_subsample = 64)

consideriter <- seq(1,length(mercat_res$Y), 3)
all_Y <- as.data.frame(do.call(rbind, lapply(consideriter, function(l){mercat_res$Y[[l]]})))
all_Y <- rbind(mercat_res$Y_init, all_Y)
colnames(all_Y) <- c("Y1", "Y2", "Y3")
all_Y$iteration <- rep(c(0, consideriter), each=nrow(mercat_res$Y[[1]]))

print(plot_ly(x=all_Y$Y1, y=all_Y$Y2, z=all_Y$Y3, mode="markers", size=.5,
              frame=all_Y$iteration))

Y_rot <- optimal_rot_vis(mercat_res$Y[[iter]])
print(plot_mercat_2D(YtoR_plain(Y_rot)))
print(plot_moll_2D(YtoR_plain(Y_rot)))


umap_X_smiley <- umap::umap(X_smiley)$layout

gg <- ggplot(data.frame(umap1=umap_X_smiley[,1],
                        umap2=umap_X_smiley[,2]),
             aes(x=umap1, y=umap2))
gg <- gg + theme_void() + geom_point(alpha=.7)
print(gg)

tsne_X_smiley <- Rtsne(X_smiley)$Y

gg <- ggplot(data.frame(tsne1=tsne_X_smiley[,1],
                        tsne2=tsne_X_smiley[,2]),
             aes(x=tsne1, y=tsne2))
gg <- gg + theme_void() + geom_point(alpha=.7)
print(gg)

ncvis_X_smiley <- ncvis_obj$fit_transform(X_smiley)

gg <- ggplot(data.frame(ncvis1=ncvis_X_smiley[,1],
                        ncvis2=ncvis_X_smiley[,2]),
             aes(x=ncvis1, y=ncvis2))
gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis()
print(gg)

densmap_X_smiley <- densmap(X_smiley, n_components=2L, init='spectral')

gg <- ggplot(data.frame(densmap1=densmap_X_smiley[,1],
                        densmap2=densmap_X_smiley[,2]),
             aes(x=densmap1, y=densmap2))
gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis()
print(gg)

ar_mercat <- angle_cor_eval(X_smiley, mercat_res$Y[[length(mercat_res$Y)]], geodesic=T)
dr_mercat <- sphere_dist_rank_eval(X_smiley, mercat_res$Y[[length(mercat_res$Y)]])
kn_mercat <- ngbh_eval(X_smiley, mercat_res$Y[[length(mercat_res$Y)]])
dp_mercat <- density_eval(X_smiley, mercat_res$Y[[length(mercat_res$Y)]])

ar_umap <- angle_cor_eval(X_smiley, umap_X_smiley)
dr_umap <- eucl_dist_rank_eval(X_smiley, umap_X_smiley)
kn_umap <- ngbh_eval(X_smiley, umap_X_smiley)
dp_umap <- density_eval(X_smiley, umap_X_smiley)

ar_tsne <- angle_cor_eval(X_smiley, tsne_X_smiley)
dr_tsne <- eucl_dist_rank_eval(X_smiley, tsne_X_smiley)
kn_tsne <- ngbh_eval(X_smiley, tsne_X_smiley)
dp_tsne <- density_eval(X_smiley, tsne_X_smiley)

ar_ncvis <- angle_cor_eval(X_smiley, ncvis_X_smiley)
dr_ncvis <- eucl_dist_rank_eval(X_smiley, ncvis_X_smiley)
kn_ncvis <- ngbh_eval(X_smiley, ncvis_X_smiley)
dp_ncvis <- density_eval(X_smiley, ncvis_X_smiley)

ar_densmap <- angle_cor_eval(X_smiley, densmap_X_smiley)
dr_densmap <- eucl_dist_rank_eval(X_smiley, densmap_X_smiley)
kn_densmap <- ngbh_eval(X_smiley, densmap_X_smiley)
dp_densmap <- density_eval(X_smiley, densmap_X_smiley)

metric_df_X_smiley <- data.frame(Mercat=c(ar_mercat, dr_mercat, kn_mercat, dp_mercat),
                                 UMAP=c(ar_umap, dr_umap, kn_umap, dp_umap),
                                 tSNE=c(ar_tsne, dr_tsne, kn_tsne, dp_tsne),
                                 NCVis=c(ar_ncvis, dr_ncvis, kn_ncvis, dp_ncvis),
                                 DensMAP=c(ar_densmap, dr_densmap, kn_densmap, dp_densmap)
)
rownames(metric_df_X_smiley) <- c("Angle", "Distance", "Neighborhood", "Density")
plot(tableGrob(round(metric_df_X_smiley,3)))

rm(mercat_res)
rm(umap_X_smiley)
rm(tsne_X_smiley)
rm(ncvis_X_smiley)
rm(densmap_X_smiley)
gc()

## mammoth data
X_mammoth <- fromJSON(file = "../data/mammoth_3d.json")
X_mammoth <- matrix(unlist(X_mammoth), ncol=3, byrow=T)
X_mammoth <- X_mammoth/sd(X_mammoth[,1])
plot_ly(x=X_mammoth[,3], y=X_mammoth[,1], z=X_mammoth[,2], mode="markers", size=.5)

mercat_res <- mercat(X_mammoth, iter=iter, lr=lr, stepsize = stepsize, batch_size = 64, angle_subsample = 64)

consideriter <- seq(1,length(mercat_res$Y), 5)
all_Y <- as.data.frame(do.call(rbind, lapply(consideriter, function(l){mercat_res$Y[[l]]})))
all_Y <- rbind(mercat_res$Y_init, all_Y)
colnames(all_Y) <- c("Y1", "Y2", "Y3")
all_Y$iteration <- rep(c(0, consideriter), each=nrow(mercat_res$Y[[1]]))

print(plot_ly(x=all_Y$Y1, y=all_Y$Y2, z=all_Y$Y3, mode="markers", size=.5,
              frame=all_Y$iteration))

Y_rot <- optimal_rot_vis(mercat_res$Y[[iter]])
#print(plot_mercat_2D(YtoR_plain(Y_rot)), long0=pi)
print(plot_mercat_2D(YtoR_plain(Y_rot)))
print(plot_moll_2D(YtoR_plain(Y_rot)))


umap_X_mammoth <- umap::umap(X_mammoth)$layout

gg <- ggplot(data.frame(umap1=umap_X_mammoth[,1],
                        umap2=umap_X_mammoth[,2]),
             aes(x=umap1, y=umap2))
gg <- gg + theme_void() + geom_point(alpha=.7)
print(gg)

tsne_X_mammoth <- Rtsne(X_mammoth)$Y

gg <- ggplot(data.frame(tsne1=tsne_X_mammoth[,1],
                        tsne2=tsne_X_mammoth[,2]),
             aes(x=tsne1, y=tsne2))
gg <- gg + theme_void() + geom_point(alpha=.7)
print(gg)

ncvis_X_mammoth <- ncvis_obj$fit_transform(X_mammoth)

gg <- ggplot(data.frame(ncvis1=ncvis_X_mammoth[,1],
                        ncvis2=ncvis_X_mammoth[,2]),
             aes(x=ncvis1, y=ncvis2))
gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis()
print(gg)

densmap_X_mammoth <- densmap(X_mammoth, n_components=2L, init='spectral')

gg <- ggplot(data.frame(densmap1=densmap_X_mammoth[,1],
                        densmap2=densmap_X_mammoth[,2]),
             aes(x=densmap1, y=densmap2))
gg <- gg + theme_void() + geom_point(alpha=.7) + scale_color_viridis()
print(gg)

ar_mercat <- angle_cor_eval(X_mammoth, mercat_res$Y[[length(mercat_res$Y)]], geodesic=T)
dr_mercat <- sphere_dist_rank_eval(X_mammoth, mercat_res$Y[[length(mercat_res$Y)]])
kn_mercat <- ngbh_eval(X_mammoth, mercat_res$Y[[length(mercat_res$Y)]])
dp_mercat <- density_eval(X_mammoth, mercat_res$Y[[length(mercat_res$Y)]])

ar_umap <- angle_cor_eval(X_mammoth, umap_X_mammoth)
dr_umap <- eucl_dist_rank_eval(X_mammoth, umap_X_mammoth)
kn_umap <- ngbh_eval(X_mammoth, umap_X_mammoth)
dp_umap <- density_eval(X_mammoth, umap_X_mammoth)

ar_tsne <- angle_cor_eval(X_mammoth, tsne_X_mammoth)
dr_tsne <- eucl_dist_rank_eval(X_mammoth, tsne_X_mammoth)
kn_tsne <- ngbh_eval(X_mammoth, tsne_X_mammoth)
dp_tsne <- density_eval(X_mammoth, tsne_X_mammoth)

ar_ncvis <- angle_cor_eval(X_mammoth, ncvis_X_mammoth)
dr_ncvis <- eucl_dist_rank_eval(X_mammoth, ncvis_X_mammoth)
kn_ncvis <- ngbh_eval(X_mammoth, ncvis_X_mammoth)
dp_ncvis <- density_eval(X_mammoth, ncvis_X_mammoth)

ar_densmap <- angle_cor_eval(X_mammoth, densmap_X_mammoth)
dr_densmap <- eucl_dist_rank_eval(X_mammoth, densmap_X_mammoth)
kn_densmap <- ngbh_eval(X_mammoth, densmap_X_mammoth)
dp_densmap <- density_eval(X_mammoth, densmap_X_mammoth)

metric_df_X_mammoth <- data.frame(Mercat=c(ar_mercat, dr_mercat, kn_mercat, dp_mercat),
                                 UMAP=c(ar_umap, dr_umap, kn_umap, dp_umap),
                                 tSNE=c(ar_tsne, dr_tsne, kn_tsne, dp_tsne),
                                 NCVis=c(ar_ncvis, dr_ncvis, kn_ncvis, dp_ncvis),
                                 DensMAP=c(ar_densmap, dr_densmap, kn_densmap, dp_densmap)
)
rownames(metric_df_X_mammoth) <- c("Angle", "Distance", "Neighborhood", "Density")
plot(tableGrob(round(metric_df_X_mammoth,3)))

```