def make_motif34lib():
    '''
    This function generates the motif34lib.mat library required for all
    other motif computations. Not to be called externally.
    '''
    from scipy import io
    import os

    def motif3generate():
        n = 0
        M = np.zeros((54, 6), dtype=bool)  # isomorphs
        # canonical labels (predecssors of IDs)
        CL = np.zeros((54, 6), dtype=np.uint8)
        cl = np.zeros((6,), dtype=np.uint8)
        for i in range(2**6):  # loop through all subgraphs
            m = '{0:b}'.format(i)
            m = str().zfill(6 - len(m)) + m
            G = np.array(((0, m[2], m[4]), (m[0], 0, m[5]),
                          (m[1], m[3], 0)), dtype=int)
            ko = np.sum(G, axis=1)
            ki = np.sum(G, axis=0)
            if np.all(ko + ki):  # if subgraph weakly connected
                u = np.array((ko, ki)).T
                cl.flat = u[np.lexsort((ki, ko))]
                CL[n, :] = cl  # assign motif label to isomorph
                M[n, :] = np.array((G.T.flat[1:4], G.T.flat[5:8])).flat
                n += 1

        # convert CLs into motif IDs
        _, ID = np.unique(
            CL.view(CL.dtype.descr * CL.shape[1]), return_inverse=True)
        ID += 1

        # convert IDs into sporns & kotter classification
        id_mika = (1, 3, 4, 6, 7, 8, 11)
        id_olaf = (-3, -6, -1, -11, -4, -7, -8)
        for mika, olaf in zip(id_mika, id_olaf):
            ID[ID == mika] = olaf
        ID = np.abs(ID)

        ix = np.argsort(ID)
        ID = ID[ix]  # sort IDs
        M = M[ix, :]  # sort isomorphs
        N = np.squeeze(np.sum(M, axis=1))  # number of edges
        Mn = np.array(np.sum(np.tile(np.power(10, np.arange(5, -1, -1)),
                                     (M.shape[0], 1)) * M, axis=1), dtype=np.uint32)
        return M, Mn, ID, N

    def motif4generate():
        n = 0
        M = np.zeros((3834, 12), dtype=bool)  # isomorphs
        CL = np.zeros((3834, 16), dtype=np.uint8)  # canonical labels
        cl = np.zeros((16,), dtype=np.uint8)
        for i in range(2**12):  # loop through all subgraphs
            m = '{0:b}'.format(i)
            m = str().zfill(12 - len(m)) + m
            G = np.array(((0, m[3], m[6], m[9]), (m[0], 0, m[7], m[10]),
                          (m[1], m[4], 0, m[11]), (m[2], m[5], m[8], 0)), dtype=int)
            Gs = G + G.T
            v = Gs[0, :]
            for j in range(2):
                v = np.any(Gs[v != 0, :], axis=0) + v
            if np.all(v):  # if subgraph weakly connected
                G2 = np.dot(G, G) != 0
                ko = np.sum(G, axis=1)
                ki = np.sum(G, axis=0)
                ko2 = np.sum(G2, axis=1)
                ki2 = np.sum(G2, axis=0)

                u = np.array((ki, ko, ki2, ko2)).T
                cl.flat = u[np.lexsort((ko2, ki2, ko, ki))]
                CL[n, :] = cl  # assign motif label to isomorph
                M[n, :] = np.array((G.T.flat[1:5], G.T.flat[6:10],
                                    G.T.flat[11:15])).flat
                n += 1

        # convert CLs into motif IDs
        _, ID = np.unique(
            CL.view(CL.dtype.descr * CL.shape[1]), return_inverse=True)
        ID += 1

        ix = np.argsort(ID)
        ID = ID[ix]  # sort IDs
        M = M[ix, :]  # sort isomorphs
        N = np.sum(M, axis=1)  # number of edges
        Mn = np.array(np.sum(np.tile(np.power(10, np.arange(11, -1, -1)),
                                     (M.shape[0], 1)) * M, axis=1), dtype=np.uint64)
        return M, Mn, ID, N

    dir = os.path.dirname(__file__)
    fname = os.path.join(dir, motiflib)
    if os.path.exists(fname):
        print("motif34lib already exists")
        return

    m3, m3n, id3, n3 = motif3generate()
    m4, m4n, id4, n4 = motif4generate()

    io.savemat(fname, mdict={'m3': m3, 'm3n': m3n, 'id3': id3, 'n3': n3,
                             'm4': m4, 'm4n': m4n, 'id4': id4, 'n4': n4})
