Skip to content

Commit

Permalink
refactor jucntion
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Nov 8, 2024
1 parent 24ea885 commit 32e9d0d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions R/class_junction.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@ junction_init <- function(
splits = character(0),
deps = list()
) {
splits <- make.unique(splits, sep = "_")
index <- seq_along(splits)
names(index) <- make.unique(splits, sep = "_")
names(deps) <- names(deps) %|||% seq_along(deps)
deps <- as_data_frame(deps)
junction_new(nexus, splits, deps)
junction_new(nexus, index, deps)
}

junction_new <- function(nexus = NULL, splits = NULL, deps = NULL) {
junction_new <- function(nexus = NULL, index = NULL, deps = NULL) {
out <- new.env(parent = emptyenv(), hash = FALSE)
out$nexus <- nexus
out$splits <- splits
out$index <- index
out$deps <- deps
out
}

junction_upstream_edges <- function(junction) {
from <- utils::stack(junction$deps)$values
to <- rep(junction$splits, times = ncol(junction$deps))
to <- rep(junction_splits(junction), times = ncol(junction$deps))
data_frame(from = from, to = to)
}

junction_splits <- function(junction) {
as.character(junction$splits)
names(junction$index)
}

junction_transpose <- function(junction) {
splits <- junction$splits
splits <- junction_splits(junction)
deps <- junction$deps
out <- map_rows(deps, ~list(deps = unname(.x))) %||%
replicate(length(splits), list(deps = character(0)), simplify = FALSE)
Expand All @@ -39,7 +40,7 @@ junction_transpose <- function(junction) {
}

junction_invalidate <- function(junction) {
junction$splits <- rep(NA_character_, length(junction$splits))
names(junction$index) <- rep(NA_character_, length(junction$index))
}

junction_validate_deps <- function(deps) {
Expand All @@ -52,6 +53,7 @@ junction_validate <- function(junction) {
tar_assert_correct_fields(junction, junction_new)
tar_assert_scalar(junction$nexus)
tar_assert_chr(junction$nexus)
tar_assert_chr(junction$splits)
tar_assert_int(junction$index)
tar_assert_chr(junction_splits(junction))
junction_validate_deps(junction$deps)
}
2 changes: 1 addition & 1 deletion tests/testthat/test-class_junction.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tar_test("junction_splits()", {
tar_test("junction_invalidate()", {
x <- junction_init("x", letters, list(a = LETTERS, b = rev(letters)))
junction_invalidate(x)
expect_equal(junction_splits(x), rep(NA_character_, length(x$splits)))
expect_equal(junction_splits(x), rep(NA_character_, length(x$index)))
})

tar_test("junction_upstream_edges()", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-class_stem.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tar_test("stem$update_junction() on a good stem", {
pipeline <- pipeline_init(list(x))
stem_update_junction(x, pipeline)
expect_silent(junction_validate(x$junction))
out <- x$junction$splits
out <- junction_splits(x$junction)
expect_length(out, 10L)
expect_true(all(grepl("abc_", out)))
})
Expand Down

0 comments on commit 32e9d0d

Please sign in to comment.