neo4j-contrib / py2neo

EOL! Py2neo is a comprehensive Neo4j driver library and toolkit for Python.
https://py2neo.org
Apache License 2.0
20 stars 8 forks source link

Question: unique ids for nodes and relationships #868

Closed pwrose closed 3 years ago

pwrose commented 3 years ago

I'm working on adding a method to the https://github.com/QuantStack/ipycytoscape project to accept py2neo graph and subgraph objects as input to Cytoscape graph visualization.

How do I access unique Node ids that match the start and end nodes of relationships?

As a workaround, I've been calculating a hash code, see below, but there must be a better way to relate nodes to relationships.

def neo_to_cytoscape_by_hash(subgraph):
    nodes = []
    for node in subgraph.nodes:
        node_props = dict(node)
        identifier = hash(repr(sorted(node_props.items())))
        node_props['id'] = identifier
        nodes.append({'data': node_props})

    edges = []
    for rel in subgraph.relationships:
        start_node_props = dict(rel.start_node)
        source_identifier = hash(repr(sorted(start_node_props.items())))
        end_node_props = dict(rel.end_node)
        target_identifier = hash(repr(sorted(end_node_props.items())))
        edges.append({'data': {'source': source_identifier, 'target': target_identifier, 'name': rel.__class__.__name__}})

    data = {'nodes': nodes, 'edges': edges}
    return data
technige commented 3 years ago

Do you mean something like the node identity? https://py2neo.org/2020.1/data/index.html#py2neo.data.Node.identity