Add to_matrix method to DataGraph - #430
Merged
Merged
Conversation
Returns the graph as a real symmetric matrix, the inverse of from_matrix: node weights go on the diagonal (since self-loops are not supported) and edge weights fill the off-diagonal entries, defaulting to 1.0 for edges without an explicit weight. Closes #429
|
weight or default treated an explicit 0.0 weight as unset, and indexing the matrix directly by node label assumed labels were exactly 0..N-1, breaking on string labels or non-contiguous integers.
matrix[idx] with a (3, 2) index array fancy-indexes whole rows, not individual (row, col) entries, collapsing the n_nodes=3 case into an all-zero matrix. Index rows/cols separately instead.
alessandro-santini
previously approved these changes
Aug 5, 2026
alessandro-santini
left a comment
Collaborator
There was a problem hiding this comment.
Seems fine to me! thanks!
alessandro-santini
approved these changes
Aug 5, 2026
alessandro-santini
left a comment
Collaborator
There was a problem hiding this comment.
fine for me thanks!
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.
Summary
DataGraph.to_matrix(), the inverse of the existingfrom_matrixclassmethod: returns a real symmetric square matrix with node weights on the diagonal (0.0 if unset, since self-loops aren't supported) and edge weights off-diagonal (defaulting to 1.0 for edges without an explicit weight).0..N-1followingself.nodesinsertion order (not label value), so it also works for non-integer or non-contiguous node labels.Known follow-up
DataGraph._edge_weightsisn't refreshed when edges are added after construction viaadd_edges_from/add_edge(only_reset_dicts()populates it, and that only runs inside the classmethod constructors). This doesn't affectto_matrixitself, but it's a pre-existing gap in the graph, tracked separately in edge_weights does not reflect edges added after construction #431.Tests
Noneweights, and ato_matrix/from_matrixround trip.Closes #429
Alternatives
networkx.adjacency_matrixis a good ready-made alternative but we cannot use it because of bug #431