Skip to content

Commit

Permalink
Prevent reticulate error when between = is given as a named list
Browse files Browse the repository at this point in the history
  • Loading branch information
bodkan committed Apr 7, 2023
1 parent f234fbf commit 7965e4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion R/tree-sequences.R
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ ts_ibd <- function(ts, coordinates = FALSE, within = NULL, between = NULL,
if (!is.null(within))
within <- unlist(purrr::map(within, ~ get_node_ids(ts, .x)))
else if (!is.null(between)) {
between <- purrr::map(between, ~ get_node_ids(ts, .x))
between <- unname(purrr::map(between, ~ get_node_ids(ts, .x)))
# another bug in reticulate? if the list has names, Python gives us:
# Error in py_call_impl(callable, dots$args, dots$keywords):
# TypeError: '<' not supported between instances of 'numpy.ndarray' and 'int'
Expand Down
17 changes: 13 additions & 4 deletions tests/testthat/test-ibd.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,19 @@ test_that("`within =` argument of ts_ibd() does what it is supposed to", {
})

test_that("`between =` argument of ts_ibd() does what it is supposed to", {
ibd_totals <- ts_ibd(ts, coordinates = FALSE, minimum_length = 100000,
between = list(pop1_samples, pop2_samples))
expect_true("POP1" == unique(unlist(ibd_totals$pop1)))
expect_true("POP2" == unique(unlist(ibd_totals$pop2)))
# individual names in a named list
ibd_totals1 <- ts_ibd(ts, coordinates = FALSE, minimum_length = 100000,
between = list(x = pop1_samples, y = pop2_samples))
expect_true("POP1" == unique(unlist(ibd_totals1$pop1)))
expect_true("POP2" == unique(unlist(ibd_totals1$pop2)))

# individual names in an unnamed list
ibd_totals2 <- ts_ibd(ts, coordinates = FALSE, minimum_length = 100000,
between = list(pop1_samples, pop2_samples))
expect_true("POP1" == unique(unlist(ibd_totals2$pop1)))
expect_true("POP2" == unique(unlist(ibd_totals2$pop2)))

expect_true(all(ibd_totals1 == ibd_totals2))
})

test_that("IBD of a given minimum length is returned", {
Expand Down

0 comments on commit 7965e4f

Please sign in to comment.