# Path: autoencoder/testing/visualize.py
# def DBN_visualize_pca_2d(doc_codes, doc_labels, classes_to_visual, save_file):
#     """
#         Visualize the input data on a 2D PCA plot. Depending on the number of components,
#         the plot will contain an X amount of subplots.
#         @param doc_codes:
#         @param number_of_components: The number of principal components for the PCA plot.
#     """
# 
#     # markers = ["p", "s", "h", "H", "+", "x", "D"]
#     markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"]
# 
#     C = len(classes_to_visual)
#     while True:
#         if C <= len(markers):
#             break
#         markers += markers
# 
#     class_ids = dict(zip(classes_to_visual.keys(), range(C)))
# 
#     codes, labels = doc_codes, doc_labels
# 
#     X = np.r_[list(codes)]
#     X = PCA(n_components=3).fit_transform(X)
#     plt.figure(figsize=(10, 10), facecolor='white')
# 
#     x_pc, y_pc = 1, 2
# 
#     for c in classes_to_visual.keys():
#         idx = np.array(labels) == c
#         # idx = get_indices(labels, c)
#         plt.plot(X[idx, x_pc], X[idx, y_pc], linestyle='None', alpha=0.6, marker=markers[class_ids[c]],
#                         markersize=6, label=classes_to_visual[c])
#         # plt.legend(c)
#     plt.title('Projected on the first 2 PCs')
#     plt.xlabel('PC %s' % x_pc)
#     plt.ylabel('PC %s' % y_pc)
#     # legend = plt.legend(loc='upper center', shadow=True)
#     plt.savefig(save_file)
#     plt.show()
# 
# def DBN_plot_tsne(doc_codes, doc_labels, classes_to_visual, save_file):
#     markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"]
# 
#     C = len(classes_to_visual)
#     while True:
#         if C <= len(markers):
#             break
#         markers += markers
# 
#     class_ids = dict(zip(classes_to_visual.keys(), range(C)))
# 
#     codes, labels = doc_codes, doc_labels
# 
#     X = np.r_[list(codes)]
#     tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
#     np.set_printoptions(suppress=True)
#     X = tsne.fit_transform(X)
# 
#     plt.figure(figsize=(10, 10), facecolor='white')
# 
#     for c in classes_to_visual.keys():
#         idx = np.array(labels) == c
#         # idx = get_indices(labels, c)
#         plt.plot(X[idx, 0], X[idx, 1], linestyle='None', alpha=0.6, marker=markers[class_ids[c]],
#                         markersize=6, label=classes_to_visual[c])
#     legend = plt.legend(loc='upper center', shadow=True)
#     plt.title("tsne")
#     plt.savefig(save_file)
#     plt.show()
# 
# Path: autoencoder/utils/io_utils.py
# def load_marshal(path_to_file):
#     try:
#         with open(path_to_file, 'r') as f:
#             data = m.load(f)
#     except Exception as e:
#         raise e
# 
#     return data


