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
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.