コード例 #1
0
def read_ep_nodal(ep_subj, filename):
    """
    This function creates an empty dataframe, builds the brain graphs,
    calculates the network features and writes them in the dataframe.
    
    Created by: Loukas Serafeim, Nov 2017

    Args:
     ep_subj: A list with subject numbers. e.g. for 3 subjects use ep_subj= [1, 2, 3]
     filename: This is the filename of the subjects. e.g. filename = 'ep_subj_{}'

    Returns:
     3 network features per node: deg, cc, ne for the EP group and concatinate them in one dataframe
    """

    counter = 0
    for s in ep_subj:
        X = pd.read_csv(filename.format(s), header=None)
        X = X.values
        np.fill_diagonal(X, 0)
        X = X.tolist()
        g = Graph.Weighted_Adjacency(X,
                                     mode=ADJ_UNDIRECTED,
                                     attr="weight",
                                     loops=False)
        N = g.vcount()
        E = g.ecount()
        g.es["label"] = g.es["weight"]
        lst = range(N)
        g.vs["name"] = lst

        d = g.strength(loops=False, weights=g.es["weight"])
        cc = g.transitivity_local_undirected(vertices=None,
                                             mode="zero",
                                             weights=g.es["weight"])
        ne = nodal_eff(g)

        d = np.array(d)
        cc = np.array(cc)

        if counter == 0:
            dt2 = pd.DataFrame(columns=['Deg'] * d.shape[0] +
                               ['Cc'] * d.shape[0] + ['Ne'] * d.shape[0] +
                               ['ID'])

        all_together = np.concatenate((d, cc, ne), axis=0)
        all_together = np.append(all_together, 2)
        dt2.loc[s] = list(all_together)
        counter += 1

    return dt2
コード例 #2
0
def decompose_lobes(grouping, subj_numbers, filename, verbose=0):

    c = [
        item for sublist in [[
            'deg_{}_lob'.format(s), 'cc_{}_lob'.format(s), 'ne_{}_lob'.format(
                s)
        ] for s in range(len(np.unique(grouping).tolist()))]
        for item in sublist
    ]
    c.append('id')
    dt_lobe = pd.DataFrame(columns=c)

    for j in subj_numbers:
        Xc = pd.read_csv(filename.format(j), header=None)
        Xc = Xc.values
        np.fill_diagonal(Xc, 0)
        Xc = Xc.tolist()
        gc = Graph.Weighted_Adjacency(Xc,
                                      mode=ADJ_UNDIRECTED,
                                      attr="weight",
                                      loops=False)
        Nc = gc.vcount()
        Ec = gc.ecount()
        gc.es["label"] = gc.es["weight"]
        lst_c = range(Nc)
        gc.vs["name"] = lst_c

        lists = []
        for i in range(len(np.unique(grouping))):
            tmp_cluster = np.where(grouping == i)[0].tolist()
            #print(tmp_cluster)
            sub = gc.induced_subgraph(tmp_cluster)
            av_d = average(sub.strength(loops=False, weights=sub.es["weight"]))
            av_cc = average(
                sub.transitivity_local_undirected(vertices=None,
                                                  mode="zero",
                                                  weights=sub.es["weight"]))
            av_ne = average(nodal_eff(sub))

            lists.extend([av_d, av_cc, av_ne])
        if filename == 'ctrl_FA_{}.csv':
            lists.append(1)
        elif filename == 'ep_FA_{}.csv':
            lists.append(2)
        elif filename == 'iugr_FA_{}.csv':
            lists.append(3)
        elif filename == 'ctrl_VAV_{}.csv':
            lists.append(4)
        dt_lobe.loc[j] = lists

    return dt_lobe, grouping
