Simplified PyTree Utilities
Utilities for working with PyTree
s.
The optree.pytree
namespace contains aliases of optree.tree_*
utilities.
>>> import optree.pytree as pytree
>>> tree = {'b': (2, [3, 4]), 'a': 1, 'c': None, 'd': 5}
>>> leaves, treespec = pytree.flatten(tree)
>>> leaves, treespec
(
[1, 2, 3, 4, 5],
PyTreeSpec({'a': *, 'b': (*, [*, *]), 'c': None, 'd': *})
)
>>> tree == pytree.unflatten(treespec, leaves)
True
Added in version 0.14.1.
Re-export PyTree Utilities as a Sub-module
|
Re-export a pytree utility module with the given namespace as default. |
- optree.pytree.reexport(*, namespace, module=None)[source]
Re-export a pytree utility module with the given namespace as default.
>>> import optree >>> pytree = optree.pytree.reexport(namespace='my-pkg', module='my_pkg.pytree') >>> pytree.flatten({'a': 1, 'b': 2}) ([1, 2], PyTreeSpec({'a': *, 'b': *}))
This function is useful for downstream libraries that want to re-export the pytree utilities with their own namespace:
# foo/__init__.py import optree pytree = optree.pytree.reexport(namespace='foo') del optree # foo/bar.py from foo import pytree @pytree.dataclasses.dataclass class Bar: a: int b: float # User code In [1]: import foo In [2]: foo.pytree.flatten({'a': 1, 'b': 2, 'c': foo.bar.Bar(3, 4.0)})) Out[2]: ( [1, 2, 3, 4.0], PyTreeSpec({'a': *, 'b': *, 'c': CustomTreeNode(Bar[()], [*, *])}, namespace='foo') ) In [3]: foo.pytree.functools.reduce(lambda x, y: x * y, {'a': 1, 'b': 2, 'c': foo.bar.Bar(3, 4.0)})) Out[3]: 24.0
Added in version 0.16.0.
- Parameters:
- Return type:
- Returns:
The re-exported module.
Tree Operations
Check section Tree Manipulation Functions and Tree Reduce Functions for more detailed documentation.
|
Context manager to temporarily set the dictionary sorting mode. |
|
Flatten a pytree. |
|
Flatten a pytree and additionally record the paths. |
|
Flatten a pytree and additionally record the accessors. |
|
Reconstruct a pytree from the treespec and the leaves. |
|
Get an iterator over the leaves of a pytree. |
|
Get the leaves of a pytree. |
|
Get the treespec for a pytree. |
|
Get the path entries to the leaves of a pytree. |
|
Get the accessors to the leaves of a pytree. |
|
Test whether the given object is a leaf node. |
|
Map a multi-input function over pytree args to produce a new pytree. |
|
Like |
|
Map a multi-input function over pytree args as well as the tree paths to produce a new pytree. |
|
Like |
|
Map a multi-input function over pytree args as well as the tree accessors to produce a new pytree. |
|
Like |
|
Replace |
|
Partition a tree into the left and right part by the given predicate function. |
|
Transform a tree having tree structure (outer, inner) into one having structure (inner, outer). |
|
Map a multi-input function over pytree args to produce a new pytree with transposed structure. |
|
Map a multi-input function over pytree args as well as the tree paths to produce a new pytree with transposed structure. |
|
Map a multi-input function over pytree args as well as the tree accessors to produce a new pytree with transposed structure. |
|
Return a pytree of same structure of |
|
Return two pytrees of common suffix structure of |
|
Map a multi-input function over pytree args to produce a new pytree. |
|
Map a multi-input function over pytree args as well as the tree paths to produce a new pytree. |
|
Map a multi-input function over pytree args as well as the tree accessors to produce a new pytree. |
|
Traversal through a pytree and reduce the leaves in left-to-right depth-first order. |
|
Sum |
|
Return the maximum leaf value in |
|
Return the minimum leaf value in |
|
Test whether all leaves in |
|
Test whether all leaves in |
|
Flatten the pytree one level, returning a 4-tuple of children, metadata, path entries, and an unflatten function. |
Node Registration
Check section PyTree Node Registration for more detailed documentation.
|
Extend the set of types that are considered internal nodes in pytrees. |
|
Extend the set of types that are considered internal nodes in pytrees. |
|
Remove a type from the pytree node registry. |