ndexbio / ndex2-client

NDEx2 Client
BSD 3-Clause "New" or "Revised" License
6 stars 6 forks source link

Converting NiceCXNetwork to networkx and back does not handle node name consistently #84

Closed coleslaw481 closed 2 years ago

coleslaw481 commented 3 years ago

Converting networkx to NiceCXNetwork should take the name attribute and set it as the node name and NOT as a separate nodeAttribute or as represents

import ndex2
import networkx as nx

# create simple 2 node / 1 edge network
net = ndex2.nice_cx_network.NiceCXNetwork()
node_one = net.create_node('Node 1')
node_two = net.create_node('Node 2')
net.create_edge(edge_source=node_one, edge_target=node_two)

print(net.get_nodes())

netx_net = net.to_networkx(mode='default')

print(nx.get_node_attributes(netx_net, 'name'))

# convert back to NiceCXNetwork
net_roundtrip = ndex2.create_nice_cx_from_networkx(netx_net)
print(net_roundtrip.get_nodes())

# there should NOT be any node attributes with name 'name'
for node_id, node_obj in net_roundtrip.get_nodes():
    print(net_roundtrip.get_node_attribute(node_id, 'name'))

Running above gives this output:

# the node names for original NiceCXNetwork
{0: 'Node 1', 1: 'Node 2'}

# the node names after round trip (they are now set to id of node and node name is set to represents)
dict_items([(0, {'@id': 0, 'n': 0, 'r': 'Node 1'}), (1, {'@id': 1, 'n': 1, 'r': 'Node 2'})])

# the node name is also set as node attributes:
{'po': 0, 'n': 'name', 'v': 'Node 1', 'd': 'string'}
{'po': 1, 'n': 'name', 'v': 'Node 2', 'd': 'string'}

Extracted from: https://ndexbio.atlassian.net/browse/UD-1753

coleslaw481 commented 2 years ago

Fixed with commit: To github.com:ndexbio/ndex2-client.git d0d90e2..3f62959 v3.5.0 -> v3.5.0