-
Notifications
You must be signed in to change notification settings - Fork 193
Add periodic
to hexagonal_lattice_graph
#1213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Pull Request Test Coverage Report for Build 9442230137Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will let people that made the feature request review the content, but overall the Rust code is looking pretty good! I left some minor comments
// Number of nodes in each vertical chain | ||
let rowlen = if periodic { | ||
2 * rows | ||
} else { | ||
// Note: in the non-periodic case the first and | ||
// last vertical chains have (2 * rows + 1) nodes | ||
2 * rows + 2 | ||
}; | ||
|
||
// Number of vertical chains | ||
let collen = if periodic { cols } else { cols + 1 }; | ||
|
||
let num_nodes = if periodic { | ||
rowlen * collen | ||
} else { | ||
rowlen * collen - 2 | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can rewrite all the variables with a single match on that condition using destructuring e.g. https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ef7020b44cc19135583d87a1dcf6d1b2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot to mention that I did this as well in 6497d44.
assert_eq!( | ||
expected_edges, | ||
g.edge_references() | ||
.map(|edge| (edge.source().index(), edge.target().index())) | ||
.collect::<Vec<(usize, usize)>>(), | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't assert the order of the edges in the generated list because that is something we reserve the right to change. You might want to compare the equality with a HashSet
or a sorted list though.
I am glad the hard-coded edge order didn't affect your PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback -- I ended up adding two functions to do this for directed and undirected graphs.
Can you leave a comment on #803 so we can assign the issue to you for unitaryhack? |
Pull Request Test Coverage Report for Build 9459717092Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Hi @mtreinish -- I can do that, though I'm not sure about closing the issue since the |
I tried adding the In Rust we started with this function
Coming from C++, I really wanted to change the argument I ended up adding a second function
and factoring out as much of the shared logic as possible into a builder object. This makes it possible to match the functionality in networkx, but seems like an awkward design and leads to some really messy code in |
Pull Request Test Coverage Report for Build 9567412491Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test cases look pretty strong and give me confidence that this code is correct.
There are some minor details like using the utils
module, leaving ToDo
s, we don't have a release note, etc. But in the grand scheme of things, this is a great addition I want to be in the 0.15 release
@@ -17,19 +17,200 @@ use petgraph::visit::{Data, NodeIndexable}; | |||
|
|||
use super::InvalidInputError; | |||
|
|||
mod utils { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really think we need this module, it could be include with the rest of the code. But that is a minor detail for now
"""In a periodic hexagonal lattice, all nodes must have | ||
degree 3 (idea copied from the networkx test suite).""" | ||
for nRows in range(2, 8): | ||
for nCols in range(2, 8, 2): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be subtests but it's something we can fix later
lattice graph, so here we check that the results from rustworkx and | ||
networkx are isomorphic for a few cases.""" | ||
for nRows in range(2, 8): | ||
for nCols in range(2, 8, 2): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be subtests but it's something we can fix later
Pull Request Test Coverage Report for Build 9672159077Details
💛 - Coveralls |
Thanks for approving the PR -- should I open another issue for the remaining ToDos or just open a second PR once I get to them? |
Sure, you can make a second PR if you want to clean up the code |
* use closure for `graph.add_edge` calls * Add periodicity * check input, adjust Python exception type * add Rust tests * Update type annotations, add Python tests * Test for isomorphism with networkx graphs * make tests insensitive to edge order * move construction of graph and nodes into a builder * move construction of edges into builder * add `hexagonal_lattice_graph_weighted`, update tests * add `with_positions` argument to Python functions * add visualization tests, fix lattice position bug --------- Co-authored-by: Ivan Carvalho <8753214+IvanIsCoding@users.noreply.github.com>
Implements one of the features needed to close #803. I think this is complementary to PR #1207, which looks like it starts implementing the other part (
with_positions
).Rust changes:
graph.add_edge
into a closure to make the code more readableperiodic
argument. The comments on Add support tohexagonal_lattice_graph
for periodcity and position coordinates #803 suggest implementing this by following the logic in networkx, which constructs a non-periodic graph and then contracts pairs of nodes on the lattice boundaries. I chose to instead create a smaller number of nodes up front.Python changes: