-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from hubverse-org/elr/refactor_task_subsetting
filter task groups on target rather than getting indexes with target
- Loading branch information
Showing
4 changed files
with
48 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,21 @@ | ||
#' For each task group, get the target_id entries from its target_metadata | ||
#' | ||
#' @return a list with one entry for each task group, | ||
#' where each entry is a character vector of target_ids for that group | ||
#' Filter task groups from a hub's tasks config to those that contain a target_id. | ||
#' Additionally, subset the target_metadata to just the entry for the target_id. | ||
#' | ||
#' @noRd | ||
get_target_ids_by_task_group <- function(task_groups) { | ||
result <- purrr::map( | ||
filter_task_groups_to_target <- function(task_groups, target_id) { | ||
# For each task group, subset the target_metadata to just the entry for the target_id | ||
# If the target_id is not in the task group, the target_metadata will be empty | ||
task_groups <- purrr::map( | ||
task_groups, | ||
function(task_group) { | ||
purrr::map_chr( | ||
task_group[["target_metadata"]], | ||
function(target) target[["target_id"]] | ||
) | ||
task_group$target_metadata <- purrr::keep(task_group$target_metadata, | ||
~ .x$target_id == target_id) | ||
return(task_group) | ||
} | ||
) | ||
|
||
return(result) | ||
} | ||
|
||
|
||
#' Get an integer vector with the indices of elements of | ||
#' target_ids_by_task_group that contain the target_id | ||
#' @noRd | ||
get_task_group_idxs_w_target <- function(target_id, target_ids_by_task_group) { | ||
result <- purrr::map2( | ||
seq_along(target_ids_by_task_group), target_ids_by_task_group, | ||
function(i, target_ids) { | ||
if (target_id %in% target_ids) { | ||
return(i) | ||
} | ||
return(NULL) | ||
} | ||
) |> | ||
purrr::compact() |> | ||
unlist() | ||
# Remove task groups that don't contain the target_id | ||
task_groups <- purrr::keep(task_groups, ~ length(.x$target_metadata) > 0) | ||
|
||
return(result) | ||
return(task_groups) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,63 @@ | ||
test_that( | ||
"get_target_ids_by_task_group works", | ||
"filter_task_groups_to_target works", | ||
{ | ||
task_groups <- list( | ||
list( | ||
group_number = 1, | ||
target_metadata = list( | ||
list(target_id = "target_id_1"), | ||
list(target_id = "target_id_2") | ||
) | ||
), | ||
list( | ||
group_number = 2, | ||
target_metadata = list( | ||
list(target_id = "target_id_3"), | ||
list(target_id = "target_id_4"), | ||
list(target_id = "target_id_5") | ||
) | ||
), | ||
list( | ||
group_number = 3, | ||
target_metadata = list( | ||
list(target_id = "target_id_4") | ||
) | ||
) | ||
) | ||
|
||
target_ids_by_task_group <- get_target_ids_by_task_group(task_groups) | ||
|
||
expect_equal( | ||
target_ids_by_task_group, | ||
filter_task_groups_to_target(task_groups, "target_id_1"), | ||
list( | ||
c("target_id_1", "target_id_2"), | ||
c("target_id_3", "target_id_4", "target_id_5"), | ||
"target_id_4" | ||
list( | ||
group_number = 1, | ||
target_metadata = list( | ||
list(target_id = "target_id_1") | ||
) | ||
) | ||
) | ||
) | ||
} | ||
) | ||
|
||
test_that( | ||
"get_task_group_idxs_w_target works", | ||
{ | ||
task_groups <- list( | ||
list( | ||
target_metadata = list( | ||
list(target_id = "target_id_1"), | ||
list(target_id = "target_id_2") | ||
) | ||
), | ||
list( | ||
target_metadata = list( | ||
list(target_id = "target_id_3"), | ||
list(target_id = "target_id_4"), | ||
list(target_id = "target_id_5") | ||
) | ||
), | ||
expect_equal( | ||
filter_task_groups_to_target(task_groups, "target_id_4"), | ||
list( | ||
target_metadata = list( | ||
list(target_id = "target_id_4") | ||
list( | ||
group_number = 2, | ||
target_metadata = list( | ||
list(target_id = "target_id_4") | ||
) | ||
), | ||
list( | ||
group_number = 3, | ||
target_metadata = list( | ||
list(target_id = "target_id_4") | ||
) | ||
) | ||
) | ||
) | ||
|
||
target_ids_by_task_group <- get_target_ids_by_task_group(task_groups) | ||
|
||
expect_equal( | ||
get_task_group_idxs_w_target("target_id_1", target_ids_by_task_group), | ||
1L | ||
) | ||
|
||
expect_equal( | ||
get_task_group_idxs_w_target("target_id_4", target_ids_by_task_group), | ||
c(2L, 3L) | ||
filter_task_groups_to_target(task_groups, "NOT A REAL TARGET ID"), | ||
list() | ||
) | ||
} | ||
) |