From a symmetrical matrix M of size n, return a vector d whose each
    component d[i] is a T-test of two samples represented by vectors of size
    window on either side of the i-th pixel on the diagonal. Edge elements may
    be extrapolated based on the vector size reduction, except in the case of
    circular genomes. If they aren't, d will be of size n - 2*(window-1)
    instead of n.

def directional(M, window=None, circ=False, extrapolate=True, log=True):
    """From a symmetrical matrix M of size n, return a vector d whose each
    component d[i] is a T-test of two samples represented by vectors of size
    window on either side of the i-th pixel on the diagonal. Edge elements may
    be extrapolated based on the vector size reduction, except in the case of
    circular genomes. If they aren't, d will be of size n - 2*(window-1)
    instead of n.
    """

    # Sanity checks
    if not type(M) is np.ndarray:
        M = np.array(M)

    if M.shape[0] != M.shape[1]:
        raise ValueError("Matrix is not square.")

    try:
        n = min(M.shape)
    except AttributeError:
        n = M.size

    # Default window argument
    if window is None:
        window = max(n // 100, 5)

    if window >= n:
        raise ValueError("Please choose a smaller window size.")

    try:
        from scipy.stats import ttest_rel
    except ImportError as e:
        print("Scipy not")
        print(str(e))
        raise
    if log:
        N = np.zeros((n, n))
        N[M > 0] = np.log(M[M > 0])
    else:
        N = M

    if circ:
        d = [ttest_rel(np.array(list(N[i, i - window:]) + list(N[i, :i])),
                       N[i, i:i + window])[0] for i in range(window)]
    elif extrapolate:
        d = [ttest_rel(N[i, 0:i], N[i, i:2 * i])[0] for i in range(window)]
    else:
        d = []

    d += [ttest_rel(N[i, i - window:i], N[i, i:i + window])[0]
          for i in range(window, n - window)]

    if circ:
        d += [ttest_rel(N[i, i - window:i],
                        np.array((list(N[i, :i - n + window]) +
                                  list(N[i, i:]))))[0]
              for i in range(n - window, n)]
    elif extrapolate:
        d += [ttest_rel(N[i, i - window:i],
                        (np.array(list(N[i, i:]) +
                         list(N[i, :window - (n - i)]))))[0]
              for i in range(n - window, n)]

    return d