def test_constructor(self): # missing data arg with pytest.raises(TypeError): # noinspection PyArgumentList SortedIndex() # data has wrong dtype data = 'foo bar' with pytest.raises(TypeError): SortedIndex(data) # data has wrong dimensions data = [[1, 2], [3, 4]] with pytest.raises(TypeError): SortedIndex(data) # values are not sorted data = [2, 1, 3, 5] with pytest.raises(ValueError): SortedIndex(data) # values are not sorted data = [4., 5., 3.7] with pytest.raises(ValueError): SortedIndex(data) # valid data (unique) data = [1, 4, 5, 7, 12] idx = SortedIndex(data) aeq(data, idx) assert np.int == idx.dtype assert 1 == idx.ndim assert 5 == len(idx) assert idx.is_unique # valid data (non-unique) data = [1, 4, 5, 5, 7, 12] idx = SortedIndex(data) aeq(data, idx) assert np.int == idx.dtype assert 1 == idx.ndim assert 6 == len(idx) assert not idx.is_unique # valid data (typed) data = [1, 4, 5, 5, 7, 12] idx = SortedIndex(data, dtype='u4') aeq(data, idx) assert np.uint32 == idx.dtype # valid data (non-numeric) data = np.array(['1', '12', '4', '5', '5', '7'], dtype=object) idx = SortedIndex(data) aeq(data, idx)
def test_masked_windowed_divergence(self): h = HaplotypeArray([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1], [0, 0, 1, 2], [0, 1, 1, 2], [0, 1, -1, -1], [-1, -1, -1, -1]]) h1 = h.take([0, 1], axis=1) h2 = h.take([2, 3], axis=1) ac1 = h1.count_alleles() ac2 = h2.count_alleles() pos = SortedIndex([2, 4, 7, 14, 15, 18, 19, 25, 27]) mask = np.tile(np.repeat(np.array([True, False]), 5), 3) expect, _, _, _ = allel.windowed_divergence(pos, ac1, ac2, size=5, start=1, stop=31) expect = expect[::2] actual, _, _, _ = allel.windowed_divergence(pos, ac1, ac2, size=10, start=1, stop=31, is_accessible=mask) assert_array_almost_equal(expect, actual)
def test_masked_windowed_diversity(self): # four haplotypes, 6 pairwise comparison h = allel.HaplotypeArray([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1], [0, 0, 1, 2], [0, 1, 1, 2], [0, 1, -1, -1], [-1, -1, -1, -1]]) ac = h.count_alleles() # mean pairwise diversity # expect = [0, 3/6, 4/6, 3/6, 0, 5/6, 5/6, 1, -1] pos = SortedIndex([2, 4, 7, 14, 15, 18, 19, 25, 27]) mask = np.tile(np.repeat(np.array([True, False]), 5), 3) # expected is every other window with size 5 expect, _, _, _ = allel.windowed_diversity(pos, ac, size=5, start=1, stop=31) # only getting every other element expect = expect[::2] # actual is window of size 10 with the last half masked out actual, _, _, _ = allel.windowed_diversity(pos, ac, size=10, start=1, stop=31, is_accessible=mask) assert_array_almost_equal(expect, actual)
def test_slice(self): data = [1, 4, 5, 5, 7, 12] idx = SortedIndex(data, dtype='u4') # row slice s = idx[1:] assert isinstance(s, SortedIndex) # index s = idx[0] assert isinstance(s, np.uint32) assert not isinstance(s, SortedIndex) assert data[0] == s
def test_windowed_diversity(self): # four haplotypes, 6 pairwise comparison h = HaplotypeArray([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1], [0, 0, 1, 2], [0, 1, 1, 2], [0, 1, -1, -1], [-1, -1, -1, -1]]) ac = h.count_alleles() # mean pairwise diversity # expect = [0, 3/6, 4/6, 3/6, 0, 5/6, 5/6, 1, -1] pos = SortedIndex([2, 4, 7, 14, 15, 18, 19, 25, 27]) expect = [(7 / 6) / 10, (13 / 6) / 10, 1 / 11] actual, _, _, _ = allel.windowed_diversity(pos, ac, size=10, start=1, stop=31) assert_array_almost_equal(expect, actual)
def test_windowed_divergence(self): # simplest case, two haplotypes in each population h = HaplotypeArray([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1], [0, 0, 1, 2], [0, 1, 1, 2], [0, 1, -1, -1], [-1, -1, -1, -1]]) h1 = h.take([0, 1], axis=1) h2 = h.take([2, 3], axis=1) ac1 = h1.count_alleles() ac2 = h2.count_alleles() # mean pairwise divergence # expect = [0/4, 2/4, 4/4, 2/4, 0/4, 4/4, 3/4, -1, -1] pos = SortedIndex([2, 4, 7, 14, 15, 18, 19, 25, 27]) expect = [(6 / 4) / 10, (9 / 4) / 10, 0 / 11] actual, _, _, _ = allel.windowed_divergence(pos, ac1, ac2, size=10, start=1, stop=31) assert_array_almost_equal(expect, actual)
def setup_instance(self, data): return SortedIndex(data)