Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement R package TSdist from python

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.

like image 434
Jingwei Zhang Avatar asked Jul 17 '17 23:07

Jingwei Zhang


People also ask

Can we use R package in Python?

package comes with a Python engine you can use in R Markdown. Reticulate allows you to run chunks of Python code, print Python output, access Python objects, and so on. Easy, right? You can import any Python library and write any Python code you want, and then access the variables and functions declared with R.


1 Answers

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)
like image 188
Parfait Avatar answered Oct 12 '22 18:10

Parfait