forked from tdaverse/ggtda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.R
476 lines (418 loc) · 15.5 KB
/
persistence.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#' @title Persistence diagrams
#'
#' @description Visualize persistence data in a (flat, diagonal, or landscape)
#' persistence diagram.
#'
#' @details
#'
#' {*Persistence diagrams*} are
#' [scatterplots](https://ggplot2.tidyverse.org/reference/geom_point.html) of
#' persistence data.
#'
#' @template persistence-data
#'
#' @section Persistence diagrams:
#' Persistence diagrams recognize extended persistence data, with negative
#' birth/death values arising from the relative part of the filtration.
#'
#' The original persistence diagrams plotted persistence against birth in what
#' we call "flat" diagrams, but most plot death against birth in "diagonal"
#' diagrams, often with a diagonal line indicating zero persistence.
#'
#' The `geom_fundamental_box()` layer renders fundamental boxes at specified
#' time points (Chung & Lawson, 2020).
#'
#' @template ref-edelsbrunner2000
#' @template ref-edelsbrunner2012
#' @template ref-chung2020
#'
#' @eval rd_sec_aesthetics(
#' stat_persistence = StatPersistence,
#' geom_fundamental_box = GeomFundamentalBox
#' )
#' @eval rd_sec_computed_vars(
#' stat = "persistence",
#' start = "birth value of each feature (from 'dataset' aesthetic).",
#' end = "death value of each feature (from 'dataset' aesthetic).",
#' dimension = "integer feature dimension (from 'dataset' aesthetic).",
#' group = "interaction of existing 'group', dataset ID, and 'dimension'.",
#' id = "character feature identifier (across 'group').",
#' part =
#' "whether features belong to ordinary, relative, or extended homology.",
#' persistence =
#' "differences between birth and death values of features."
#' )
#' @name persistence
#' @import ggplot2
#' @family plot layers for persistence data
#' @seealso [ggplot2::layer()] for additional arguments.
#' @inheritParams ggplot2::layer
#' @param na.rm Logical: if `FALSE`, the default, `NA` lodes are not included;
#' if `TRUE`, `NA` lodes constitute a separate category, plotted in grey
#' (regardless of the color scheme).
#' @param ... Additional arguments passed to [ggplot2::layer()].
#' @param order_by A character vector of required or computed variables
#' (`"start"`, `"end"`, `"part"`, and/or `"persistence"`) by which the
#' features should be ordered (within `group`); defaults to `c("persistence",
#' "start")`. This will most notably impact the appearance of [barcode]s.
#' @param decreasing Logical; whether to sort features by decreasing values of
#' `order_by` (again, within `group`).
#' @param diagram One of `"flat"`, `"diagonal"`, or `"landscape"`; the
#' orientation for the diagram should take.
#' @param t A numeric vector of time points at which to place fundamental boxes.
# REVIEW: Single data set param?
# @param point_cloud Optional; a single data set for which methods exist to
# compute persistent homology. Alternatively, a list column of data sets can
# be passed to the `dataset` aesthetic.
#' @param filtration The type of filtration from which to compute persistent
#' homology; one of `"Rips"`, `"Vietoris"` (equivalent) or `"alpha"`.
#' @param diameter_max,radius_max Maximum diameter or radius for the simplicial
#' filtration. Both default to `NULL`, in which case the complete filtration
#' is constructed.
#' @param dimension_max Maximum dimension of the simplicial filtration.
#' @param field_order (Prime) order of the field over which to compute
#' persistent homology.
#' @param engine The computational engine to use (see 'Details'). Reasonable
#' defaults are chosen based on `filtration`.
#' @example inst/examples/ex-persistence.R
#' @example inst/examples/ex-persistence-extended.R
#' @example inst/examples/ex-persistence-dataset.R
NULL
# file.edit("tests/testthat/test-persistence.R")
# file.edit("inst/examples/ex-persistence.R")
# file.edit("inst/examples/ex-persistence-extended.R")
# file.edit("inst/examples/ex-persistence-dataset.R")
#' @rdname ggtda-ggproto
#' @format NULL
#' @usage NULL
#' @export
StatPersistence <- ggproto(
"StatPersistence", Stat,
required_aes = c("start|dataset", "end|dataset"),
# optional_aes = c("dataset"),
dropped_aes = c("start", "end"),
# only explicitly passed params
setup_params = function(data, params) {
if (is.null(data$dataset)) {
# discard unused parameters
dataset_params <- intersect(
names(data),
c("filtration", "diameter_max", "radius_max", "dimension_max",
"field_order")
)
if (length(dataset_params) > 0L) {
params_vec <- paste0("`", dataset_params, "`", collapse = ", ")
warning("Parameters ", params_vec,
" are only used with the `dataset` aesthetic.")
params <- params[setdiff(names(params), params_vec)]
}
} else {
# pre-process filtration parameters
# logic to deduce reasonable values of engine
# + issue warnings when choices are incompatible
params$filtration <-
match.arg(params$filtration, c("Vietoris", "Rips", "alpha"))
if (! is.null(params$engine)) params$engine <-
match.arg(params$engine, c("TDA", "GUDHI", "Dionysus", "ripserr"))
params$engine <-
assign_filtration_engine(params$filtration, params$engine)
}
# reconcile thresholds
if (is.null(params$radius_max) && is.null(params$diameter_max)) {
params$diameter_max <- Inf
}
if (! is.null(params$radius_max)) {
if (! is.null(params$diameter_max)) {
warning("Both `radius_max` and `diameter_max` were passed; ",
"only `diameter_max` value will be used.")
} else {
params$diameter_max <- params$radius_max * 2
}
}
# discard unrecognized feature properties with a warning
if (! all(params$order_by %in% order_by_options)) {
ignore_by <- setdiff(params$order_by, order_by_options)
warning(
"`order_by` recognizes only: `",
paste0(order_by_options, collapse = "`, `"),
"`; `",
paste0(ignore_by, collapse = "`, `"),
"` will be ignored."
)
params$order_by <- intersect(params$order_by, order_by_options)
}
params
},
setup_data = function(data, params) {
# REVIEW: Single data set param?
# # check if data was provided via 'point_cloud' argument
# if (! is.null(params$point_cloud)) {
#
# if (is.null(data$dataset)) {
#
# if (nrow(data) > 1) {
# # TODO: fix this message
# stop("The 'point_cloud' argument requires at most 1 row of `data`.")
# }
# data$dataset <- I(list(point_cloud))
#
# } else {
#
# warning(
# "An argument was passed to the 'dataset' aesthetic,",
# " so the 'point_cloud' argument will be ignored."
# )
# params$point_cloud <- NULL
#
# }
#
# }
if (! is.null(data$dataset)) {
if (! is.null(data$start) || ! is.null(data$end)) {
warning("Map to either `start` and `end` or to `dataset` aesthetic, ",
"not both. `dataset` will be used.")
data$end <- data$start <- NULL
}
# compute PH listwise
ph_list <- switch(
params$engine,
"TDA" = simplicial_filtration_TDA(
data$dataset, params$filtration,
params$diameter_max, params$dimension_max, params$field_order,
library = "GUDHI"
),
"GUDHI" = simplicial_filtration_TDA(
data$dataset, params$filtration,
params$diameter_max, params$dimension_max, params$field_order,
library = "GUDHI"
),
"Dionysus" = simplicial_filtration_TDA(
data$dataset, params$filtration,
params$diameter_max, params$dimension_max, params$field_order,
library = "Dionysus"
),
"ripserr" = simplicial_filtration_ripserr(
data$dataset,
params$diameter_max, params$dimension_max, params$field_order
)
)
# introduce identifier (and overwrite `dataset` column)
data$dataset <- seq(nrow(data))
for (i in seq_along(ph_list)) ph_list[[i]]$dataset <- i
# bind the list of output data frames
ph_data <- do.call(rbind, ph_list)
# merge persistent homology data back into original data
data <- merge(data, ph_data, by = "dataset")
# introduce or interact with 'group' aesthetic
data$group <- if (is.null(data$group)) {
interaction(as.character(data$dataset), data$dimension)
} else {
interaction(data$group, as.character(data$dataset), data$dimension)
}
data$dataset <- NULL
}
data
},
compute_panel = function(
data, scales,
order_by = c("persistence", "start"),
decreasing = FALSE,
diagram = "diagonal",
# REVIEW: Single data set param?
# point_cloud = NULL,
# 'dataset' aesthetic
filtration = "Rips",
diameter_max = NULL, radius_max = NULL, dimension_max = 1L,
field_order = 2L,
engine = NULL
) {
# points in cartesian coordinates (un-negated from opposite filtration)
data$x <- abs(data$start)
data$y <- abs(data$end)
# compute 'part'
data$part <- with(data, {
part <- NA_character_
part[start >= 0 & end >= 0] <- "ordinary"
part[start < 0 & end < 0] <- "relative"
part[start >= 0 & end < 0] <- "extended"
factor(part, levels = c("ordinary", "relative", "extended"))
})
# compute 'persistence'
data$persistence <- data$end - data$start
# (negative or infinite for extended points?)
# data$persistence <- ifelse(data$persistence < 0, Inf, data$persistence)
# computed variable: `id` (sort by `group`, then `order_by`)
interaction_args <- c(
# always sort first by `group`
if (! is.null(data$group)) list(data$group),
# sort by specified properties in order
lapply(order_by, \(f) if (decreasing) -xtfrm(data[[f]]) else data[[f]]),
# drop unused levels and use lexicographic order
list(drop = TRUE, lex.order = TRUE)
)
data$id <- do.call(interaction, args = interaction_args)
# re-distinguish duplicates
data$id <- order(order(data$id))
# diagram transformation
data <- diagram_transform(data, diagram)
# return point data
data
}
)
#' @rdname persistence
#' @export
stat_persistence <- function(mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
# REVIEW: Single data set param?
# point_cloud = NULL,
filtration = "Rips",
diameter_max = NULL, radius_max = NULL,
dimension_max = 1L,
field_order = 2L,
engine = NULL,
order_by = c("persistence", "start"),
decreasing = FALSE,
diagram = "diagonal",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...) {
layer(
stat = StatPersistence,
data = data,
mapping = mapping,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
# REVIEW: Single data set param?
# point_cloud = point_cloud,
filtration = filtration,
diameter_max = diameter_max, radius_max = radius_max,
dimension_max = dimension_max,
field_order = field_order,
engine = engine,
order_by = order_by,
decreasing = decreasing,
diagram = diagram,
na.rm = na.rm,
...
)
)
}
#' @rdname ggtda-ggproto
#' @format NULL
#' @usage NULL
#' @export
GeomFundamentalBox <- ggproto(
"GeomFundamentalBox", GeomPolygon,
required_aes = c(),
default_aes = aes(colour = "black", fill = "grey",
linewidth = 0.5, linetype = 1, alpha = .25),
setup_data = function(data, params) {
# keep only columns that are constant throughout the data
data <- dplyr::select_if(
data,
function(x) length(unique(x)) == 1L
)[1L, , drop = FALSE]
rownames(data) <- NULL
data
},
draw_panel = function(data, panel_params, coord,
order_by = c("persistence", "start"),
decreasing = FALSE,
diagram = "diagonal", t = NULL) {
# expand ranges if appropriate
# adapted from `GeomAbline$draw_panel`
ranges <- coord$backtransform_range(panel_params)
if (coord$clip == "on" && coord$is_linear()) {
#ranges$x <- ranges$x + c(-1, 1) * diff(ranges$x)
ranges$y <- ranges$y + c(-1, 1) * diff(ranges$y)
}
# define segments and interior in default (diagonal) layout
# use `group` to separate boxes for different times
data$x <- NULL; data$xend <- NULL
data$y <- NULL; data$yend <- NULL
data$group <- NULL
ray_data <- data.frame(
x = as.vector(rbind(0, t)), xend = rep(t, each = 2L),
y = rep(t, each = 2L), yend = as.vector(rbind(t, ranges$y[[2L]])),
group = rep(seq_along(t), each = 2L)
)
ray_data <- merge(ray_data, data)
ray_data$group <- -1L
int_data <- data.frame(
x = as.vector(rbind(t, t, 0, 0)),
y = as.vector(rbind(t, ranges$y[[2L]], ranges$y[[2L]], t)),
group = rep(seq_along(t), each = 4L)
)
int_data <- merge(int_data, data)
int_data$colour <- "transparent"
# diagram transformations
ray_data <- diagram_transform(ray_data, diagram)
int_data <- diagram_transform(int_data, diagram)
# combine grobs for rays and interior
ray_grob <- GeomSegment$draw_panel(ray_data, panel_params, coord)
int_grob <- GeomPolygon$draw_panel(int_data, panel_params, coord)
grob <- do.call(grid::grobTree, list(ray_grob, int_grob))
grob$name <- grid::grobName(grob, "geom_fundamental_box")
grob
},
draw_key = draw_key_blank,
# https://www.tidyverse.org/blog/2022/08/ggplot2-3-4-0-size-to-linewidth/
non_missing_aes = "size",
rename_size = TRUE
)
#' @rdname persistence
#' @export
geom_fundamental_box <- function(mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
diagram = "diagonal",
t = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...) {
layer(
geom = GeomFundamentalBox,
data = data,
mapping = mapping,
stat = stat,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
diagram = diagram,
t = t,
na.rm = na.rm,
...
)
)
}
# Helper functions ---------------------------------------------------------
order_by_options <- c("start", "end", "part", "persistence")
diagram_transform <- function(data, diagram) {
switch(
match.arg(diagram, c("flat", "diagonal", "landscape")),
flat = transform(
data,
y = data$y - data$x
),
diagonal = data,
landscape = transform(
data,
x = (data$x + data$y) / 2,
y = ifelse(
is.infinite(data$x) & is.infinite(data$y),
# accommodate landscape horizons
0,
(data$y - data$x) / 2
)
)
)
}