# 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, -30,30), runif(200, 5,6))
Noise2 <- cbind(runif(200, -6,30), runif(200, -6,-5))
Noise3 <- cbind(runif(200, -6,-5), runif(200, -20,20))
Noise4<- cbind(runif(200, 5,6), runif(200, -20,20))
Noise5<- cbind(runif(200, -6,6), runif(200, -6,6))

flares <- rbind(Noise1, Noise2, Noise3, Noise4, Noise5)
plot(flares, asp=1)


## Apply a linear transformation to circl1 data

# create 2x2 matrix
A <- matrix(c(1, 2, 2, 0), nrow=2, ncol=2) 
C <- t(flares)  # take the transpose of circle1 data
M <- A %*% C     # multiply A and C
E <- eigen(A)    # calculate eigenvalues and eigenvectors of A
E                # display E (note its data structure)

plot(t(M), asp=1)  # note M is the image of circle C under map A


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) )
