Skip to content
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

Update mpl_draw() to fix multigraph plots #1204

Merged
merged 13 commits into from
Jun 10, 2024
Merged

Conversation

maxwell04-wq
Copy link
Contributor

Fixes #774.
Updates:

  • draw_edges(): set rad=0.25 for looped nodes.
  • draw_edge_labels(): Modify label offsets for looped nodes.

@coveralls
Copy link

coveralls commented May 31, 2024

Pull Request Test Coverage Report for Build 9448236772

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 8 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.04%) to 95.837%

Files with Coverage Reduction New Missed Lines %
rustworkx-core/src/generators/random_graph.rs 2 85.04%
src/shortest_path/all_pairs_bellman_ford.rs 6 95.53%
Totals Coverage Status
Change from base Build 9447555728: -0.04%
Covered Lines: 17380
Relevant Lines: 18135

💛 - Coveralls

@maxwell04-wq
Copy link
Contributor Author

@mtreinish one major hiccup in the implementation is that the position of the edges is calculated on the plot in the draw_edges() function and the position of the labels can only be evaluated relative to the position of the nodes and not the edges in the draw_edge_labels() function. While the current parameters for the curved edges of multigraphs work for plotting the labels, maybe there's a better way to evaluate the position of the labels.

Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to work on the loop condition. You'll also need to add a release note (https://github.com/Qiskit/rustworkx/blob/main/CONTRIBUTING.md#release-notes)

rustworkx/visualization/matplotlib.py Show resolved Hide resolved

reverse_edge = np.concatenate(([dst], [src]))
for edge in edge_pos: # the loop can be optimized
if bool(np.sum(np.all(np.equal(edge, reverse_edge)))):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are easier and simpler ways to write this. I don't quite understand why we are using NumPy for this check either

@maxwell04-wq
Copy link
Contributor Author

@IvanIsCoding I have not tried to align the labels vertically as, following the logic of rad in connectionstyle=arc3, simply offsetting the y coordinates of labels to rad generates a figure wherein the labels are perfectly placed on the edges.
test_fig
Would you suggest that I work on aligning the edge labels?

rustworkx/visualization/matplotlib.py Show resolved Hide resolved
# radius of edges
reverse_edge = [dst, src]
if (
len(np.where(np.all(edge_pos == reverse_edge, axis=(1, 2)))[0]) != 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this scales quadratically on the number of edges. O(E^2) is not ideal, if we plot a dense graph this is going to be a problem. You need to rewrite this condition using a set or something similar instead of naively looking through all the edges

Comment on lines 7 to 10
other:
- |
The radius of the edges of self-loops in multigraphs is set to `0.25`.
The edge labels are offset accordingly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the other section

- |
Fixes the plots of multigraphs using the `mpl_draw()` function. Refer to
`#12345 <https://github.com/Qiskit/rustworkx/issues/774>` for more
details.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The release notes would be better if you actually drew a multigraph with mpl_draw as an example. You can use

as an example, it becomes this: https://www.rustworkx.org/release_notes.html#new-features

@IvanIsCoding
Copy link
Collaborator

release_notes_2_0
It seems to be working, I will merge the PR

@IvanIsCoding IvanIsCoding added the automerge Queue a approved PR for merging label Jun 10, 2024
@IvanIsCoding IvanIsCoding merged commit 646057a into Qiskit:main Jun 10, 2024
28 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Queue a approved PR for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mpl_draw() does not work for multigraphs
3 participants