Example #1
0
def test_compute_ricci_curvature():
    Gd = nx.DiGraph()
    Gd.add_edges_from([(1, 2), (2, 3), (3, 4), (2, 4), (4, 2)])
    orc_directed = FormanRicci(Gd)
    orc_directed.compute_ricci_curvature()

    frc = nx.get_edge_attributes(orc_directed.G, "formanCurvature")

    assert frc == {(1, 2): -2, (2, 3): 0, (2, 4): 0, (3, 4): 1, (4, 2): 0}
Example #2
0
def test_compute_ricci_curvature():
    G = nx.Graph()
    G.add_edges_from([(1, 2), (2, 3), (3, 4), (2, 4)])
    G.add_node(5)
    frc = FormanRicci(G, method="1d")
    frc.compute_ricci_curvature()

    frc_edges = list(nx.get_edge_attributes(frc.G, "formanCurvature").values())
    frc_nodes = list(nx.get_node_attributes(frc.G, "formanCurvature").values())
    frc_edges_ans = [0.0, -1.0, -1.0, 0.0]
    frc_nodes_ans = [0.0, -0.6666666666666666, -0.5, -0.5, 0]

    npt.assert_array_almost_equal(frc_edges, frc_edges_ans)
    npt.assert_array_almost_equal(frc_nodes, frc_nodes_ans)

    frc_a = FormanRicci(G, method="augmented")
    frc_a.compute_ricci_curvature()

    frc_a_edges = list(
        nx.get_edge_attributes(frc_a.G, "formanCurvature").values())
    frc_a_nodes = list(
        nx.get_node_attributes(frc_a.G, "formanCurvature").values())
    frc_a_edges_ans = [0.0, 2.0, 2.0, 3.0]
    frc_a_nodes_ans = [0.0, 1.3333333333333333, 2.5, 2.5, 0]

    npt.assert_array_almost_equal(frc_a_edges, frc_a_edges_ans)
    npt.assert_array_almost_equal(frc_a_nodes, frc_a_nodes_ans)
Example #3
0
def main():

    # import an example NetworkX karate club graph
    G = nx.karate_club_graph()

    # compute the Ollivier-Ricci curvature of the given graph G
    orc = OllivierRicci(G, alpha=0.5, verbose="INFO")
    orc.compute_ricci_curvature()
    print(
        "Karate Club Graph: The Ollivier-Ricci curvature of edge (0,1) is %f" %
        orc.G[0][1]["ricciCurvature"])

    # compute the Forman-Ricci curvature of the given graph G
    frc = FormanRicci(G)
    frc.compute_ricci_curvature()
    print("Karate Club Graph: The Forman-Ricci curvature of edge (0,1) is %f" %
          frc.G[0][1]["formanCurvature"])

    # -----------------------------------
    # Compute Ricci flow metric - Optimal Transportation Distance
    G = nx.karate_club_graph()
    orc_OTD = OllivierRicci(G, alpha=0.5, method="OTD", verbose="INFO")
    orc_OTD.compute_ricci_flow(iterations=10)
Example #4
0
    np.save(path1+"lig_IDM_graphs/"+pdbid+"_ollivier_15_C_ligand.npy", orc_temp)
    np.save(path1+"lig_IDM_graphs/"+pdbid+"_forman_15_C_ligand.npy", frc_temp)
