RedisGraph / redisgraph-py

RedisGraph python client
https://redisgraph.io
BSD 3-Clause "New" or "Revised" License
189 stars 49 forks source link

Loading graph from previous session #22

Closed tomas-wood closed 5 years ago

tomas-wood commented 5 years ago

Hey folks,

I was wondering if anyone could point me to an example of how to load the graph stored in redis from a previous session into a new Graph?

I can see the name of the graph in redis but I can't retrieve it with r.get() and I'd really like to be able to use the saved graph for new instances through the python API.

def _array2bytes(arr):
    with BytesIO() as b:
        np.save(b, arr)
        return b.getvalue()

def _bytes2array(byts):
    return np.load(BytesIO(byts))

def _add_nodes(r, graph, n_nodes):
    nodes = []
    for k in range(n_nodes):
        n = Node(label="node")
        graph.add_node(n)
        nodes.append(n)
        _id = n.alias
        feat = np.random.rand(1,100,7,7)
        feat_bytes = _array2bytes(feat)
        r.set(_id, feat_bytes)
    # graph.commit()
    return nodes

def _add_edges(nodes, graph, edge_prob):
    t = time.time()
    edges = []
    for k, node0 in enumerate(nodes):
        for kk, node1 in enumerate(nodes):
            if np.random.rand() < edge_prob:
                edge = Edge(node0, "adjacent_to", node1)
                graph.add_edge(edge)
                edges.append(edge)
    # graph.commit()
    print("_add_edges: {}".format(time.time() - t))
    return edges

def _create_random_graph(r, graphname="random", n_nodes=1000, edge_prob=0.1):
    redis_graph = Graph(graphname, r)
    nodes = _add_nodes(r, redis_graph, n_nodes)
    edges = _add_edges(nodes, redis_graph, edge_prob)
    return redis_graph

def _main():
    r = redis.Redis(host='localhost', port=6379)
    g = _create_random_graph(r)
    t = time.time()
    g.commit()
    print("after calling graph.commit: {}".format(time.time() - t))
    # pprint(g.nodes.keys()[:10])
    # print(len(g.nodes.keys()))
    # g.delete()
    # r.flushall()

if __name__ == "__main__":
    a = time.time()
    _main()
    print(time.time() - a)
odellus commented 5 years ago

Same person, different account.

So I used r.type('random') to see what kind of value is stored in redis since it's obviously not a string and I see the data type graphdata. I'm not seeing any methods in redis-py about how to return graphdata from redis.

I asked a question about it on stack overflow. Any help would be greatly appreciated.

swilly22 commented 5 years ago

@odellus see my reply at stackoverflow

odellus commented 5 years ago

Sounds good. Making the alias/index of the nodes stored in the graph accessible via query in this case could help with reconstructing the original graph. As it stands now I can't think of another way of reconstructing the graph if I don't assign nodes an redundant index as a property.

I think a NetworkX-like interface to saved graphs would be helpful. I'm working on a adjacency_matrix(redis_graph) function to return a sp.csc_matrix right now so the demand is there.

But this answers my question.

Closing.