コード例 #1
0
def modelNet(model):
    #Returns a grakel.Graph object from a cobra.model object

    edges_in = []
    edges_out = []
    edges = []

    for r in model.reactions:
        # enumerate 'substrates -> reactions' edges
        substrates = [s.id for s in r.reactants]
        edges_in.extend([(s, r.id) for s in substrates])
        # enumerate 'reactions -> products' edges
        products = [p.id for p in r.products]
        edges_out.extend([(p, r.id) for p in products])

    # Join lists
    edges.extend(edges_in)
    edges.extend(edges_out)

    #labels
    label_m = {m.id: m.name for m in model.metabolites}
    label_r = {r.id: r.name for r in model.reactions}
    label_nodes = label_m
    label_nodes.update(label_r)
    label_edges = {p: p for p in edges}

    g = gk.Graph(edges, node_labels=label_nodes, edge_labels=label_edges)
    return g
def createGrakel(trajectory):
    node_isnotenum = dict()
    edges_with_weights = dict()
    for dicts in trajectory.chordPositions:
        for k, v in dicts.items():
            node_isnotenum[v] = k
    trajectoryEdges = TrajectoryNoteEdges(trajectory)
    setOfEdges, multiSetOfEdges = EdgesSetCreate(trajectoryEdges)
    edges_with_weights = EdgeWeights(setOfEdges, multiSetOfEdges)
    return grakel.Graph(edges_with_weights, node_labels=node_isnotenum)
コード例 #3
0
ファイル: generateData.py プロジェクト: twistedcubic/copt
def generate_real_data(dataset_name='msrc'):
    '''
    Parse graphs and generate Laplacians from real datasets.
    '''
    
    dataset = grakel.datasets.fetch_dataset(dataset_name)
    graphs = dataset.data
    target = [] #dataset.target
    node_labels = []
    lap_l = []
    total_nodes = 0
    for j, graph in enumerate(graphs):
        g = grakel.Graph(graph_format='adjacency')
        #geaph[1] eg {1: 3, 2: 3, 3: 3
        #g_adj_l = g[0]        
        g.build_graph(*graph)
        lap = g.laplacian()
        #pdb.set_trace()
        g1=utils.lap_to_graph(lap.copy())
        if not nx.is_connected(g1):
            print('not connected!')
            continue
        node_label = np.zeros((len(lap))) #[-1]*len(g[1])
        if len(lap) < len(graph[1]):
            print('len except!')
            continue
        k_prev = -1
        for i, (k,l) in enumerate(graph[1].items()):
            assert k > k_prev
            k_prev = k
            #pdb.set_trace()
            try:
                #node_label[k-1] = l
                node_label[i] = l
            except Exception as e:
                print('Exception ', e)
                #pdb.set_trace()
                break
            
        total_nodes += len(lap)
        node_labels.append(node_label)
        target.append(dataset.target[j])
        lap_l.append(torch.from_numpy(lap))

    print('dataset {} avg nodes {}'.format(dataset_name, total_nodes / len(lap_l)))
    
    torch.save({'lap':lap_l, 'labels':node_labels, 'target':target}, 'data/{}_lap.pt'.format(dataset_name)) #os.path.join('data', dataset_name)))
    return lap_l, node_labels, target
コード例 #4
0
def cdg_to_grakel(graphs, directed, label_ndim, **kwargs):
    import grakel

    has_node_attr = False if graphs[0].nf is None or graphs[0].nf.shape[1] == 0 else True
    has_edge_attr = False if graphs[0].ef is None or graphs[0].ef.shape[2] == 0 else True

    g_grakel = []
    for g in graphs:

        wh = np.where(g.adj == 1)
        edge_set = set()
        node_set = set()
        for i in range(len(wh[0])):
            node_set.add(wh[0][i])
            node_set.add(wh[1][i])
            edge_set.add((wh[0][i], wh[1][i]))

        if has_node_attr:
            node_attr_dict = {}
            for n in node_set:
                node_attr_dict[n] = g.nf[n]
        else:
            node_attr_dict = None

        if has_edge_attr:
            edge_attr_dict = {}
            for e in edge_set:
                edge_attr_dict[e] = g.ef[e[0], e[1]]
        else:
            edge_attr_dict = None

        g_grakel.append(grakel.Graph(initialization_object=edge_set,
                                     node_labels=node_attr_dict,
                                     edge_labels=edge_attr_dict))

    return g_grakel