Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameters to control when to pass extreme parameter values to tests #52

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions tests/lsbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_examples_diploid(self, ts, include_ancestors):
queries = [query1, query2, query_miss_last, query_miss_mid, query_miss_most]
# Exclude the arbitrarily chosen queries from the reference panel.
ref_panel = ref_panel[:, 2:-2]
num_ref_haps = ref_panel.shape[1] # Haplotypes, not individuals.
num_ref_haps = ref_panel.shape[1] # Haplotypes, not individuals.
# Reference panel contains phased genotypes.
G = np.zeros((num_sites, num_ref_haps, num_ref_haps))
for i in range(num_sites):
Expand All @@ -103,17 +103,17 @@ def get_examples_diploid(self, ts, include_ancestors):
def get_examples_pars(
self,
ts,
ploidy=None,
scale_mutation_rate=None,
include_ancestors=None,
mean_r=None,
mean_mu=None,
ploidy,
scale_mutation_rate,
include_ancestors,
include_extreme_rates,
seed=42,
):
"""Returns an iterator over combinations of examples and parameters."""
assert ploidy in [1, 2]
assert scale_mutation_rate in [True, False]
assert include_ancestors in [True, False]
assert include_extreme_rates in [True, False]

np.random.seed(seed)
if ploidy == 1:
Expand All @@ -129,16 +129,19 @@ def get_examples_pars(
np.zeros(m) + 0.999, # Extreme
np.zeros(m) + 1e-6, # Extreme
np.random.rand(m), # Random
1e-5 * (np.random.rand(m) + 0.5) / 2,
]
mus = [
np.zeros(m) + 0.01, # Equal recombination and mutation
np.zeros(m) + 0.2, # Extreme
np.zeros(m) + 1e-6, # Extreme
np.random.rand(m) * 0.2, # Random
1e-5 * (np.random.rand(m) + 0.5) / 2,
]
if mean_r is not None and mean_mu is not None:
rs.append(mean_r * (np.random.rand(m) + 0.5) / 2)
mus.append(mean_mu * (np.random.rand(m) + 0.5) / 2)

if include_extreme_rates:
rs.append(np.zeros(m) + 0.2)
rs.append(np.zeros(m) + 1e-6)
mus.append(np.zeros(m) + 0.2)
mus.append(np.zeros(m) + 1e-6)

for s, r, mu in itertools.product(queries, rs, mus):
r[0] = 0
Expand Down
60 changes: 12 additions & 48 deletions tests/test_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@


class TestForwardBackwardHaploid(lsbase.ForwardBackwardAlgorithmBase):
def verify(
self, ts, scale_mutation_rate, include_ancestors, mean_r=None, mean_mu=None
):
def verify(self, ts, scale_mutation_rate, include_ancestors):
for n, m, H_vs, s, e_vs, r, mu in self.get_examples_pars(
ts,
ploidy=1,
scale_mutation_rate=scale_mutation_rate,
include_ancestors=include_ancestors,
mean_r=mean_r,
mean_mu=mean_mu,
include_extreme_rates=True,
):
F_vs, c_vs, ll_vs = fbh.forwards_ls_hap(n, m, H_vs, s, e_vs, r)
B_vs = fbh.backwards_ls_hap(n, m, H_vs, s, e_vs, c_vs, r)
Expand Down Expand Up @@ -54,33 +51,24 @@ def test_ts_simple_n16(self, include_ancestors):

@pytest.mark.parametrize("include_ancestors", [True, False])
def test_ts_larger(self, include_ancestors):
ref_panel_size = 46
length = 1e5
mean_r = 1e-5
mean_mu = 1e-5
ts = self.get_ts_custom_pars(
ref_panel_size=ref_panel_size, length=length, mean_r=mean_r, mean_mu=mean_mu
ref_panel_size=45, length=1e5, mean_r=1e-5, mean_mu=1e-5
)
self.verify(
ts,
scale_mutation_rate=True,
include_ancestors=include_ancestors,
mean_r=mean_r,
mean_mu=mean_mu,
)


