# Use TDA mapper to analyze known data sets.
# Author:  Isabel Darcy 
# some parts from https://cran.r-project.org/web/packages/TDAmapper/README.html
# Date:  Feb 5, 2017

# install.packages("TDAmapper")
library("TDAmapper")

# The fastcluster package is not necessary.  By loading the
# fastcluster package, the fastcluster::hclust() function 
# automatically replaces the slower stats::hclust() function
# whenever hclust() is called.
# install.packages("fastcluster") 
require(fastcluster) 

# The igraph package is necessary to view simplicial complexes
# (undirected graph) resulting from mapper1D().

library(igraph)

# create data set with 7 flares.

Noise1 <- cbind(runif(200, -20,20), runif(200, 4,5))
Noise2 <- cbind(runif(200, -5,20), runif(200, -5,-4))
Noise3 <- cbind(runif(200, -5,-4), runif(200, -20,20))
Noise4<- cbind(runif(200, 4,5), runif(200, -20,20))
Noise5<- cbind(runif(200, -5,5), runif(200, -5,5))

noise <- rbind(Noise1, Noise2, Noise3, Noise4, Noise5)
plot(noise, asp=1)

x <- noise[, 1]
y <- noise[, 2]
z <- sqrt(x^2 + y^2)


# Apply mapper with filter function = projection to x-axis

plot(noise, asp=1, col= x+20)

m1 <- mapper1D(
  distance_matrix = dist(data.frame(noise)),
  filter_values = x,
  num_intervals = 10,
  percent_overlap = 50,
  num_bins_when_clustering = 10)

# create and plot mapper graph
g1 <- graph.adjacency(m1$adjacency, mode="undirected")
plot(g1, layout = layout.auto(g1) )

## Apply mapper with filter function = projection to y-axis

plot(noise, asp=1, col= y+20)

m2 <- mapper1D(
  distance_matrix = dist(data.frame(noise)),
  filter_values = y,
  num_intervals = 10,
  percent_overlap = 50,
  num_bins_when_clustering = 10)

# create and plot mapper graph
g2 <- graph.adjacency(m2$adjacency, mode="undirected")
plot(g2, layout = layout.auto(g2) )

## Apply mapper with filter function = length

plot(noise, asp=1, col= z)

# Apply mapper to these 100 points
m3 <- mapper1D(
  distance_matrix = dist(data.frame(noise)),
  filter_values = z,
  num_intervals = 10,
  percent_overlap = 50,
  num_bins_when_clustering = 10)

# create and plot mapper graph
g3 <- graph.adjacency(m3$adjacency, mode="undirected")
plot(g3, layout = layout.auto(g3) )



#######################################################
## Apply mapper to circle data, filter = proj to x-axis.

library("TDA")

circle1 = circleUnif(100, r = 1)
x <- circle1[,1]

m4 <- mapper1D(
  distance_matrix = dist(data.frame(circle1)),
  filter_values = x,
  num_intervals = 10,
  percent_overlap = 50,
  num_bins_when_clustering = 10)

# create and plot mapper graph
g4 <- graph.adjacency(m4$adjacency, mode="undirected")
plot(g4, layout = layout.auto(g4) )

plot(circle1, asp=1, col=100)

## Apply mapper to theta shape, filter = proj to x-axis.

lin <- cbind(runif(50, -1,1), runif(50, -0.1,0.1))
theta <- rbind(circle1, lin)

x <- theta[,1]

# Apply mapper to these 100 points
m5 <- mapper1D(
  distance_matrix = dist(data.frame(theta)),
  filter_values = x,
  num_intervals = 10,
  percent_overlap = 50,
  num_bins_when_clustering = 10)

# create and plot mapper graph
g5 <- graph.adjacency(m5$adjacency, mode="undirected")
plot(g5, layout = layout.auto(g5) )




