2.1. Embedded graphs

class ect.embed_graph.EmbeddedGraph[source]

A class to represent a graph with embedded coordinates for each vertex with simple geometric graph operations.

Attributes
graphnx.Graph

a NetworkX graph object

coord_matrixnp.ndarray

a matrix of embedded coordinates for each vertex

node_listlist

a list of node names

node_to_indexdict

a dictionary mapping node ids to their index in the coord_matrix

dimint

the dimension of the embedded coordinates

__init__()[source]

Initialize a graph with edges, name, or graph attributes.

Parameters:
  • incoming_graph_data (input graph (optional, default: None)) – Data to initialize graph. If None (default) an empty graph is created. The data can be an edge list, or any NetworkX graph object. If the corresponding optional Python packages are installed the data can also be a 2D NumPy array, a SciPy sparse array, or a PyGraphviz graph.

  • attr (keyword arguments, optional (default= no attributes)) – Attributes to add to graph as key=value pairs.

See also

convert

Examples

>>> G = nx.Graph()  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G = nx.Graph(name="my graph")
>>> e = [(1, 2), (2, 3), (3, 4)]  # list of edges
>>> G = nx.Graph(e)

Arbitrary graph attribute pairs (key=value) may be assigned

>>> G = nx.Graph(e, day="Friday")
>>> G.graph
{'day': 'Friday'}
property coord_matrix

Return the N x D coordinate matrix

property dim

Return the dimension of the embedded coordinates

property node_list

Return ordered list of node names.

property node_to_index

Return a dictionary mapping node ids to their index in the coord_matrix

property position_dict

Return a dictionary mapping node ids to their coordinates

property edge_indices

Return edges as array of index pairs

add_node(*args, **kwargs)[source]

Add a single node node_for_adding and update node attributes.

Parameters:
  • node_for_adding (node) – A node can be any hashable Python object except None.

  • attr (keyword arguments, optional) – Set or change node attributes using key=value.

See also

add_nodes_from

Examples

>>> G = nx.Graph()  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_node(1)
>>> G.add_node("Hello")
>>> K3 = nx.Graph([(0, 1), (1, 2), (2, 0)])
>>> G.add_node(K3)
>>> G.number_of_nodes()
3

Use keywords set/change node attributes:

>>> G.add_node(1, size=10)
>>> G.add_node(3, weight=0.4, UTM=("13S", 382871, 3972649))

Notes

A hashable object is one that can be used as a key in a Python dictionary. This includes strings, numbers, tuples of strings and numbers, etc.

On many platforms hashable items also include mutables such as NetworkX Graphs, though one should be careful that the hash doesn’t change on mutables.

add_nodes_from_dict(nodes_with_coords)[source]
add_nodes_from(nodes_with_coords)[source]

Add multiple nodes.

Parameters:
  • nodes_for_adding (iterable container) – A container of nodes (list, dict, set, etc.). OR A container of (node, attribute dict) tuples. Node attributes are updated using the attribute dict.

  • attr (keyword arguments, optional (default= no attributes)) – Update attributes for all nodes in nodes. Node attributes specified in nodes as a tuple take precedence over attributes specified via keyword arguments.

See also

add_node

Notes

When adding nodes from an iterator over the graph you are changing, a RuntimeError can be raised with message: RuntimeError: dictionary changed size during iteration. This happens when the graph’s underlying dictionary is modified during iteration. To avoid this error, evaluate the iterator into a separate object, e.g. by using list(iterator_of_nodes), and pass this object to G.add_nodes_from.

Examples

>>> G = nx.Graph()  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_nodes_from("Hello")
>>> K3 = nx.Graph([(0, 1), (1, 2), (2, 0)])
>>> G.add_nodes_from(K3)
>>> sorted(G.nodes(), key=str)
[0, 1, 2, 'H', 'e', 'l', 'o']

Use keywords to update specific node attributes for every node.

