Skip to content

Commit

Permalink
Remove checking expected number of paths
Browse files Browse the repository at this point in the history
  • Loading branch information
szhan committed Mar 24, 2024
1 parent e45ff53 commit 8e5095a
Showing 1 changed file with 41 additions and 57 deletions.
98 changes: 41 additions & 57 deletions tests/test_API_noncopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ def get_test_queries_biallelic():
# TODO: Add a case with two equally likely paths.

return [
(query_a3_clone, expected_path_a3_clone, 1),
(query_s0_clone, expected_path_s0_clone, 1),
(query_s1_x_s2, expected_path_s1_x_s2, 1),
(query_s0_x_a1, expected_path_s0_x_a1, 1),
(query_s0_x_a2_x_s0, expected_path_s0_x_a2_x_s0, 1),
(query_a0_x_s0, expected_path_a0_x_s0, 1),
(query_a0_x_s0_miss_a, expected_path_a0_x_s0_miss_a, 1),
(query_a0_x_s0_miss_b, expected_path_a0_x_s0_miss_b, 1),
(query_a3_clone, expected_path_a3_clone),
(query_s0_clone, expected_path_s0_clone),
(query_s1_x_s2, expected_path_s1_x_s2),
(query_s0_x_a1, expected_path_s0_x_a1),
(query_s0_x_a2_x_s0, expected_path_s0_x_a2_x_s0),
(query_a0_x_s0, expected_path_a0_x_s0),
(query_a0_x_s0_miss_a, expected_path_a0_x_s0_miss_a),
(query_a0_x_s0_miss_b, expected_path_a0_x_s0_miss_b),
]


Expand All @@ -107,156 +107,140 @@ def get_test_queries_multiallelic():
query_m_a3_x_a2_x_s0 = np.array([[ 0, 0, 0, 1, 1, 1, 1, 2, 2, 2]])
expected_path_m_a3_x_a2_x_s0 = np.array([ 3, 3, 3, 2, 2, 2, 2, 4, 4, 4])
# Sample 0 x ancestor 1.
query_s0_x_a1 = np.array([[ 2, 2, 2, 2, 2, 1, 1, 1, 1, 1]])
expected_path_s0_x_a1 = np.array([ 4, 4, 4, 4, 4, 1, 1, 1, 1, 1])
query_m_s0_x_a1 = np.array([[ 2, 2, 2, 2, 2, 1, 1, 1, 1, 1]])
expected_path_m_s0_x_a1 = np.array([ 4, 4, 4, 4, 4, 1, 1, 1, 1, 1])

return [
(query_m_a3_x_a1, expected_path_m_a3_x_a1, 1),
(query_m_a3_x_a2_x_s0, expected_path_m_a3_x_a2_x_s0, 1),
(query_s0_x_a1, expected_path_s0_x_a1, 1),
(query_m_a3_x_a1, expected_path_m_a3_x_a1),
(query_m_a3_x_a2_x_s0, expected_path_m_a3_x_a2_x_s0),
(query_m_s0_x_a1, expected_path_m_s0_x_a1),
]


# Tests for naive matrix-based implementation.
@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_biallelic()
)
def test_forwards_viterbi_hap_naive_biallelic(query, expected_path, expected_num_paths):
def test_forwards_viterbi_hap_naive_biallelic(query, expected_path):
n, m, H, e, r = get_test_data(use_multiallelic_sites=False)

V, P, _ = vh.forwards_viterbi_hap_naive(n, m, H, query, e, r)
best_path = vh.backwards_viterbi_hap(m, V[-1, :], P)
num_best_paths = np.sum(V[-1, :] == np.max(V[-1, :]))

assert np.array_equal(expected_path, best_path)
assert expected_num_paths == num_best_paths


@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_multiallelic()
)
def test_forwards_viterbi_hap_naive_multiallelic(
query, expected_path, expected_num_paths
query, expected_path
):
n, m, H, e, r = get_test_data(use_multiallelic_sites=True)

V, P, _ = vh.forwards_viterbi_hap_naive(n, m, H, query, e, r)
best_path = vh.backwards_viterbi_hap(m, V[-1, :], P)
num_best_paths = np.sum(V[-1, :] == np.max(V[-1, :]))

assert np.array_equal(expected_path, best_path)
assert expected_num_paths == num_best_paths


# Tests for naive matrix-based implementation using numpy.
@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_biallelic()
)
def test_forwards_viterbi_hap_naive_vec_biallelic(query, expected_path, expected_num_paths):
def test_forwards_viterbi_hap_naive_vec_biallelic(query, expected_path):
n, m, H, e, r = get_test_data(use_multiallelic_sites=False)

V, P, _ = vh.forwards_viterbi_hap_naive_vec(n, m, H, query, e, r)
best_path = vh.backwards_viterbi_hap(m, V[-1, :], P)
num_best_paths = np.sum(V[-1, :] == np.max(V[-1, :]))

assert np.array_equal(expected_path, best_path)
assert expected_num_paths == num_best_paths


@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_multiallelic()
)
def test_forwards_viterbi_hap_naive_vec_multiallelic(
query, expected_path, expected_num_paths
query, expected_path
):
n, m, H, e, r = get_test_data(use_multiallelic_sites=True)

V, P, _ = vh.forwards_viterbi_hap_naive_vec(n, m, H, query, e, r)
best_path = vh.backwards_viterbi_hap(m, V[-1, :], P)
num_best_paths = np.sum(V[-1, :] == np.max(V[-1, :]))

assert np.array_equal(expected_path, best_path)
assert expected_num_paths == num_best_paths


