def test_len(self): rate_map = msprime.RateMap(position=[0, 1], rate=[0.1]) assert len(rate_map) == 1 rate_map = msprime.RateMap(position=[0, 1, 2], rate=[0.1, 0.2]) assert len(rate_map) == 2 rate_map = msprime.RateMap(position=[0, 1, 2, 3], rate=[0.1, 0.2, 0.3]) assert len(rate_map) == 3
def test_slice_trim_left(self): a = msprime.RateMap(position=[0, 100, 200, 300, 400], rate=[1, 2, 3, 4]) b = a.slice(left=100, trim=True) assert b == msprime.RateMap(position=[0, 100, 200, 300], rate=[2, 3, 4]) b = a.slice(left=50, trim=True) assert b == msprime.RateMap(position=[0, 50, 150, 250, 350], rate=[1, 2, 3, 4])
def test_slice_trim_right(self): a = msprime.RateMap(position=[0, 100, 200, 300, 400], rate=[1, 2, 3, 4]) b = a.slice(right=300, trim=True) assert b == msprime.RateMap(position=[0, 100, 200, 300], rate=[1, 2, 3]) b = a.slice(right=350, trim=True) assert b == msprime.RateMap(position=[0, 100, 200, 300, 350], rate=[1, 2, 3, 4])
def test_interval_properties_all_known(self): rate_map = msprime.RateMap(position=[0, 1, 2, 3], rate=[0.1, 0.2, 0.3]) assert list(rate_map.left) == [0, 1, 2] assert list(rate_map.right) == [1, 2, 3] assert list(rate_map.mid) == [0.5, 1.5, 2.5] assert list(rate_map.span) == [1, 1, 1] assert list(rate_map.mass) == [0.1, 0.2, 0.3]
def test_get_cumulative_mass(self): positions = np.array([0, 1, 2]) rates = np.array([1, 4]) rate_map = msprime.RateMap(position=positions, rate=rates) assert np.allclose(rate_map.get_cumulative_mass([0.5, 1.5]), np.array([0.5, 3])) assert rate_map.get_cumulative_mass([2]) == rate_map.total_mass
def test_slice_with_floats(self): # test RateMap.slice(..., trim=False) with floats a = msprime.RateMap([np.pi * x for x in [0, 100, 200, 300, 400]], [0, 1, 2, 3]) b = a.slice(start=50 * np.pi) assert a.sequence_length == b.sequence_length assert np.array_equal(a.position, b.position) assert np.array_equal(a.rate, b.rate) b = a.slice(start=150 * np.pi) assert a.sequence_length == b.sequence_length assert np.array_equal([np.pi * x for x in [0, 150, 200, 300, 400]], b.position) assert np.array_equal([0, 1, 2, 3], b.rate) b = a.slice(end=300 * np.pi) assert a.sequence_length == b.sequence_length assert np.array_equal([np.pi * x for x in [0, 100, 200, 300, 400]], b.position) assert np.array_equal([0, 1, 2, 0], b.rate) b = a.slice(end=250 * np.pi) assert a.sequence_length == b.sequence_length assert np.array_equal([np.pi * x for x in [0, 100, 200, 250, 400]], b.position) assert np.array_equal([0, 1, 2, 0], b.rate) b = a.slice(start=50 * np.pi, end=300 * np.pi) assert a.sequence_length == b.sequence_length assert np.array_equal([np.pi * x for x in [0, 100, 200, 300, 400]], b.position) assert np.array_equal([0, 1, 2, 0], b.rate) b = a.slice(start=150 * np.pi, end=160 * np.pi) assert a.sequence_length == b.sequence_length assert np.array_equal([np.pi * x for x in [0, 150, 160, 400]], b.position) assert np.array_equal([0, 1, 0], b.rate)
def test_read_only(self): positions = np.array([0, 0.25, 0.5, 0.75, 1]) rates = np.array([0.125, 0.25, 0.5, 0.75]) # 1 shorter than positions rate_map = msprime.RateMap(position=positions, rate=rates) assert np.all(rates == rate_map.rate) assert np.all(positions == rate_map.position) with pytest.raises(AttributeError): rate_map.rate = 2 * rate_map.rate with pytest.raises(AttributeError): rate_map.position = 2 * rate_map.position with pytest.raises(AttributeError): rate_map.left = 1234 with pytest.raises(AttributeError): rate_map.right = 1234 with pytest.raises(AttributeError): rate_map.mid = 1234 with pytest.raises(ValueError): rate_map.rate[0] = 1 with pytest.raises(ValueError): rate_map.position[0] = 1 with pytest.raises(ValueError): rate_map.left[0] = 1 with pytest.raises(ValueError): rate_map.mid[0] = 1 with pytest.raises(ValueError): rate_map.right[0] = 1
def test_getitem_slice_with_negative_indexes_and_floats(self): # test RateMap slice syntax with negative indexes and floats a = msprime.RateMap([0, 100, 200, 300, 400], [0, 1, 2, 3]) b = a[150:250] c = a[150:-150] self.assertTrue(np.array_equal(b.position, c.position)) self.assertTrue(np.array_equal(b.rate, c.rate)) b = a[150:250] c = a[-250:250] self.assertTrue(np.array_equal(b.position, c.position)) self.assertTrue(np.array_equal(b.rate, c.rate)) b = a[150:250] c = a[-250:-150] self.assertTrue(np.array_equal(b.position, c.position)) self.assertTrue(np.array_equal(b.rate, c.rate)) b = a[:-np.pi] c = a[:400 - np.pi] self.assertTrue(np.array_equal(b.position, c.position)) self.assertTrue(np.array_equal(b.rate, c.rate)) b = a[-50 * np.pi:-np.pi] c = a[400 - 50 * np.pi:400 - np.pi] self.assertTrue(np.array_equal(b.position, c.position)) self.assertTrue(np.array_equal(b.rate, c.rate))
def hotspots_to_recomb_map(hotspots, background_rate, seq_length): """ Translates the background recombination rate and recombination hotspots according to the msHOT cli spec to a msprime recombination map. hotspots is of the form: [n] + [start, stop, factor]+ where n is the number of hotspots and each hotspot spans [start, stop) with a recombination rate of background_rate * factor. Intervals between hotspots have a recombination rate of background_rate. """ assert len(hotspots) > 0 n_hotspots = hotspots[0] assert len(hotspots[1:]) == 3 * n_hotspots positions = [] rates = [] if hotspots[1] != 0: positions.append(0) rates.append(background_rate) for i in range(1, len(hotspots) - 2, 3): [start, stop, factor] = hotspots[i : i + 3] # Beginning hotspot positions.append(start) rates.append(factor * background_rate) if i == len(hotspots) - 3 or stop != hotspots[i + 3]: # Ending hotspot, back to normal recombination rate positions.append(stop) if stop != seq_length: rates.append(background_rate) if positions[-1] != seq_length: positions.append(seq_length) return msprime.RateMap(positions, rates)
def test_get_bad_cumulative_mass(self): positions = np.array([0, 1, 2]) rates = np.array([1, 4]) rate_map = msprime.RateMap(positions, rates) with pytest.raises(ValueError, match="physical positions"): rate_map.get_cumulative_mass([1, -0.1]) with pytest.raises(ValueError, match="physical positions"): rate_map.get_cumulative_mass([1, 2.1])
def test_with_recomb_map(self, recipe): ts = recipe["ts"] recomb_rate = 1.0 / ts.sequence_length recombination_map = msprime.RateMap( position = [0.0, ts.sequence_length], rate = [recomb_rate]) recap = self.do_recapitate(ts, recombination_rate=recombination_map, ancestral_Ne=1e-6) self.check_recap_consistency(ts, recap)
def test_get_rate_out_of_bounds(self): positions = np.array([0, 1, 2]) rates = np.array([1, 4]) rate_map = msprime.RateMap(position=positions, rate=rates) with pytest.raises(ValueError, match="out of bounds"): rate_map.get_rate([1, -0.1]) with pytest.raises(ValueError, match="out of bounds"): rate_map.get_rate([2])
def test_getitem_slice(self): r1 = msprime.RateMap(position=[0, 1, 2], rate=[0.1, 0.2]) # The semantics of the slice() function are tested elsewhere. assert r1[:] == r1.copy() assert r1[:] is not r1 assert r1[1:] == r1.slice(left=1) assert r1[:1.5] == r1.slice(right=1.5) assert r1[0.5:1.5] == r1.slice(left=0.5, right=1.5)
def test_repr_html(self): rate_map = msprime.RateMap(position=[0, 10], rate=[0.1]) html = rate_map._repr_html_() root = xml.etree.ElementTree.fromstring(html) assert root.tag == "div" table = root.find("table") rows = list(table.find("tbody")) assert len(rows) == 1
def test_in_points(self): rate_map = msprime.RateMap(position=[0, 1, 2, 3], rate=[0.1, 0.2, 0.3]) # Any point within the map are True for x in [0, 0.5, 1, 2.9999]: assert x in rate_map # Points outside the map are False for x in [-1, -0.0001, 3, 3.1]: assert x not in rate_map
def test_slice_with_floats(self): # test RateMap.slice(..., trim=False) with floats a = msprime.RateMap( position=[np.pi * x for x in [0, 100, 200, 300, 400]], rate=[0, 1, 2, 3]) b = a.slice(left=50 * np.pi) assert a.sequence_length == b.sequence_length assert_array_equal([0, 50 * np.pi] + list(a.position[1:]), b.position) assert_array_equal([np.nan] + list(a.rate), b.rate)
def test_two_intervals(self): rate_map = msprime.RateMap(position=[0, 5, 10], rate=[0.1, 0.1]) assert rate_map.find_index(0) == 0 assert rate_map.find_index(0.0001) == 0 assert rate_map.find_index(4.9999) == 0 assert rate_map.find_index(5) == 1 assert rate_map.find_index(5.1) == 1 assert rate_map.find_index(7) == 1 assert rate_map.find_index(9.999) == 1
def test_slice_no_params(self): # test RateMap.slice(..., trim=False) a = msprime.RateMap(position=[0, 100, 200, 300, 400], rate=[0, 1, 2, 3]) b = a.slice() assert a.sequence_length == b.sequence_length assert_array_equal(a.position, b.position) assert_array_equal(a.rate, b.rate) assert a == b
def test_short_table(self): n = 10 rate_map = msprime.RateMap(position=range(n + 1), rate=[0.1] * n) headers, data = rate_map._display_table() assert len(headers) == 5 assert len(data) == n # check some left values. assert int(data[0][0]) == 0 assert int(data[-1][0]) == n - 1
def test_str(self): rate_map = msprime.RateMap(position=[0, 10], rate=[0.1]) s = """ ┌──────────────────────────────────┐ │left │right │ mid│ span│ rate│ ├──────────────────────────────────┤ │0 │10 │ 5│ 10│ 0.10│ └──────────────────────────────────┘ """ assert textwrap.dedent(s) == str(rate_map)
def test_slice_error(self): recomb_map = msprime.RateMap(position=[0, 100], rate=[1]) with pytest.raises(KeyError): recomb_map.slice(left=-1) with pytest.raises(KeyError): recomb_map.slice(right=-1) with pytest.raises(KeyError): recomb_map.slice(left=200) with pytest.raises(KeyError): recomb_map.slice(right=200) with pytest.raises(KeyError): recomb_map.slice(left=20, right=10)
def test_slice_left_missing(self): a = msprime.RateMap(position=[0, 100, 200, 300, 400], rate=[np.nan, 1, 2, 3]) b = a.slice(left=50) assert a.sequence_length == b.sequence_length assert_array_equal(a.position, b.position) assert_array_equal(a.rate, b.rate) b = a.slice(left=100) assert a.sequence_length == b.sequence_length assert_array_equal(a.position, b.position) assert_array_equal(a.rate, b.rate)
def test_slice_error(self): recomb_map = msprime.RateMap([0, 100], [1]) with self.assertRaises(IndexError): recomb_map.slice(start=-1) with self.assertRaises(IndexError): recomb_map.slice(end=-1) with self.assertRaises(IndexError): recomb_map.slice(start=200) with self.assertRaises(IndexError): recomb_map.slice(end=200) with self.assertRaises(IndexError): recomb_map.slice(start=20, end=10)
def test_slice_right_examples(self): a = msprime.RateMap(position=[0, 100, 200, 300, 400], rate=[0, 1, 2, 3]) b = a.slice(right=300) assert a.sequence_length == b.sequence_length assert_array_equal([0, 100, 200, 300, 400], b.position) assert_array_equal([0, 1, 2, np.nan], b.rate) b = a.slice(right=250) assert a.sequence_length == b.sequence_length assert_array_equal([0, 100, 200, 250, 400], b.position) assert_array_equal([0, 1, 2, np.nan], b.rate)
class TestGetRateAllKnown: examples = [ msprime.RateMap(position=[0, 1], rate=[0]), msprime.RateMap(position=[0, 1], rate=[0.1]), msprime.RateMap(position=[0, 1, 2], rate=[0.1, 0.2]), msprime.RateMap(position=[0, 1, 2], rate=[0, 0.2]), msprime.RateMap(position=[0, 1, 2], rate=[0.1, 1e-6]), msprime.RateMap(position=range(100), rate=range(99)), ] @pytest.mark.parametrize("rate_map", examples) def test_get_rate_mid(self, rate_map): rate = rate_map.get_rate(rate_map.mid) assert len(rate) == len(rate_map) for j in range(len(rate_map)): assert rate[j] == rate_map[rate_map.mid[j]] @pytest.mark.parametrize("rate_map", examples) def test_get_rate_left(self, rate_map): rate = rate_map.get_rate(rate_map.left) assert len(rate) == len(rate_map) for j in range(len(rate_map)): assert rate[j] == rate_map[rate_map.left[j]] @pytest.mark.parametrize("rate_map", examples) def test_get_rate_right(self, rate_map): rate = rate_map.get_rate(rate_map.right[:-1]) assert len(rate) == len(rate_map) - 1 for j in range(len(rate_map) - 1): assert rate[j] == rate_map[rate_map.right[j]]
def test_getitem_slice(self): # test RateMap slice syntax a = msprime.RateMap([0, 100, 200, 300, 400], [0, 1, 2, 3]) b = a[:] self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal(a.position, b.position)) self.assertTrue(np.array_equal(a.rate, b.rate)) b = a[50:] self.assertEqual(350, b.sequence_length) self.assertTrue(np.array_equal([0, 50, 150, 250, 350], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 3], b.rate)) b = a[100:] self.assertEqual(300, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200, 300], b.position)) self.assertTrue(np.array_equal([1, 2, 3], b.rate)) b = a[150:] self.assertEqual(250, b.sequence_length) self.assertTrue(np.array_equal([0, 50, 150, 250], b.position)) self.assertTrue(np.array_equal([1, 2, 3], b.rate)) b = a[:300] self.assertEqual(300, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200, 300], b.position)) self.assertTrue(np.array_equal([0, 1, 2], b.rate)) b = a[:250] self.assertEqual(250, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200, 250], b.position)) self.assertTrue(np.array_equal([0, 1, 2], b.rate)) b = a[50:300] self.assertEqual(250, b.sequence_length) self.assertTrue(np.array_equal([0, 50, 150, 250], b.position)) self.assertTrue(np.array_equal([0, 1, 2], b.rate)) b = a[100:300] self.assertEqual(200, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200], b.position)) self.assertTrue(np.array_equal([1, 2], b.rate)) b = a[150:250] self.assertEqual(100, b.sequence_length) self.assertTrue(np.array_equal([0, 50, 100], b.position)) self.assertTrue(np.array_equal([1, 2], b.rate)) b = a[150:160] self.assertEqual(10, b.sequence_length) self.assertTrue(np.array_equal([0, 10], b.position)) self.assertTrue(np.array_equal([1], b.rate))
def test_getitem_slice(self): # test RateMap slice syntax a = msprime.RateMap([0, 100, 200, 300, 400], [0, 1, 2, 3]) b = a[:] assert a.sequence_length == b.sequence_length assert np.array_equal(a.position, b.position) assert np.array_equal(a.rate, b.rate) b = a[50:] assert 350 == b.sequence_length assert np.array_equal([0, 50, 150, 250, 350], b.position) assert np.array_equal([0, 1, 2, 3], b.rate) b = a[100:] assert 300 == b.sequence_length assert np.array_equal([0, 100, 200, 300], b.position) assert np.array_equal([1, 2, 3], b.rate) b = a[150:] assert 250 == b.sequence_length assert np.array_equal([0, 50, 150, 250], b.position) assert np.array_equal([1, 2, 3], b.rate) b = a[:300] assert 300 == b.sequence_length assert np.array_equal([0, 100, 200, 300], b.position) assert np.array_equal([0, 1, 2], b.rate) b = a[:250] assert 250 == b.sequence_length assert np.array_equal([0, 100, 200, 250], b.position) assert np.array_equal([0, 1, 2], b.rate) b = a[50:300] assert 250 == b.sequence_length assert np.array_equal([0, 50, 150, 250], b.position) assert np.array_equal([0, 1, 2], b.rate) b = a[100:300] assert 200 == b.sequence_length assert np.array_equal([0, 100, 200], b.position) assert np.array_equal([1, 2], b.rate) b = a[150:250] assert 100 == b.sequence_length assert np.array_equal([0, 50, 100], b.position) assert np.array_equal([1, 2], b.rate) b = a[150:160] assert 10 == b.sequence_length assert np.array_equal([0, 10], b.position) assert np.array_equal([1], b.rate)
def test_slice(self): # test RateMap.slice(..., trim=False) a = msprime.RateMap([0, 100, 200, 300, 400], [0, 1, 2, 3]) b = a.slice() self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal(a.position, b.position)) self.assertTrue(np.array_equal(a.rate, b.rate)) b = a.slice(start=50) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200, 300, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 3], b.rate)) b = a.slice(start=100) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200, 300, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 3], b.rate)) b = a.slice(start=150) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 150, 200, 300, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 3], b.rate)) b = a.slice(end=300) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200, 300, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 0], b.rate)) b = a.slice(end=250) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200, 250, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 0], b.rate)) b = a.slice(start=50, end=300) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 100, 200, 300, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 0], b.rate)) b = a.slice(start=150, end=250) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 150, 200, 250, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 0], b.rate)) b = a.slice(start=150, end=300) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 150, 200, 300, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 2, 0], b.rate)) b = a.slice(start=150, end=160) self.assertEqual(a.sequence_length, b.sequence_length) self.assertTrue(np.array_equal([0, 150, 160, 400], b.position)) self.assertTrue(np.array_equal([0, 1, 0], b.rate))
def test_bad_input(self): bad_inputs = [ ([], []), ([0], []), ([0], [0]), ([1, 2], [0]), ([0, -1], [0]), ([0, 1], [-1]), ] for pos, rate in bad_inputs: with self.assertRaises(ValueError): msprime.RateMap(pos, rate)
def test_recombination_map(self): def f(recomb_map): return msprime.simulator_factory(10, recombination_map=recomb_map) self.assertRaises(TypeError, f, "wrong type") for n in range(2, 10): positions = list(range(n)) rates = [0.1 * j for j in range(n - 1)] # Use the old-form RecombinationMap recomb_map = msprime.RecombinationMap(positions, rates + [0.0]) sim = msprime.simulator_factory(10, recombination_map=recomb_map) other_map = msprime.RateMap(**sim.recombination_map) self.assertEqual(list(other_map.position), positions) self.assertEqual(list(other_map.rate), rates) self.assertEqual(sim.sequence_length, other_map.sequence_length) # Use the new-form RateMap rate_map = msprime.RateMap(positions, rates) sim = msprime.simulator_factory(10, recombination_map=rate_map) other_map = msprime.RateMap(**sim.recombination_map) self.assertEqual(list(other_map.position), positions) self.assertEqual(list(other_map.rate), rates) self.assertEqual(sim.sequence_length, other_map.sequence_length)