import argparse
from autoencoder.testing.visualize import DBN_visualize_pca_2d, DBN_plot_tsne
from autoencoder.utils.io_utils import load_marshal
'''
from __future__ import absolute_import



def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('doc_codes_file', type=str, help='path to the input corpus file')
    parser.add_argument('doc_labels_file', type=str, help='path to the output doc codes file')
    parser.add_argument('cmd', choices=['pca', 'tsne'], help='plot cmd')
    parser.add_argument('-o', '--output', type=str, default='out.png', help='path to the output file')
    args = parser.parse_args()

    cmd = args.cmd.lower()


    # 20news
    class_names = ['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc']

    classes_to_visual = ['comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware',
                        'comp.sys.mac.hardware', 'comp.windows.x']
    # classes_to_visual = class_names
    # classes_to_visual = ["rec.sport.hockey", "comp.graphics", "sci.crypt", \
    #                     "soc.religion.christian", "talk.politics.mideast", \
    #                     "talk.politics.guns"]
    classes_to_visual = dict([(class_names.index(x), x) for x in classes_to_visual if x in class_names])

    if cmd == 'pca':
        DBN_visualize_pca_2d(load_marshal(args.doc_codes_file), load_marshal(args.doc_labels_file), classes_to_visual, args.output)
    elif cmd == 'tsne':

# Path: autoencoder/testing/visualize.py
# def DBN_visualize_pca_2d(doc_codes, doc_labels, classes_to_visual, save_file):
#     """
#         Visualize the input data on a 2D PCA plot. Depending on the number of components,
#         the plot will contain an X amount of subplots.
#         @param doc_codes:
#         @param number_of_components: The number of principal components for the PCA plot.
#     """
# 
#     # markers = ["p", "s", "h", "H", "+", "x", "D"]
#     markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"]
# 
#     C = len(classes_to_visual)
#     while True:
#         if C <= len(markers):
#             break
#         markers += markers
# 
#     class_ids = dict(zip(classes_to_visual.keys(), range(C)))
# 
#     codes, labels = doc_codes, doc_labels
# 
#     X = np.r_[list(codes)]
#     X = PCA(n_components=3).fit_transform(X)
#     plt.figure(figsize=(10, 10), facecolor='white')
# 
#     x_pc, y_pc = 1, 2
# 
#     for c in classes_to_visual.keys():
#         idx = np.array(labels) == c
#         # idx = get_indices(labels, c)
#         plt.plot(X[idx, x_pc], X[idx, y_pc], linestyle='None', alpha=0.6, marker=markers[class_ids[c]],
#                         markersize=6, label=classes_to_visual[c])
#         # plt.legend(c)
#     plt.title('Projected on the first 2 PCs')
#     plt.xlabel('PC %s' % x_pc)
#     plt.ylabel('PC %s' % y_pc)
#     # legend = plt.legend(loc='upper center', shadow=True)
#     plt.savefig(save_file)
#     plt.show()
# 
# def DBN_plot_tsne(doc_codes, doc_labels, classes_to_visual, save_file):
#     markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"]
# 
#     C = len(classes_to_visual)
#     while True:
#         if C <= len(markers):
#             break
#         markers += markers
# 
#     class_ids = dict(zip(classes_to_visual.keys(), range(C)))
# 
#     codes, labels = doc_codes, doc_labels
# 
#     X = np.r_[list(codes)]
#     tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
#     np.set_printoptions(suppress=True)
#     X = tsne.fit_transform(X)
# 
#     plt.figure(figsize=(10, 10), facecolor='white')
# 
#     for c in classes_to_visual.keys():
#         idx = np.array(labels) == c
#         # idx = get_indices(labels, c)
#         plt.plot(X[idx, 0], X[idx, 1], linestyle='None', alpha=0.6, marker=markers[class_ids[c]],
#                         markersize=6, label=classes_to_visual[c])
#     legend = plt.legend(loc='upper center', shadow=True)
#     plt.title("tsne")
#     plt.savefig(save_file)
#     plt.show()
# 
# Path: autoencoder/utils/io_utils.py
# def load_marshal(path_to_file):
#     try:
#         with open(path_to_file, 'r') as f:
#             data = m.load(f)
#     except Exception as e:
#         raise e
# 
#     return data


# Path: plot_DBN.py
import argparse
from autoencoder.testing.visualize import DBN_visualize_pca_2d, DBN_plot_tsne
from autoencoder.utils.io_utils import load_marshal

'''
from __future__ import absolute_import



def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('doc_codes_file', type=str, help='path to the input corpus file')
    parser.add_argument('doc_labels_file', type=str, help='path to the output doc codes file')
    parser.add_argument('cmd', choices=['pca', 'tsne'], help='plot cmd')
    parser.add_argument('-o', '--output', type=str, default='out.png', help='path to the output file')
    args = parser.parse_args()

    cmd = args.cmd.lower()


    # 20news
    class_names = ['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc']

    classes_to_visual = ['comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware',
                        'comp.sys.mac.hardware', 'comp.windows.x']
    # classes_to_visual = class_names
    # classes_to_visual = ["rec.sport.hockey", "comp.graphics", "sci.crypt", \
    #                     "soc.religion.christian", "talk.politics.mideast", \
    #                     "talk.politics.guns"]
    classes_to_visual = dict([(class_names.index(x), x) for x in classes_to_visual if x in class_names])

    if cmd == 'pca':
        DBN_visualize_pca_2d(load_marshal(args.doc_codes_file), load_marshal(args.doc_labels_file), classes_to_visual, args.output)
    elif cmd == 'tsne':

        DBN_plot_tsne(load_marshal(args.doc_codes_file), load_marshal(args.doc_labels_file), classes_to_visual, args.output)