rpy2 dtw缺少参数window.size

时间:2014-04-15 16:31:31

标签: python r rpy2

我正在使用带有rpy2的R DTW包。我希望能够指定一个窗口类型和大小来运行DTW分析。

我运行了以下代码:

import numpy as np
import rpy2.robjects as robjects
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

r = robjects.r
r('library("dtw")')

query = np.array([0.0,1.0,2.0,3.0])
reference = np.array([0.0,1.9,2.4,3.0])

# Attempt 1:
kwargs = {'step':r("asymmetric"),'window_type':r("sakoeChibaWindow"),'window_size':r("as.integer(\"3\")")}
alig = r.dtw(query, reference, **kwargs)

# Attempt 2:
alig = r.dtw(query, reference, keep=r('TRUE'), step=r('asymmetric'),window_type=r('sakoeChibaWindow'),window_size="as.integer(\"3\")")

# Attempt 3:
alig = r.dtw(query, reference, keep=r('TRUE'), step=r('asymmetric'),window_type=r('sakoeChibaWindow'),window_size=3)

# Note: The line of code below works correctly.
# alig = r.dtw(query, reference, keep=r('TRUE'), step=r('asymmetric'))

robjects.globalenv["alignment"] =  alig
print r('alignment$distance')

我收到以下错误消息:

Error in abs(jw - iw) <= window.size : 'window.size' is missing
Traceback (most recent call last):
  File "testrdtw.py", line 19, in <module>
    alig = r.dtw(query, reference, **kwargs)
  File "/Users/jsmith/Dropbox/IW/env/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 86, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/Users/jsmith/Dropbox/IW/env/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 35, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in abs(jw - iw) <= window.size : 'window.size' is missing

如何正确指定window.size参数以使其正确传递?

我对R和rpy很新,所以我很可能错误地使用这些库。 任何建议,提示或帮助都非常感激。

- js

2 个答案:

答案 0 :(得分:0)

使用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)

答案 1 :(得分:0)

这对我有用:

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

documentation状态:&#34; window.type也可以是用户定义的窗口函数。有关所有可用的窗口函数,请参阅dtwWindowingFunctions&#34; 你可以修复window.size。

希望有所帮助