Reference for SWMManywhere/graph_utilities.py
Graph utilities module for SWMManywhere.
A module to contain graphfcns, the graphfcn registry object, and other graph utilities (such as save/load functions).
BaseGraphFunction
Bases: ABC
Base class for graph functions.
On a SWMManywhere project the intention is to iterate over a number of graph functions. Each graph function may require certain attributes to be present in the graph. Each graph function may add attributes to the graph. This class provides a framework for graph functions to check their requirements and additions a-priori when the list is provided.
Source code in swmmanywhere/graph_utilities.py
__call__(G, *args, **kwargs)
abstractmethod
__init_subclass__(required_edge_attributes=None, adds_edge_attributes=None, required_node_attributes=None, adds_node_attributes=None)
Set the required and added attributes for the subclass.
Source code in swmmanywhere/graph_utilities.py
add_graphfcn(edge_attributes, node_attributes)
Add the attributes that the graph function adds.
Source code in swmmanywhere/graph_utilities.py
validate_requirements(edge_attributes, node_attributes)
Validate that the graph has the required attributes.
Source code in swmmanywhere/graph_utilities.py
GraphFunctionRegistry
Bases: dict
Registry object.
Source code in swmmanywhere/graph_utilities.py
__getattr__(name)
Get a graph function from the graphfcn dict.
register(cls)
filter_streets(G)
Filter streets.
This function removes non streets from a graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G
|
Graph
|
A graph |
required |
Returns:
Type | Description |
---|---|
Graph
|
A graph of only street edges |
Source code in swmmanywhere/graph_utilities.py
iterate_graphfcns(G, graphfcn_list, params=parameters.get_full_parameters(), addresses=temp_addresses)
Iterate a list of graph functions over a graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G
|
Graph
|
The graph to iterate over. |
required |
graphfcn_list
|
list[str]
|
A list of graph functions to iterate. |
required |
params
|
dict
|
A dictionary of parameters to pass to the graph functions. |
get_full_parameters()
|
addresses
|
FilePaths
|
A FilePaths parameter object |
temp_addresses
|
Returns:
Type | Description |
---|---|
Graph
|
nx.Graph: The graph after the graph functions have been applied. |
Source code in swmmanywhere/graph_utilities.py
load_graph(fid)
Load a graph from a file saved with save_graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fid
|
Path
|
The path to the file |
required |
Returns:
Name | Type | Description |
---|---|---|
G |
Graph
|
A graph |
Source code in swmmanywhere/graph_utilities.py
register_graphfcn(cls)
Register a graph function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
Callable
|
A class that inherits from BaseGraphFunction |
required |
Returns:
Name | Type | Description |
---|---|---|
cls |
Callable
|
The same class |
Source code in swmmanywhere/graph_utilities.py
save_graph(G, fid)
Save a graph to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G
|
Graph
|
A graph |
required |
fid
|
Path
|
The path to the file |
required |
Source code in swmmanywhere/graph_utilities.py
validate_graphfcn_list(graphfcn_list, starting_graph=None)
Validate that the graph functions are registered.
Tests that the graph functions are registered.
Tests that the graph functions have the required attributes in the graph
and updates the attributes that are added to the graph.
required_edge_attributes
and required_node_attributes
currently only
specify that one element in the graph must have the attribute (e.g., if a
graphfcn has required_node_attributes=['id']
, and only one node in the
graph has the id
attribute, then it will be valid).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graphfcn_list
|
list[str]
|
A list of graph functions |
required |
starting_graph
|
Graph
|
A graph to check the starting attributes of. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If a graph function is not registered |
ValueError
|
If a graph function requires an attribute that is not in the graph |