Repository navigation
ENH: adds ISMAGS support for directed and multigraph with tests and refactor - #8274
Conversation
…ore FIXME DiGraph
…ighten docs & comments; use Counter and list to good effect
…-- as indicated in the paper.
Peiffap
left a comment
There was a problem hiding this comment.
This looks great! It'll take some time for me to do a deep dive to understand what each part is doing here but in the meantime, here are some surface-level comments.
Co-authored-by: Gilles Peiffer <[email protected]>
rossbar
left a comment
There was a problem hiding this comment.
Thanks @dschult , this is great work! I've done an initial readthrough and had mostly a couple questions about mid-level details. The layout of the PR and in particular the PR summary are very helpful for motivating/providing context for the changes. I'd like to take another pass and spend more time absorbing some of the more detailed methods e.g. _map_nodes and _process_ordered_pair_partitions, but I'd need to beef up on ISMAGs itself before I'd be confident weighing in more generally. After my first pass I'm very impressed and excited about these improvements!
dschult
left a comment
There was a problem hiding this comment.
I've tried to address all comments either in the new commits or in the discussions.
split _refine_node_partition from _refine_opp add tests from referenced papers
|
I added a (maybe too long) description of the algorithm in the module's doc_string. If its too much into the weeds, we can turn it into comments -- or shift it to a doc_string of maybe the class object. I also dug into the Ordered-Pair-Paritition (Symmetry) DFS shortcuts. The ISMAGS paper has one fairly simple example and refers to a paper by Kategi, et al. which has more details. It turns out that we were not short-cutting some of the suggested short-cuts. I looked at the DFS nodes we are visiting and pruning and it all makes more sense to me now. Plus I was able to simplify some of the logic for pruning branches and add the missing cases (involving "Matching OPPs"). I hope that code is also more readable. I started getting into moving the OPP related methods to be defied near the process_OPP method, but decided moving methods around is a slippery slope and we should get this in, with a follow-up PR to reorganize the class structure, etc. Added tests from the papers. A good one is a graph with two distinct cycles (triangle and square). And I included the Petersen Graph since that is known to have many symmetries for not many nodes. Lots of misc tweaking here and there too. Hard to stop myself sometimes. But I think its all for the good. I will go through one more time later today for left-behind or commented out lines that should be removed. :) |
rossbar
left a comment
There was a problem hiding this comment.
Thanks for the updates @dschult , they look great to me!
I'm personally in favor of the giant explainer in the notes section - reading through it helped developed a more concrete idea of some of the key concepts like OPPs, cosets, etc. and how they relate to symmetries and DFS constraints. If we wanted to move the text elsewhere (e.g. the ISMAGS module docs might be a relevant place) that's fine too, but I'm +1 on making sure this text is available somewhere (and would vote to push off deciding where exactly to a followup PR).
I left a bunch of minor suggestions related to grammar/formatting which are generally inconsequential.
Re: the big picture, if you think this is in a state where it's ready to go in (i.e. there's no major changes you think need to be added - we can always continue tweaking) then I'd be +1. For this round I only focused on the changes in 65b133c, but would happy to go over the entire PR again if you think it's ready!
8c41162 to
fad3c2b
Compare
|
Thanks @rossbar for the review -- I've got another set of wording tweaks, varnames, doc_strings, comments. I also switched from using set operation I found a strange feature of I think this PR is ready to merge! In summary: |
a31de74 to
31a42da
Compare
rossbar
left a comment
There was a problem hiding this comment.
I had a chance to do one more look-over of the entire PR and everything LGTM! I noticed that there was one instance of the ISMAGS class docstring Attributes section having gotten out-of-date so I went ahead and removed the attributes which no longer exist1. This is a very nice refactor and provides nice new functionality - I'm excited to get this in users' hands!
On that note I'd like to get this into 3.6!
Footnotes
-
We could store the
edge_matchandnode_matchfunctions as attributes on the ISMAGS object if we wanted to, though with this refactor they're only used during problem setup so there's no reason to any longer ↩
|
I'm not sure I'll be able to go over this in depth until next week-end (Oct. 11-12) :(. I haven't forgotten about this PR though. |
There is no exact date, but NX typically does a release in October following the Python version bump. |
|
I worked through the rabbit hole of finding a case where refining partitions gives different size partitions in the same refinement. So the "assert" I put in this PR is warranted, and needs a fix. The example involves finding a case where an initial refinement passes just fine, but a refinement of the refined parts produces multiple lengths for the refined partitions it considers. The construction looks at an underlying graph with a 4-cycle and a separated edge. I used multiedges to make all nodes have degree 3. That gets them past the initial refinement, and then past the first layer of refinements inside the algorithm. Then to get inside the next layer of refinements, I stretch the graph by replacing each edge with a path (with one node along the path). That means looking at neighbors will not be enough to find new colors until we have refined more than once on the same part of the partition. My intuition there is that each refinement of the partition checks colors that have changed on the previous refinement -- essentially moving out from the initially changed node to its neighbors-neighbors-neighbors, etc. So long as parts keep getting split (new colors formed) the refinement continues. So stretching the edges into paths gets us into the inner layer of refinements. The example in a test causes the All areas of my concern have now been addressed. So any follow-up PRs can focus on code organization and alignment across methods. (I hope:). Let's wait for comments from @Peiffap unless the release comes up too quickly. |
Peiffap
left a comment
There was a problem hiding this comment.
This PR adds directed graph and multigraph support to the ISMAGS implementation. The core algorithmic improvement is a refactor from recursive to iterative implementations for three methods: _refine_node_partition, _map_nodes, and _process_ordered_pair_partitions. This eliminates the need for sortable nodes and resolves a bug in partition refinement.
The PR also adds (very) helpful and extensive documentation for the ISMAGS algorithm, as well as a ton of tests for the new behavior.
Globally, this looks good to me! I have a bunch of nits from my initial read-through, some questions about the algorithm, a suggestion to avoid a loop. The only thing I feel somewhat strongly about are the assertions (the two I flagged + a few others); I think those should raise instead.
I agree with Ross that this should go in before the release! This is 100% a net improvement to the ISMAGS code, and if there are any issues with it, the fastest way to find out is to let people try to break it :) Thanks Dan!
dschult
left a comment
There was a problem hiding this comment.
Thanks for these comments! I especially like the dict-comprehension, and the discussion about 2-tuple edges vs 2-sets of edges and how that affects items(). Also a good catch about assert statements. Those shouldn't be here. I hope to find an example test that causes the exception to be raised, but that will have to wait for another day. :)
Peiffap
left a comment
There was a problem hiding this comment.
The exception needs an nx. prefix, and I have a test for it if you want to add one.
Once that is fixed this is good to go in for me! Thanks Dan :)
|
I think we need to overhaul the match function system. It's too easy to make a funky match function that messes things up. This latest commit adds a check that the partitions made via a user's match function gets checked for 1) having every pair of nodes within each part match and 2) having no two nodes in different parts match. Perhaps that turns out not to be time instensive. Then we can leave it in -- and spread it to the other isomorphism algs. Otherwise lets create some good tools for folks to roll their own and check it! Or to restrict what they are allowed to do (e.g. we could require them to have a node/edge attribute indicating the group -- and then we only match the values of a specified attribute). Anyway, I think this solution here is overkill, but we can clean the marked unneeded part later -- and figure out how to handle matching more generally. Thanks @Peiffap ! |
With this PR, ISMAGS supports directed and multigraph inputs. If inputs do not have the same directed status an exception is raised. But multigraph and graph can be compared. So no restriction there.
Refactored methods
_refine_node_partition,_map_nodes, and_process_ordered_pair_partitionsto replace the recursive with a (DFS) queue approach. In addition, the ISMAGS class no longer requires that nodes be sortable.Corrected error while handling
candidate_setswhich is a dict-of-set-of-frozensets. For each node, we want to find the minimum length of the frozensets associated with that node. So we should usemin(len(x) for x in candidate_sets[n])rather thanmin(candidate_sets[n], key=len)which returns the smallest length frozenset. This ensures that the nodes with the fewest candidate mapping-image nodes are considered first, which in theory should return the first isomorphism faster. (Not a correctness issue -- just a performance hit.)In switching away from recursive methods, we somehow no longer need the bug-fix for ISMAGS. The bug arose for strange cases where symmetry was getting top/bottom partitions with different numbers of elements. I haven't chased down how that error occurred in the original code, but I suspect we were not respecting the node order or the ordering of partition list by the size of the parts. It doesn't affect the answer, but reduces the number of partitions to consider (the strange cases were aborted once detected).
This PR updates and adds tests especially DiGraph tests from VF, VF2, VF2pp.
Many comment changes, variable name changes and removal or renaming of helper functions. I also removed the manually provided cached properties in favor of just computing those attributes in the
__init__method (they would never change unless the graphs change which would violate other assumptions).Names: a "partition" is a list of "parts". Each "part" is a set of nodes.
We should only use the plural "partitions" when we have more than one partition.
I did not change the term "color" to "label" in this PR. I also did not move functions in or out of the class structure (to make a function oriented setup instead of methods).
I did not refactor the methods like
is_isomorphicto be functions. We can discuss those changes for the whole isomorphism subpackage separately.I thought about keeping the old code alongside the new, but given the needed correction and the bug-fix shortcut, I don't think we should retain that version. The Sandia folks mentioned that they saw some suspicious parts of the ismags code.
I did not use the Sandia python version of ISMAGS because it was clearly a translation of the original java code, and didn't feel right for NetworkX. It is also a command line tool used for graphs stored in a specific ascii file structure e.g. "XX00YX" to indicate which edges exist between, e.g., 3 nodes and their edge type/color/label.
Are there things I can do to make this easier to review? It's often hard to review the switch from recursive to non-recursive. And we've got 3 such conversions here. Should I split that from the handling of DiGraph changes? It might be worth reading the new ismags.py as its own new file rather than the diff, but I'm not sure. I will wait a week or two and then review it with fresh eyes if that helps.