Skip to content

Commit 6131530

Browse files
Merge pull request #33 from frederikfabriciusbjerre/copilot/fix-scrambled-vertex-indices
Fix vertex index scrambling when converting from igraph to caugi
2 parents 3c98acf + 8b2191f commit 6131530

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

R/caugi_graph.R

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,21 @@ caugi_graph <- S7::new_class(
224224
}
225225

226226
# All unique nodes
227-
nodes <- tibble::tibble(name = unique(c(edges$from, edges$to, declared)))
227+
# When declared nodes are provided and contain ALL edge nodes,
228+
# preserve their order (important for igraph conversion)
229+
edge_node_names <- unique(c(edges$from, edges$to))
230+
if (length(declared) > 0L && all(edge_node_names %in% declared)) {
231+
# Declared contains all edge nodes: preserve declared order
232+
all_node_names <- unique(declared)
233+
} else if (length(declared) > 0L) {
234+
# Declared exists but doesn't contain all edge nodes:
235+
# use edge order first, then add declared isolates (old behavior)
236+
all_node_names <- unique(c(edge_node_names, declared))
237+
} else {
238+
# No declared nodes: use edge order (old behavior)
239+
all_node_names <- edge_node_names
240+
}
241+
nodes <- tibble::tibble(name = all_node_names)
228242
n <- nrow(nodes)
229243
id <- seq_len(n) - 1L
230244
names(id) <- nodes$name

tests/testthat/test-as_caugi.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,23 @@ test_that("igraph with no edges, but with nodes, works", {
219219
expect_setequal(nodes(cg0)[["name"]], c("A", "B", "C"))
220220
})
221221

222+
test_that("igraph vertex order is preserved when converting to caugi", {
223+
skip_if_not_installed("igraph")
224+
225+
# Create igraph with specific vertex names (V1...V10)
226+
# Add edges that would naturally scramble the order if we use unique(c(from, to))
227+
set.seed(1022)
228+
g <- igraph::sample_gnm(10, 5) |> igraph::as_directed(mode = "acyclic")
229+
igraph::V(g)$name <- paste0("V", 1:length(igraph::V(g)))
230+
231+
# Convert to caugi
232+
cg <- as_caugi(g, class = "DAG")
233+
234+
# Vertex order should be preserved
235+
expect_equal(V(cg)$name, igraph::V(g)$name)
236+
expect_equal(V(cg)$name, paste0("V", 1:10))
237+
})
238+
222239
# ──────────────────────────────────────────────────────────────────────────────
223240
# ─────────────────────────── graphNEL conversion ──────────────────────────────
224241
# ──────────────────────────────────────────────────────────────────────────────

tests/testthat/test-caugi_graph.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,26 @@ test_that("caugi_graph(vector mode) works with isolated nodes", {
373373
expect_equal(cg_vec_1, cg_vec_2)
374374
})
375375

376+
test_that("caugi_graph preserves node order from nodes parameter", {
377+
# When nodes parameter is provided, its order should be preserved
378+
# even when edges reference nodes in different order
379+
cg <- caugi_graph(
380+
from = c("V3", "V7", "V2"),
381+
edge = c("-->", "-->", "-->"),
382+
to = c("V1", "V9", "V5"),
383+
nodes = paste0("V", 1:10),
384+
class = "DAG"
385+
)
386+
387+
# Node order should match the provided nodes parameter
388+
expect_equal(V(cg)$name, paste0("V", 1:10))
389+
390+
# Edges should still be present and correct
391+
expect_equal(nrow(edges(cg)), 3)
392+
})
393+
376394
# ──────────────────────────────────────────────────────────────────────────────
377-
# ────────────────────────────────── Errors ────────────────────────────────────
395+
# ────────────────────────────────────── Errors ────────────────────────────────
378396
# ──────────────────────────────────────────────────────────────────────────────
379397

380398
test_that("caugi_graph errors with trailing commas", {

0 commit comments

Comments
 (0)