Metadata-Version: 2.1
Name: trainplot
Version: 0.2.0
Summary: Dynamically updating plots in Jupyter notebooks, e.g. for visualizing training progress.
Author-email: Jonas Loos <trainplot@jloos.de>
License: MIT License
        
        Copyright (c) 2023 Jonas Loos
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/JonasLoos/trainplot
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: matplotlib
Requires-Dist: numpy
Requires-Dist: ipywidgets
Requires-Dist: ipython

# trainplot

Dynamically updating plots in Jupyter notebooks, e.g. for visualizing training progress. Inspired by [livelossplot](https://github.com/stared/livelossplot), and aims to be easier to use with better jupyter notebook support.

Trainplot outputs the matplotlib figure to an `ipywidgets.Output` widget, so it doesn't interfere with other outputs like `tqdm` or print statements. To avoid wasting resources and flickering, the figure is only updated with a given `update_period`.


## Installation

```bash
pip install trainplot
```


## Usage

In your Jupyter notebook import it using:

```python
from trainplot import TrainPlot
```

Then you can use it like this:

```python
trainplot = TrainPlot()

for i in range(100):
    trainplot(i = i+random.random()*10, root = i**.5*3)
    time.sleep(0.1)

trainplot.close()
```

<img src="https://github.com/JonasLoos/trainplot/assets/33965649/614f8ed4-8646-4100-b869-187ea89f1bb2" width="500">

---

There is a tf/keras callback:

```python
from trainplot import TrainPlotKeras

...

model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, callbacks=[TrainPlotKeras()])
```

<img src="https://github.com/JonasLoos/trainplot/assets/33965649/4ddff79a-978e-434c-a6c3-571cf48c0892" width="500">

---

It also works together with e.g. `tqdm.notebook` and printing:

```python
trainplot = TrainPlot()

for i in trange(50):
    trainplot(i=i, root=i**.5)
    if i % 10 == 0:
        print(f'currently at {i} iterations')
    time.sleep(0.1)

trainplot.close()
```

<img src="https://github.com/JonasLoos/trainplot/assets/33965649/7571efab-7a3f-4414-b537-a2dffd9e1bec" width="400">

---

You can also add a bunch of custumizations, e.g.:

```python
trainplot = TrainPlot(
    update_period=.2,
    fig_args=dict(nrows=2, ncols=2, figsize=(10, 8), gridspec_kw={'height_ratios': [1, 1], 'width_ratios': [1, 1]}),
    plot_pos={'loss': (0, 0, 0), 'accuracy': (0, 1, 0), 'val_loss': (1, 0, 0), 'val_accuracy': (1, 1, 0)},
    plot_args={'loss': {'color': 'orange'}, 'accuracy': {'color': 'green'}, 'val_loss': {'color': 'orange', 'label': 'validation loss'}, 'val_accuracy': {'color': 'green', 'label': 'validation accuracy'}},
)

for i in range(100, 200):
    trainplot(step=i, loss=(i/100-2)**4, accuracy=i/2, val_loss=(i/100-2.1)**4, val_accuracy=i/2.1)
    time.sleep(0.1)

trainplot.close()
```

<img src="https://github.com/JonasLoos/trainplot/assets/33965649/599314e2-d1c1-4044-a915-6316722a2324" width="600">

---

Check the [examples](examples) folder for example notebooks.
