首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用multidplyr进行向量化不会呈现正确的输出。

使用multidplyr进行向量化不会呈现正确的输出。
EN

Stack Overflow用户
提问于 2018-06-08 15:00:24
回答 2查看 230关注 0票数 1

我试图并行化ape::dist_topo(),这是一个计算未根树之间距离的函数。

通常,该函数的工作方式如下(reprex: 4棵随机树,每个树有5片叶子):

代码语言:javascript
复制
library(tidyverse)
# devtools::install_github("hadley/multidplyr")
library(multidplyr)
library(ape)
set.seed(3)

trees <- 
  map(rep(5, 4), rtree) %>% 
  do.call(c.phylo, .) %>% # To transform my list of phylo objects in a multiPhylo object
  unroot.multiPhylo()

dist.topo(trees)
#      tree1 tree2 tree3
# tree2     4            
# tree3     4     2      
# tree4     4     4     2

我创建了一个函数来计算data.frame中的距离为2乘2(为了按行划分成簇):

代码语言:javascript
复制
dist.topo2 <- function(multiphylo){
  expand.grid(multiphylo, multiphylo) %>% 
    as.tibble() %>% 
    mutate(dist = map2(Var1, Var2, dist.topo)) %>% 
    pull(dist) %>% 
    matrix(., nrow = sqrt(length(.))) %>% 
    as.dist()
}

dist.topo2(trees)
#   1 2 3
# 2 4    
# 3 4 2  
# 4 4 4 2

正如预期的那样,结果是相同的(不管名称)。

然后,我在管道中添加了multidplyr::partition()multidplyr::collect()函数:

代码语言:javascript
复制
dist.topo3 <- function(multiphylo){
  expand.grid(multiphylo, multiphylo) %>% 
    as.tibble() %>% 
    partition() %>%
    mutate(dist = purrr::map2(Var1, Var2, ape::dist.topo)) %>% 
    collect() %>%
    pull(dist) %>% 
    matrix(., nrow = sqrt(length(.))) %>% 
    as.dist()
}

dist.topo3(trees)
#   1 2 3
# 2 4    
# 3 0 4  
# 4 2 4 4
# Warning messages:
# 1: In bind_rows_(x, .id) :
#   Vectorizing 'multiPhylo' elements may not preserve their attributes
# 2: In bind_rows_(x, .id) :
#   Vectorizing 'multiPhylo' elements may not preserve their attributes
# 3: In bind_rows_(x, .id) :
#   Vectorizing 'multiPhylo' elements may not preserve their attributes
# 4: In bind_rows_(x, .id) :
#   Vectorizing 'multiPhylo' elements may not preserve their attributes
# 5: In bind_rows_(x, .id) :
#   Vectorizing 'multiPhylo' elements may not preserve their attributes
# 6: In bind_rows_(x, .id) :
#   Vectorizing 'multiPhylo' elements may not preserve their attributes

正如您所看到的,距离是不同的,而操作没有改变。

我怎么才能解决呢?也许是不可能的(看这里)

谢谢

注意:我知道这个解决方案可能不是最优的(特别是因为它计算每个距离两次),但这不是重点。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-08 15:52:07

问题是partition将随机分解data.frame,collect将随机解压缩data.frame。如果将行号作为列添加并在收集后进行排列,则会解决此问题。

代码语言:javascript
复制
dist.topo3 <- function(multiphylo){
  expand.grid(multiphylo, multiphylo) %>% 
    as.tibble() %>% 
    mutate(rn = row_number()) %>%
    partition(rn) %>%
    mutate(dist = purrr::map2(Var1, Var2, ape::dist.topo)) %>% 
    collect() %>%
    arrange(rn) %>%
    pull(dist) %>% 
    matrix(., nrow = sqrt(length(.))) %>% 
    as.dist()
}
dist.topo3(trees)
#   1 2 3
# 2 4    
# 3 4 2  
# 4 4 4 2
票数 4
EN

Stack Overflow用户

发布于 2018-06-08 15:41:44

我宁愿使用包{furrr}而不是{multidplyr}:

代码语言:javascript
复制
library(furrr)
plan(multiprocess)

dist.topo4 <- function(multiphylo) {

  dists <- expand.grid(multiphylo, multiphylo) %>% 
    setNames(c("x", "y")) %>%
    future_pmap_dbl(ape::dist.topo)

  n <- length(multiphylo)
  dim(dists) <- c(n, n)

  as.dist(dists)
}

结果

代码语言:javascript
复制
> dist.topo4(trees)
  1 2 3
2 4    
3 4 2  
4 4 4 2
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50763412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档