R、python、网状结构和 limmbo:将数组 'float64' 转换为 'int64' 错误

R, python, reticulate, and limmbo: cast array 'float64' to 'int64' error

我正在尝试通过 reticulate R 包将 python 模块 limmbo (https://github.com/HannahVMeyer/limmbo) 与 R 一起使用。我已经用 Anaconda2 成功安装了 limmbo。我现在正在尝试使用函数 limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation,如下面的代码所示。当我 运行 下面的代码时,我收到有关将 float64 转换为 integer64 的错误。

```{r}
library(reticulate)
import("limmbo") -> limmbo
```

然后我运行 python代码:

```{python}
import numpy
from numpy.random import RandomState
from numpy.linalg import cholesky as chol
from limmbo.core.vdsimple import vd_reml
from limmbo.io.input import InputData
random = RandomState(15)
N = 100
S = 1000
P = 3
snps = (random.rand(N, S) < 0.2).astype(float)
kinship = numpy.dot(snps, snps.T) / float(10)
y  = random.randn(N, P)
pheno = numpy.dot(chol(kinship), y)
pheno_ID = [ 'PID{}'.format(x+1) for x in range(P)]
samples = [ 'SID{}'.format(x+1) for x in range(N)]
datainput = InputData()
datainput.addPhenotypes(phenotypes = pheno,
phenotype_ID = pheno_ID, pheno_samples = samples)
datainput.addRelatedness(relatedness = kinship,
relatedness_samples = samples)
```

当我尝试 运行 R 函数时出现问题 limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation:

```{r}
(limmbo$core$vdbootstrap$LiMMBo(py$datainput, timing = TRUE, iterations = 100, S = 2) -> foo)
limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation(foo, cpus = 1, seed = 12345)
```



Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: Cannot cast array from dtype('float64') to dtype('int64') according to the rule 'safe'

Detailed traceback: 
  File "/Users/frederickboehm/anaconda2/lib/python2.7/site-packages/limmbo/core/vdbootstrap.py", line 96, in runBootstrapCovarianceEstimation
    minCooccurrence=minCooccurrence)
  File "/Users/frederickboehm/anaconda2/lib/python2.7/site-packages/limmbo/core/vdbootstrap.py", line 353, in __generateBootstrapMatrix
    rand_state = np.random.RandomState(seed)
  File "mtrand.pyx", line 644, in mtrand.RandomState.__init__
  File "mtrand.pyx", line 687, in mtrand.RandomState.seed
Calls: <Anonymous> ... eval -> eval -> <Anonymous> -> py_call_impl ->     .Call
Execution halted

首先,通过以下方式导入你的numpy模块 np <- import("numpy", convert = FALSE)

然后您可以 re-create 使用 reticulate::np_array(datainput, dtype = np$int64) 显式类型 int64 的 numpy 数组。

您可以在 this tutorial 中了解有关如何操作和创建数组的更多信息。

希望对您有所帮助。

Yuan 的教程(link 在上面的回答中)包含让我回答问题的建议。这是我修改后的 R 代码,目前有效:

np <- import("numpy", convert = FALSE)
(limmbo$core$vdbootstrap$LiMMBo(datainput, timing = TRUE, iterations = np_array(10, dtype = "int64"), S = np_array(2, dtype = "int64")) -> foo)
limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation(foo, cpus = np$int(1), seed = np_array(1232, dtype = "int64"))