Skip to content

Commit

Permalink
Fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjeffery committed Jun 26, 2024
1 parent ddda831 commit 4a5d98d
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 65 deletions.
25 changes: 12 additions & 13 deletions python/requirements/CI-complete/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
biopython==1.81
coverage==7.2.7
dendropy==4.6.1
biopython==1.83
coverage==7.5.4
dendropy==5.0.1
h5py==3.9.0
kastore==0.3.2
kastore==0.3.3
lshmm==0.0.8
msgpack==1.0.5
msprime==1.2.0
networkx==3.1
portion==2.4.1
pytest==7.4.0
pytest-cov==4.1.0
pytest-xdist==3.3.1
tszip==0.2.2
msgpack==1.0.8
msprime==1.3.1
networkx==3.3
portion==2.4.2
pytest==8.2.2
pytest-cov==5.0.0
pytest-xdist==3.6.1
tszip==0.2.3
xmlunittest==0.5.0
llvmlite==0.39.1
16 changes: 2 additions & 14 deletions python/requirements/CI-tests-conda/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
biopython==1.79
msprime==1.3.1
tszip==0.2.3
h5py==3.11.0
kastore==0.3.3
lxml==4.9.2
msgpack-python==1.0.4
msprime==1.2.0
networkx==3.1
numba<=0.59.1 #Pinned directly as 0.60.0 fails
numpy==1.26.4
portion==2.3.0
pytest-cov==4.0.0
pytest-xdist==2.5.0
pytest # Relaxing pin because pytest is picky about versions
svgwrite==1.4.3
tszip==0.2.2
14 changes: 12 additions & 2 deletions python/requirements/CI-tests-pip/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
dendropy==4.5.2
lshmm==0.0.8
newick==1.3.2
pytest==8.2.2
pytest-cov==5.0.0
pytest-xdist==3.6.1
svgwrite==1.4.3
portion==2.4.2
xmlunittest==0.5.0
biopython==1.83
dendropy==5.0.1
networkx==3.3
msgpack==1.0.8
newick==1.9.0
kastore==0.3.3
jsonschema==4.22.0
4 changes: 3 additions & 1 deletion python/tests/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def __init__(
if self.update_sample_flags:
flags = self.tables.nodes.flags
# Zero out other sample flags
flags = np.bitwise_and(flags, ~tskit.NODE_IS_SAMPLE)
flags = np.bitwise_and(
flags, np.uint32(~tskit.NODE_IS_SAMPLE & 0xFFFFFFFF)
)
flags[sample] |= tskit.NODE_IS_SAMPLE
self.tables.nodes.flags = flags.astype(np.uint32)

Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_coalrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def test_missing_leaves(self):
"""
t = self.example_ts().dump_tables()
ss0 = np.flatnonzero(t.nodes.population == 0)
remove = np.in1d(t.edges.child, ss0)
remove = np.isin(t.edges.child, ss0)
assert np.any(remove)
t.edges.set_columns(
left=t.edges.left[~remove],
Expand Down
6 changes: 3 additions & 3 deletions python/tests/test_ld_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ def pi2_unbiased(
w_Ab = state[1, k]
w_aB = state[2, k]
w_ab = n - (w_AB + w_Ab + w_aB)
with suppress_division_by_zero_warning():
with np.errstate(over="ignore", divide="ignore", invalid="ignore"):
result[k] = (1 / (n * (n - 1) * (n - 2) * (n - 3))) * (
((w_AB + w_Ab) * (w_aB + w_ab) * (w_AB + w_aB) * (w_Ab + w_ab))
- ((w_AB * w_ab) * (w_AB + w_ab + (3 * w_Ab) + (3 * w_aB) - 1))
Expand All @@ -1052,7 +1052,7 @@ def dz_unbiased(
w_Ab = state[1, k]
w_aB = state[2, k]
w_ab = n - (w_AB + w_Ab + w_aB)
with suppress_division_by_zero_warning():
with np.errstate(over="ignore", divide="ignore", invalid="ignore"):
result[k] = (1 / (n * (n - 1) * (n - 2) * (n - 3))) * (
(
((w_AB * w_ab) - (w_Ab * w_aB))
Expand All @@ -1074,7 +1074,7 @@ def d2_unbiased(
w_Ab = state[1, k]
w_aB = state[2, k]
w_ab = n - (w_AB + w_Ab + w_aB)
with suppress_division_by_zero_warning():
with np.errstate(over="ignore", divide="ignore", invalid="ignore"):
result[k] = (1 / (n * (n - 1) * (n - 2) * (n - 3))) * (
((w_aB**2) * (w_Ab - 1) * w_Ab)
+ ((w_ab - 1) * w_ab * (w_AB - 1) * w_AB)
Expand Down
28 changes: 12 additions & 16 deletions python/tests/test_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,7 @@ def test_divergence_matrix(self):
ts.divergence_matrix(windows, [1, 1], [0, bad_node])
with pytest.raises(ValueError, match="Sum of sample_set_sizes"):
ts.divergence_matrix(windows, [1, 2], [0, 1])
with pytest.raises(ValueError, match="Overflow"):
with pytest.raises((ValueError, OverflowError), match="Overflow|out of bounds"):
ts.divergence_matrix(windows, [-1, 2], [0])

with pytest.raises(TypeError, match="str"):
Expand Down Expand Up @@ -3321,23 +3321,19 @@ def test_bad_tracked_samples(self):
)
for bad_sample in [10**6, -1e6]:
with pytest.raises(ValueError):
# Implicit conversion to integers using __int__ is deprecated
with pytest.deprecated_call():
_tskit.Tree(
ts,
options=options,
tracked_samples=[bad_sample],
)
_tskit.Tree(
ts,
options=options,
tracked_samples=[bad_sample],
)
with pytest.raises(ValueError):
with pytest.deprecated_call():
_tskit.Tree(
ts,
options=options,
tracked_samples=[1, bad_sample],
)
_tskit.Tree(
ts,
options=options,
tracked_samples=[1, bad_sample],
)
with pytest.raises(ValueError):
with pytest.deprecated_call():
_tskit.Tree(ts, tracked_samples=[1, bad_sample, 1])
_tskit.Tree(ts, tracked_samples=[1, bad_sample, 1])

def test_while_loop_semantics(self):
for ts in self.get_example_tree_sequences():
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ def verify_metadata_vector(self, table, key, dtype, default_value=9999):
else:
md = default_value
break
assert np.all(np.cast[dtype](md) == x)
assert np.all(np.asarray(md, dtype=dtype) == x)

def test_metadata_vector_errors(self):
table = self.table_class()
Expand Down
16 changes: 8 additions & 8 deletions python/tests/test_tree_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ def site_segregating_sites(ts, sample_sets, windows=None, span_normalise=True):
haps = ts.genotype_matrix(isolated_as_missing=False)
site_positions = [x.position for x in ts.sites()]
for i, X in enumerate(sample_sets):
X_index = np.where(np.in1d(samples, X))[0]
X_index = np.where(np.isin(samples, X))[0]
for k in range(ts.num_sites):
if (site_positions[k] >= begin) and (site_positions[k] < end):
num_alleles = len(set(haps[k, X_index]))
Expand Down Expand Up @@ -1428,7 +1428,7 @@ def site_tajimas_d(ts, sample_sets, windows=None):
nn = n[i]
S = 0
T = 0
X_index = np.where(np.in1d(samples, X))[0]
X_index = np.where(np.isin(samples, X))[0]
for k in range(ts.num_sites):
if (site_positions[k] >= begin) and (site_positions[k] < end):
hX = haps[k, X_index]
Expand Down Expand Up @@ -4571,7 +4571,7 @@ def branch_trait_covariance(ts, W, windows=None, span_normalise=True):
has_trees = True
SS = 0
for u in range(ts.num_nodes):
below = np.in1d(samples, list(tr.samples(u)))
below = np.isin(samples, list(tr.samples(u)))
branch_length = tr.branch_length(u)
SS += covsq(w, below) * branch_length
S += SS * (min(end, tr.interval.right) - max(begin, tr.interval.left))
Expand Down Expand Up @@ -4606,7 +4606,7 @@ def node_trait_covariance(ts, W, windows=None, span_normalise=True):
break
SS = np.zeros(ts.num_nodes)
for u in range(ts.num_nodes):
below = np.in1d(samples, list(tr.samples(u)))
below = np.isin(samples, list(tr.samples(u)))
SS[u] += covsq(w, below)
S += SS * (min(end, tr.interval.right) - max(begin, tr.interval.left))
out[j, :, i] = S
Expand Down Expand Up @@ -4782,7 +4782,7 @@ def branch_trait_correlation(ts, W, windows=None, span_normalise=True):
has_trees = True
SS = 0
for u in range(ts.num_nodes):
below = np.in1d(samples, list(tr.samples(u)))
below = np.isin(samples, list(tr.samples(u)))
p = np.mean(below)
if p > 0 and p < 1:
branch_length = tr.branch_length(u)
Expand Down Expand Up @@ -4823,7 +4823,7 @@ def node_trait_correlation(ts, W, windows=None, span_normalise=True):
break
SS = np.zeros(ts.num_nodes)
for u in range(ts.num_nodes):
below = np.in1d(samples, list(tr.samples(u)))
below = np.isin(samples, list(tr.samples(u)))
p = np.mean(below)
if p > 0 and p < 1:
# SS[u] += sum(w[below])**2 / 2
Expand Down Expand Up @@ -5046,7 +5046,7 @@ def branch_trait_linear_model(ts, W, Z, windows=None, span_normalise=True):
has_trees = True
SS = 0
for u in range(ts.num_nodes):
below = np.in1d(samples, list(tr.samples(u)))
below = np.isin(samples, list(tr.samples(u)))
branch_length = tr.branch_length(u)
SS += linear_model(w, below, Z) * branch_length
S += SS * (min(end, tr.interval.right) - max(begin, tr.interval.left))
Expand Down Expand Up @@ -5081,7 +5081,7 @@ def node_trait_linear_model(ts, W, Z, windows=None, span_normalise=True):
break
SS = np.zeros(ts.num_nodes)
for u in range(ts.num_nodes):
below = np.in1d(samples, list(tr.samples(u)))
below = np.isin(samples, list(tr.samples(u)))
SS[u] += linear_model(w, below, Z)
S += SS * (min(end, tr.interval.right) - max(begin, tr.interval.left))
out[j, :, i] = S
Expand Down
5 changes: 2 additions & 3 deletions python/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2018-2023 Tskit Developers
# Copyright (c) 2018-2024 Tskit Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -196,8 +196,7 @@ def test_nonrectangular_input(self):
# On some platforms and Python / numpy versions, a ValueError
# occurs instead
with pytest.raises((TypeError, ValueError)):
with pytest.deprecated_call():
util.safe_np_int_cast(bad_input, dtype)
util.safe_np_int_cast(bad_input, dtype)


class TestIntervalOps:
Expand Down
6 changes: 3 additions & 3 deletions python/tskit/formats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2018-2023 Tskit Developers
# Copyright (c) 2018-2024 Tskit Developers
# Copyright (c) 2016-2017 University of Oxford
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -150,9 +150,9 @@ def _load_legacy_hdf5_v2(root, remove_duplicate_positions):
population[cr_node] = cr_population
if "samples" in root:
samples_group = root["samples"]
population[:sample_size] = samples_group["population"]
population[:sample_size] = np.array(samples_group["population"], copy=True)
if "time" in samples_group:
time[:sample_size] = samples_group["time"]
time[:sample_size] = np.array(samples_group["time"], copy=True)
tables.nodes.set_columns(flags=flags, population=population, time=time)
_set_populations(tables)

Expand Down

0 comments on commit 4a5d98d

Please sign in to comment.