def motif3funct_wei(W):
    '''
    Functional motifs are subsets of connection patterns embedded within
    anatomical motifs. Motif frequency is the frequency of occurrence of
    motifs around a node. Motif intensity and coherence are weighted
    generalizations of motif frequency.

    Parameters
    ----------
    W : NxN np.ndarray
        weighted directed connection matrix (all weights between 0 and 1)

    Returns
    -------
    I : 13xN np.ndarray
        motif intensity matrix
    Q : 13xN np.ndarray
        motif coherence matrix
    F : 13xN np.ndarray
        motif frequency matrix

    Notes
    -----
    Average intensity and coherence are given by I./F and Q./F.
    '''
    from scipy import io
    import os
    fname = os.path.join(os.path.dirname(__file__), motiflib)
    mot = io.loadmat(fname)
    m3 = mot['m3']
    id3 = mot['id3'].squeeze()
    n3 = mot['n3'].squeeze()

    n = len(W)
    I = np.zeros((13, n))  # intensity
    Q = np.zeros((13, n))  # coherence
    F = np.zeros((13, n))  # frequency

    A = binarize(W, copy=True)  # create binary adjmat
    As = np.logical_or(A, A.T)  # symmetrized adjmat

    for u in range(n - 2):
        # v1: neighbors of u (>u)
        V1 = np.append(np.zeros((u,), dtype=int), As[u, u + 1:n + 1])
        for v1 in np.where(V1)[0]:
            # v2: neighbors of v1 (>u)
            V2 = np.append(np.zeros((u,), dtype=int), As[v1, u + 1:n + 1])
            V2[V1] = 0  # not already in V1
            # and all neighbors of u (>v1)
            V2 = np.logical_or(
                np.append(np.zeros((v1,)), As[u, v1 + 1:n + 1]), V2)
            for v2 in np.where(V2)[0]:
                a = np.array((A[v1, u], A[v2, u], A[u, v1],
                              A[v2, v1], A[u, v2], A[v1, v2]))
                ix = (np.dot(m3, a) == n3)
                m = np.sum(ix)

                w = np.array((W[v1, u], W[v2, u], W[u, v1],
                              W[v2, v1], W[u, v2], W[v1, v2]))

                M = m3[ix, :] * np.tile(w, (m, 1))
                id = id3[ix] - 1
                l = n3[ix]
                x = np.sum(M, axis=1) / l  # arithmetic mean
                M[M == 0] = 1  # enable geometric mean
                i = np.prod(M, axis=1)**(1 / l)  # intensity
                q = i / x  # coherence

                # unique motif occurrences
                idu, jx = np.unique(id, return_index=True)
                jx = np.append((0,), jx + 1)

                mu = len(idu)  # number of unique motifs
                i2, q2, f2 = np.zeros((3, mu))

                for h in range(mu):
                    i2[h] = np.sum(i[jx[h] + 1:jx[h + 1] + 1])
                    q2[h] = np.sum(q[jx[h] + 1:jx[h + 1] + 1])
                    f2[h] = jx[h + 1] - jx[h]

                # then add to cumulative count
                I[idu, u] += i2
                I[idu, v1] += i2
                I[idu, v2] += i2
                Q[idu, u] += q2
                Q[idu, v1] += q2
                Q[idu, v2] += q2
                F[idu, u] += f2
                F[idu, v1] += f2
                F[idu, v2] += f2

    return I, Q, F
Functional motifs are subsets of connection patterns embedded within
    anatomical motifs. Motif frequency is the frequency of occurrence of
    motifs around a node. Motif intensity and coherence are weighted
    generalizations of motif frequency.

    Parameters
    ----------
    W : NxN np.ndarray
        weighted directed connection matrix (all weights between 0 and 1)

    Returns
    -------
    I : 13xN np.ndarray
        motif intensity matrix
    Q : 13xN np.ndarray
        motif coherence matrix
    F : 13xN np.ndarray
        motif frequency matrix

    Notes
    -----
    Average intensity and coherence are given by I./F and Q./F.