# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Visualization tools for tensorflow_docs.

Use this module for plotting and visualization code that is too long to inline
into a notebook.
"""
import matplotlib.pyplot as plt
import numpy as np

prop_cycle = plt.rcParams['axes.prop_cycle']
COLOR_CYCLE = prop_cycle.by_key()['color']


def _smooth(values, std):
  """Smooths a list of values by convolving with a gaussian.

  Assumes equal spacing.

  Args:
    values: A 1D array of values to smooth.
    std: The standard devistion of the gaussian. The units are array elements.

  Returns:
    The smoothed array.
  """
  width = std * 4
  x = np.linspace(-width, width, min(2 * width + 1, len(values)))
  kernel = np.exp(-(x / 5)**2)

  values = np.array(values)
  weights = np.ones_like(values)

  smoothed_values = np.convolve(values, kernel, mode='same')
  smoothed_weights = np.convolve(weights, kernel, mode='same')

  return smoothed_values / smoothed_weights


class HistoryPlotter(object):
  """A class for plotting named set of keras-histories.

  The class maintains colors for each key from plot to plot.
  """

  def __init__(self, metric=None, smoothing_std=None):
    self.color_table = {}
    self.metric = metric
    self.smoothing_std = smoothing_std

  def plot(self, histories, metric=None, smoothing_std=None):
    """Plots a {name: history} dictionary of keras histories.

    Colors are assigned to the name-key, and maintained from call to call.
    Training metrics are shown as a solid line, validation metrics dashed.

    Args:
      histories: {name: history} dictionary of keras histories.
      metric: which metric to plot from all the histories.
      smoothing_std: the standard-deviaation of the smoothing kernel applied
        before plotting. The units are in array-indices.
    """
    if metric is None:
      metric = self.metric
    if smoothing_std is None:
      smoothing_std = self.smoothing_std

    for name, history in histories.items():
      # Remember name->color asociations.
      if name in self.color_table:
        color = self.color_table[name]
      else:
        color = COLOR_CYCLE[len(self.color_table) % len(COLOR_CYCLE)]
        self.color_table[name] = color

      train_value = history.history[metric]
      val_value = history.history['val_' + metric]
      if smoothing_std is not None:
        train_value = _smooth(train_value, std=smoothing_std)
        val_value = _smooth(val_value, std=smoothing_std)

      plt.plot(
          history.epoch,
          train_value,
          color=color,
          label=name.title() + ' Train')
      plt.plot(
          history.epoch,
          val_value,
          '--',
          label=name.title() + ' Val',
          color=color)

    plt.xlabel('Epochs')
    plt.ylabel(metric.replace('_', ' ').title())
    plt.legend()

    plt.xlim(
        [0, max([history.epoch[-1] for name, history in histories.items()])])
    plt.grid(True)

@@ -1046,24 +1046,37 @@
  with a 
-g
+G
 aussian
+ distribution
 .%0A%0A  Ass
@@ -1174,17 +1174,17 @@
 ard devi
-s
+a
 tion of 
@@ -1191,16 +1191,29 @@
 the 
-g
+G
 aussian
+ distribution
 . Th
@@ -1687,16 +1687,18 @@
 lotting 
+a 
 named se
@@ -1698,25 +1698,25 @@
 amed set of 
-k
+K
 eras-histori
@@ -2033,33 +2033,33 @@
 %7D dictionary of 
-k
+K
 eras histories.%0A
@@ -2244,32 +2244,34 @@
  %7Bname: history%7D
+ a
  dictionary of k
@@ -2269,17 +2269,17 @@
 nary of 
-k
+K
 eras his
@@ -2382,15 +2382,14 @@
 dard
--
+ 
 devia
-a
 tion
@@ -2683,16 +2683,17 @@
 color as
+s
 ociation

Fix typos in tensorflow_docs/plots init comments