def test_unsupported_version(self): ts = msprime.simulate(10) with pytest.raises(ValueError): tskit.dump_legacy(ts, self.temp_file, version=4) # Cannot read current files. ts.dump(self.temp_file) # Catch Exception here because h5py throws different exceptions on py2 and py3 with pytest.raises(Exception): tskit.load_legacy(self.temp_file)
def verify_malformed_json_v2(self, ts, group_name, attr, bad_json): tskit.dump_legacy(ts, self.temp_file, 2) # Write some bad JSON to the provenance string. root = h5py.File(self.temp_file, "r+") group = root[group_name] group.attrs[attr] = bad_json root.close() tsp = tskit.load_legacy(self.temp_file) self.verify_tree_sequences_equal(ts, tsp)
def test_v2_non_binary_records(self): demographic_events = [ msprime.SimpleBottleneck(time=0.01, population=0, proportion=1) ] ts = msprime.simulate(sample_size=10, demographic_events=demographic_events, random_seed=1) with pytest.raises(ValueError): tskit.dump_legacy(ts, self.temp_file, 2)
def verify_round_trip(self, ts, version): tskit.dump_legacy(ts, self.temp_file, version=version) tsp = tskit.load_legacy(self.temp_file) simplify = version < 10 self.verify_tree_sequences_equal(ts, tsp, simplify=simplify) tsp.dump(self.temp_file) tsp = tskit.load(self.temp_file) self.verify_tree_sequences_equal(ts, tsp, simplify=simplify) for provenance in tsp.provenances(): tskit.validate_provenance(json.loads(provenance.record))
def test_no_h5py(self): ts = msprime.simulate(10) path = os.path.join(test_data_dir, "hdf5-formats", "msprime-0.3.0_v2.0.hdf5") msg = ( "Legacy formats require h5py. Install via `pip install h5py` or" " `conda install h5py`" ) with mock.patch.dict(sys.modules, {"h5py": None}): with pytest.raises(ImportError, match=msg): tskit.load_legacy(path) with pytest.raises(ImportError, match=msg): tskit.dump_legacy(ts, path)
def test_duplicate_positions_error(self): ts = msprime.simulate(10, mutation_rate=10) for version in [2, 3]: tskit.dump_legacy(ts, self.legacy_file_name, version=version) root = h5py.File(self.legacy_file_name, "r+") root['mutations/position'][:] = 0 root.close() with mock.patch("sys.exit", side_effect=TestException) as mocked_exit: with self.assertRaises(TestException): capture_output( cli.tskit_main, ["upgrade", self.legacy_file_name, self.current_file_name]) self.assertEqual(mocked_exit.call_count, 1)
def test_duplicate_mutation_positions_single_value(self): ts = multi_locus_with_mutation_example() for version in [2, 3]: tskit.dump_legacy(ts, self.temp_file, version=version) root = h5py.File(self.temp_file, "r+") root["mutations/position"][:] = 0 root.close() with pytest.raises(tskit.DuplicatePositionsError): tskit.load_legacy(self.temp_file) tsp = tskit.load_legacy(self.temp_file, remove_duplicate_positions=True) assert tsp.num_sites == 1 sites = list(tsp.sites()) assert sites[0].position == 0
def test_no_h5py(self): ts = msprime.simulate(10) msg = ("Legacy formats require h5py. Install via `pip install h5py` or" " `conda install h5py`") with h5py.File(self.temp_file, "w") as root: root["x"] = np.zeros(10) with mock.patch.dict(sys.modules, {"h5py": None}): with pytest.raises(ImportError, match=msg): tskit.load(self.temp_file) with pytest.raises(ImportError, match=msg): tskit.load_legacy(self.temp_file) with pytest.raises(ImportError, match=msg): tskit.dump_legacy(ts, self.temp_file)
def test_duplicate_positions(self): ts = msprime.simulate(10, mutation_rate=10) for version in [2, 3]: tskit.dump_legacy(ts, self.legacy_file_name, version=version) root = h5py.File(self.legacy_file_name, "r+") root['mutations/position'][:] = 0 root.close() stdout, stderr = capture_output( cli.tskit_main, ["upgrade", "-d", self.legacy_file_name, self.current_file_name]) self.assertEqual(stdout, "") tsp = tskit.load(self.current_file_name) self.assertEqual(tsp.sample_size, ts.sample_size) self.assertEqual(tsp.num_sites, 1)
def test_conversion(self): ts1 = msprime.simulate(10) for version in [2, 3]: tskit.dump_legacy(ts1, self.legacy_file_name, version=version) stdout, stderr = capture_output( cli.tskit_main, [ "upgrade", self.legacy_file_name, self.current_file_name]) ts2 = tskit.load(self.current_file_name) self.assertEqual(stdout, "") self.assertEqual(stderr, "") # Quick checks to ensure we have the right tree sequence. # More thorough checks are done elsewhere. self.assertEqual(ts1.get_sample_size(), ts2.get_sample_size()) self.assertEqual(ts1.num_edges, ts2.num_edges) self.assertEqual(ts1.get_num_trees(), ts2.get_num_trees())
def test_duplicate_mutation_positions(self): ts = multi_locus_with_mutation_example() for version in [2, 3]: tskit.dump_legacy(ts, self.temp_file, version=version) root = h5py.File(self.temp_file, "r+") position = np.array(root["mutations/position"]) position[0] = position[1] root["mutations/position"][:] = position root.close() with pytest.raises(tskit.DuplicatePositionsError): tskit.load_legacy(self.temp_file) tsp = tskit.load_legacy(self.temp_file, remove_duplicate_positions=True) assert tsp.num_sites == position.shape[0] - 1 position_after = list(s.position for s in tsp.sites()) assert list(position[1:]) == position_after
def test_general_mutation_example(self): ts = general_mutation_example() for version in [2, 3]: with pytest.raises(ValueError): tskit.dump_legacy(ts, self.temp_file, version) self.verify_round_trip(ts, 10)