Reputation: 424
I would like to ignore the error that is returned by cytoscape when attempting to create an edge that points to an unexisting target.
For technical reasons, I can not make sure my edges are pointing to an existing targets.
If I directly pass
elements: myListOfEdgesAndNodes
When declaring the cy object, it fails and doesn't display anything because of that error. I have thought of a workaround, which would be to initialize cy object with my nodes
elements: myListOfNodes
And then dynamically add edges in a loop with cy.add() in which case I believe the add will fail when the edge's target is non-existant, but it should not prevent other nodes/edges from being displayed. Does that sound like a good workaround ? Is there anything better to do ?
Upvotes: 0
Views: 1437
Reputation: 6549
If you have the final elements
array in your python scope, then you can easily filter out all those edges which point to a non-existing vertex.
Here is an example, which assumes the following typical scenario (as described in the docs of the cytoscape-dash):
def removeDanglingEdges(elements):
vids = set([v['data']['id'] for v in elements if not 'source' in v['data']])
return [e for e in elements if not 'source' in e['data'] or (e['data']['source'] in vids and e['data']['target'] in vids)]
# example usage
elements = [{'data':{'id':1}},{'data':{'source':1,'target':3}},{'data':{'id':3}},{'data':{'source':1,'target':6}}]
cleaned_elements = removeDanglingEdges(elements)
print(cleaned_elements) # -> [{'data':{'id':1}},{'data':{'source':1,'target':3}},{'data':{'id':3}}]
Upvotes: 1