Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bug3():
series = np.array([
np.array([1, 2, 1]),
np.array([0., 1, 2, 0, 0, 0, 0, 0, 0]),
np.array([1., 2, 0, 0, 0, 0, 0, 1, 1, 3, 4, 5]),
np.array([0., 0, 1, 2, 1, 0, 1]),
np.array([0., 1, 2, 0, 0, 0, 0, 0]),
np.array([1., 2, 0, 0, 0, 0, 0, 1, 1])])
ds = dtw.distance_matrix(series)
print(ds)
model = clustering.LinkageTree(dtw.distance_matrix, {})
cluster_idx = model.fit(series)
print(cluster_idx)
if directory:
fn = directory / "bug3.png"
else:
file = tempfile.NamedTemporaryFile()
fn = Path(file.name + "_bug3.png")
model.plot(fn, show_ts_label=True)
def test_distance_matrix1_b():
s = [[0, 0, 1, 2, 1, 0, 1, 0, 0],
[0, 1, 2, 0, 0, 0, 0, 0, 0]]
s = [np.array(si) for si in s]
m2 = dtw.distance_matrix(s, parallel=True, use_c=False)
assert m2[0, 1] == pytest.approx(math.sqrt(2))
def d():
return dtw.distance_matrix(s, parallel=False, use_c=True, use_nogil=False, compact=True)
def test_bug1_psi():
s = [np.array([0., 0, 1, 2, 1, 0, 1, 0, 0]),
np.array([9., 0, 1, 2, 1, 0, 1, 0, 9])]
res1 = dtw.distance_matrix(s, compact=True, psi=1)
res2 = dtw.distance_matrix_fast(s, compact=True, psi=1)
print(res1)
print(res2)
assert res1 == pytest.approx(res2)
def d():
return dtw.distance_matrix(s, parallel=False, use_c=False, use_nogil=False, compact=True)
def test_distance_matrix2_e():
n = 1
nn = 1
s = [[0., 0, 1, 2, 1, 0, 1, 0, 0] * n,
[0., 1, 2, 0, 0, 0, 0, 0, 0] * n,
[1., 2, 0, 0, 0, 0, 0, 1] * n] * nn
s = [np.array(si) for si in s]
m1 = dtw_c.distance_matrix_nogil(s, is_parallel=True)
m1 = dtw.distances_array_to_matrix(m1, len(s))
m2 = dtw.distance_matrix(s, parallel=True, use_c=True, use_nogil=True)
assert m1[0, 1] == math.sqrt(2) * n, "m1[0,1]={} != {}".format(m1[0, 1], math.sqrt(2) * n)
assert m2[0, 1] == math.sqrt(2) * n, "m2[0,1]={} != {}".format(m2[0, 1], math.sqrt(2) * n)
def run_distance_matrix_block(parallel=False, use_c=False, use_nogil=False):
# print(parallel, use_c, use_nogil)
s = [[0., 0, 1, 2, 1, 0, 1, 0, 0],
[0., 1, 2, 0, 0, 0, 0, 0, 0],
[1., 2, 0, 0, 0, 0, 0, 1, 1],
[0., 0, 1, 2, 1, 0, 1, 0, 0],
[0., 1, 2, 0, 0, 0, 0, 0, 0],
[1., 2, 0, 0, 0, 0, 0, 1, 1]]
s = np.array(s)
m = dtw.distance_matrix(s, block=((1, 4), (3, 5)), parallel=parallel, use_c=use_c, use_nogil=use_nogil)
print(m)
assert m[1, 3] == pytest.approx(math.sqrt(2))
assert np.isinf(m[1, 2])