ysig / GraKeL

A scikit-learn compatible library for graph kernels
https://ysig.github.io/GraKeL/
Other
587 stars 96 forks source link

Print <class 'grakel.graph.Graph'> #102

Open AmirDavoodi opened 7 months ago

AmirDavoodi commented 7 months ago

Describe the bug I am trying to print the generated 'grakel.graph.Graph' Object. I am getting this error.

ValueError: purpose is either "adjacency" of "dictionary"

To Reproduce Steps to reproduce the behavior:

from grakel import Graph
# Create a dictionary representation of a graph
graph_dict = {
    0: [(1, 1), (2, 1)],
    1: [(0, 1), (2, 1)],
    2: [(0, 1), (1, 1)]
}

# Create a Graph object
g = Graph(graph_dict, node_labels=None, edge_labels=None, graph_format='dictionary')
print(g)

Expected behavior Expect Output is:

#vertices
0,1,2,(0, 1),(2, 1),(1, 1)
#edges
(0, (1, 1)),1.0
(0, (2, 1)),1.0
(1, (0, 1)),1.0
(1, (2, 1)),1.0
(2, (0, 1)),1.0
(2, (1, 1)),1.0

Stack Trace The error stack trace is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File ~/miniconda3/lib/python3.9/site-packages/IPython/core/formatters.py:706, in PlainTextFormatter.__call__(self, obj)
    699 stream = StringIO()
    700 printer = pretty.RepresentationPrinter(stream, self.verbose,
    701     self.max_width, self.newline,
    702     max_seq_length=self.max_seq_length,
    703     singleton_pprinters=self.singleton_printers,
    704     type_pprinters=self.type_printers,
    705     deferred_pprinters=self.deferred_printers)
--> 706 printer.pretty(obj)
    707 printer.flush()
    708 return stream.getvalue()

File ~/miniconda3/lib/python3.9/site-packages/IPython/lib/pretty.py:410, in RepresentationPrinter.pretty(self, obj)
    407                         return meth(obj, self, cycle)
    408                 if cls is not object \
    409                         and callable(cls.__dict__.get('__repr__')):
--> 410                     return _repr_pprint(obj, self, cycle)
    412     return _default_pprint(obj, self, cycle)
    413 finally:

File ~/miniconda3/lib/python3.9/site-packages/IPython/lib/pretty.py:778, in _repr_pprint(obj, p, cycle)
    776 """A pprint that just redirects to the normal repr function."""
    777 # Find newlines and replace them with p.break_()
...
-> 1133     raise ValueError('purpose is either "adjacency" of "dictionary"')
   1135 if purpose == "adjacency":
   1136     self.desired_format("adjacency", warn=True)

ValueError: purpose is either "adjacency" of "dictionary"

I already fixed it on the source code. If possible I can send you a PR for this fix.

Adding following code to the get_edges function at grakel/graph.py

        if purpose not in ["adjacency", "dictionary", "any"]:
            raise ValueError('purpose is either "adjacency" of "dictionary"')

        if purpose == "any":
            if self._format in ['all', 'adjacency']:
                purpose = "adjacency"
            else:
                purpose = "dictionary"