>>> G.add_nodes_from([1, 2], size=10)
>>> G.add_nodes_from([3, 4], weight=0.4)

Use (node, attrdict) tuples to update attributes for specific nodes.

>>> G.add_nodes_from([(1, dict(size=11)), (2, {"color": "blue"})])
>>> G.nodes[1]["size"]
11
>>> H = nx.Graph()
>>> H.add_nodes_from(G.nodes(data=True))
>>> H.nodes[1]["size"]
11

Evaluate an iterator over a graph if using it to modify the same graph

>>> G = nx.Graph([(0, 1), (1, 2), (3, 4)])
>>> # wrong way - will raise RuntimeError
>>> # G.add_nodes_from(n + 1 for n in G.nodes)
>>> # correct way
>>> G.add_nodes_from(list(n + 1 for n in G.nodes))
get_coord(*args, **kwargs)[source]
set_coord(*args, **kwargs)[source]
add_cycle(coord_matrix)[source]

Add nodes in a cyclic pattern from coordinate matrix

get_center(method='bounding_box')[source]

Calculate center of coordinates

get_bounding_box()[source]

Get (min, max) for each dimension

get_bounding_radius(center_type='bounding_box')[source]

Get radius of minimal bounding sphere

get_normal_angle_matrix(edges_only=False, decimals=None)[source]

Optimized angle matrix computation using vectorized operations.

Parameters:
  • edges_only – Only compute angles between connected vertices

  • decimals – Round angles to specified decimal places

Returns:

NaN-filled matrix with pair angles vertex_labels: Ordered node identifiers

Return type:

angle_matrix

get_normal_angles(edges_only=False, decimals=6)[source]

Optimized angle dictionary construction using NumPy grouping.

Parameters:
  • edges_only – Only include edge-connected pairs

  • decimals – Round angles to specified decimal places

Returns:

Dictionary mapping rounded angles to vertex pairs

transform_coordinates(center_type='bounding_box', projection_type='pca')[source]

Transform coordinates center and orientation

center_coordinates(center_type='mean')[source]
scale_coordinates(radius=1)[source]

Scale coordinates to fit within given radius

project_coordinates(projection_type='pca')[source]

Project coordinates using a function

pca_projection(target_dim=2)[source]

Dimensionality reduction using PCA

add_edge(*args, **kwargs)[source]

Add an edge between u and v.

The nodes u and v will be automatically added if they are not already in the graph.

Edge attributes can be specified with keywords or by directly accessing the edge’s attribute dictionary. See examples below.

Parameters:
  • u_of_edge (nodes) – Nodes can be, for example, strings or numbers. Nodes must be hashable (and not None) Python objects.

  • v_of_edge (nodes) – Nodes can be, for example, strings or numbers. Nodes must be hashable (and not None) Python objects.

  • attr (keyword arguments, optional) – Edge data (or labels or objects) can be assigned using keyword arguments.

See also

add_edges_from

add a collection of edges

Notes

Adding an edge that already exists updates the edge data.

Many NetworkX algorithms designed for weighted graphs use an edge attribute (by default weight) to hold a numerical value.

Examples

The following all add the edge e=(1, 2) to graph G:

>>> G = nx.Graph()  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> e = (1, 2)
>>> G.add_edge(1, 2)  # explicit two-node form
>>> G.add_edge(*e)  # single edge as tuple of two nodes
>>> G.add_edges_from([(1, 2)])  # add edges from iterable container

Associate data to edges using keywords:

>>> G.add_edge(1, 2, weight=3)
>>> G.add_edge(1, 3, weight=7, capacity=15, length=342.7)

For non-string attribute keys, use subscript notation.

>>> G.add_edge(1, 2)
>>> G[1][2].update({0: 5})
>>> G.edges[1, 2].update({0: 5})
validate_plot_parameters()[source]

Decorator to validate plot method parameters

plot(*args, **kwargs)[source]