# Tests for naive matrix-based implementation with reduced memory.
@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_biallelic()
)
def test_forwards_viterbi_hap_naive_low_mem_biallelic(query, expected_path, expected_num_paths):
def test_forwards_viterbi_hap_naive_low_mem_biallelic(query, expected_path):
n, m, H, e, r = get_test_data(use_multiallelic_sites=False)

V, P, _ = vh.forwards_viterbi_hap_naive_low_mem(n, m, H, query, e, r)
best_path = vh.backwards_viterbi_hap(m, V, P)
num_best_paths = np.sum(V == np.max(V))

assert np.array_equal(expected_path, best_path)
assert expected_num_paths == num_best_paths


@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_multiallelic()
)
def test_forwards_viterbi_hap_naive_low_mem_multiallelic(
query, expected_path, expected_num_paths
query, expected_path
):
n, m, H, e, r = get_test_data(use_multiallelic_sites=True)

V, P, _ = vh.forwards_viterbi_hap_naive_low_mem(n, m, H, query, e, r)
best_path = vh.backwards_viterbi_hap(m, V, P)
num_best_paths = np.sum(V == np.max(V))

assert np.array_equal(expected_path, best_path)
assert expected_num_paths == num_best_paths


# Tests for naive matrix-based implementation with reduced memory and rescaling.
@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_biallelic()
)
def test_forwards_viterbi_hap_naive_low_mem_rescaling_biallelic(query, expected_path, expected_num_paths):
def test_forwards_viterbi_hap_naive_low_mem_rescaling_biallelic(query, expected_path):
n, m, H, e, r = get_test_data(use_multiallelic_sites=False)

V, P, _ = vh.forwards_viterbi_hap_naive_low_mem_rescaling(n, m, H, query, e, r)
best_path = vh.backwards_viterbi_hap(m, V, P)
num_best_paths = np.sum(V == np.max(V))

assert np.array_equal(expected_path, best_path)
assert expected_num_paths == num_best_paths


@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_multiallelic()
)
def test_forwards_viterbi_hap_naive_low_mem_rescaling_multiallelic(
query, expected_path, expected_num_paths
query, expected_path
):
n, m, H, e, r = get_test_data(use_multiallelic_sites=True)

V, P, _ = vh.forwards_viterbi_hap_naive_low_mem_rescaling(n, m, H, query, e, r)
best_path = vh.backwards_viterbi_hap(m, V, P)
num_best_paths = np.sum(V == np.max(V))

assert np.array_equal(expected_path, best_path)
assert expected_num_paths == num_best_paths


# Tests for implementation with reduced memory and rescaling.
# Hereon, compare the log-likelihood of the most likely path with that of the naive implementation
# instead of the paths themselves.
@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_biallelic()
)
def test_forwards_viterbi_hap_low_mem_rescaling_biallelic(query, expected_path, expected_num_paths):
def test_forwards_viterbi_hap_low_mem_rescaling_biallelic(query, expected_path):
n, m, H, e, r = get_test_data(use_multiallelic_sites=False)

V_naive, P_naive, ll_naive = vh.forwards_viterbi_hap_naive(n, m, H, query, e, r)
Expand All @@ -266,11 +250,11 @@ def test_forwards_viterbi_hap_low_mem_rescaling_biallelic(query, expected_path,


@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_multiallelic()
)
def test_forwards_viterbi_hap_low_mem_rescaling_multiallelic(
query, expected_path, expected_num_paths
query, expected_path
):
n, m, H, e, r = get_test_data(use_multiallelic_sites=True)

Expand All @@ -282,10 +266,10 @@ def test_forwards_viterbi_hap_low_mem_rescaling_multiallelic(

# Tests for implementation with even more reduced memory and rescaling.
@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_biallelic()
)
def test_forwards_viterbi_hap_lower_mem_rescaling_biallelic(query, expected_path, expected_num_paths):
def test_forwards_viterbi_hap_lower_mem_rescaling_biallelic(query, expected_path):
n, m, H, e, r = get_test_data(use_multiallelic_sites=False)

V_naive, P_naive, ll_naive = vh.forwards_viterbi_hap_naive(n, m, H, query, e, r)
Expand All @@ -295,11 +279,11 @@ def test_forwards_viterbi_hap_lower_mem_rescaling_biallelic(query, expected_path


@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_multiallelic()
)
def test_forwards_viterbi_hap_lower_mem_rescaling_multiallelic(
query, expected_path, expected_num_paths
query, expected_path
):
n, m, H, e, r = get_test_data(use_multiallelic_sites=True)

Expand All @@ -311,10 +295,10 @@ def test_forwards_viterbi_hap_lower_mem_rescaling_multiallelic(

# Tests for implementation with even more reduced memory and rescaling, without keeping pointers.
@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_biallelic()
)
def test_forwards_viterbi_hap_lower_mem_rescaling_no_pointer_biallelic(query, expected_path, expected_num_paths):
def test_forwards_viterbi_hap_lower_mem_rescaling_no_pointer_biallelic(query, expected_path):
n, m, H, e, r = get_test_data(use_multiallelic_sites=False)

V_naive, P_naive, ll_naive = vh.forwards_viterbi_hap_naive(n, m, H, query, e, r)
Expand All @@ -324,11 +308,11 @@ def test_forwards_viterbi_hap_lower_mem_rescaling_no_pointer_biallelic(query, ex


@pytest.mark.parametrize(
"query, expected_path, expected_num_paths",
"query, expected_path",
get_test_queries_multiallelic()
)
def test_forwards_viterbi_hap_lower_mem_rescaling_no_pointer_multiallelic(
query, expected_path, expected_num_paths
query, expected_path
):
n, m, H, e, r = get_test_data(use_multiallelic_sites=True)

Expand Down

0 comments on commit 8e5095a

Please sign in to comment.