|
1 | 1 | pybaum |
2 | 2 | ====== |
3 | 3 |
|
4 | | -Tools to work with pytrees. |
| 4 | +.. start-badges |
| 5 | +
|
| 6 | +.. image:: https://img.shields.io/pypi/v/pybaum?color=blue |
| 7 | + :alt: PyPI |
| 8 | + :target: https://pypi.org/project/pybaum |
| 9 | + |
| 10 | +.. image:: https://img.shields.io/pypi/pyversions/pybaum |
| 11 | + :alt: PyPI - Python Version |
| 12 | + :target: https://pypi.org/project/pybaum |
| 13 | + |
| 14 | +.. image:: https://img.shields.io/conda/vn/conda-forge/pybaum.svg |
| 15 | + :target: https://anaconda.org/conda-forge/pybaum |
| 16 | + |
| 17 | +.. image:: https://img.shields.io/conda/pn/conda-forge/pybaum.svg |
| 18 | + :target: https://anaconda.org/conda-forge/pybaum |
| 19 | + |
| 20 | +.. image:: https://img.shields.io/pypi/l/pybaum |
| 21 | + :alt: PyPI - License |
| 22 | + :target: https://pypi.org/project/pybaum |
| 23 | + |
| 24 | +.. image:: https://readthedocs.org/projects/pybaum/badge/?version=latest |
| 25 | + :target: https://pybaum.readthedocs.io/en/latest |
| 26 | + |
| 27 | +.. image:: https://img.shields.io/github/workflow/status/OpenSourceEconomics/pybaum/main/main |
| 28 | + :target: https://github.com/OpenSourceEconomics/pybaum/actions?query=branch%3Amain |
| 29 | + |
| 30 | +.. image:: https://codecov.io/gh/OpenSourceEconomics/pybaum/branch/main/graph/badge.svg |
| 31 | + :target: https://codecov.io/gh/OpenSourceEconomics/pybaum |
| 32 | + |
| 33 | +.. image:: https://results.pre-commit.ci/badge/github/OpenSourceEconomics/pybaum/main.svg |
| 34 | + :target: https://results.pre-commit.ci/latest/github/OpenSourceEconomics/pybaum/main |
| 35 | + :alt: pre-commit.ci status |
| 36 | + |
| 37 | +.. image:: https://img.shields.io/badge/code%20style-black-000000.svg |
| 38 | + :target: https://github.com/psf/black |
| 39 | + |
| 40 | +.. end-badges |
| 41 | +
|
| 42 | +Installation |
| 43 | +------------ |
| 44 | + |
| 45 | +pybaum is available on `PyPI <https://pypi.org/project/pybaum>`_ and `Anaconda.org |
| 46 | +<https://anaconda.org/conda-forge/pybaum>`_. Install it with |
| 47 | + |
| 48 | +.. code-block:: console |
| 49 | +
|
| 50 | + $ pip install pybaum |
| 51 | +
|
| 52 | + # or |
| 53 | +
|
| 54 | + $ conda install -c conda-forge pybaum |
| 55 | +
|
| 56 | +
|
| 57 | +About |
| 58 | +----- |
| 59 | + |
| 60 | +pybaum provides tools to work with pytrees which is a concept burrowed from `JAX |
| 61 | +<https://jax.readthedocs.io/en/latest/>`_. |
| 62 | + |
| 63 | +What is a pytree? |
| 64 | + |
| 65 | +In pybaum, we use the term pytree to refer to a tree-like structure built out of |
| 66 | +container-like Python objects. Classes are considered container-like if they are in the |
| 67 | +pytree registry, which by default includes lists, tuples, and dicts. That is: |
| 68 | + |
| 69 | +1. Any object whose type is not in the pytree container registry is considered a leaf |
| 70 | + pytree. |
| 71 | + |
| 72 | +2. Any object whose type is in the pytree container registry, and which contains |
| 73 | + pytrees, is considered a pytree. |
| 74 | + |
| 75 | +For each entry in the pytree container registry, a container-like type is registered |
| 76 | +with a pair of functions that specify how to convert an instance of the container type |
| 77 | +to a (children, metadata) pair and how to convert such a pair back to an instance of the |
| 78 | +container type. Using these functions, pybaum can canonicalize any tree of registered |
| 79 | +container objects into tuples. |
| 80 | + |
| 81 | +Example pytrees: |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + [1, "a", object()] # 3 leaves |
| 86 | +
|
| 87 | + (1, (2, 3), ()) # 3 leaves |
| 88 | +
|
| 89 | + [1, {"k1": 2, "k2": (3, 4)}, 5] # 5 leaves |
| 90 | +
|
| 91 | +pybaum can be extended to consider other container types as pytrees. |
0 commit comments