Plot Keras History
=========================================================================================
|travis| |sonar_quality| |sonar_maintainability| |codacy| |pip| |downloads|

A python package to print a `Keras model training history <https://keras.io/callbacks/#history>`_

How do I install this package?
----------------------------------------------
As usual, just download it using pip:

.. code:: shell

    pip install plot_keras_history

Tests Coverage
----------------------------------------------
Since some software handling coverages sometime get slightly different results, here's three of them:

|coveralls| |sonar_coverage| |code_climate_coverage|

Usage
------------------------------------------------
Let's say you have a model generated by the function my_keras_model:

Plotting a training history
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the following example we will see how to plot and either show or save the training history:

|standard|

.. code:: python

    from plot_keras_history import plot_history
    import matplotlib.pyplot as plt

    model = my_keras_model()
    history = model.fit(...).history
    plot_history(history)
    plt.show()
    plot_history(history, path="standard.png")
    plt.close()

Plotting into separate graphs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
By default, the graphs are all in one big image, but for various reasons you might need them one by one:

.. code:: python

    from plot_keras_history import plot_history
    import matplotlib.pyplot as plt

    model = my_keras_model()
    history = model.fit(...).history
    plot_history(history, path="singleton", single_graphs=True)
    plt.close()

Reducing the history noise with Savgol Filters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In some occasion it is necessary to be able to see the progress of the history to interpolate the results to remove a bit of noise. A parameter is offered to automatically apply a Savgol filter:

|interpolated|

.. code:: python

    from plot_keras_history import plot_history
    import matplotlib.pyplot as plt

    model = my_keras_model()
    history = model.fit(...).history
    plot_history(history, path="interpolated.png", interpolate=True)
    plt.close()

Automatic aliases
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A number of metrics are automatically converted from the default ones to more talking ones, for example "lr" becomes "Learning Rate", or "acc" becomes "Accuracy".

All the available options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

    def plot_history(
        history, # Either the history object or a pandas DataFrame. When using a dataframe, the index name is used as abscissae label.
        style:str="-", # The style of the lines.
        interpolate: bool = False, # Wethever to interpolate or not the graphs datapoints.
        side: float = 5, # Dimension of the graphs side.
        graphs_per_row: int = 4, # Number of graphs for each row.
        customization_callback: Callable = None, # Callback for customizing the graphs.
        path: str = None, # Path where to store the resulting image or images (in the case of single_graphs)
        single_graphs: bool = False #  Wethever to save the graphs as single of multiples.
    )

Chaining histories
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It's common to stop and restart a model's training, and this would break the history object into two: for this reason the method `chain_histories <https://github.com/LucaCappelletti94/plot_keras_history/blob/dd590ce7f89b2a52236f231a9a6377b3e1d76489/plot_keras_history/utils.py#L3-L8>`_ is available:

.. code:: python

    from plot_keras_history import chain_histories

    model = my_keras_model()
    history1 = model.fit(...).history
    history2 = model.fit(...).history
    history = chain_histories(history1, history2)

Extras
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Numerous additional metrics are available in `extra_keras_metrics <https://github.com/LucaCappelletti94/extra_keras_metrics>`_

.. |travis| image:: https://travis-ci.org/LucaCappelletti94/plot_keras_history.png
   :target: https://travis-ci.org/LucaCappelletti94/plot_keras_history
   :alt: Travis CI build

.. |sonar_quality| image:: https://sonarcloud.io/api/project_badges/measure?project=LucaCappelletti94_plot_keras_history&metric=alert_status
    :target: https://sonarcloud.io/dashboard/index/LucaCappelletti94_plot_keras_history
    :alt: SonarCloud Quality

.. |sonar_maintainability| image:: https://sonarcloud.io/api/project_badges/measure?project=LucaCappelletti94_plot_keras_history&metric=sqale_rating
    :target: https://sonarcloud.io/dashboard/index/LucaCappelletti94_plot_keras_history
    :alt: SonarCloud Maintainability

.. |sonar_coverage| image:: https://sonarcloud.io/api/project_badges/measure?project=LucaCappelletti94_plot_keras_history&metric=coverage
    :target: https://sonarcloud.io/dashboard/index/LucaCappelletti94_plot_keras_history
    :alt: SonarCloud Coverage

.. |coveralls| image:: https://coveralls.io/repos/github/LucaCappelletti94/plot_keras_history/badge.svg?branch=master
    :target: https://coveralls.io/github/LucaCappelletti94/plot_keras_history?branch=master
    :alt: Coveralls Coverage

.. |pip| image:: https://badge.fury.io/py/plot-keras-history.svg
    :target: https://badge.fury.io/py/plot_keras_history
    :alt: Pypi project

.. |downloads| image:: https://pepy.tech/badge/plot-keras-history
    :target: https://pepy.tech/badge/plot-keras-history
    :alt: Pypi total project downloads 

.. |codacy|  image:: https://api.codacy.com/project/badge/Grade/4f09666f140a4fc785fecc94b0ed9a6a
    :target: https://www.codacy.com/app/LucaCappelletti94/plot_keras_history?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=LucaCappelletti94/plot_keras_history&amp;utm_campaign=Badge_Grade
    :alt: Codacy Maintainability

.. |code_climate_maintainability| image:: https://api.codeclimate.com/v1/badges/5540f8112de448ac3298/maintainability
    :target: https://codeclimate.com/github/LucaCappelletti94/plot_keras_history/maintainability
    :alt: Maintainability

.. |code_climate_coverage| image:: https://api.codeclimate.com/v1/badges/5540f8112de448ac3298/test_coverage
    :target: https://codeclimate.com/github/LucaCappelletti94/plot_keras_history/test_coverage
    :alt: Code Climate Coverate

.. |standard| image:: https://github.com/LucaCappelletti94/plot_keras_history/blob/master/plots/normal.png?raw=true
.. |interpolated| image:: https://github.com/LucaCappelletti94/plot_keras_history/blob/master/plots/interpolated.png?raw=true