class TestForwardBackwardDiploid(lsbase.ForwardBackwardAlgorithmBase):
def verify(
self, ts, scale_mutation_rate, include_ancestors, mean_r=None, mean_mu=None
):
def verify(self, ts, scale_mutation_rate, include_ancestors):
for n, m, G_vs, s, e_vs, r, mu in self.get_examples_pars(
ts,
ploidy=2,
scale_mutation_rate=scale_mutation_rate,
include_ancestors=include_ancestors,
mean_r=mean_r,
mean_mu=mean_mu,
include_extreme_rates=True,
):
F_vs, c_vs, ll_vs = fbd.forward_ls_dip_loop(
n, m, G_vs, s, e_vs, r, norm=True
Expand Down Expand Up @@ -119,33 +107,24 @@ def test_ts_simple_n16(self, include_ancestors):

@pytest.mark.parametrize("include_ancestors", [False])
def test_ts_larger(self, include_ancestors):
ref_panel_size = 46
length = 1e5
mean_r = 1e-5
mean_mu = 1e-5
ts = self.get_ts_custom_pars(
ref_panel_size=ref_panel_size, length=length, mean_r=mean_r, mean_mu=mean_mu
ref_panel_size=45, length=1e5, mean_r=1e-5, mean_mu=1e-5
)
self.verify(
ts,
scale_mutation_rate=True,
include_ancestors=include_ancestors,
mean_r=mean_r,
mean_mu=mean_mu,
)


class TestViterbiHaploid(lsbase.ViterbiAlgorithmBase):
def verify(
self, ts, scale_mutation_rate, include_ancestors, mean_r=None, mean_mu=None
):
def verify(self, ts, scale_mutation_rate, include_ancestors):
for n, m, H_vs, s, e_vs, r, mu in self.get_examples_pars(
ts,
ploidy=1,
scale_mutation_rate=scale_mutation_rate,
include_ancestors=include_ancestors,
mean_r=mean_r,
mean_mu=mean_mu,
include_extreme_rates=True,
):
V_vs, P_vs, ll_vs = vh.forwards_viterbi_hap_lower_mem_rescaling(
n, m, H_vs, s, e_vs, r
Expand Down Expand Up @@ -182,33 +161,24 @@ def test_ts_simple_n16(self, include_ancestors):

@pytest.mark.parametrize("include_ancestors", [True, False])
def test_ts_larger(self, include_ancestors):
ref_panel_size = 46
length = 1e5
mean_r = 1e-5
mean_mu = 1e-5
ts = self.get_ts_custom_pars(
ref_panel_size=ref_panel_size, length=length, mean_r=mean_r, mean_mu=mean_mu
ref_panel_size=46, length=1e5, mean_r=1e-5, mean_mu=1e-5
)
self.verify(
ts,
scale_mutation_rate=True,
include_ancestors=include_ancestors,
mean_r=mean_r,
mean_mu=mean_mu,
)


class TestViterbiDiploid(lsbase.ViterbiAlgorithmBase):
def verify(
self, ts, scale_mutation_rate, include_ancestors, mean_r=None, mean_mu=None
):
def verify(self, ts, scale_mutation_rate, include_ancestors):
for n, m, G_vs, s, e_vs, r, mu in self.get_examples_pars(
ts,
ploidy=2,
scale_mutation_rate=scale_mutation_rate,
include_ancestors=include_ancestors,
mean_r=mean_r,
mean_mu=mean_mu,
include_extreme_rates=True,
):
V_vs, P_vs, ll_vs = vd.forwards_viterbi_dip_low_mem(n, m, G_vs, s, e_vs, r)
path_vs = vd.backwards_viterbi_dip(m, V_vs, P_vs)
Expand Down Expand Up @@ -244,17 +214,11 @@ def test_ts_simple_n16(self, include_ancestors):

@pytest.mark.parametrize("include_ancestors", [False])
def test_ts_larger(self, include_ancestors):
ref_panel_size = 46
length = 1e5
mean_r = 1e-5
mean_mu = 1e-5
ts = self.get_ts_custom_pars(
ref_panel_size=ref_panel_size, length=length, mean_r=mean_r, mean_mu=mean_mu
ref_panel_size=45, length=1e5, mean_r=1e-5, mean_mu=1e-5
)
self.verify(
ts,
scale_mutation_rate=True,
include_ancestors=include_ancestors,
mean_r=mean_r,
mean_mu=mean_mu,
)
2 changes: 2 additions & 0 deletions tests/test_API_multiallelic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def verify(self, ts, scale_mutation_rate, include_ancestors):
ploidy=1,
scale_mutation_rate=scale_mutation_rate,
include_ancestors=include_ancestors,
include_extreme_rates=True,
):
F_vs, c_vs, ll_vs = fbh.forwards_ls_hap(n, m, H_vs, s, e_vs, r)
B_vs = fbh.backwards_ls_hap(n, m, H_vs, s, e_vs, c_vs, r)
Expand Down Expand Up @@ -59,6 +60,7 @@ def verify(self, ts, scale_mutation_rate, include_ancestors):
ploidy=1,
scale_mutation_rate=scale_mutation_rate,
include_ancestors=include_ancestors,
include_extreme_rates=True,
):
V_vs, P_vs, ll_vs = vh.forwards_viterbi_hap_lower_mem_rescaling(
n, m, H_vs, s, e_vs, r
Expand Down
Loading
Loading