def test_tril_triu(): A = np.random.randn(20, 20) for chk in [5, 4]: dA = da.from_array(A, (chk, chk)) assert np.allclose(da.triu(dA).compute(), np.triu(A)) assert np.allclose(da.tril(dA).compute(), np.tril(A)) for k in [ -25, -20, -19, -15, -14, -9, -8, -6, -5, -1, 1, 4, 5, 6, 8, 10, 11, 15, 16, 19, 20, 21, ]: assert np.allclose(da.triu(dA, k).compute(), np.triu(A, k)) assert np.allclose(da.tril(dA, k).compute(), np.tril(A, k))
def test_tril_triu(): A = cupy.random.randn(20, 20) for chk in [5, 4]: dA = da.from_array(A, (chk, chk), asarray=False) assert_eq(da.triu(dA), np.triu(A)) assert_eq(da.tril(dA), np.tril(A)) for k in [-25, -20, -9, -1, 1, 8, 19, 21]: assert_eq(da.triu(dA, k), np.triu(A, k)) assert_eq(da.tril(dA, k), np.tril(A, k))
def test_tril_triu(): A = np.random.randn(20, 20) for chk in [5, 4]: dA = da.from_array(A, (chk, chk)) assert np.allclose(da.triu(dA).compute(), np.triu(A)) assert np.allclose(da.tril(dA).compute(), np.tril(A)) for k in [-25, -20, -19, -15, -14, -9, -8, -6, -5, -1, 1, 4, 5, 6, 8, 10, 11, 15, 16, 19, 20, 21]: assert np.allclose(da.triu(dA, k).compute(), np.triu(A, k)) assert np.allclose(da.tril(dA, k).compute(), np.tril(A, k))
def test_tril_triu_non_square_arrays(): A = np.random.randint(0, 11, (30, 35)) dA = da.from_array(A, chunks=(5, 5)) assert_eq(da.triu(dA), np.triu(A)) assert_eq(da.tril(dA), np.tril(A))
def _check_lu_result(p, l, u, A): assert np.allclose(p.dot(l).dot(u), A) # check triangulars assert_eq(l, da.tril(l), check_graph=False) assert_eq(u, da.triu(u), check_graph=False)
def Weir_Goudet_beta( ds: Dataset, *, stat_identity_by_state: Hashable = variables.stat_identity_by_state, merge: bool = True, ) -> Dataset: """Estimate pairwise beta between all pairs of samples as described in Weir and Goudet 2017 [1]. Beta is the kinship scaled by the average kinship of all pairs of individuals in the dataset such that the non-diagonal (non-self) values sum to zero. Beta may be corrected to more accurately reflect pedigree based kinship estimates using the formula :math:`\\hat{\\beta}^c=\\frac{\\hat{\\beta}-\\hat{\\beta}_0}{1-\\hat{\\beta}_0}` where :math:`\\hat{\\beta}_0` is the estimated beta between samples which are known to be unrelated [1]. Parameters ---------- ds Genotype call dataset. stat_identity_by_state Input variable name holding stat_identity_by_state as defined by :data:`sgkit.variables.stat_identity_by_state_spec`. If the variable is not present in ``ds``, it will be computed using :func:`identity_by_state`. merge If True (the default), merge the input dataset and the computed output variables into a single dataset, otherwise return only the computed output variables. See :ref:`dataset_merge` for more details. Returns ------- A dataset containing :data:`sgkit.variables.stat_Weir_Goudet_beta_spec` which is a matrix of estimated pairwise kinship relative to the average kinship of all pairs of individuals in the dataset. The dimensions are named ``samples_0`` and ``samples_1``. Examples -------- >>> import sgkit as sg >>> ds = sg.simulate_genotype_call_dataset(n_variant=3, n_sample=3, n_allele=10, seed=3) >>> # sample 2 "inherits" alleles from samples 0 and 1 >>> ds.call_genotype.data[:, 2, 0] = ds.call_genotype.data[:, 0, 0] >>> ds.call_genotype.data[:, 2, 1] = ds.call_genotype.data[:, 1, 0] >>> sg.display_genotypes(ds) # doctest: +NORMALIZE_WHITESPACE samples S0 S1 S2 variants 0 7/1 8/6 7/8 1 9/5 3/6 9/3 2 8/8 8/3 8/8 >>> # estimate beta >>> ds = sg.Weir_Goudet_beta(ds).compute() >>> ds.stat_Weir_Goudet_beta.values # doctest: +NORMALIZE_WHITESPACE array([[ 0.5 , -0.25, 0.25], [-0.25, 0.25, 0. ], [ 0.25, 0. , 0.5 ]]) >>> # correct beta assuming least related samples are unrelated >>> beta = ds.stat_Weir_Goudet_beta >>> beta0 = beta.min() >>> beta_corrected = (beta - beta0) / (1 - beta0) >>> beta_corrected.values # doctest: +NORMALIZE_WHITESPACE array([[0.6, 0. , 0.4], [0. , 0.4, 0.2], [0.4, 0.2, 0.6]]) References ---------- [1] - Bruce, S. Weir, and Jérôme Goudet 2017. "A Unified Characterization of Population Structure and Relatedness." Genetics 206 (4): 2085-2103. """ ds = define_variable_if_absent( ds, variables.stat_identity_by_state, stat_identity_by_state, identity_by_state ) variables.validate( ds, {stat_identity_by_state: variables.stat_identity_by_state_spec} ) ibs = ds[stat_identity_by_state].data # average matching is the mean of non-diagonal elements num = da.nansum(da.tril(ibs, -1)) denom = da.nansum(da.tril(~da.isnan(ibs), -1)) avg = num / denom beta = (ibs - avg) / (1 - avg) new_ds = create_dataset( { variables.stat_Weir_Goudet_beta: ( ("samples_0", "samples_1"), beta, ) } ) return conditional_merge_datasets(ds, new_ds, merge)
def _check_lu_result(p, l, u, A): assert np.allclose(p.dot(l).dot(u), A) # check triangulars assert_eq(l, da.tril(l)) assert_eq(u, da.triu(u))
def test_tril_triu_non_square_arrays(): A = np.random.randint(0, 11, (30, 35)) dA = da.from_array(A, chunks=(5, 5)) assert_eq(da.triu(dA), np.triu(A)) assert_eq(da.tril(dA), np.tril(A))