7

I'm trying to implement the R package TSdist from python jupyter notebook.

import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr
rpy2.robjects.numpy2ri.activate()

R = rpy2.robjects.r
## load in package 
TSdist = importr('TSdist')
## t,c are two series 
dist = TSdist.ERPDistance(t.values,c.values,g=0,sigma =30)
## dist is a R Boolean vector with one value
dist[0]

This gives me an NA and I got a warning:

/usr/lib64/python3.4/site-packages/rpy2/rinterface/init.py:186: RRuntimeWarning: Error : The series must be univariate vectors

warnings.warn(x, RRuntimeWarning)

Any ideas of how to properly implement it? Or how to measure time series similarity with python packages using discrete Fourier transforms (DFT), Auto-regressive coefficient, Edit distance on real sequence(EDR). Methods mentioned in this paper.

epo3
  • 2,991
  • 2
  • 33
  • 60
  • The error is likely *before* `dist[0]`, when calling `ERPDistance()`, and as the error message issued by the R code you are trying to run indicates it your input is invalid. – lgautier Jul 22 '17 at 16:12
  • 1
    I've tried this implementation (`TSdist.ERPDistance(t.values,c.values,g=0,sigma =30)`) of the function in R studio and it works. And the implementation in python is referenced with this post: https://stackoverflow.com/questions/5695388/dynamic-time-warping-in-python. – Jingwei Zhang Jul 23 '17 at 09:28

1 Answers1

2

Likely the reason is the two series objects being passed into the method. Assuming series mean pandas series, calling values returns a numpy array. And per the docs, the ERPDistance expects numeric vectors, not arrays.

print(type(pd.Series(np.random.randn(5))))
# <class 'pandas.core.series.Series'>

print(type(pd.Series(np.random.randn(5)).values))
# <class 'numpy.ndarray'>

Consider simply casting series as numeric vectors with base R or use rpy2's FloatVector:

from rpy2.robjects.packages import importr

R = rpy2.robjects.r
## load in package 
base = importr('base')
TSdist = importr('TSdist')

new_t = base.as_numeric(t.tolist())
print(type(new_t))
# <class 'rpy2.robjects.vectors.FloatVector'>

new_c = rpy2.robjects.FloatVector(c.tolist())
print(type(new_c))
# <class 'rpy2.robjects.vectors.FloatVector'>

## new_t, new_c are now numeric vectors
dist = TSdist.ERPDistance(new_t, new_c, g=0, sigma =30)
Parfait
  • 104,375
  • 17
  • 94
  • 125