# Path: autoencoder/testing/visualize.py
# def heatmap(data, save_file='heatmap.png'):
#     ax = plt.figure().gca()
#     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
#     ax.yaxis.set_major_locator(MultipleLocator(5))
#     plt.pcolor(data, cmap=plt.cm.jet)
#     plt.savefig(save_file)
#     # plt.show()
# 
# Path: autoencoder/utils/op_utils.py
# def unitmatrix(matrix, norm='l2', axis=1):
#     if norm == 'l1':
#         maxtrixlen = np.sum(np.abs(matrix), axis=axis)
#     if norm == 'l2':
#         maxtrixlen = np.linalg.norm(matrix, axis=axis)
# 
#     if np.any(maxtrixlen <= 0):
#         return matrix
#     else:
#         maxtrixlen = maxtrixlen.reshape(1, len(maxtrixlen)) if axis == 0 else maxtrixlen.reshape(len(maxtrixlen), 1)
#         return matrix / maxtrixlen


import os
import numpy as np
import keras.backend as K
import tensorflow as tf
import warnings
from keras.layers import Dense
from keras.callbacks import Callback
from keras.engine import Layer
from keras import initializers
from ..testing.visualize import heatmap
from .op_utils import unitmatrix
                            model.save(filepath, overwrite=True)
                    else:
                        if self.verbose > 0:
                            print('Epoch %05d: %s did not improve' %
                                  (epoch, self.monitor))
            else:
                if self.verbose > 0:
                    print('Epoch %05d: saving model to %s' % (epoch, filepath))
                if self.save_weights_only:
                    model.save_weights(filepath, overwrite=True)
                else:
                    model.save(filepath, overwrite=True)

class VisualWeights(Callback):
    def __init__(self, save_path, per_epoch=15):
        super(VisualWeights, self).__init__()
        self.per_epoch = per_epoch
        self.filename, self.ext = os.path.splitext(save_path)

    def on_epoch_end(self, epoch, logs=None):
        """Called at the end of an epoch.
        # Arguments
            epoch: integer, index of epoch.
            logs: dictionary of logs.
        """
        if epoch % self.per_epoch == 0:
            weights = self.model.get_weights()[0]
            # weights /= np.max(np.abs(weights))
            weights = unitmatrix(weights, axis=0) # normalize
            # weights[np.abs(weights) < 1e-2] = 0

# Path: autoencoder/testing/visualize.py
# def heatmap(data, save_file='heatmap.png'):
#     ax = plt.figure().gca()
#     ax.yaxis.set_major_locator(MaxNLocator(integer=True))
#     ax.yaxis.set_major_locator(MultipleLocator(5))
#     plt.pcolor(data, cmap=plt.cm.jet)
#     plt.savefig(save_file)
#     # plt.show()
# 
# Path: autoencoder/utils/op_utils.py
# def unitmatrix(matrix, norm='l2', axis=1):
#     if norm == 'l1':
#         maxtrixlen = np.sum(np.abs(matrix), axis=axis)
#     if norm == 'l2':
#         maxtrixlen = np.linalg.norm(matrix, axis=axis)
# 
#     if np.any(maxtrixlen <= 0):
#         return matrix
#     else:
#         maxtrixlen = maxtrixlen.reshape(1, len(maxtrixlen)) if axis == 0 else maxtrixlen.reshape(len(maxtrixlen), 1)
#         return matrix / maxtrixlen


# Path: autoencoder/utils/keras_utils.py
import os
import numpy as np
import keras.backend as K
import tensorflow as tf
import warnings
from keras.layers import Dense
from keras.callbacks import Callback
from keras.engine import Layer
from keras import initializers
from ..testing.visualize import heatmap
from .op_utils import unitmatrix

                            model.save(filepath, overwrite=True)
                    else:
                        if self.verbose > 0:
                            print('Epoch %05d: %s did not improve' %
                                  (epoch, self.monitor))
            else:
                if self.verbose > 0:
                    print('Epoch %05d: saving model to %s' % (epoch, filepath))
                if self.save_weights_only:
                    model.save_weights(filepath, overwrite=True)
                else:
                    model.save(filepath, overwrite=True)

class VisualWeights(Callback):
    def __init__(self, save_path, per_epoch=15):
        super(VisualWeights, self).__init__()
        self.per_epoch = per_epoch
        self.filename, self.ext = os.path.splitext(save_path)

    def on_epoch_end(self, epoch, logs=None):
        """Called at the end of an epoch.
        # Arguments
            epoch: integer, index of epoch.
            logs: dictionary of logs.
        """
        if epoch % self.per_epoch == 0:
            weights = self.model.get_weights()[0]
            # weights /= np.max(np.abs(weights))
            weights = unitmatrix(weights, axis=0) # normalize
            # weights[np.abs(weights) < 1e-2] = 0

            heatmap(weights.T, '%s_%s%s'%(self.filename, epoch, self.ext))