"""

for i in range(len(all_comb)):
    print("Processing: {:20}".format(comb_d[i]))
    for pdbid in train_list_2013["arr_0"]:
        print("Processing v2013: {:4}".format(pdbid))
        orc_temp = []
        frc_temp = []
        for c in np.linspace(0, 15, 150):
            G = gen_lignet(all_comb[i], pdbid, c, path2)
            if G.number_of_edges() > 0:
                orc = OllivierRicci(G, alpha=0.5)
                orc.compute_ricci_curvature()
                frc = FormanRicci(G)
                frc.compute_ricci_curvature()
                orc_temp.append(orc.G)
                frc_temp.append(frc.G)
            else:
                orc_temp.append(G)
                frc_temp.append(G)

        nx.write_gpickle([orc_temp, frc_temp], path2 + "lig_IDM_graphs/" +
                         pdbid + "_15_" + comb_d[i] + "_ligand.gpickle")

    for pdbid in test_list_2013["arr_0"]:
        print("Processing v2013: ", pdbid)
        orc_temp = []
        frc_temp = []
        for c in np.linspace(0, 15, 150):
import networkx as nx
from GraphRicciCurvature.OllivierRicci import OllivierRicci
from GraphRicciCurvature.FormanRicci import FormanRicci

# import an example NetworkX karate club graph
G = nx.karate_club_graph()

# compute the Ollivier-Ricci curvature of the given graph G
orc = OllivierRicci(G, alpha=0.5, verbose="INFO")
orc.compute_ricci_curvature()
print("Karate Club Graph: The Ollivier-Ricci curvature of edge (0,1) is %f" % orc.G[0][1]["ricciCurvature"])

# compute the Forman-Ricci curvature of the given graph G
frc = FormanRicci(G)
frc.compute_ricci_curvature()
print("Karate Club Graph: The Forman-Ricci curvature of edge (0,1) is %f" % frc.G[0][1]["formanCurvature"])

# -----------------------------------
# Construct a directed graph example
Gd = nx.DiGraph()
Gd.add_edges_from([(1, 2), (2, 3), (3, 4), (2, 4), (4, 2)])

# compute the Ollivier-Ricci curvature of the given directed graph Gd
orc_directed = OllivierRicci(Gd)
orc_directed.compute_ricci_curvature()
for n1, n2 in Gd.edges():
    print("Directed Graph: The Ollivier-Ricci curvature of edge(%d,%d) id %f" %
          (n1, n2, orc_directed.G[n1][n2]["ricciCurvature"]))

# compute the Forman-Ricci curvature of the given directed graph Gd
frc_directed = FormanRicci(Gd)
Example #6
0
def create_test(threshold, cutoff, num_of_gaps):
    test_list = np.load("ORC PLB IDM 2016/test_list.npz", allow_pickle=True)
    for i in range(len(test_list["arr_0"])):
        pdbid = test_list["arr_0"][i]
        print("Processing: ", pdbid)
        data = np.load("ORC PLB IDM 2016/Complexes/" + pdbid + "_complex_" +
                       str(cutoff) + ".npz",
                       allow_pickle=True)
        pro_data, lig_data = data['PRO'], data['LIG']

        pro_hvy_atom = ['C', 'N', 'O', 'S']
        lig_hvy_atom = ['C', 'N', 'O', 'S', 'P', 'F', 'Cl', 'Br', 'I']
        pro_coords, lig_coords = [], []
        for p in pro_hvy_atom:
            temp = []
            for proatm in pro_data:
                for j in range(len(proatm['typ'])):
                    pt = str(proatm['typ'][j]).replace(" ", "")
                    pt = pt.upper()
                    if pt == p:
                        temp.append(proatm['pos'][j])
            pro_coords.append(temp)

        for q in lig_hvy_atom:
            temp = []
            for ligatm in lig_data:
                for j in range(len(ligatm['typ'])):
                    lt = str(ligatm['typ'][j]).replace(" ", "")
                    lt = lt.upper()
                    if lt == q:
                        temp.append(ligatm['pos'][j])
            lig_coords.append(temp)

        X_graphs = []
        for i in range(0, len(pro_coords)):
            for j in range(0, len(lig_coords)):
                temp_feature = []
                # generate graph with protein and ligand atom combination and specified cutoff distance
                for l in np.linspace(0, threshold, num_of_gaps):
                    #l = 7.4
                    #print(pro_hvy_atom[i],"--",lig_hvy_atom[j], ": ", l, end="\r")
                    G = nx.Graph()
                    G = gen_graph(pro_coords[i], lig_coords[j],
                                  pro_hvy_atom[i], lig_hvy_atom[j], l)
                    #vertices = np.zeros(G.number_of_nodes())
                    if G.number_of_edges() > 0:
                        frc = FormanRicci(G)
                        frc.compute_ricci_curvature()
                        temp_feature.append(frc.G)
                    else:
                        temp_feature.append(G)
                    """
                        for v in orc.G.nodes():
                            try:
                                vertices[v] = orc.G.nodes[v]["ricciCurvature"]
                            except:
                                vertices[v] = 0
                                
                    # Binning the vertex ORCs
                    index = np.linspace(-1, 1, num_of_bins)
                    bins = np.zeros(num_of_bins)
                    for v in range(len(vertices)):
                        for ind in range(len(index)-1):
                            if vertices[v] >= index[ind] and vertices[v] < index[ind+1]:
                                bins[ind] += 1
                    temp_feature.append(bins)
                    """
                X_graphs.append(temp_feature)
        #feature_x.append(X_feature)
        np.save(
            "ORC PLB IDM 2016/forman_test_graphs/" + pdbid + "_" +
            str(cutoff) + "_" + str(threshold) + ".npy", X_graphs)