Skip to content Skip to sidebar Skip to footer

Rpy2 Dtw Missing Argument Window.size

I'm using the R DTW package with rpy2. I would like to be able specify a window type and size for running the DTW analysis. I have run the following code: import numpy as np impor

Solution 1:

Use importr():

from rpy2.robjects.packages import importr
dtw = importr('dtw')
alig = dtw.dtw(query, reference, keep=True,
               step='asymmetric',
               window_type='sakoeChibaWindow',
               window_size=3)

Solution 2:

This works for me:

import numpy as np
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr

rpy2.robjects.numpy2ri.activate()
R = rpy2.robjects.r
DTW = importr('dtw')

x = np.array([0.0, 1.0, 2.0, 3.0])
y = np.array([0.0, 1.9, 2.4, 3.0])

alignment1 = R.dtw(x, y, keep=True,  dist_method="Euclidean",step_pattern=DTW.asymmetric,type="sakoechiba")
alignment2 = R.dtw(x, y, keep=True, dist_method="Euclidean",step_pattern=DTW.symmetric1,type="itakura")
alignment3 = R.dtw(x, y, keep=True, dist_method="Euclidean", step_pattern=DTW.symmetric2, type=DTW.sakoeChibaWindow, window_size=2)
dist1 = alignment1.rx('distance')[0][0]
dist2 = alignment2.rx('distance')[0][0]
dist3= alignment3.rx('distance')[0][0]
print(dist1)
#1.0
print(dist2)
#1.3
print(dist3)
#1.3

The documentation states:"window.type can also be an user-defined windowing function. See dtwWindowingFunctions for all available windowing functions" There u can fix the window.size.

Hope it helps


Post a Comment for "Rpy2 Dtw Missing Argument Window.size"