0

I want to use the Python package peakutils from within R. To do this, I use the reticulate package. Here's an example of what I'd like to do.

# 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)

enter image description here

That's great and works as expected. Now, say I want to change the min_dist parameter, which according to the documentation is an integer. I pass min_dist = 3L, like so:

# Indices pf peaks now with a larger minimum distance
ind <- pu$indexes(dt$y, thres = 0.7, min_dist = 3L)

and receive the following error:

Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: only integer arrays with one element can be converted to an index

It is expecting an array with a single element that is an integer, but – as far as I can see – this is exactly what I'm passing so why am I receiving this error?

Dan
  • 11,370
  • 4
  • 43
  • 68

1 Answers1

0

The issue is with the y argument rather than the min_dist argument. The solution below was originally posted on the reticulate GitHub repo 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)

Created on 2019-10-10 by the reprex package (v0.3.0)

Dan
  • 11,370
  • 4
  • 43
  • 68