Repository navigation
Improving views exceptions - #8588
Conversation
rossbar
left a comment
There was a problem hiding this comment.
AIUI, this PR only changes the exception messages, not the exception types (at least AFAICT). I'm generally +1 for more informative messages, and since the exception types are all consistent we shouldn't be breaking anyone's code!
Thanks @amcandio !
| try: | ||
| return self._adjdict[u][v] | ||
| except KeyError as ex: # Customize msg to indicate exception origin | ||
| raise KeyError(f"The edge {e} is not in the graph.") |
There was a problem hiding this comment.
I guess the one subjective point is what the grammar should be for the exception messages. I'm personally fine on removing the period (and leaving periods off of all the newly-added messages) for consistency with the built-in exception messages.
I don't have an opinion one way or the other - just mentioning here in case others do!
|
Do we lose information here? The old error had a traceback that shows which part of the edge had a KeyError. And the KeyError message shows which node part of the edge led to the keyerror. Now we just get that the edge tuple caused an error, but we can't tell which part of it was the problem. The traceback show the You state that you thought return self._adjdict[u][v][k]
~~~~~~~~~~~~~~~~^^^
KeyError: 0meant that node 0 was not present. And it isn't present -- in the dict returned by Is there a way that we can change the error message to make it clear that the edge was not found, and still make it clear that the 2nd entry in the edge tuple was the trouble-maker? And I think we should retain the Traceback that shows where the error actually occurred. That is quite helpful when debugging. How do we change the message while keeping the old message and keeping the old traceback? I'm also a little concerned about try/except slowing down the inner working of our dicts. It used to be a minor issue, but I'm not sure it is any more. |
dschult
left a comment
There was a problem hiding this comment.
Ahh... I found it.
We can "add_note" to an exception message (as of Python 3.11) and if we use a bare raise it keeps the Traceback the same -- without adding the misdirection of having an exception raise another exception.
Co-authored-by: Dan Schult <[email protected]>
|
Good idea, will add it on the relevant places! |
dschult
left a comment
There was a problem hiding this comment.
I checked the timing for these changes.
I don't see any change significant timing change in the nodeviews, but the edgeviews are consistently 10-15% slower. I'm just doing an ipython run in one branch and then switching branches and running it again. Using G=binomial_graph(500, 0.3) and then converting to DiGraph(G.edges) for the other 3 graph types.
In [10]: node_ratio
Out[10]:
[1.0180193811817848,
0.9928326001439324,
0.9933429942939951,
1.031412567801611]
In [11]: edge_ratio
Out[11]:
[1.1344763924264316,
1.1639668887977164,
1.0999263146032279,
1.0883453134196077]
The only thing I can figure is that pre-checking the length of the edge is eating up time. Do we need that? Isn't the standard error message for incorrect length clear? In fact it actually gives more information.
ValueError: not enough values to unpack (expected 2, got 1)
vs
ValueError: Edge must have length 2
I'm starting to lean against this PR. The current error messages have more information and the extra error checking makes the code uglier and takes longer. If I understand correctly, the advantage is that the new error messages talk about "edges" or "nodes". So you know that it is an edge lookup that failed. But isn't that clear from the traceback?
|
My main confusion as a user came from the KeyError refering to a node when the actual missing entity is the edge (I thought that a node was missing when it was not). I reverted the node exception wrappers as they don't add much clarity and make the code uglier. With the current version we have: In [1]: import networkx as nx
In [2]: G = nx.MultiDiGraph([(0, 1)])
In [3]: G.edges[1, 0, 42]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[3], line 1
----> 1 G.edges[1, 0, 42]
File ~/workplace/networkx/networkx/classes/reportviews.py:1479, in OutMultiEdgeView.__getitem__(self, e)
1477 except KeyError as err:
1478 err.add_note(f"The edge {e} is not in the graph")
-> 1479 raise err
File ~/workplace/networkx/networkx/classes/reportviews.py:1476, in OutMultiEdgeView.__getitem__(self, e)
1474 u, v, k = e
1475 try:
-> 1476 return self._adjdict[u][v][k]
1477 except KeyError as err:
1478 err.add_note(f"The edge {e} is not in the graph")
KeyError: 0
The edge (1, 0, 42) is not in the graphwhich clarifies that the edge is the actualy entity missing. I do not see regressions with the following benchmark: import networkx as nx
import timeit
G = nx.binomial_graph(500, 0.3)
edges = G.edges
def bench(stmt, number=10):
return timeit.timeit(stmt, number=number, globals=globals())
print("list(G.nodes):",
bench("list(G.nodes)"))
print("list(G.edges):",
bench("list(G.edges)"))
def consume_edges(G):
s = 0
for u, v in G.edges:
s += u + v
return s
print("edge unpack loop:",
bench("consume_edges(G)"))
print("list(edges):",
bench("list(edges)"))
print("Graph(G.edges):",
bench("nx.Graph(G.edges)"))
print("DiGraph(G.edges):",
bench("nx.DiGraph(G.edges)"))
print("MultiGraph(G.edges):",
bench("nx.MultiGraph(G.edges)"))
print("MultiDiGraph(G.edges):",
bench("nx.MultiDiGraph(G.edges)")) |
|
Thanks for those benchmarks -- and for hearing my concerns. [Edited!!] Yay!! old postimport timeit
import networkx as nx
G = nx.binomial_graph(500, 0.3, seed=1234)
def bench(stmt, number=10):
return timeit.timeit(stmt, number=number, globals=globals())
for GC in [nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph]:
H = GC(G.edges)
print(f"{H.__class__.__name__}: for e in H.edges: H.edges[e]",
bench("for e in H.edges: H.edges[e]"))Results: on Results on |
|
Oops! My previous post had timings for the version of the PR with the length checks included. So the remaining nit I have is a request to use vs the current PR branch error: |
|
Good point, fixed! |
When debugging a KeyError in MultiDiGraph edges views, I got confused by the exception message refering to one specific node. For example:
That made me think that the node
0was not present and misdirected my debugging efforts.This PR standarizes
KeyErrormessages across views so this one now shows:This is what originally motivated #8533 but here I just focus on the logging part (as it was what orignally caused my confusion!).