Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import pytest
import numpy as np
import sys, os
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
from dtaidistance import dtw, dtw_c
import logging
logger = logging.getLogger("be.kuleuven.dtai.distance")
if dtw_c is None:
print('ERROR: dtw_c is not build')
sys.exit(1)
def test_distance1_a():
# dist_opts = {'max_dist': 0.201, 'max_step': 0.011, 'max_length_diff': 8, 'window': 3}
dist_opts = {'window': 3}
s1 = np.array([ 0., 0.01, 0., 0.01, 0., 0., 0., 0.01, 0.01, 0.02, 0., 0.])
s2 = np.array([ 0., 0.02, 0.02, 0., 0., 0.01, 0.01, 0., 0., 0., 0.])
d1 = dtw.distance(s1, s2, **dist_opts)
d2 = dtw_c.distance_nogil(s1, s2, **dist_opts)
assert d1 == d2
assert d1 == pytest.approx(0.02)
def test_distance1_b():
dist_opts = {}
def test_distance2_c():
dist_opts = {}
s1 = np.array([0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0])
s2 = np.array([0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
d1 = dtw.distance(s1, s2, **dist_opts)
d2 = dtw_c.distance_nogil(s1, s2, **dist_opts)
assert d1 == d2
assert d1 == pytest.approx(1.0)
def test_psi_dtw_1c():
x = np.arange(0, 20, .5)
s1 = np.sin(x)
s2 = np.sin(x - 1)
d = dtw_c.distance_nogil(s1, s2, psi=2)
np.testing.assert_equal(d, 0.0)
def test_psi_dtw_2c():
x = np.arange(0, 20, .5)
s1 = np.sin(x - 1)
s2 = np.sin(x)
d = dtw_c.distance_nogil(s1, s2, psi=2, window=3)
np.testing.assert_equal(d, 0.0)
def test_distance1_c():
s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0])
s2 = np.array([0, 1, 2, 0, 0, 0, 0, 0, 0], dtype=np.double)
d3 = dtw_c.distance(s1, s2)
assert(d3) == pytest.approx(math.sqrt(2))
def test_distance1_d():
s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0])
s2 = np.array([0., 1, 2, 0, 0, 0, 0, 0, 0])
d = dtw_c.distance_nogil(s1, s2)
assert(d) == pytest.approx(math.sqrt(2))
def d():
return dtw_c.distance(s1, s2)
def d():
return dtw_c.distance_nogil(s1, s2)