nxgrph

Class

Graph

Graph object holding relevant energy system data as node and edge attributes.

API Functionaility

create_nodes

Takes a nodelist of string uids as positional argument.

create_edges

Convenience function to populate the Graph-object with edges.

nxgrph is a tessif interface transforming the dictionairy representation (see tessif.transform.es2mapping) of an (optimized) energy system simulation model into a networkx.DiGraph.

This module follows a 2 way approach on constructing networkx.Graph like objects:

  1. Approach is to expose a class like structure (Graph) needing an ESTransformer object for construction. Allowing automated postprocessing.

  2. Approach is exposing two explicit functions to create nodes and edges assuming a Graph like object already exists (which obviously could have been just constructed). This emulates puplic API functionality and allows the use of the dict processing capabilities coming with this module seperatey.

Note

When using this module to perform NetworkX operations on the energy system it might be required to temporariliy relabel nodes to integers because of complex node labeling.

tessif.transform.nxgrph.create_nodes(graph, nodes, defaults={}, **kwargs)[source]

Takes a nodelist of string uids as positional argument. All other arguments are aggregated into kwargs and end up as node attributes which are accesible via networkx.Graph.nodes(data='attribute')

Parameters:
  • graph (networkx.Graph like object) – Graph object the nodes are created for and added to.

  • nodes (Iterable) –

    Iterable of node uids as strings as in:

    ['1', '2', '3']
    

    or:

    [str(node.uid) for node in energy_sytem.nodes]
    

  • defaults (dict, default={}) – In case a dict is provided via kwargs and not every node is present in this dict, the keyword argument will be looked for in defaults.

  • kwargs (value, dict) –

    Node attributes as keyword arguments to pass to the created nodes. Using a node dict as in {node: attribute} allows different attributes for each node.

    All keyword arguments can be single value arguments or {node_uid: value} dictionairies. defaults are used for those nodes not present in the dictionairy. Otherwise value will be set to None.

    Note

    To pass a bunch of keyword arguments directly use networkx.Graph.add_node() to supply them directly as in

    >>> import networkx
    >>> grph = networkx.Graph()
    >>> kwargs = {'arg_1': 'value_1', 'arg_2': 'value_2'}
    >>> grph.add_node('node_1', **kwargs)
    >>> print(grph.nodes(data=True))
    [('node_1', {'arg_1': 'value_1', 'arg_2': 'value_2'})]
    

Returns:

node_attr – A dictionairy holding the processed and passed node attributes.

Return type:

dict

Examples

Use a dict to populate each node seperatey. Use a single value for uniform value setting (logging stuff is done to enable tessif internal doctesting and can be ignored here):

>>> from tessif.transform import nxgrph
>>> import networkx as nx
>>> import pprint
>>> nodes = ['1', '2', '3']
>>> grph = nx.DiGraph(name='my_graph')
>>> node_attributes = nxgrph.create_nodes(
...     grph, nodes,
...     d=dict(zip(nodes, [10, 20, 30])),
...     i=1)
>>> pprint.pprint(node_attributes)
{'1': {'d': 10, 'i': 1}, '2': {'d': 20, 'i': 1}, '3': {'d': 30, 'i': 1}}
tessif.transform.nxgrph.create_edges(graph, edges, defaults={}, **kwargs)[source]

Convenience function to populate the Graph-object with edges.

Takes an iterable of edge tuple uids as positional argument. All other arguments are aggregated into kwargs and end up as edge attributes which are accesible via networkx.Graph.edges(data='attribute')

Parameters:
  • graph (networkx.Graph like object) – Graph object the nodes are created for and added to.

  • edges (Iterable) –

    Iterable of edge uids as strings as in:

    [('1', '2'), ('2', '3'), ('3', '1')]
    

    or:

    [(str(inflow.uid), str(node.uid))
        for node in esystem.nodes for inflow in node.inputs.keys()]
    

  • defaults (dict, default={}) – In case a dict is provided via kwargs and not every node is present in this dict, the keyword argument will be looked for in defaults.

  • kwargs (value, dict) –

    Edge attributes as keyword arguments to pass to the created nodes. Using a node dict as in {('node_from_uid', 'node_to_uid'): value} allows different attributes for each node.

    All keyword arguments can be single value arguments or {('node_from_uid', 'node_to_uid'): value} dictionairies. defaults are used for those edges not present in the dictionairy. Otherwise value will set to None.

Returns:

edge_attr – A dictionairy holding the processed and passed edge attributes.

Return type:

dict

Examples

Use a dict to populate each edge seperatey. Use a single value for uniform value setting (logging stuff is done to enable tessif internal doctesting and can be ignored here):

>>> from tessif.transform import nxgrph
>>> import networkx as nx
>>> import pprint
>>> edges = [('1', '2'), ('2', '3')]
>>> grph = nx.DiGraph(name='my_graph')
>>> edge_attributes = nxgrph.create_edges(
...     grph, edges,
...     d=dict(zip(edges, [10, 20])),
...     v=1)
>>> pprint.pprint(edge_attributes)
{Edge(source='1', target='2'): {'d': 10, 'v': 1},
 Edge(source='2', target='3'): {'d': 20, 'v': 1}}
class tessif.transform.nxgrph.Graph(es_transformer, **kwargs)[source]

Bases: DiGraph

Graph object holding relevant energy system data as node and edge attributes.

Convenience wrapper for creating a networkx.DiGraph Designed to be used with a ESTransformer object.

For more flexibility and control use create_nodes() and create_edges().

Parameters:
  • es_transformer (ESTransformer) – Energy system to dictionairy transformer object returning its data as a 2 layer nested dict in the form of {attribute: {node/edge: parameter}} if accessed for node_data() / edge_data() respectively. As well es a default dictionairy for node and edge attributes if accessed for defaults

  • **kwargs (key word arguments) – kwargs are passed to networkx.DiGraph

Examples

Use the Example Resultier to demonstrate behaviour:

>>> from tessif.transform import nxgrph
>>> from tessif.transform.es2mapping.base import XmplResultier
>>> import pprint
>>> grph = nxgrph.Graph(XmplResultier())
>>> pprint.pprint(list(grph.nodes(data=True)))
[('1', {'attr_xmpl': 'red'}),
 ('2', {'attr_xmpl': 'red'}),
 ('3', {'attr_xmpl': 'red'})]
>>> pprint.pprint(list(grph.edges(data=True)))
[('1', '2', {'attr_xmpl': 3}),
 ('2', '3', {'attr_xmpl': 5}),
 ('3', '1', {'attr_xmpl': 4})]

Use the oemof minimum working example together with the AllResultier:

>>> from tessif.transform.es2mapping.omf import AllResultier
>>> import tessif.examples.data.omf.py_hard as omf_examples
>>> grph = nxgrph.Graph(AllResultier(omf_examples.create_mwe()))
>>> pprint.pprint(list(grph.edges(data='specific_flow_costs')))
[('Power Line', 'Demand', 0),
 ('Renewable', 'Power Line', 9),
 ('CBET', 'Transformer', 0),
 ('CBE', 'CBET', 0),
 ('Transformer', 'Power Line', 10)]