Converting NiceCXNetwork objects to other formats¶
Below are converters that facilitate conversion of
NiceCXNetwork
object to other types
(such as NetworkX)
Networkx¶
-
class
ndex2.nice_cx_network.
DefaultNetworkXFactory
(legacymode=False)[source]¶ Converts
NiceCXNetwork
tonetworkx.Graph
object or one of its subtypesFor details on implementation see
get_graph()
Constructor
Note: the parameters in the constructor change behavior of
get_graph()
Parameters: legacymode (bool) – If set to True then get_graph()
behaves like NDEx2 Python client version 3.1 and earlier in that this method returns anetworkx.Graph
object. seeget_graph()
for more informationRaises: NDExError – If invalid value is set in legacymode parameter -
get_graph
(nice_cx_network, networkx_graph=None)[source]¶ Creates a
networkx.Graph
, or a subtype, object from nice_cx_network passed in.Warning
Converting large networks (10,000+ edges or nodes) may take a long time and consume lots of memory.
The conversion is done as follows:
Any network attributes are copied to the
networkx.Graph
in manner described here:add_network_attributes_from_nice_cx_network()
For nodes:
All nodes are added with the node id set to the id or
NODE_ID
of input network nodes.A node attribute named ‘name’ is set for each node with its value set to the value of the ‘name’ attribute from the input network.
If ‘r’ exists on node, the value is added as a node attribute named ‘represents’ (unless legacymode is set to True in constructor)
All other node attributes are added using the same attribute name as found in the input network. The value is directly set as it was found in input network (could be single object or list)
For edges:
Each edge is added setting the source to the value of
EDGE_SOURCE
attribute and target set asEDGE_TARGET
attribute of input network.Any edge attributes named
EDGE_INTERACTION
are renamed ‘interaction’ and stored as an attribute for the edgeIf the value of an edge attribute is a list then the list values are turned into a string separated by a comma and then enclosed by double quotes.
Coordinates are copied in manner described here:
copy_cartesian_coords_into_graph()
Warning
If legacymode is set to True in constructor then:
networkx.Graph
created by this method does NOT support multiple edges between the same nodes. Extra edges encountered are ignored and not converted.- In addition, the ‘r’ attribute in the node dict is NOT copied to the resulting
networkx.Graph
object. - networkx_graph parameter is ignored
Parameters: - nice_cx_network (
NiceCXNetwork
) – Network to extract graph from - networkx_graph (
networkx.Graph
or subtype) – Empty networkx graph to populate which is IGNORED if legacymode is set to True in constructor. If unset and legacymode is False in constructor then anetworkx.MultiDiGraph
is created
Raises: NDExError – if input network is None
Returns: Input network converted to networkx Graph
Return type: networkx.Graph
if legacymode is set to True in constructor otherwisenetworkx.MultiDiGraph
unless networkx_graph is set in which case networkx_graph is returned
-
This networkx converter is still callable, but has been deprecated
-
class
ndex2.nice_cx_network.
LegacyNetworkXVersionTwoPlusFactory
[source]¶ Deprecated since version 3.2.0: This implementation contains errors, but is left for backwards compatibility of
NiceCXNetwork.to_networkx()
Converts
NiceCXNetwork
tonetworkx.Graph
object following logic in legacy NDEx2 Python client when networkx 2.0+ is installed.Warning
This implementation assumes networkx 2.0+ is installed and will fail with older versions.
For conversion details see
get_graph()
Constructor
-
get_graph
(nice_cx_network, networkx_graph=None)[source]¶ Creates a
networkx.Graph
object from nice_cx_network passed in.Deprecated since version 3.2.0: This implementation contains errors, but is left for backwards compatibility of
NiceCXNetwork.to_networkx()
Warning
Converting large networks (10,000+ edges or nodes) may take a long time and consume lots of memory.
This implementation uses node name as ID for nodes, which is problematic if multiple nodes share the same name and results in invalid mapping of node positions
networkx.Graph
created by this method does NOT support multiple edges between the same nodes. Extra edges encountered are ignored and not converted.The conversion is done as follows:
Any network attributes are copied to the
networkx.Graph
in manner described here:add_network_attributes_from_nice_cx_network()
For nodes:
All nodes are added with the node id set to value of ‘n’ on node. For multiple nodes with same ‘n’ value behavior is unknown
A node attribute named ‘name’ is set for each node with its value set to the value of the ‘name’ attribute from the input network.
If ‘r’ exists on node, the value is added as a node attribute named ‘represents’
All other node attributes are added using the same attribute name as found in the input network. The value is directly set name as found in the input network. The value is directly set as it was found in input network (could be single object or list)
For edges:
Each edge is added setting the source to the value of ‘s’ attribute and target set as ‘t’ attribute of input network.
Any edge attributes named ‘i’ are renamed ‘interaction’ and stored as an attribute for the edge
If the value of an edge attribute is a list then the list values are turned into a string separated by a comma and then enclosed by double quotes.
Coordinates are copied in manner described here:
copy_cartesian_coords_into_graph()
Parameters: - nice_cx_network (
NiceCXNetwork
) – Network to extract graph from - networkx_graph (
networkx.Graph
or subtype) – ignored by this implementation
Returns: Input network converted to networkx Graph
Return type: networkx.Graph
- nice_cx_network (
-
Base class for `Networkx <https://networkx.org/>`__ converters above
-
class
ndex2.nice_cx_network.
NetworkXFactory
[source]¶ Base class for subclasses that implement a factory that creates
networkx.Graph
objects and contains a couple utility methods used by implementing factory classes-
add_edge
(networkx_graph, source_node, target_node, attribute_dict)[source]¶ Adds edge to graph dealing with differences between networkx 1.x and 2.x+
Parameters: - networkx_graph (
networkx.Graph
or one of its subtypes) – networkx graph to add node to - source_node – id of source node
- target_node – id of target node
- attribute_dict (dict) – dictionary of edge attributes
Returns: None
- networkx_graph (
-
add_network_attributes_from_nice_cx_network
(nice_cx_network, networkx_graph)[source]¶ Iterates through network attributes of input nice_cx_network appending the attributes to the graph object passed in setting the values like so:
networkx_graph.graph[attribute_name] = attribute_value
If the value of a network attribute is of type list then the values are converted to strings and concatenated into a single string separated by commas.
Parameters: - nice_cx_network (
NiceCXNetwork
) – Network to extract network attributes from - networkx_graph (
networkx.Graph
) – networkx Graph object, should work with any of the types of Graphs ie MultiGraph etc..
Raises: NDExError – If either input parameter is None
Returns: None
- nice_cx_network (
-
add_node
(networkx_graph, nodeid, node_attributes, name=None, represents=None)[source]¶ Adds node to graph dealing with differences between networkx 1.x and 2.x+
Parameters: - networkx_graph (
networkx.Graph
or one of its subtypes) – networkx graph to add node to - nodeid – node identifier can be string, int etc.
- node_attributes (dict) – dictionary of key => value data to set set node attributes with
- name (string) – name of node that is set as attribute with key ‘name’ on node
- represents – represents value for node that is set as attribute with key ‘represents’ on node
Returns: None
- networkx_graph (
-
copy_cartesian_coords_into_graph
(nice_cx_network, networkx_graph)[source]¶ Examines the nice_cx_network extracting the content of the opaque aspect
CARTESIAN_LAYOUT_ASPECT
If data is found in above aspect, then this method iterates through the list of values which is assumed to be a dictionary of node ids with coordinates as seen here:
[ { 'node': <id>, 'x': <x coord>, 'y': <y coord}, { 'node': <id>, 'x': <x coord>, 'y': <y coord}, . . ]
These values (as seen in example above) are stored in the networkx_graph object as tuples with id of node set as key like so:
networkx_graph.pos[<id from above>] = (<x coord>, <y coord>)
Parameters: - nice_cx_network (
NiceCXNetwork
) – Input network - networkx_graph (
networkx.Graph
) – Network to append coordinates to
Raises: NDExError – If either input parameter is None
Returns: None
- nice_cx_network (
-