https://github.com/opensourceeconomics/pybaum
Tools to work with pytrees.
https://github.com/opensourceeconomics/pybaum
Last synced: 8 months ago
JSON representation
Tools to work with pytrees.
- Host: GitHub
- URL: https://github.com/opensourceeconomics/pybaum
- Owner: OpenSourceEconomics
- License: mit
- Created: 2022-01-24T17:23:11.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-10-20T20:39:36.000Z (8 months ago)
- Last Synced: 2025-10-21T20:56:10.731Z (8 months ago)
- Language: Python
- Homepage: https://pybaum.readthedocs.io
- Size: 35.2 KB
- Stars: 18
- Watchers: 1
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES.rst
- License: LICENSE
Awesome Lists containing this project
README
pybaum
======
.. start-badges
.. image:: https://img.shields.io/pypi/v/pybaum?color=blue
:alt: PyPI
:target: https://pypi.org/project/pybaum
.. image:: https://img.shields.io/pypi/pyversions/pybaum
:alt: PyPI - Python Version
:target: https://pypi.org/project/pybaum
.. image:: https://img.shields.io/conda/vn/conda-forge/pybaum.svg
:target: https://anaconda.org/conda-forge/pybaum
.. image:: https://img.shields.io/conda/pn/conda-forge/pybaum.svg
:target: https://anaconda.org/conda-forge/pybaum
.. image:: https://img.shields.io/pypi/l/pybaum
:alt: PyPI - License
:target: https://pypi.org/project/pybaum
.. image:: https://readthedocs.org/projects/pybaum/badge/?version=latest
:target: https://pybaum.readthedocs.io/en/latest
.. image:: https://img.shields.io/github/actions/workflow/status/OpenSourceEconomics/pybaum/main.yml?branch=main
:target: https://github.com/OpenSourceEconomics/pybaum/actions?query=branch%3Amain
.. image:: https://codecov.io/gh/OpenSourceEconomics/pybaum/branch/main/graph/badge.svg
:target: https://codecov.io/gh/OpenSourceEconomics/pybaum
.. image:: https://results.pre-commit.ci/badge/github/OpenSourceEconomics/pybaum/main.svg
:target: https://results.pre-commit.ci/latest/github/OpenSourceEconomics/pybaum/main
:alt: pre-commit.ci status
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
.. end-badges
Installation
------------
pybaum is available on `PyPI `_ and `Anaconda.org
`_. Install it with
.. code-block:: console
$ pip install pybaum
# or
$ conda install -c conda-forge pybaum
About
-----
pybaum provides tools to work with pytrees which is a concept borrowed from `JAX
`_.
What is a pytree?
In pybaum, we use the term pytree to refer to a tree-like structure built out of
container-like Python objects. Classes are considered container-like if they are in the
pytree registry, which by default includes lists, tuples, and dicts. That is:
1. Any object whose type is not in the pytree container registry is considered a leaf
pytree.
2. Any object whose type is in the pytree container registry, and which contains
pytrees, is considered a pytree.
For each entry in the pytree container registry, a container-like type is registered
with a pair of functions that specify how to convert an instance of the container type
to a (children, metadata) pair and how to convert such a pair back to an instance of the
container type. Using these functions, pybaum can canonicalize any tree of registered
container objects into tuples.
Example pytrees:
.. code-block:: python
[1, "a", object()] # 3 leaves
(1, (2, 3), ()) # 3 leaves
[1, {"k1": 2, "k2": (3, 4)}, 5] # 5 leaves
pybaum can be extended to consider other container types as pytrees.