-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
filter task groups on target rather than getting indexes with target #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of returning task groups with empty target metadata, it might be better to return an empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestions! (I didn't know the I'm going to merge this as is so that I can keep moving on other pieces |
||
) | ||
|
||
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) | ||
} |
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() | ||
) | ||
} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a much better second iteration! I would even go so far as to say that you could move this anonymous function into a named function (maybe
trim_to_target_id
).