ragraph.io.matrix#

Adjacency and mapping matrices support.

Module Contents#

Functions#

from_matrix(→ ragraph.graph.Graph)

Create a graph from an adjacency or mapping matrix.

to_matrix(→ Union[numpy.ndarray, List[List[float]]])

Convert graph data into a directed numerical adjacency or mapping matrix.

_parse_labels(→ List[ragraph.node.Node])

Parse matrix labels into a list of nodes.

_get_weight(→ float)

Get numerical weight from an edge.

Attributes#

np

ragraph.io.matrix.np#
ragraph.io.matrix.from_matrix(matrix: Union[numpy.ndarray, List[List[int]], List[List[float]]], rows: Optional[Union[List[ragraph.node.Node], List[str]]] = None, cols: Optional[Union[List[ragraph.node.Node], List[str]]] = None, weight_label: str = 'default', empty: Optional[Union[int, float]] = 0.0, **graph_args) ragraph.graph.Graph#

Create a graph from an adjacency or mapping matrix.

Parameters:
  • matrix – Matrix to convert into a graph.

  • rows – Nodes or node labels corresponding to the rows of the matrix.

  • cols – Nodes or node labels corresponding to the columns of the matrix. If none are provided, the row labels are re-used.

  • weight_label – Weight label to use for matrix values.

  • empty – Cell value to be considered “empty”, e.g. no edge should be created.

  • **graph_args – Additional arguments to Graph constructor.

Returns:

Graph object.

Note

If no row labels are provided, they are generated in a “node#” format. If no column labels are provided, they are assumed to be equal to the rows. For non-square matrices, you should provide node labels!

ragraph.io.matrix.to_matrix(graph, rows: Optional[Union[List[ragraph.node.Node], List[str]]] = None, cols: Optional[Union[List[ragraph.node.Node], List[str]]] = None, inherit: bool = False, loops: bool = False, only: Optional[List[str]] = None) Union[numpy.ndarray, List[List[float]]]#

Convert graph data into a directed numerical adjacency or mapping matrix.

Parameters:
  • graph – Graph to fetch data from.

  • rows – Nodes representing the matrix rows.

  • cols – Nodes representing the matrix columns if different from the rows.

  • inherit – Whether to count weights between children of the given nodes.

  • loops – Whether to calculate self-loops from a node to itself.

  • only – Optional subset of edge weights to consider. See ragraph.edge.Edge for default edge weight implementation.

Returns:

Adjacency matrix as a 2D numpy array if numpy is present. Otherwise it will return a 2D nested list.

Note

Note that the matrix is directed! Columns are inputs to rows.

ragraph.io.matrix._parse_labels(graph: ragraph.graph.Graph, labels: Union[List[ragraph.node.Node], List[str]]) List[ragraph.node.Node]#

Parse matrix labels into a list of nodes.

ragraph.io.matrix._get_weight(e: ragraph.edge.Edge, only: Optional[List[str]] = None) float#

Get numerical weight from an edge.