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_numpymatrix():
"""Passing a matrix instead of a list failed because the array is now a
view instead of the original data structure."""
s = np.array([
[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, 0]])
m = dtw_c.distance_matrix_nogil(s)
m = dtw.distances_array_to_matrix(m, len(s))
m2 = dtw.distance_matrix(s)
correct = np.array([
[np.inf, 1.41421356, 1.73205081],
[np.inf, np.inf, 1.41421356],
[np.inf, np.inf, np.inf]])
assert m[0, 1] == pytest.approx(math.sqrt(2))
assert m2[0, 1] == pytest.approx(math.sqrt(2))
np.testing.assert_almost_equal(correct, m, decimal=4)
np.testing.assert_almost_equal(correct, m2, decimal=4)
def test_numpymatrix_compact():
"""Passing a matrix instead of a list failed because the array is now a
view instead of the original data structure."""
s = np.array([
[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, 0]])
m = dtw_c.distance_matrix_nogil(s)
m2 = dtw.distance_matrix(s, compact=True)
correct = np.array([1.41421356, 1.73205081, 1.41421356])
assert m[0] == pytest.approx(math.sqrt(2))
assert m2[0] == pytest.approx(math.sqrt(2))
np.testing.assert_almost_equal(correct, m, decimal=4)
np.testing.assert_almost_equal(correct, m2, decimal=4)
def test_distance_matrix1_e():
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]]
s = [np.array(si) for si in s]
m = dtw_c.distance_matrix_nogil(s, is_parallel=True)
m = dtw.distances_array_to_matrix(m, len(s))
print(m)
assert m[0, 1] == pytest.approx(math.sqrt(2))
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 test_distance_matrix1_d():
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]]
s = [np.array(si) for si in s]
m = dtw_c.distance_matrix_nogil(s)
m = dtw.distances_array_to_matrix(m, len(s))
assert m[0, 1] == pytest.approx(math.sqrt(2))
def test_numpymatrix_transpose():
"""Passing a matrix instead of a list failed because the array is now a
view instead of the original data structure."""
s = np.array([
[0., 0., 1.,],
[0, 1, 2],
[1, 2, 0],
[2, 0, 0],
[1, 0, 0],
[0, 0, 0],
[1, 0, 0],
[0, 0, 1],
[0, 0, 0]
]).T
m = dtw_c.distance_matrix_nogil(s)
m = dtw.distances_array_to_matrix(m, len(s))
m2 = dtw.distance_matrix(s)
correct = np.array([
[np.inf, 1.41421356, 1.73205081],
[np.inf, np.inf, 1.41421356],
[np.inf, np.inf, np.inf]])
assert m[0, 1] == pytest.approx(math.sqrt(2))
assert m2[0, 1] == pytest.approx(math.sqrt(2))
np.testing.assert_almost_equal(correct, m, decimal=4)
np.testing.assert_almost_equal(correct, m2, decimal=4)