示例#1
0
def networkx_to_katanagraph(x: NetworkXGraph, **props) -> KatanaGraph:
    aprops = NetworkXGraph.Type.compute_abstract_properties(
        x, {"node_dtype", "node_type", "edge_type", "is_directed"})
    is_weighted = aprops["edge_type"] == "map"
    # get the edge list directly from the NetworkX Graph
    elist_raw = list(x.value.edges(data=True))
    # sort the eddge list and node list
    if aprops["is_directed"]:
        elist = sorted(elist_raw, key=lambda each: (each[0], each[1]))
    else:
        inv_elist = [(each[1], each[0], each[2]) for each in elist_raw]
        elist = sorted(elist_raw + inv_elist,
                       key=lambda each: (each[0], each[1]))
    nlist = sorted(list(x.value.nodes(data=True)), key=lambda each: each[0])
    # build the CSR format from the edge list (weight, (src, dst))
    row = np.array([each_edge[0] for each_edge in elist])
    col = np.array([each_edge[1] for each_edge in elist])
    data = np.array([each_edge[2]["weight"] for each_edge in elist])
    csr = csr_matrix((data, (row, col)), shape=(len(nlist), len(nlist)))
    # call the katana api to build a Graph (unweighted) from the CSR format
    # noting that the first 0 in csr.indptr is excluded
    pg = from_csr(csr.indptr[1:], csr.indices)
    # add the edge weight as a new property
    t = pyarrow.table(dict(value_from_translator=data))
    pg.add_edge_property(t)
    # use the metagraph's Graph warpper to wrap the katana.local.Graph
    return KatanaGraph(
        pg_graph=pg,
        is_weighted=is_weighted,
        edge_weight_prop_name="value_from_translator",
        is_directed=aprops["is_directed"],
        node_weight_index=0,
    )
示例#2
0
def test_from_csr_k3():
    pg = from_csr(np.array([2, 4, 6]), np.array([1, 2, 0, 2, 0, 1]))
    assert pg.num_nodes() == 3
    assert pg.num_edges() == 6
    assert list(pg.edge_ids(2)) == [4, 5]
    assert pg.get_edge_dest(4) == 0
    assert pg.get_edge_dest(5) == 1
示例#3
0
def test_from_csr_int16():
    pg = from_csr(np.array([1, 1], dtype=np.int16),
                  np.array([1], dtype=np.int16))
    assert pg.num_nodes() == 2
    assert pg.num_edges() == 1
    # assert list(pg.out_edge_ids(0)) == [0]
    assert pg.get_edge_dst(0) == 1
示例#4
0
def gen_pg_cleaned_8_12_from_csr(is_directed):
    """
    A helper function for the test, generating Katana's Graph from an edge list
    """
    katana.local.initialize()
    elist_raw = [
        (0, 1, 4),
        (0, 3, 2),
        (0, 4, 7),
        (1, 3, 3),
        (1, 4, 5),
        (2, 4, 5),
        (2, 5, 2),
        (2, 6, 8),
        (3, 4, 1),
        (4, 7, 4),
        (5, 6, 4),
        (5, 7, 6),
    ]
    src_list = [each[0] for each in elist_raw]
    dest_list = [each[1] for each in elist_raw]
    nlist_raw = list(set(src_list) | set(dest_list))
    # sort the eddge list and node list
    if is_directed:
        elist = sorted(elist_raw, key=lambda each: (each[0], each[1]))
    else:
        inv_elist = [(each[1], each[0], each[2]) for each in elist_raw]
        elist = sorted(elist_raw + inv_elist,
                       key=lambda each: (each[0], each[1]))
    nlist = sorted(nlist_raw, key=lambda each: each)
    # build the CSR format from the edge list (weight, (src, dst))
    row = np.array([each_edge[0] for each_edge in elist])
    col = np.array([each_edge[1] for each_edge in elist])
    data = np.array([each_edge[2] for each_edge in elist])
    csr = csr_matrix((data, (row, col)), shape=(len(nlist), len(nlist)))
    # call the katana api to build a Graph (unweighted) from the CSR format
    # noting that the first 0 in csr.indptr is excluded
    pg = from_csr(csr.indptr[1:], csr.indices)
    t = pyarrow.table(dict(value=data))
    pg.add_edge_property(t)
    return pg
示例#5
0
def test_from_csr():
    pg = from_csr(np.array([1, 1], dtype=np.uint32), np.array([1], dtype=np.uint64))
    assert pg.num_nodes() == 2
    assert pg.num_edges() == 1
    assert list(pg.edge_ids(0)) == [0]
    assert pg.get_edge_dest(0) == 1