Added RG tree API and visualization support#9
Open
Alphaharrius wants to merge 1 commit into
Open
Conversation
Introduce qrg.tree.Node as a first-class tree abstraction for renormalization
group workflows, with a coherent set of APIs for growth, navigation,
addressing, structural inspection, subtree manipulation, transform access,
and visualization.
This commit defines the core tree model around parent/child structure and
leaf-key addressing, and adds the main operational interfaces needed to work
with RG trees in practice: branches(), leaf(), parent(), root(), is_root(),
is_leaf(), path(), trace(), cut(), find(), and get_transform(). Together
these make the tree usable as a navigable and queryable computational object
rather than only a storage container for tensors.
The plotting layer is also established here, with tree rendering backends for
networkx, matplotlib, and plotly. The implementation supports both structural
graph extraction and direct visual inspection of RG trees, including default
tree rendering behavior and backend-specific output.
Top-level package exports and dependency metadata are updated so the tree API
is part of the public package surface and its plotting backends are declared
explicitly. Tests, typing, lint, and formatting are brought into alignment
with the expanded API.
Add a real Plotly tree backend while preserving matplotlib as the default
for plot("tree"), and update tests to cover tree traversal, search,
branch detachment, path/trace round-tripping, Plotly rendering, and
transform composition behavior.
Document get_transform() with a warning that inverse/backward use can
create very large intermediates, especially for momentum-block tensors,
and recommend staged application via composed=False for actual transport
workflows.
There was a problem hiding this comment.
Pull request overview
This PR introduces a foundational qrg.tree.Node API for RG tree construction, navigation, subtree operations, transform retrieval, and visualization, while exposing it at the package top level.
Changes:
- Adds the new
Nodetree abstraction with growth, traversal, mutation, search, transform, and plotting support. - Adds plotting dependencies and typed-package metadata.
- Adds a comprehensive tree test suite and updates ignore rules.
Reviewed changes
Copilot reviewed 4 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/qrg/tree.py |
Implements the new tree API and visualization backends. |
src/qrg/__init__.py |
Exposes Node as a top-level package export. |
src/qrg/py.typed |
Marks the package as typed. |
pyproject.toml |
Adds plotting dependencies. |
tests/test_tree.py |
Adds coverage for tree behavior and plotting. |
.gitignore |
Ignores notebooks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+890
to
+893
| new_leaves = { | ||
| target: _LeavePath(node=node, transform=node[node._target]) | ||
| for target, node in new_nodes.items() | ||
| } |
| """ | ||
| if name not in self._leaves: | ||
| raise ValueError(f"Target {name} not found in leaves of node {self.name}") | ||
| leaf = self._leaves.pop(name) |
| target: _LeavePath(node=node, transform=node[node._target]) | ||
| for target, node in new_nodes.items() | ||
| } | ||
| for leaf in new_leaves.values(): |
| resolved = parts[:index] | ||
| unresolved = parts[index + 1 :] | ||
| highlighted = ".".join(resolved + [f"[{part}]"] + unresolved) | ||
| raise ValueError(f'trace cannot be resolve at "{highlighted}"') |
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.
See #4
Summary
This change introduces
qrg.tree.Nodeas a first-class tree abstraction forrenormalization group workflows and establishes the initial public tree API for
growth, navigation, structural inspection, path-based addressing, subtree
manipulation, transform access, and visualization.
The commit is foundational rather than incremental. It creates the tree module,
adds the package export surface, declares the plotting dependencies needed by
the new backends, adds typed-package metadata, and lands a large test suite
covering the new behavior.
Motivation
Before this change, the package did not expose a dedicated RG tree abstraction
that could:
This commit establishes that base layer so higher-level RG workflows can be
implemented against a coherent tree API instead of ad hoc data structures.
Main Changes
1. Introduce
qrg.tree.NodeAdd a new
Nodetype insrc/qrg/tree.pywith:grow(),_LeavePath,2. Add foundational tree APIs
The new tree API includes:
register_method()register_methods()growth()grow()target()compute()leaf()branches()parent()root()is_root()is_leaf()trace()path()cut()find()get_transform()These APIs make the tree usable as an operational RG object, not just a
container for tensors.
3. Add tree visualization backends
The plotting layer now supports tree rendering through:
networkxmatplotlibplotlyImportant behavior:
plot("tree")defaults to thematplotlibbackend.plot("tree", backend="networkx")returns a structural graph view.plot("tree", backend="plotly")returns a real Plotly figure rather than asilent redirect.
4. Expose
Nodeat package top levelUpdate
src/qrg/__init__.pyso users can write:instead of importing only from
qrg.tree.5. Declare dependencies and typing metadata
Update project metadata to reflect the new plotting support:
matplotlibplotlyAlso add:
src/qrg/py.typedso mypy treats
qrgas a typed package.6. Add comprehensive tests
Introduce
tests/test_tree.pycovering:cut()behavior,branches(),is_root(),is_leaf(),trace()andpath()round-tripping,find(),get_transform(),networkx,matplotlib, andplotlytree plotting behavior.Design Notes
Addressing model
The tree uses parent leaf keys, not
node.name, as the canonical structuraladdress. This is why:
trace()consumes dot-delimited leaf-key paths,path()returns leaf-key paths,root.trace(node.path()) is nodeis a valid round-trip.Transform semantics
get_transform()can return either:composed=False.The docstring explicitly warns that inverse/backward use may create very large
intermediates, especially for momentum-block tensors. The intended safer usage
for actual transport workflows is staged application via the stepwise list.
Plotting choice
matplotlibremains the default forplot("tree")to preserve a stable,always-available static rendering path.
plotlyis supported explicitly ratherthan through a misleading fallback.
Files Changed
.gitignorepyproject.tomlsrc/qrg/__init__.pysrc/qrg/py.typedsrc/qrg/tree.pytests/test_tree.pyVerification
The change was verified with:
Final status at the end of the work:
ruff check: passedmypy src: passedmypy tests: passedtox: passedReviewer Notes
Reviewers should treat this as a foundational API introduction, not a narrow
follow-up patch. The most important review points are:
get_transform()exposes the correct exact semantics,