Skip to content

Add to_matrix method to DataGraph - #430

Merged
sgrava merged 6 commits into
mainfrom
sg/429-add-to-matrix-method-in-datagraph
Aug 5, 2026
Merged

sgrava merged 6 commits into
mainfrom
sg/429-add-to-matrix-method-in-datagraph

Conversation

@sgrava

@sgrava sgrava commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds DataGraph.to_matrix(), the inverse of the existing from_matrix classmethod: 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).
  • Nodes are mapped to matrix indices 0..N-1 following self.nodes insertion order (not label value), so it also works for non-integer or non-contiguous node labels.

Known follow-up

  • While testing, found that DataGraph._edge_weights isn't refreshed when edges are added after construction via add_edges_from/add_edge (only _reset_dicts() populates it, and that only runs inside the classmethod constructors). This doesn't affect to_matrix itself, but it's a pre-existing gap in the graph, tracked separately in edge_weights does not reflect edges added after construction #431.

Tests

  • Unit tests for unweighted graphs, weighted graphs, custom/non-integer node labels with None weights, and a to_matrix/from_matrix round trip.

Closes #429

Alternatives

networkx.adjacency_matrix is a good ready-made alternative but we cannot use it because of bug #431

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
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-08-05 15:33 UTC

@sgrava sgrava mentioned this pull request Aug 5, 2026
6 tasks
sgrava added 3 commits August 5, 2026 09:10
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.
@sgrava
sgrava marked this pull request as ready for review August 5, 2026 09:05
@sgrava sgrava added this to the v1.3.0 milestone Aug 5, 2026
@sgrava sgrava added the feature New feature or request label Aug 5, 2026
Comment thread qoolqit/graphs/data_graph.py Outdated

@alessandro-santini alessandro-santini left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems fine to me! thanks!

@alessandro-santini alessandro-santini left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

fine for me thanks!

@sgrava
sgrava merged commit c96e3df into main Aug 5, 2026
16 checks passed
@sgrava
sgrava deleted the sg/429-add-to-matrix-method-in-datagraph branch August 5, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add to_matrix method in DataGraph

2 participants