Implement abbreviation system to avoid very-long-line outputs
Based on the idea from #53.
The abbreviation system has these main components:
- Abbreviation levels mark layers at which we should increment a counter, for "levels of depth" into an object. Usually each foldable tree node should increase the level of depth.
- Abbreviatable parts indicate parts of an output that should change if the abbreviation level is too high.
- The abbreviation threshold (one for regular and one for roundtrip) specifies the number of abbreviation levels *past* the first collaped tree node where abbreviation should apply.
Configurable as follows:
```python
# as part of the basic setup
treescope.basic_interactive_setup(
autovisualize_arrays=True,
abbreviation_threshold=1,
)
# set globally
treescope.abbreviation_threshold.set_globally(1)
# override in a context
with treescope.abbreviation_threshold.set_scoped(1):
print(treescope.render_to_text(nested))
```
Produces outputs like this in HTML mode:
<img width="880" alt="image" src="https://github.com/user-attachments/assets/ace123f6-1365-45a9-8da2-a3f8c108e096" />
or like this in text mode:
```python
{
'x0': {
'foo': <np.ndarray float64(100, 100) ≈-0.0084 ±0.99 [≥-3.6, ≤3.8] nonzero:10_000>,
'bar': {'x': 1111, 'y': 2222, 'z': 3333, 'c': Config(<6 fields...>)},
'baz': [3, 3, 3, 3, 3, 3, 3],
},
'x1': {
'foo': <np.ndarray float64(100, 100) ≈-0.02 ±0.99 [≥-4.2, ≤3.6] nonzero:10_000>,
'bar': {'x': 1111, 'y': 2222, 'z': 3333, 'c': Config(<6 fields...>)},
'baz': [3, 3, 3, 3, 3, 3, 3],
},
'x2': {
'foo': <np.ndarray float64(100, 100) ≈0.0071 ±1.0 [≥-3.6, ≤3.7] nonzero:10_000>,
'bar': {'x': 1111, 'y': 2222, 'z': 3333, 'c': Config(<6 fields...>)},
'baz': [3, 3, 3, 3, 3, 3, 3],
},
'x3': {'foo': <np float64(100, 100)>, 'bar': {<4 items...>}, 'baz': [<7 elements...>]},
'x4': {'foo': <np float64(100, 100)>, 'bar': {<4 items...>}, 'baz': [<7 elements...>]},
}
```
合并状态:已合并 合并于 2025-02-17 关闭于 2025-02-17 0 条评论