-
Notifications
You must be signed in to change notification settings - Fork 0
/
3D_TDA.R
executable file
·221 lines (196 loc) · 7.93 KB
/
3D_TDA.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
library("TDA")
library("rgl")
library("scatterplot3d")
charge_data <- file.choose()
#Read in the file
inputted.file <- data.frame(read.csv(charge_data), header = TRUE)
x.grid <- data.frame(inputted.file[1:100,c(1,4,5)], stringsAsFactors = FALSE)
#Creates categorical variables for nonnumerical columns
# if (is.factor(x.grid)){
# x.grid <- as.numeric(unlist(x.grid))
# task.data <- x.grid[,1]
# task.data.f <- factor(task.data, levels = as.character(unique(task.data)),
# labels=c(1:length(unique(task.data))))
# task.data.f <- as.numeric(task.data.f)
#
# group.data <- x.grid[,2]
# group.data.f <- factor(group.data, levels = as.character(unique(group.data)),
# labels=c(1:length(unique(group.data))))
# group.data.f <- as.numeric(group.data.f)
#
# x.grid[,1] <- task.data.f
# x.grid[,2] <- group.data.f
# }
x.grid <- data.frame(x.grid)
Xlim <- c(-1.5, 1.45)
Ylim <- c(-1.5, 1.45)
by <- 0.06
Xseq <- seq(from = Xlim[1], to = Xlim[2], by = by)
Yseq <- seq(from = Ylim[1], to = Ylim[2], by = by)
total.grid <- expand.grid(Xseq, Yseq)
#distance <- distFct(X = x.grid, Grid = total.grid)
distance <- dist(x.grid)
#DTM (distance to measure) is measured by this complicated math formula
#calculate DTM for every point in Grid:
m0 <- 0.1
DTM <- dtm(X = x.grid, Grid = x.grid, m0 = m0)
#calculates nearest neighbor
k <- 60
kNN <- knnDE(X = x.grid, Grid = x.grid, k = k)
#estimates density?
h <- 0.3
KDE <- kde(X = x.grid, Grid = x.grid, h = h)
#estimates distance
h <- 0.3
Kdist <- kernelDist(X = x.grid, Grid = x.grid, h = h)
#color changes based on z value
z<-matrix(DTM, ncol = length(Yseq), nrow = length(Xseq))
nbcol = 100
color = rev(rainbow(nbcol, start = 0/6, end = 4/6))
zcol = cut(z, nbcol)
persp3d(Xseq, Yseq, z, xlim = NULL, ylim = NULL, zlim = NULL, col = color[zcol],
xlab = NULL, ylab = NULL, zlab = NULL, add = FALSE,
forceClipregion = FALSE)
tree <- clusterTree(x.grid, k, density = "knn")
tree.KDE <- clusterTree(x.grid, k, h = 0.3, density = "kde")
#plots lambda trees
plot(tree, type = "lambda", color = NULL, add = FALSE, main = "knn lambda tree")
plot(tree.KDE, type = "lambda", main = "lambda Tree (kde)")
# plot clusters
plot(x.grid, pch = 19, cex = 0.6, main = "cluster labels")
for (i in tree[["id"]]){
points(matrix(x.grid[tree[["DataPoints"]][[i]]], ncol = 2), col = i, pch = 19,
cex = 0.6)
}
#b/w scatterplot
scatterplot3d(x.grid, pch = 16)
#blue interactive scatterplot
colors <- c('#4AC6B7', '#1972A4', '#965F8A', '#FF7070', '#C61951')
plot_ly(x.grid, x = x.grid[,1], y = x.grid[,2], z = x.grid[,3],
marker = list(symbol = 'circle'), colors = colors,
sizes = c(5, 10)) %>%
layout(scene = list(gridcolor = 'rgb(255, 255, 255)',
range = c(2.003297660701705, 5.191505530708712),
type = 'log', zerolinewidth = 1, ticklen = 5, gridwidth = 2))
#surface plot
plot_ly(z = as.matrix(as.data.frame(lapply(x.grid, as.numeric)))) %>% add_surface(
contours = list(
z = list(
show=TRUE,
usecolormap=TRUE,
highlightcolor="#ff0000",
project=list(z=TRUE)
)
)) %>% layout(
scene = list(
camera=list(
eye = list(x=1.87, y=0.88, z=-0.64)
)
)
)
par(mfrow = c(1, 1))
scatter3D(x.grid[,1], x.grid[,2], x.grid[,3], phi = 0, bty = "n",
type = "b", ticktype = "detailed", pch = 20,
cex = c(0.5, 1, 1.5))
p <- plot_ly(x.grid, x = x.grid[,1], y = x.grid[,2], z = x.grid[,3],
type = 'scatter3d', mode = 'lines+markers', opacity = 1,
line = list(width = 6, reverscale = FALSE),
marker = list(size = 3.5, colorscale = 'Greens', cmin = -20, cmax = 50))
p
# NOTE: find some way to accomodate multiple dimensions into 2D info graph/etc
# band <- bootstrapBand(X = x.grid, FUN = kde, Grid = total.grid, B = 100,
# parallel = FALSE, alpha = 0.1, h = h)
#
# #computes the persistent homology of the superlevel sets
# #if trying other functions, FUN and k/h/m0 must line up
# DiagGrid <- gridDiag(X = x.grid, FUN = knnDE, k = 60,
# lim = cbind(Xlim, Ylim), by = 0.1,
# sublevel = FALSE, library = "Dionysus", location = TRUE,
# printProgress = FALSE)
#
# plot(DiagGrid[["diagram"]], band = 2 * band[["width"]],
# main = "KDE Diagram")
#
# par(mfrow = c(1, 2), mai = c(0.8, 0.8, 0.3, 0.1))
# plot(DiagGrid[["diagram"]], rotated = TRUE, band = band[["width"]],
# main = "Rotated Diagram")
# plot(DiagGrid[["diagram"]], barcode = TRUE, main = "Barcode")
#
# max.scale <- 3 # limit of the filtration
# max.dimension <- 1 # components and loops
# #0 for components, 1 for loops, 2 for voids, etc.
#
# DiagRips <- ripsDiag(X = total.grid, max.dimension, max.scale,
# library = c("GUDHI", "Dionysus"), location = TRUE,
# printProgress = FALSE)
#
# plot(DiagRips[["diagram"]], rotated = TRUE, band = band[["width"]],
# main = "Rotated Diagram")
# plot(DiagRips[["diagram"]], barcode = TRUE, main = "Barcode")
#
# # persistence diagram of alpha complex
# DiagAlphaCmplx <- alphaComplexDiag(X = total.grid,
# library = c("GUDHI", "Dionysus"),
# location = TRUE, printProgress = TRUE)
#
# # plot
# par(mfrow = c(1, 2))
# plot(DiagAlphaCmplx[["diagram"]], main = "Alpha complex persistence diagram")
# one <- which(DiagAlphaCmplx[["diagram"]][, 1] == 1)
# one <- one[which.max( + DiagAlphaCmplx[["diagram"]][one, 3] -
# DiagAlphaCmplx[["diagram"]][one, 2])]
# plot(total.grid, col = 1, main = "Representative loop")
# for (i in seq(along = one)) {
# for (j in seq_len(dim(DiagAlphaCmplx[["cycleLocation"]][[one[i]]])[1])) {
# lines(DiagAlphaCmplx[["cycleLocation"]][[one[i]]][j, , ],
# pch = 19, cex = 1, col = i + 1)
# }
# }
# par(mfrow = c(1, 1))
#
# n <- 30
# x.grid <- cbind(circleUnif(n = n), runif(n = n, min = -0.1, max = 0.1))
#
# DiagAlphaShape <- alphaShapeDiag(X = total.grid, maxdimension = 1,
# library = c("GUDHI", "Dionysus"),
# location = TRUE, printProgress = TRUE)
#
# par(mfrow = c(1, 2))
# plot(DiagAlphaShape[["diagram"]], main = "Alpha complex persistence diagram")
# one <- which(DiagAlphaShape[["diagram"]][, 1] == 1)
# one <- one[which.max( + DiagAlphaShape[["diagram"]][one, 3] -
# DiagAlphaShape[["diagram"]][one, 2])]
# plot(total.grid, col = 1, main = "Representative loop")
# for (i in seq(along = one)) {
# for (j in seq_len(dim(DiagAlphaShape[["cycleLocation"]][[one[i]]])[1])) {
# lines(DiagAlphaShape[["cycleLocation"]][[one[i]]][j, , ],
# pch = 19, cex = 1, col = i + 1)
# }
# }
#
# max.scale <- 0.4
# # limit of the filtration
# max.dimension <- 1
# # components and loops
# FltRips <- ripsFiltration(X = total.grid, maxdimension = max.dimension,
# maxscale = max.scale, dist = "euclidean",
# library = "GUDHI",
# printProgress = TRUE)
#
# #another alpha persistance diagram?
# DiagAlphaShape <- alphaShapeDiag(X = x.grid, printProgress = FALSE)
# plot(DiagAlphaShape[["diagram"]], main = "Persistance Diagram")
#
# #bottleneck and wasserstein distances
# Diag1 <- ripsDiag(x.grid[,1], maxdimension = 1, maxscale = 5)
# Diag2 <- ripsDiag(x.grid[,2], maxdimension = 1, maxscale = 5)
#
# print (bottleneck(Diag1[["diagram"]], Diag2[["diagram"]],dimension = 1))
# print (wasserstein(Diag1[["diagram"]], Diag2[["diagram"]], p = 2, dimension = 1))
#
# #landscape and silhouettes
# tseq <- seq(0, maxscale, length = 1000) #domain
# Diag <- ripsDiag(X = x.grid, maxdimension, maxscale,library = "GUDHI",
# printProgress = FALSE)
# Land <- landscape(Diag[["diagram"]], dimension = 1, KK = 1, tseq)
# Sil <- silhouette(Diag[["diagram"]], p = 1, dimension = 1, tseq)