`TreeValue` is a generalized tree-based data structure mainly developed by [OpenDILab Contributors](https://github.com/opendilab).
Almost all the operation can be supported in form of trees in a convenient way to simplify the structure processing when the calculation is tree-based.
Almost all the operations can be supported in the form of trees in a convenient way to simplify the structure processing when the calculation is tree-based.
## Outline
*[Overview](#overview)
*[Getting Started](#getting-started)
*[Prerequisite](#prerequisite)
*[Installation](#installation)
*[Quick Usage](#quick-usage)
*[Tutorials](#tutorials)
*[External](#external)
*[Speed Performance](#speed-performance)
*[Change Log](#change-log)
*[Feedback and Contribute](#feedback-and-contribute)
*[Citation](#citation)
*[License](#license)
## Overview
When we build a complex nested structure, we need to model it as a tree structure, and the native list and dict in Python are often used to solve this problem. However, it takes a lot of codes and some complex and non-intuitive calculation logic, which is not easy to modify and extend related code and data, and parallelization is impossible.
Therefore, we need a kind of more proper data container, named `TreeValue`. It is designed for solving the following problems:
-**Ease of Use**: When the existing operations are applied to tree structures such as dict, they will become completely unrecognizable, with really low readability and maintainability.
-**Diversity of Data**: In the tree structure operation, various abnormal conditions (structure mismatch, missing key-value, type mismatch, etc.) occur from time to time, and the code will be more complicated if it needs to be handled properly.
-**Scalability and Parallelization**: When any multivariate operation is performed, the calculation logic needs to be redesigned under the native Python code implementation, and the processing will be more complicated and confusing, and the code quality is difficult to control.
## Getting Started
## Installation
### Prerequisite
`treevalue` has been fully tested in the Linux, macOS and Windows environments and with multiple Python versions, and it works properly on all these platforms.
However, **`treevalue` currently does not support PyPy**, so just pay attention to this when using it.
### Installation
You can simply install it with `pip` command line from the official PyPI site.
...
...
@@ -32,15 +70,22 @@ You can simply install it with `pip` command line from the official PyPI site.
pip install treevalue
```
For more information about installation, you can refer to the [installation guide](https://opendilab.github.io/treevalue/main/tutorials/installation/index.html).
For more information about installation, you can refer to the [installation guide](https://opendilab.github.io/treevalue/main/tutorials/installation/index.html).
The detailed documentation are hosted on [https://opendilab.github.io/treevalue](https://opendilab.github.io/treevalue/).
After this, you can check if the installation is processed properly with the following code
Only english version is provided now, the chinese documentation is still under development.
```python
fromtreevalueimport__version__
print('TreeValue version is',__version__)
```
## Quick Start
### Quick Usage
You can easily create a tree value object based on `FastTreeValue`.
*[Create a Tree](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#create-a-tree)
*[Edit a Tree](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#edit-the-tree)
*[Do Index or Slice Calculation on The Tree](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#do-index-or-slice-calculation-on-the-tree)
*[Do Math Calculation on The Tree](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#do-calculation-on-the-tree)
*[Make Function Tree-Supported](https://opendilab.github.io/treevalue/main/tutorials/basic_usage/index.html#make-function-tree-supported)
*[Costumize My TreeValue Class](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#diy-treevalue-class)
*[Visualization of TreeValue](https://opendilab.github.io/treevalue/main/tutorials/advanced_usage/index.html#draw-graph-for-treevalue)
### External
We provide an official treevalue-based-wrapper for numpy and torch called [DI-treetensor](https://github.com/opendilab/DI-treetensor) since the `treevalue` is often used with libraries like `numpy` and `torch`. It will actually be helpful while working with AI fields.
## Speed Performance
Here is the speed performance of all the operations in `FastTreeValue`, the following table is the performance comparison result with [dm-tree](https://github.com/deepmind/tree).
Here is the speed performance of all the operations in `FastTreeValue`; the following table is the performance comparison result with [dm-tree](https://github.com/deepmind/tree).
And this is the comparison between tianshou Batch and us, with `cat` , `stack` and `split` operations (**lower value means less time cost and runs faster**)
And this is the comparison between Tianshou Batch and us, with `cat` , `stack` and `split` operations (**lower value means less time cost and runs faster**)
![Time cost of cat operation](docs/source/_static/Time%20cost%20of%20cat%20operation.svg)
...
...
@@ -197,36 +270,62 @@ Test benchmark code can be found here:
*[Comparison with jax-libtree](https://github.com/opendilab/treevalue/blob/main/test/compare/jax/test_jax.py)
*[Comparison with tianshou Batch](https://github.com/opendilab/treevalue/blob/main/test/compare/tianshou/test_tianshou_batch.py)
## Extension
## Change Log
If you need to translate `treevalue` object to runnable source code, you may use the [potc-treevalue](https://github.com/potc-dev/potc-treevalue) plugin with the installation command below
<details><summary><b>Version History</b><i>[click to expand]</i></summary>
<div>
```shell
pip install potc-treevalue
```
* 2022-05-03
1.3.1: Change definition of getitem, setitem and delitem; add pop method for TreeValue class.
* 2022-03-15
1.3.0: Add getitem, setitem and delitem for adding, editing and removing items in TreeValue class.
* 2022-02-22
1.2.2: Optimize union function; add walk utility method.
* 2022-01-26
1.2.1: Update tree printing; add keys, values, items on TreeValue; add comparision to facebook nest library.
* 2022-01-04
1.2.0: Add flatten_values and flatten_keys; fix problem in mapping function; add support for potc.
* 2021-12-03
1.1.0: Add version information; fix bug of default value; add flatten and unflatten; optimization speed performance.
* 2021-10-24
1.0.0: Greatly optimize the speed performance using cython, overhead has been reduced to a negligible level.
</div>
</details>
Or just install it with `treevalue` itself
## Feedback and Contribute
```shell
pip install treevalue[potc]
```
Welcome to **OpenDILab** community - treevalue!
In potc, you can translate the objects to runnable python source code, which can be loaded to objects afterwards by the python interpreter, like the following graph
If you meet some problem or have some brilliant ideas, you can [file an issue](https://github.com/opendilab/treevalue/issues/new/choose).
Or just contact us with [slack](https://opendilab.slack.com/join/shared_invite/zt-v9tmv4fp-nUBAQEH1_Kuyu_q4plBssQ#/shared-invite/email) or email (opendilab.contact@gmail.com).
We appreciate all contributions to improve treevalue, both logic and system designs. Please refer to CONTRIBUTING.md for more guides.
Thanks to the following contributors!
And users can join our [slack communication channel](https://join.slack.com/t/opendilab/shared_invite/zt-v9tmv4fp-nUBAQEH1_Kuyu_q4plBssQ), or contact the core developer [HansBug](https://github.com/HansBug) for more detailed discussion.