Example #1
0
def test_normxcorr2_kernels(kernel_config):
    """Test corrfoef2d on all built-in patterns"""
    # Loop over the different kernel matrices for the current pattern
    for kernel in kernel_config["kernels"]:
        km, kn = kernel.shape
        # Generate fake Hi-C matrix: empty with pattern centered at 60,80
        pattern_signal = np.zeros((100, 100), dtype=float)
        pattern_signal[60 - km // 2:60 + (km // 2 + 1),
                       80 - kn // 2:80 + (kn // 2 + 1), ] = kernel
        pattern_signal = sp.csr_matrix(np.triu(pattern_signal))
        # Compute correlation between fake matrix and kernel
        corr, _ = cud.normxcorr2(
            pattern_signal,
            kernel,
            max_dist=None,
            sym_upper=False,
        )

        # Check if the max correlation is where we inserted the pattern
        corr = corr.tocoo()
        corr.data = np.round(corr.data, 3)
        obs_row = corr.row[np.where(corr.data == np.max(corr.data))]
        obs_col = corr.col[np.where(corr.data == np.max(corr.data))]
        assert 60 in obs_row
        assert 80 in obs_col
Example #2
0
def test_normxcorr2_dense_sparse(signal):
    """Check if normxcorr2 yields identical values for dense and sparse versions"""
    corr_d, pval_d = cud.normxcorr2(
        signal.todense(),
        gauss_kernel,
        max_dist=None,
        sym_upper=False,
        pval=True,
    )
    corr_s, pval_s = cud.normxcorr2(signal,
                                    gauss_kernel,
                                    max_dist=None,
                                    sym_upper=False,
                                    pval=True)
    assert np.allclose(corr_s.toarray(), corr_d, rtol=10e-4)
    assert np.allclose(pval_s.toarray(), pval_d, rtol=10e-4)
Example #3
0
def test_normxcorr2(signal):
    """Check if Pearson and cross-product correlations yield appropriate values"""
    corr, pval = cud.normxcorr2(
        signal,
        gauss_kernel,
        max_dist=None,
        sym_upper=False,
    )
    if len(corr.data):
        assert np.min(corr.data) >= -1
        assert np.max(corr.data) <= 1
Example #4
0
def test_validate_patterns(matrix, coords):
    """Test pattern validation"""
    contact_map = DummyMap(matrix)
    conv_mat = cud.normxcorr2(matrix,
                              gauss_kernel,
                              max_dist=None,
                              sym_upper=False)
    cud.validate_patterns(
        np.array([coords]),
        matrix,
        conv_mat,
        contact_map.detectable_bins,
        gauss_kernel,
        10.0,
    )
Example #5
0
def test_missing_corr(cfg):
    """Test if chromosight's correlation scores are identical to
    scipy.stats' pearsonr values.
    """
    # We'll correlate the kernel with a zoomed (centered) version
    # of itself
    kernel = np.array(cfg['kernels'][0])
    mat = sp.csr_matrix(cup.resize_kernel(kernel, factor=10))
    # Mask rows in the matrix to simulate missing bins
    mask = sp.csr_matrix(mat.shape, dtype=bool)
    center_m, center_n = np.array(mat.shape) // 2
    kh, kw = np.array(kernel.shape) // 2 + 1
    miss_rows = np.array([-2, 1, 2])
    miss_rows += center_m
    mat[miss_rows, :] = 0.0
    mask[miss_rows, :] = True
    # Use triu to simulate intrachromosomal matrix
    mat = sp.triu(mat).tocsr()
    # Convolute kernel on the fake map
    corr_mat = cud.normxcorr2(mat,
                              kernel,
                              missing_mask=mask,
                              sym_upper=True,
                              full=True)[0]
    # Retrieve the center pixel: the correlation between the
    # kernel and the center of the zoomed kernel.
    obs = corr_mat[center_m, center_n]
    # Compute Pearson correlation between the center of the
    # zoomed kernel and the kernel, after removing missing values
    left, right = center_m - kh + 1, center_m + kh
    high, low = center_n - kw + 1, center_n + kh
    mask += sp.tril(sp.csr_matrix(np.ones(mask.shape, dtype=bool)))
    flat_mask = mask.toarray()[high:low, left:right].flat == 1
    exp = pearsonr(mat[high:low, left:right].toarray().flat[~flat_mask],
                   kernel.flat[~flat_mask])[0]
    assert np.isclose(obs, exp, rtol=0.1)