Metadata-Version: 2.1
Name: rich-logger
Version: 0.2.1
Summary: Table logger using Rich
License: BSD 3-Clause
Author: Perceval Wajsbürt
Author-email: perceval.wajsburt@sorbonne-universite.fr
Requires-Python: >=3.7,<4.0
Classifier: License :: Other/Proprietary License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Requires-Dist: pydantic (>=1.0.0)
Requires-Dist: rich (>=10.11.0)
Requires-Dist: tqdm (>=2.0.0)
Description-Content-Type: text/markdown

![tests](https://github.com/percevalw/rich-logger/actions/workflows/tests.yml/badge.svg)
[![pypi](https://badge.fury.io/py/rich-logger.svg)](https://pypi.org/project/rich-logger)

# rich-logger
Table logger using Rich, aimed at Pytorch Lightning logging

## Features

- display your training logs with pretty [rich](https://github.com/willmcgugan/rich) tables
- describe your fields with `goal` ("higher_is_better" or "lower_is_better"), `format` and `name`
- a field descriptor can be matched with any regex
- a field name can be computed as a regex substitution
- works in Jupyter notebooks as well as in a command line
- integrates easily with [Pytorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning)

## Demo
```python
from rich_logger import RichTablePrinter
import time
import random
from tqdm import trange

logger_fields = {
    "step": {},
    "(.*)_precision": {
        "goal": "higher_is_better",
        "format": "{:.4f}",
        "name": r"\1_p",
    },
    "(.*)_recall": {
        "goal": "higher_is_better",
        "format": "{:.4f}",
        "name": r"\1_r",
    },
    "duration": {"format": "{:.1f}", "name": "dur(s)"},
}


def optimization():
    printer = RichTablePrinter(key="step", fields=logger_fields)
    printer.hijack_tqdm()

    t = time.time()
    for i in trange(10):
        time.sleep(random.random() / 3)
        printer.log(
            {
                "step": i,
                "task_precision": i / 10.0 if i < 5 else 0.5 - (i - 5) / 10.0,
            }
        )
        time.sleep(random.random() / 3)
        printer.log(
            {
                "step": i,
                "task_recall": 0.0 if i < 3 else (i - 3) / 10.0,
                "duration": time.time() - t,
            }
        )
        printer.log({"test": i})
        t = time.time()
        for j in trange(5):
            time.sleep(random.random() / 10)

    printer.finalize()


optimization()
```
![Demo](demo.gif)

## Use it with PytorchLightning
```python
from rich_logger import RichTableLogger

trainer = pl.Trainer(..., logger=[RichTableLogger(key="epoch", fields=logger_fields)])
```

