Conversation
…edges_from
`BaseGraph._reset_dicts` seeds `_edge_weights` (and `_node_weights`) from
the graph's nodes/edges only at construction time. `add_edges_from` is
inherited directly from `nx.Graph` and bypasses `_reset_dicts`, so edges
(and any nodes they implicitly create) added after construction were
silently missing from `edge_weights`, with no error or warning:
g = DataGraph.from_nodes([0, 1, 2])
g.add_edges_from([(0, 1), (1, 2)])
print(g.edge_weights) # {} -- expected {(0, 1): None, (1, 2): None}
Override `add_edges_from` on `BaseGraph` to call through to NetworkX and
then seed any newly-added node/edge with a weight/coordinate of None
(matching `_reset_dicts`'s own convention), leaving existing weights
untouched. `DataGraph` inherits this fix automatically since it does not
override `add_edges_from` itself.
Also removes the `_reset_dicts()` workaround (with its `# FIXME: ... see
issue pasqal-io#431` comment) that `test_to_matrix_unweighted` needed to route
around the bug, and adds a dedicated regression test covering: the
exact case from the issue, that existing weights survive a later
`add_edges_from` call, and that edges introducing brand-new nodes extend
`node_weights`/`coords` for those nodes too (instead of leaving them
missing, which would otherwise surface later as a KeyError).
Fixes pasqal-io#431
Collaborator
|
Hi @agu2347 , If you wish to contribute to the library, please consider other open issues. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
BaseGraph._reset_dicts()seeds_edge_weights(and_node_weights) from the graph's current nodes/edges, but is only ever called at construction time (or explicitly, by the alternative constructors).add_edges_fromis inherited directly fromnx.Graphand never routes through it, so edges (and any nodes they implicitly create) added after construction are silently missing fromedge_weights, with no error or warning:This repo's own test suite was already routing around the bug --
test_to_matrix_unweightedcalledgraph._reset_dicts()right afteradd_edges_fromwith a# FIXME: ... see issue #431comment above it.Fix
Override
add_edges_fromonBaseGraph(inherited byDataGraph, which doesn't define its own): call through tonx.Graph.add_edges_from, then seed any node/edge that's genuinely new with a weight/coordinate ofNone, matching_reset_dicts()'s own convention. Existing weights are left untouched, so this can't clobberedge_weights/node_weightsset explicitly after construction.New nodes are also handled (not just edges), since
add_edges_fromcan implicitly introduce nodes that don't exist yet -- leaving those out of_node_weights/_coordswould surface later as aKeyErrorin unrelated code paths (e.g.to_matrix(),distances()).Testing
_reset_dicts()workaround (and its# FIXME) fromtest_to_matrix_unweighted.test_add_edges_from_updates_weights_and_coords, covering: the exact case from the issue, that existing weights survive a lateradd_edges_fromcall, and that edges introducing brand-new nodes extendnode_weights/coordsfor those nodes too.test_base_graph.pysuite (47 tests): all pass except one pre-existing, unrelated failure (test_from_nx_wrong_pos_attr, a&vsandshort-circuit bug infrom_nx's validation, confirmed present onmainbefore this change too).base_graph.pychange makes the new regression test fail with the exact symptom from the issue (edge_weights == {}instead of{(0, 1): None, (1, 2): None}).ruff check/ruff format --checkclean on both changed files.Fixes #431