Metadata-Version: 2.4
Name: tf-matplotlib
Version: 0.1.0
Summary: Connect TensorFlow tensors with Matplotlib plotting
Author-email: Yurui <yrming@gmail.com>
Requires-Python: <3.13,>=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: tensorflow>=2.10
Requires-Dist: matplotlib>=3.6
Requires-Dist: numpy>=1.21
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: ipython; extra == "dev"
Dynamic: license-file

### This is a cloned repo to try to support the TensorFlow 2.x version. All the credits go to the original author! 
### Due to the retracing mechanism introduced in TensorFlow 2.x, the execution behaviour of functions makes some of the assertions in previous test scripts no more valid. Temporarily workaround is adopted, need a clean fixing.
### **tf-matplotlib** - seamless integration of matplotlib figures into TensorFlow summaries

**tf-matplotlib** renders your everyday matplotlib figures tinside TensorFlow's Tensorboard visualization interface. The library
 - takes care of evaluating input tensors prior to plotting, 
 - avoids matplotlib threading issues,
 - support multiple figures and,
 - provides blitting for runtime critical plotting. 
 
The following TensorFlow summary is generated by [sgd.py](tfmpl/samples/sgd.py). It plots the progress of gradient descent optimizers on a test surface. To avoid redrawing the test surface, it makes use of blitting. See [usage](#usage) below for a more introductory example.

![](etc/sgd.gif)

### Installation

```
pip install tfmpl
```

Requirements
 - Python 3.5/3.6
 - TensorFlow 1.x
 - matplotlib 2.2.0

### Build status

|Branch|Linux|Windows|
|------|------|------|
|master|![](https://travis-ci.org/cheind/tf-matplotlib.svg?branch=master)| ![](https://ci.appveyor.com/api/projects/status/reo8nucumqhb93q5/branch/master?svg=true) |
|develop|![](https://travis-ci.org/cheind/tf-matplotlib.svg?branch=master)|![](https://ci.appveyor.com/api/projects/status/reo8nucumqhb93q5/branch/develop?svg=true)|

### Usage
<a name="usage"></a>

Below are the relevant snippets to render a simple scatter plot. See [scatter.py](tfmpl/samples/scatter.py) for the complete self-contained example.

```python
from datetime import datetime
import tensorflow as tf
import numpy as np
import os

import tf_matplotlib as tfmpl

if __name__ == '__main__':
    @tfmpl.figure_tensor
    def draw_scatter(scaled, colors): 
        '''Draw scatter plots. One for each color.'''  
        figs = tfmpl.create_figures(len(colors), figsize=(4,4))
        for idx, f in enumerate(figs):
            ax = f.add_subplot(111)
            ax.axis('off')
            ax.scatter(scaled[:, 0], scaled[:, 1], c=colors[idx])
            f.tight_layout()

        return figs  

    points = tf.random.normal((100, 2), dtype=tf.float32)
    scale = tf.constant(2., dtype=tf.float32)        
    scaled = points*scale
   
    os.makedirs('log', exist_ok=True)
    now = datetime.now()
    logdir = "log/" + now.strftime("%Y%m%d-%H%M%S") + "/"
    writer = tf.summary.create_file_writer(logdir)
    with writer.as_default():
        image_tensor = draw_scatter(scaled, ['r', 'g'])
        image_summary = tf.summary.image('scatter', image_tensor, step=0)
        writer.flush()
```

![](etc/scatter.png)

### Draw utilities

When doing classification, a common task is to generate a confusion matrix. **tf-matplotlib** provides `tfmpl.draw.confusion_matrix` to quickly generate such a plot from labels and predictions. The following plot shows classification training progress on the MNIST classification task. Full sample code is provided in [mnist.py](tfmpl/samples/mnist.py).

![](etc/cm.gif)

### License

```
MIT License

Copyright (c) 2018 Christoph Heindl

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.
```


