From 4a5d98d3fc339c5ec7ac65e337c27564450d52a0 Mon Sep 17 00:00:00 2001 From: Ben Jeffery Date: Wed, 26 Jun 2024 14:28:01 +0100 Subject: [PATCH] Fix up tests --- .../requirements/CI-complete/requirements.txt | 25 ++++++++--------- .../CI-tests-conda/requirements.txt | 16 ++--------- .../CI-tests-pip/requirements.txt | 14 ++++++++-- python/tests/simplify.py | 4 ++- python/tests/test_coalrate.py | 2 +- python/tests/test_ld_matrix.py | 6 ++-- python/tests/test_lowlevel.py | 28 ++++++++----------- python/tests/test_tables.py | 2 +- python/tests/test_tree_stats.py | 16 +++++------ python/tests/test_util.py | 5 ++-- python/tskit/formats.py | 6 ++-- 11 files changed, 59 insertions(+), 65 deletions(-) diff --git a/python/requirements/CI-complete/requirements.txt b/python/requirements/CI-complete/requirements.txt index 62e2d3d365..257b08c4ce 100644 --- a/python/requirements/CI-complete/requirements.txt +++ b/python/requirements/CI-complete/requirements.txt @@ -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 diff --git a/python/requirements/CI-tests-conda/requirements.txt b/python/requirements/CI-tests-conda/requirements.txt index 609331f619..3c58381b9e 100644 --- a/python/requirements/CI-tests-conda/requirements.txt +++ b/python/requirements/CI-tests-conda/requirements.txt @@ -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 diff --git a/python/requirements/CI-tests-pip/requirements.txt b/python/requirements/CI-tests-pip/requirements.txt index f8910ec249..7fa7730bc9 100644 --- a/python/requirements/CI-tests-pip/requirements.txt +++ b/python/requirements/CI-tests-pip/requirements.txt @@ -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 \ No newline at end of file diff --git a/python/tests/simplify.py b/python/tests/simplify.py index 1e62c9d11b..9e416c3e78 100644 --- a/python/tests/simplify.py +++ b/python/tests/simplify.py @@ -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) diff --git a/python/tests/test_coalrate.py b/python/tests/test_coalrate.py index dcfcfc8d66..471f63ff1d 100644 --- a/python/tests/test_coalrate.py +++ b/python/tests/test_coalrate.py @@ -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], diff --git a/python/tests/test_ld_matrix.py b/python/tests/test_ld_matrix.py index 1733ea140f..693bc32880 100644 --- a/python/tests/test_ld_matrix.py +++ b/python/tests/test_ld_matrix.py @@ -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)) @@ -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)) @@ -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) diff --git a/python/tests/test_lowlevel.py b/python/tests/test_lowlevel.py index 54b19b1d6f..0fd20b3442 100644 --- a/python/tests/test_lowlevel.py +++ b/python/tests/test_lowlevel.py @@ -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"): @@ -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(): diff --git a/python/tests/test_tables.py b/python/tests/test_tables.py index 51852a2c79..6ab55f4e1e 100644 --- a/python/tests/test_tables.py +++ b/python/tests/test_tables.py @@ -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() diff --git a/python/tests/test_tree_stats.py b/python/tests/test_tree_stats.py index a272c4f8cf..4b608b5d75 100644 --- a/python/tests/test_tree_stats.py +++ b/python/tests/test_tree_stats.py @@ -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])) @@ -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] @@ -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)) @@ -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 @@ -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) @@ -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 @@ -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)) @@ -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 diff --git a/python/tests/test_util.py b/python/tests/test_util.py index eaed4d07e3..4a124619fa 100644 --- a/python/tests/test_util.py +++ b/python/tests/test_util.py @@ -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 @@ -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: diff --git a/python/tskit/formats.py b/python/tskit/formats.py index ac466ce1a8..76d0ad7376 100644 --- a/python/tskit/formats.py +++ b/python/tskit/formats.py @@ -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 @@ -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)