"""This file is part of DINGO, the DIstribution Network GeneratOr.
DINGO is a tool to generate synthetic medium and low voltage power
distribution grids based on open data.

It is developed in the project open_eGo: https://openegoproject.wordpress.com

DINGO lives at github: https://github.com/openego/dingo/
The documentation is available on RTD: http://dingo.readthedocs.io"""

__copyright__  = "Reiner Lemoine Institut gGmbH"
__license__    = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__        = "https://github.com/openego/dingo/blob/master/LICENSE"
__author__     = "nesnoj, gplssm"


# This file provides some useful functions for debugging DINGO

import dingo
import os.path as path
import networkx as nx


def compare_graphs(graph, mode):
    """ Compares graph with saved one which is loaded via networkx' gpickle
    Args:
        graph:
        mode: 'write' or 'compare'

    Returns:

    """

    # get path
    package_path = dingo.__path__[0]
    file = path.join(package_path, 'output', 'debug', 'graph.gpickle')

    if mode is 'write':
        try:
            nx.write_gpickle(graph, file)
            print('=====> DEBUG: Graph written to', file)
        except:
            raise FileNotFoundError('Could not write to file', file)

    elif mode is 'compare':
        try:
            graph2 = nx.read_gpickle(file)
            print('=====> DEBUG: Graph read from', file)
        except:
            raise FileNotFoundError('File not found:', file)

        # get data
        nodes1 = sorted(graph.nodes(), key=lambda _: repr(_))
        nodes2 = sorted(graph2.nodes(), key=lambda _: repr(_))
        edges1 = sorted(graph.edges(), key=lambda _: repr(_))
        edges2 = sorted(graph2.edges(), key=lambda _: repr(_))

        graphs_are_isomorphic = True

        # check nodes
        if len(nodes1) > len(nodes2):
            print('Node count in graph 1 > node count in graph 2')
            print('Difference:', [node for node in nodes1 if repr(node) not in repr(nodes2)])
            graphs_are_isomorphic = False
        elif len(nodes2) > len(nodes1):
            print('Node count in graph 2 > node count in graph 1')
            print('Difference:', [node for node in nodes2 if repr(node) not in repr(nodes1)])
            graphs_are_isomorphic = False

        # check edges
        if len(edges1) > len(edges2):
            print('Edge count in graph 1 > edge count in graph 2')
            print('Difference:', [edge for edge in edges1 if (repr(edge) not in repr(edges2)) and
                                  (repr(tuple(reversed(edge))) not in repr(edges2))])
            graphs_are_isomorphic = False
        elif len(edges2) > len(edges1):
            print('Edge count in graph 2 > edge count in graph 1')
            print('Difference:', [edge for edge in edges2 if (repr(edge) not in repr(edges1)) and
                                  (repr(tuple(reversed(edge))) not in repr(edges1))])
            graphs_are_isomorphic = False
        elif (len(edges1) == len(edges1)) and (len([edge for edge in edges1 if (repr(edge) not in repr(edges2)) and
            (repr(tuple(reversed(edge))) not in repr(edges2))]) > 0):
            print('Edge count in graph 1 = edge count in graph 2')
            print('Difference:', [edge for edge in edges2 if (repr(edge) not in repr(edges1)) and
                                  (repr(tuple(reversed(edge))) not in repr(edges1))])
            graphs_are_isomorphic = False

        if graphs_are_isomorphic:
            print('=====> DEBUG: Graphs are isomorphic')
        else:
            print('=====> DEBUG: Graphs are NOT isomorphic')

    else:
        raise ValueError('Invalid value for mode, use mode=\'write\' or \'compare\'')

    exit(0)

@@ -757,14 +757,28 @@
 raph
+1
 , mode
+, graph2=None
 ):%0A 
@@ -856,37 +856,282 @@
 kle%0A
+%0A
     
-Args:%0A        graph:%0A    
+Parameters%0A    ----------%0A    graph1 : networkx.graph%0A        First Dingo MV graph for comparison%0A    graph2 : networkx.graph%0A        Second Dingo MV graph for comparison. If a second graph is not provided%0A        it will be laoded from disk with hard-coded file name.%0A
     
@@ -1293,16 +1293,17 @@
 , 'graph
+1
 .gpickle
@@ -1377,16 +1377,17 @@
 le(graph
+1
 , file)%0A
@@ -1554,24 +1554,55 @@
  'compare':%0A
+        if graph2 is None:%0A    
         try:
@@ -1594,32 +1594,36 @@
            try:%0A
+    
             grap
@@ -1645,24 +1645,28 @@
 ickle(file)%0A
+    
             
@@ -1714,32 +1714,36 @@
 le)%0A        
+    
 except:%0A
             
@@ -1722,32 +1722,36 @@
         except:%0A
+    
             rais
@@ -1844,16 +1844,17 @@
 ed(graph
+1
 .nodes()
@@ -1970,16 +1970,17 @@
 ed(graph
+1
 .edges()

Extend compare_graphs() to take both graph as argument