def despeckle_simple(B, th2=<NUM_LIT:2>):
A = np.copy(B)<EOL>n1 = A.shape[<NUM_LIT:0>]<EOL>dist = {u: np.diag(A, u) for u in range(n1)}<EOL>medians, stds = {}, {}<EOL>for u in dist:<EOL><INDENT>medians[u] = np.median(dist[u])<EOL>stds[u] = np.std(dist[u])<EOL><DEDENT>for nw, j in itertools.product(range(n1), range(n1)):<EOL><INDENT>lp = j + nw<EOL>kp = j - nw<EOL>if lp < n1:<EOL><INDENT>if A[j, lp] > medians[nw] + th2 * stds[nw]:<EOL><INDENT>A[j, lp] = medians[nw]<EOL><DEDENT><DEDENT>if kp >= <NUM_LIT:0>:<EOL><INDENT>if A[j, kp] > medians[nw] + th2 * stds[nw]:<EOL><INDENT>A[j, kp] = medians[nw]<EOL><DEDENT><DEDENT><DEDENT>return A<EOL>
Single-chromosome despeckling

    Simple speckle removing function on a single chromomsome. It also works
    for multiple chromosomes but trends may be disrupted.

    Parameters
    ----------
    B : array_like
        The input matrix to despeckle
    th2 : float
        The number of standard deviations above the mean beyond which
        despeckling should be performed

    Returns
    -------
    array_like
        The despeckled matrix