Skip to content

Commit 7112558

Browse files
authored
Add function to list available edges (#256)
* Add function list_caugi_edges()
1 parent 6216937 commit 7112558

5 files changed

Lines changed: 75 additions & 0 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export(is_valid_adjustment_admg)
6464
export(is_valid_backdoor)
6565
export(latent_project)
6666
export(length)
67+
export(list_caugi_edges)
6768
export(m_separated)
6869
export(markov_blanket)
6970
export(meek_closure)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# caugi (development version)
22

3+
## New Features
4+
5+
- Add `list_caugi_edges()` function to list all available edge types.
6+
37
## Improvements
48

59
- Improved performance of all queries. Speedups are more significant on larger graphs,

R/registry.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#' # create a new registry
3030
#' reg <- caugi_registry()
3131
#'
32+
#' # list registered edges
33+
#' list_caugi_edges()
34+
#'
3235
#' # register an edge
3336
#' register_caugi_edge(
3437
#' glyph = "<--",
@@ -39,6 +42,7 @@
3942
#' )
4043
#'
4144
#' # now, this edge is available for caugi graphs:
45+
#' list_caugi_edges()
4246
#' cg <- caugi(A %-->% B, B %<--% C, class = "DAG")
4347
#'
4448
#' # reset the registry to default
@@ -65,6 +69,44 @@ caugi_registry <- function() {
6569
.caugi_env$reg
6670
}
6771

72+
#' @describeIn registry List all registered edge types in the global registry.
73+
#'
74+
#' @returns A `data.table` with columns `glyph`, `tail`, `head`, `class`, and
75+
#' `symmetric`, where each row corresponds to a registered edge type.
76+
#'
77+
#' @export
78+
list_caugi_edges <- function() {
79+
reg <- caugi_registry()
80+
n <- edge_registry_len(reg)
81+
82+
empty_dt <- data.table::data.table(
83+
glyph = character(),
84+
tail = character(),
85+
head = character(),
86+
class = character(),
87+
symmetric = logical()
88+
)
89+
90+
if (n == 0) {
91+
return(empty_dt)
92+
}
93+
94+
dt <- data.table::rbindlist(
95+
lapply(seq_len(n) - 1L, function(code) {
96+
spec <- edge_registry_spec_of_code(reg, code)
97+
data.table::data.table(
98+
glyph = spec$glyph,
99+
tail = spec$tail,
100+
head = spec$head,
101+
class = spec$class,
102+
symmetric = spec$symmetric
103+
)
104+
})
105+
)
106+
107+
dt
108+
}
109+
68110
#' @describeIn registry Reset the global edge registry to its default state.
69111
#'
70112
#' @export

man/registry.Rd

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-registry.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ test_that("glyph arbitary length works", {
8282
reset_caugi_registry()
8383
})
8484

85+
# ──────────────────────────────────────────────────────────────────────────────
86+
# ───────────────────────────────── View edges ─────────────────────────────────
87+
# ──────────────────────────────────────────────────────────────────────────────
88+
89+
test_that("View edges work with caugi_registry", {
90+
reset_caugi_registry()
91+
before_edges <- list_caugi_edges()
92+
register_caugi_edge("<--", "arrow", "tail", "directed", FALSE)
93+
after_edges <- list_caugi_edges()
94+
95+
expect_equal(nrow(after_edges), nrow(before_edges) + 1)
96+
expect_true("<--" %in% after_edges$glyph)
97+
98+
reset_caugi_registry()
99+
})
100+
85101

86102
# ──────────────────────────────────────────────────────────────────────────────
87103
# ───────────────────────────────── Sealing ────────────────────────────────────

0 commit comments

Comments
 (0)