ragraph.graph
¶
Graph class module¶
The Graph
class is the core of this package. It is the internal storage
format that facilitates all conversions. These class definitions are designed to
accommodate a wide range of graph formats.
Graph
¶
|
Bases: Metadata
Graph of Node
objects and Edge
objects between
them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Optional[Iterable[Node]]
|
Iterable of graph nodes. |
None
|
edges
|
Optional[Iterable[Edge]]
|
Iterable of graph edges. |
None
|
add_parents
|
bool
|
Recursively add parent nodes of the provided nodes. |
False
|
add_children
|
bool
|
Recursively add child nodes of the provided nodes. |
False
|
name
|
Optional[str]
|
Instance name. Given a UUID if none provided. |
None
|
kind
|
Optional[str]
|
Kind or main category of this instance. |
None
|
labels
|
Optional[List[str]]
|
Labels categorizing this instance. |
None
|
weights
|
Optional[Dict[str, Union[int, float]]]
|
Dictionary of weights attached to this instance. |
None
|
annotations
|
Optional[Union[Annotations, Dict[str, Any]]]
|
Miscellaneous properties of this instance. |
None
|
uuid
|
Optional[Union[str, UUID]]
|
Fixed UUID if desired, generated when left set to None. |
None
|
Source code in ragraph/graph.py
directed_edges
property
¶
Nested edge dictionary from source name to target name to a list of edges.
reversed_edges
property
¶
Nested edge dictionary from target name to source name to a list of edges.
add_edge
¶
Add a new edge to the graph. Source and target need to exist in the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge
|
Edge
|
Edge to add. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if the source or target doesn't exist or the edge instance already does. |
Source code in ragraph/graph.py
add_node
¶
Add a new node to this graph.
If the node instance already exists in the graph, it is ignored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
Node to add. |
required |
Source code in ragraph/graph.py
as_dict
¶
Return a copy as a (serializable) dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_uuid
|
bool
|
Whether to use UUIDs instead of names. |
False
|
Returns:
Name | Type | Description |
---|---|---|
nodes |
Dict[str, Any]
|
Node names or UUIDs to Node dictionaries. |
kind |
Dict[str, Any]
|
Kind as str. |
labels |
Dict[str, Any]
|
Labels as list of str. |
weights |
Dict[str, Any]
|
Weights as dict. |
annotations |
Dict[str, Any]
|
Annotations as a dictionary. |
uuid |
Dict[str, Any]
|
UUID as str if toggled. |
Source code in ragraph/graph.py
check_consistency
¶
Check the consistency of this graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raise_error
|
bool
|
Whether to raise an error instead of returning a bool. |
True
|
Returns:
Type | Description |
---|---|
bool
|
Whether nodes aren't their own ancestor/descendant and if all their children |
bool
|
and parents exist in the graph. Raises an error for inconsistencies if |
bool
|
|
Source code in ragraph/graph.py
del_edge
¶
Remove an edge from the graph.
Source code in ragraph/graph.py
del_node
¶
Remove a node and related edges from the graph by name or Node instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Union[Node, str, UUID]
|
Node to remove. |
required |
inherit
|
bool
|
Whether to inherit hierarchy for underlying nodes to the parent. |
False
|
Source code in ragraph/graph.py
edges_between
¶
Yield edges between source and target node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Union[str, Node]
|
Source node. |
required |
target
|
Union[str, Node]
|
Target node. |
required |
inherit
|
bool
|
Whether to include edges between descendants of given nodes. |
False
|
loops
|
bool
|
Whether to include self-loop edges. |
True
|
Source code in ragraph/graph.py
edges_between_all
¶
Yield all edges between a set of source and a set of target nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sources
|
Iterable[Union[str, Node]]
|
Source nodes. |
required |
targets
|
Iterable[Union[str, Node]]
|
Target nodes. |
required |
inherit
|
bool
|
Whether to include edges between descendants of given nodes. |
False
|
loops
|
bool
|
Whether to include self-loop edges. |
True
|
Source code in ragraph/graph.py
edges_from
¶
Yield edges originating from a given source node.
Source code in ragraph/graph.py
edges_to
¶
Yield edges towards a given target node.
Source code in ragraph/graph.py
get_adjacency_matrix
¶
|
Convert graph data into a numerical adjacency matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Optional[Union[List[Node], List[str]]]
|
Optional list of nodes for which to get the adjacency matrix. |
None
|
inherit
|
bool
|
Whether to count weights between children of the given nodes. |
False
|
loops
|
bool
|
Whether to calculate self-loops from a node to itself. |
False
|
only
|
Optional[List[str]]
|
Optional subset of edge weights to consider. See |
None
|
Returns:
Type | Description |
---|---|
Union[ndarray, List[List[float]]]
|
Adjacency matrix as a 2D numpy array if numpy is present. Otherwise it will |
Union[ndarray, List[List[float]]]
|
return a 2D nested list. |
Source code in ragraph/graph.py
get_ascii_art
¶
|
Get a unicode ASCII art representation of a binary adjacency matrix for a given set of nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Optional[List[Node]]
|
Nodes to include in the art. |
None
|
edge
|
str
|
Mark to use when there's an edge between nodes. (X by default) |
'X'
|
diag
|
str
|
Mark to use on the diagonal. |
'■'
|
show
|
bool
|
Whether to directly print the ASCII art. |
False
|
Returns:
Type | Description |
---|---|
Optional[str]
|
ASCII art representation of this graph. |
Source code in ragraph/graph.py
get_edge_selection
¶
Select specific edges from this graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Optional[List[Node]]
|
The list of nodes between which the edges must be selected. |
None
|
edge_kinds
|
Optional[List[str]]
|
The kinds of edges to be selected. |
None
|
Returns:
Type | Description |
---|---|
List[Edge]
|
List of selected edges. |
Source code in ragraph/graph.py
get_edges_by_kind
¶
get_graph_slice
¶
Return the graph slice with only the given nodes and the edges between those.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Optional[Iterable[Node]]
|
Optional set of nodes to include in the new deep copy. Defaults to all. |
None
|
edges
|
Optional[Iterable[Edge]]
|
Optional set of edges to include in the new deep copy. Defaults to all between selected nodes. |
None
|
inherit
|
bool
|
Whether to include child nodes of the given nodes and edges between them (unless edges are explicitly specified). |
False
|
Note
This returns a deepcopy, so the copied nodes and edges do NOT share any changes! All parent and child nodes not in the list are excluded.
Source code in ragraph/graph.py
get_hierarchy_dict
¶
Return a dictionary of the (partial) hierarchical node structure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
roots
|
Optional[List[Node]]
|
Root nodes of the hierarchy to calculate. |
None
|
levels
|
Optional[int]
|
Number of levels to include below the roots. |
None
|
Source code in ragraph/graph.py
get_kinds
¶
Get an alphabetically sorted list of unique node/edge kinds in the graph.
get_mapping_matrix
¶
|
Convert graph data into a numerical mapping matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rows
|
Optional[Union[List[Node], List[str]]]
|
Nodes representing the matrix rows. |
None
|
cols
|
Optional[Union[List[Node], List[str]]]
|
Nodes representing the matrix columns if different from the rows. |
None
|
inherit
|
bool
|
Whether to count weights between children of the given nodes. |
False
|
loops
|
bool
|
Whether to calculate self-loops from a node to itself. |
False
|
only
|
Optional[List[str]]
|
Optional subset of edge weights to consider. See |
None
|
Returns:
Type | Description |
---|---|
Union[ndarray, List[List[float]]]
|
Adjacency matrix as a 2D numpy array if numpy is present. Otherwise it will |
Union[ndarray, List[List[float]]]
|
return a 2D nested list. |
Source code in ragraph/graph.py
get_node_and_edge_selection
¶
Select specific nodes and edges from this graph in a structured order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_kinds
|
Optional[List[str]]
|
The kind of nodes to be selected. |
None
|
edge_kinds
|
Optional[List[str]]
|
The kind of edges to be selected. |
None
|
depth
|
int
|
The maximum depth of node to be selected. |
2
|
selection_mode
|
str
|
The selection mode. Either 'dependent' or 'independent'. |
'dependent'
|
Note
The selection mode argument determines how nodes of different kinds are
selected. If the selection mode is set to 'dependent', the first node kind
in the node_kinds
list is considered to be the 'lead node kind'.
Nodes of different kind than the lead node kind, are only selected if they
have a dependency with at least one of the selected nodes of the lead node
kind. If the selection mode is set to 'independent' this dependency
condition is dropped.
Source code in ragraph/graph.py
get_node_selection
¶
|
Select specific nodes from this graph in a structured order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_kinds
|
Optional[List[str]]
|
The kind of nodes to be selected. |
None
|
edge_kinds
|
Optional[List[str]]
|
The kind of edges to be selected. |
None
|
depth
|
int
|
The maximum depth of node to be selected. |
2
|
selection_mode
|
str
|
The selection mode. Either 'dependent' or 'independent'. |
'dependent'
|
Note
The selection mode argument determines how nodes of different kinds are
selected. If the selection mode is set to 'dependent', the first node kind
in the node_kinds
list is considered to be the 'lead node kind'.
Nodes of different kind than the lead node kind, are only selected if they
have a dependency with at least one of the selected nodes of the lead node
kind. If the selection mode is set to 'independent' this dependency
condition is dropped.
Source code in ragraph/graph.py
get_nodes_by_kind
¶
get_weight_labels
¶
Get an alphabetically sorted list of unique labels of node/edge weights.
Source code in ragraph/graph.py
sources_of
¶
Yield source nodes of a given target node.
Source code in ragraph/graph.py
targets_of
¶
Yield target nodes of a given source node.
Source code in ragraph/graph.py
GraphView
¶
Bases: Graph
A view on a Graph
object. It works exactly like the
Graph
object, except for that the nodes and edges are filtered
according to the view function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
Graph to provide a view on. |
required |
view_func
|
Optional[Callable[[Any], Tuple[Union[Iterable[Node], Dict[str, Node]], Union[Iterable[Edge], Dict[str, Edge]]]]]
|
View function that filters the |
None
|
view_kwargs
|
Optional[Dict[str, Any]]
|
Optional arguments to pass to the view function. |
None
|
Source code in ragraph/graph.py
proxy
¶
Undo any filtering and just set this view to proxy the Graph 1:1.
Source code in ragraph/graph.py
reset
¶
Reset the private properties after the original graph might have changed.
Source code in ragraph/graph.py
update
¶
Apply the set view function with arguments.