From 403df3b3bf9c568b1c71c44e8f938ba5f168f3ab Mon Sep 17 00:00:00 2001 From: Martin Petr Date: Wed, 28 Sep 2022 13:41:41 +0200 Subject: [PATCH] Return all tskit statistics as numerical vectors (not 1D arrays) --- NEWS.md | 2 ++ R/tree-sequences.R | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index fd36eba1a..2bbd496cb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,6 +16,8 @@ - Avoid the unnecessary `array` type of _tskit_ results returned via reticulate. Numeric vectors (columns of data frames with numerical results) obtained in this way are simple R numeric vector ([#c690334](https://github.com/bodkan/slendr/commit/c690334)). +- One-way and multi-way statistics results are now returned as simple numerical vectors. Previously, results were returned as a type `array` despite "looking" as vectors (this is how values are returned to R from the reticulate-Python layer), which caused unnecessary annoyances and type-conversions on the R side of things and was not even intended ([#e1d178e](https://github.com/bodkan/slendr/commit/e1d178e)). + # slendr 0.3.0 - SLiM 4.0 is now required for running simulations with the `slim()` engine. If you want to run _slendr_ simulations with SLiM (spatial or non-spatial), you will need to upgrade you SLiM installation. SLiM 3.7.1 version is no longer supported as the upcoming new _slendr_ spatial features will depend on SLiM 4.x and maintaining two functionally identical yet syntactically different back ends is not feasible (PR [#104](https://github.com/bodkan/slendr/pull/104)). diff --git a/R/tree-sequences.R b/R/tree-sequences.R index bd0af1e1d..979bb2390 100644 --- a/R/tree-sequences.R +++ b/R/tree-sequences.R @@ -1667,6 +1667,8 @@ multiway_stat <- function(ts, stat = c("fst", "divergence"), if (is.matrix(values)) values <- split(values, col(values)) + if (!is.list(values)) values <- as.numeric(values) # convert 1D arrays to simple vectors + if (is.null(names(sample_sets))) set_names <- paste0("set_", seq_len(n_sets)) else @@ -1677,7 +1679,7 @@ multiway_stat <- function(ts, stat = c("fst", "divergence"), as.data.frame(t(matrix(set)), stringsAsFactors = FALSE) }) %>% dplyr::as_tibble() %>% - dplyr::mutate(stat = as.numeric(values)) + dplyr::mutate(stat = values) result } @@ -1786,6 +1788,8 @@ oneway_stat <- function(ts, stat, sample_sets, mode, windows, span_normalise = N if (is.matrix(values)) values <- split(values, col(values)) + if (!is.list(values)) values <- as.numeric(values) # convert 1D arrays to simple vectors + if (all(sapply(sample_sets, length) == 1)) set_names <- unlist(sample_sets) else if (is.null(names(sample_sets))) @@ -1794,7 +1798,7 @@ oneway_stat <- function(ts, stat, sample_sets, mode, windows, span_normalise = N set_names <- names(sample_sets) result <- dplyr::tibble(set = set_names) - result[[stat]] <- as.numeric(values) + result[[stat]] <- values result }