def make_motif34lib ( ) : from scipy import io import os def motif3generate ( ) : n = 0 M = np . zeros ( ( 54 , 6 ) , dtype = bool ) CL = np . zeros ( ( 54 , 6 ) , dtype = np . uint8 ) cl = np . zeros ( ( 6 , ) , dtype = np . uint8 ) for i in range ( 2 ** 6 ) : m = '{0:b}' . format ( i ) m = str ( ) . zfill ( 6 - len ( m ) ) + m G = np . array ( ( ( 0 , m [ 2 ] , m [ 4 ] ) , ( m [ 0 ] , 0 , m [ 5 ] ) , ( m [ 1 ] , m [ 3 ] , 0 ) ) , dtype = int ) ko = np . sum ( G , axis = 1 ) ki = np . sum ( G , axis = 0 ) if np . all ( ko + ki ) : u = np . array ( ( ko , ki ) ) . T cl . flat = u [ np . lexsort ( ( ki , ko ) ) ] CL [ n , : ] = cl M [ n , : ] = np . array ( ( G . T . flat [ 1 : 4 ] , G . T . flat [ 5 : 8 ] ) ) . flat n += 1 _ , ID = np . unique ( CL . view ( CL . dtype . descr * CL . shape [ 1 ] ) , return_inverse = True ) ID += 1 id_mika = ( 1 , 3 , 4 , 6 , 7 , 8 , 11 ) id_olaf = ( - 3 , - 6 , - 1 , - 11 , - 4 , - 7 , - 8 ) for mika , olaf in zip ( id_mika , id_olaf ) : ID [ ID == mika ] = olaf ID = np . abs ( ID ) ix = np . argsort ( ID ) ID = ID [ ix ] M = M [ ix , : ] N = np . squeeze ( np . sum ( M , axis = 1 ) ) Mn = np . array ( np . sum ( np . tile ( np . power ( 10 , np . arange ( 5 , - 1 , - 1 ) ) , ( M . shape [ 0 ] , 1 ) ) * M , axis = 1 ) , dtype = np . uint32 ) return M , Mn , ID , N def motif4generate ( ) : n = 0 M = np . zeros ( ( 3834 , 12 ) , dtype = bool ) CL = np . zeros ( ( 3834 , 16 ) , dtype = np . uint8 ) cl = np . zeros ( ( 16 , ) , dtype = np . uint8 ) for i in range ( 2 ** 12 ) : m = '{0:b}' . format ( i ) m = str ( ) . zfill ( 12 - len ( m ) ) + m G = np . array ( ( ( 0 , m [ 3 ] , m [ 6 ] , m [ 9 ] ) , ( m [ 0 ] , 0 , m [ 7 ] , m [ 10 ] ) , ( m [ 1 ] , m [ 4 ] , 0 , m [ 11 ] ) , ( m [ 2 ] , m [ 5 ] , m [ 8 ] , 0 ) ) , dtype = int ) Gs = G + G . T v = Gs [ 0 , : ] for j in range ( 2 ) : v = np . any ( Gs [ v != 0 , : ] , axis = 0 ) + v if np . all ( v ) : G2 = np . dot ( G , G ) != 0 ko = np . sum ( G , axis = 1 ) ki = np . sum ( G , axis = 0 ) ko2 = np . sum ( G2 , axis = 1 ) ki2 = np . sum ( G2 , axis = 0 ) u = np . array ( ( ki , ko , ki2 , ko2 ) ) . T cl . flat = u [ np . lexsort ( ( ko2 , ki2 , ko , ki ) ) ] CL [ n , : ] = cl M [ n , : ] = np . array ( ( G . T . flat [ 1 : 5 ] , G . T . flat [ 6 : 10 ] , G . T . flat [ 11 : 15 ] ) ) . flat n += 1 _ , ID = np . unique ( CL . view ( CL . dtype . descr * CL . shape [ 1 ] ) , return_inverse = True ) ID += 1 ix = np . argsort ( ID ) ID = ID [ ix ] M = M [ ix , : ] N = np . sum ( M , axis = 1 ) Mn = np . array ( np . sum ( np . tile ( np . power ( 10 , np . arange ( 11 , - 1 , - 1 ) ) , ( M . shape [ 0 ] , 1 ) ) * M , axis = 1 ) , dtype = np . uint64 ) return M , Mn , ID , N dir = os . path . dirname ( __file__ ) fname = os . path . join ( dir , motiflib ) if os . path . exists ( fname ) : print ( "motif34lib already exists" ) return m3 , m3n , id3 , n3 = motif3generate ( ) m4 , m4n , id4 , n4 = motif4generate ( ) io . savemat ( fname , mdict = { 'm3' : m3 , 'm3n' : m3n , 'id3' : id3 , 'n3' : n3 , 'm4' : m4 , 'm4n' : m4n , 'id4' : id4 , 'n4' : n4 } )
This function generates the motif34lib.mat library required for all
    other motif computations. Not to be called externally.