rpy2 DTW缺少参数window.size(rpy2 dtw missing argument

2019-10-19 21:13发布

我使用R DTW包rpy2。 我希望能够指定运行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

Answer 1:

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


Answer 2:

这对我的作品:

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

该文件指出:“window.type也可以是用户定义的窗口函数见所有可用的窗口函数dtwWindowingFunctions。”有ü可以修复window.size。

希望能帮助到你



文章来源: rpy2 dtw missing argument window.size
标签: python r rpy2