Skip to content

feat: Add NodeDict collection for named node mappings#80

Merged
ConnorStoneAstro merged 12 commits intodevfrom
copilot/add-dictionary-of-nodes
Mar 5, 2026
Merged

feat: Add NodeDict collection for named node mappings#80
ConnorStoneAstro merged 12 commits intodevfrom
copilot/add-dictionary-of-nodes

Conversation

Copy link

Copilot AI commented Mar 3, 2026

  • Add NodeDict class to src/caskade/collection.py
  • Export NodeDict from src/caskade/__init__.py
  • Update Module.__setattr__ to auto-convert plain dicts of nodes to NodeDict
  • Add tests for NodeDict to tests/test_collection.py
  • Add test coverage for dynamic property
  • Add test coverage for update with kwargs
  • Add Node Collections section to docs/source/notebooks/AdvancedGuide.ipynb
  • Wrap all NodeList and NodeDict mutation methods in try/finally so _link_nodes() is always called even when an error occurs
Original prompt

This section details on the original issue you should resolve

<issue_title>feat: dictionary of nodes collection</issue_title>
<issue_description>Currently have list and tuple of nodes. A dictionary of nodes would also be potentially useful</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: ConnorStoneAstro <78555321+ConnorStoneAstro@users.noreply.github.com>
Copilot AI changed the title [WIP] Add dictionary of nodes collection feat: Add NodeDict collection for named node mappings Mar 3, 2026
@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e075e4b) to head (6ee3af7).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff            @@
##               dev       #80   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           13        13           
  Lines         1612      1688   +76     
=========================================
+ Hits          1612      1688   +76     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ConnorStoneAstro
Copy link
Owner

@copilot can you add unit tests for the dynamic property and for "update" with kwargs. Looks like those cases aren't covered.

Co-authored-by: ConnorStoneAstro <78555321+ConnorStoneAstro@users.noreply.github.com>
Copy link
Author

Copilot AI commented Mar 4, 2026

@copilot can you add unit tests for the dynamic property and for "update" with kwargs. Looks like those cases aren't covered.

Added in 9b605d4. test_node_dict_manipulation now includes:

  • a dynamic/static property check (static after to_static, dynamic after to_dynamic)
  • an update with kwargs test (passes a kwarg mkw=Module("mkw") and asserts the key is linked correctly)

@ConnorStoneAstro
Copy link
Owner

@copilot can you add a section to the advanced tutorial for the NodeTuple, NodeList and NodeDict collections? It should just demo basic behaviour while adding some commentary on what these are useful for. The use cases include creating subsets of params for Gibbs sampling to make dynamic/static, picking out a list of params of interest to single them out for saving/updating, to select collections of modules/params for quick printing, etc. Should mention that collections of nodes assigned to a Module will automatically be turned into an appropriate node collection. Should also mention that the get_values and set_values methods work the same for collections as they do for Modules.

Co-authored-by: ConnorStoneAstro <78555321+ConnorStoneAstro@users.noreply.github.com>
Copy link
Author

Copilot AI commented Mar 4, 2026

@copilot can you add a section to the advanced tutorial for the NodeTuple, NodeList and NodeDict collections? It should just demo basic behaviour while adding some commentary on what...

Added in b6f81d9. The new "Node Collections: NodeList, NodeTuple, and NodeDict" section in AdvancedGuide.ipynb (inserted after "Param groups") includes:

  • An intro explaining the three types and when to use them
  • A note on auto-conversion when assigned to a Module attribute
  • Enumerated use-cases (Gibbs sampling, selective saving/updating, quick inspection)
  • Four code cells demonstrating:
    1. Direct construction and attribute access
    2. Auto-conversion inside a Module (listNodeList, dictNodeDict) with graphviz display
    3. get_values / set_values on a collection subset
    4. Gibbs-style Param toggling (group_A.to_static() / group_B.to_dynamic())

@ConnorStoneAstro ConnorStoneAstro marked this pull request as ready for review March 4, 2026 04:33
Copilot AI review requested due to automatic review settings March 4, 2026 04:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a NodeDict collection type to the caskade library, filling the gap between the existing NodeList (mutable, ordered) and NodeTuple (immutable, ordered) collections with a mutable, key-indexed (str → Node) collection that integrates fully into the caskade DAG. It also auto-promotes plain Python dict assignments on Module attributes to NodeDict, consistent with existing list→NodeList / tuple→NodeTuple promotion.

Changes:

  • NodeDict implementation in collection.py: a dict subclass of NodeCollection with full mutation support and automatic link/unlink cycle management.
  • Auto-promotion in module.py: Module.__setattr__ now converts plain dict of nodes to NodeDict.
  • Tests and docs: comprehensive tests in test_collection.py and a new "Node Collections" section in AdvancedGuide.ipynb.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/caskade/collection.py Adds the NodeDict class with all mutation methods
src/caskade/module.py Extends __setattr__ to auto-convert dict of nodes to NodeDict
src/caskade/__init__.py Exports NodeDict as part of the public API
tests/test_collection.py Tests for creation, mutation, param values, and module auto-conversion
docs/source/notebooks/AdvancedGuide.ipynb New "Node Collections" section with usage examples
.gitignore Adds safety-net entries for graphviz intermediate test files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@ConnorStoneAstro ConnorStoneAstro changed the base branch from main to dev March 4, 2026 20:43
ConnorStoneAstro and others added 4 commits March 4, 2026 16:22
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@ConnorStoneAstro
Copy link
Owner

@copilot the review pointed out that if there is an error during one of the operations on the collection (i.e. update for the NodeDict) then the collection will be left in a invalid state since the nodes will not be re-linked together. Could you use a try - finally block to ensure that after whatever happens with the collection operation, the nodes will be re-linked to keep the state valid? Please make this change for all the collection objects as it affects each of them.

…sure re-linking

Co-authored-by: ConnorStoneAstro <78555321+ConnorStoneAstro@users.noreply.github.com>
Copy link
Author

Copilot AI commented Mar 4, 2026

@copilot the review pointed out that if there is an error during one of the operations on the collection (i.e. update for the NodeDict) then the collection will be left in a invalid state since the ...

Done in a87467e. Every mutating method in NodeList (append, insert, extend, clear, pop, remove, __setitem__, __delitem__, __iadd__) and NodeDict (__setitem__, __delitem__, update, pop) now wraps the raw data-structure call in a try/finally block, so _link_nodes() is always called regardless of whether the operation raises.

@ConnorStoneAstro ConnorStoneAstro merged commit 55c32e9 into dev Mar 5, 2026
32 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: dictionary of nodes collection

3 participants