使用网状包从 Python peakutils 包调用函数时出现 TypeError
TypeError when using reticulate package to call a function from Python peakutils package
我想在 R 中使用 Python 包 peakutils
。为此,我使用 reticulate
包。这是我想做的一个例子。
# Load packages
library(data.table)
library(reticulate)
# Set RNG
set.seed(-1)
# Synthetic data function
foo <- function(x) sin(x) * sqrt(x) + rnorm(length(x), 0, 0.1)
# Create data
dt <- data.table(x = seq(0, 10, by = 0.1))
dt$y <- foo(dt$x)
# Import Python library
pu <- import("peakutils")
# Indices pf peaks
ind <- pu$indexes(dt$y, thres = 0.7)
# Have a look at data
plot(dt)
points(dt[ind], col = "red", pch = 19)
太好了,按预期工作。现在,假设我要更改 min_dist
参数,根据 documentation 它是一个整数。我通过 min_dist = 3L
,像这样:
# Indices pf peaks now with a larger minimum distance
ind <- pu$indexes(dt$y, thres = 0.7, min_dist = 3L)
并收到以下错误:
Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: only integer arrays with one element can be converted to an
index
它期待一个包含单个元素的数组,该元素是一个整数,但是——据我所知——这正是我传递的内容,所以我为什么要传递收到这个错误?
问题出在 y
参数而不是 min_dist
参数。下面的解决方案最初发布在 reticulate
GitHub 存储库 here.
# Load packages
library(data.table)
library(reticulate)
# Set RNG
set.seed(-1)
# Synthetic data function
foo <- function(x) sin(x) * sqrt(x) + rnorm(length(x), 0, 0.1)
# Create data
dt <- data.table(x = seq(0, 10, by = 0.1))
dt$y <- foo(dt$x)
# Import Python library
pu <- import("peakutils")
# Indices pf peaks
ind <- pu$indexes(as.array(dt$y), thres = 0.7, min_dist = 3L)
# Have a look at data
plot(dt)
points(dt[ind], col = "red", pch = 19)
由 reprex package (v0.3.0)
于 2019-10-10 创建
我想在 R 中使用 Python 包 peakutils
。为此,我使用 reticulate
包。这是我想做的一个例子。
# Load packages
library(data.table)
library(reticulate)
# Set RNG
set.seed(-1)
# Synthetic data function
foo <- function(x) sin(x) * sqrt(x) + rnorm(length(x), 0, 0.1)
# Create data
dt <- data.table(x = seq(0, 10, by = 0.1))
dt$y <- foo(dt$x)
# Import Python library
pu <- import("peakutils")
# Indices pf peaks
ind <- pu$indexes(dt$y, thres = 0.7)
# Have a look at data
plot(dt)
points(dt[ind], col = "red", pch = 19)
太好了,按预期工作。现在,假设我要更改 min_dist
参数,根据 documentation 它是一个整数。我通过 min_dist = 3L
,像这样:
# Indices pf peaks now with a larger minimum distance
ind <- pu$indexes(dt$y, thres = 0.7, min_dist = 3L)
并收到以下错误:
Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: only integer arrays with one element can be converted to an index
它期待一个包含单个元素的数组,该元素是一个整数,但是——据我所知——这正是我传递的内容,所以我为什么要传递收到这个错误?
问题出在 y
参数而不是 min_dist
参数。下面的解决方案最初发布在 reticulate
GitHub 存储库 here.
# Load packages
library(data.table)
library(reticulate)
# Set RNG
set.seed(-1)
# Synthetic data function
foo <- function(x) sin(x) * sqrt(x) + rnorm(length(x), 0, 0.1)
# Create data
dt <- data.table(x = seq(0, 10, by = 0.1))
dt$y <- foo(dt$x)
# Import Python library
pu <- import("peakutils")
# Indices pf peaks
ind <- pu$indexes(as.array(dt$y), thres = 0.7, min_dist = 3L)
# Have a look at data
plot(dt)
points(dt[ind], col = "red", pch = 19)
由 reprex package (v0.3.0)
于 2019-10-10 创建