コード例 #3
0
def read_ep(ep_subj, filename):
    """
    This function creates an empty dataframe, builds the brain graphs,
    calculates the network features and writes them in the dataframe.
    
    Created by: Loukas Serafeim, Nov 2017

    Args:
     ep_subj: A list with subject numbers. e.g. for 3 subjects use ep_subj= [1, 2, 3]
     filename: This is the filename of the subjects. e.g. filename = 'ep_subj_{}'

    Returns:
     3 Global network features: deg, cc, ne for the EP group and concatinate them in one dataframe
    """

    dt2 = pd.DataFrame(columns=['Deg', 'Cc', 'Ne', 'ID'])
    for s in ep_subj:
        X = pd.read_csv(filename.format(s), header=None)
        X = X.values
        np.fill_diagonal(X, 0)
        X = X.tolist()
        g = Graph.Weighted_Adjacency(X,
                                     mode=ADJ_UNDIRECTED,
                                     attr="weight",
                                     loops=False)
        N = g.vcount()
        E = g.ecount()
        g.es["label"] = g.es["weight"]
        lst = range(N)
        g.vs["name"] = lst

        d = g.strength(loops=False, weights=g.es["weight"])
        av_d = average(d)
        cc = g.transitivity_local_undirected(vertices=None,
                                             mode="zero",
                                             weights=g.es["weight"])
        av_cc = average(cc)
        ne = nodal_eff(g)
        av_ne = average(ne)

        dt2.loc[s] = [av_d, av_cc, av_ne, 2]

    return dt2
コード例 #4
0
def decompose_iugr_lev(X, iugr_subj, filename, verbose=0):
    """
    This function decomposes the IUGR subjects using the leading eigenvector algorithm
    based on the prior information.
    
    Created by: Loukas Serafeim, Nov 2017

    Args:
     X: The MEAN ctrl network
     iugr_subj: A list with the IUGR subject numbers. e.g. for 3 subjects use iugr_subj= [1, 2, 3]
     filename: This is the filename of the IUGR subjects. e.g. filename = 'iugr_subj_{}'

    Returns:
     The average degree, clustering coefficient and nodal efficiency of each community
     (3*comms features)
    """

    # c =['deg', 'cc','ne']*6
    # c.append('id')
    # dt_iugr_lev = pd.DataFrame(columns = c)
    X = X.values
    np.fill_diagonal(X, 0)
    X = X.tolist()

    g = Graph.Weighted_Adjacency(X,
                                 mode=ADJ_UNDIRECTED,
                                 attr="weight",
                                 loops=False)
    N = g.vcount()
    E = g.ecount()
    g.es["label"] = g.es["weight"]
    g.vs["name"] = range(N)

    comms = g.community_leading_eigenvector(weights=g.es["weight"])
    clusters = comms
    if verbose:
        print(
            "\nafter calculating the average control matrix the comms are:  ")
        print(clusters)

    #c =['deg', 'cc','ne'] * len(clusters)
    c = [
        item for sublist in
        [['deg_{}'.format(s), 'cc_{}'.format(s), 'ne_{}'.format(s)]
         for s in range(len(clusters))] for item in sublist
    ]
    c.append('id')
    dt_iugr_lev = pd.DataFrame(columns=c)

    for j in iugr_subj:
        Xc = pd.read_csv(filename.format(j), header=None)
        Xc = Xc.values
        np.fill_diagonal(Xc, 0)
        Xc = Xc.tolist()
        gc = Graph.Weighted_Adjacency(Xc,
                                      mode=ADJ_UNDIRECTED,
                                      attr="weight",
                                      loops=False)
        Nc = gc.vcount()
        Ec = gc.ecount()
        gc.es["label"] = gc.es["weight"]
        lst_c = range(Nc)
        gc.vs["name"] = lst_c

        lists = []

        for i in range(len(clusters)):
            sub = gc.induced_subgraph(clusters[i])
            av_d = average(sub.strength(loops=False, weights=sub.es["weight"]))
            av_cc = average(
                sub.transitivity_local_undirected(vertices=None,
                                                  mode="zero",
                                                  weights=sub.es["weight"]))
            av_ne = average(nodal_eff(sub))

            lists.extend([av_d, av_cc, av_ne])
        lists.append(3)
        dt_iugr_lev.loc[j] = lists

    return dt_iugr_lev, clusters