# Path: autoencoder/testing/visualize.py
# def reuters_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_names = classes_to_visual.keys()
#     class_ids = dict(zip(class_names, range(C)))
#     class_names = set(class_names)
#     codes, labels = zip(*[(code, class_names.intersection(set(doc_labels[doc]))) for doc, code in doc_codes.items() if len(class_names.intersection(set(doc_labels[doc]))) == 1])
#     # codes = []
#     # labels = []
#     # for doc, code in doc_codes.items():
#     #     y = set(doc_labels[doc])
#     #     x = list(class_names.intersection(y))
#     #     if x:
#     #         codes.append(code)
#     #         labels.append(x[0])
#     # x = 0
#     # pairs = []
#     # for each in labels:
#     #     if len(class_names.intersection(set(each))) > 1:
#     #         x += 1
#     #         pairs.append(class_names.intersection(set(each)))
#     # print x
# 
# 
#     X = np.r_[list(codes)]
#     X = PCA(n_components=3).fit_transform(X)
#     plt.figure(figsize=(10, 10), facecolor='white')
# 
#     x_pc, y_pc = 0, 1
# 
#     for c in class_names:
#         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 reuters_visualize_tsne(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_names = classes_to_visual.keys()
#     class_ids = dict(zip(class_names, range(C)))
#     class_names = set(class_names)
#     codes, labels = zip(*[(code, doc_labels[doc]) for doc, code in doc_codes.items() if class_names.intersection(set(doc_labels[doc]))])
# 
#     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 = 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_json(file):
#     try:
#         with open(file, 'r') as datafile:
#             data = json.load(datafile)
#     except Exception as e:
#         raise e
# 
#     return data


import argparse
from autoencoder.testing.visualize import reuters_visualize_pca_2d, reuters_visualize_tsne
from autoencoder.utils.io_utils import load_json
'''
Created on Dec, 2016

@author: hugo

'''
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()
    classes_to_visual = {'ECAT': 'ECONOMICS', 'MCAT': 'MARKETS',
                        'CCAT': 'CORPORATE/INDUSTRIAL', 'GCAT': 'GOVERNMENT/SOCIAL'}

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

# Path: autoencoder/testing/visualize.py
# def reuters_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_names = classes_to_visual.keys()
#     class_ids = dict(zip(class_names, range(C)))
#     class_names = set(class_names)
#     codes, labels = zip(*[(code, class_names.intersection(set(doc_labels[doc]))) for doc, code in doc_codes.items() if len(class_names.intersection(set(doc_labels[doc]))) == 1])
#     # codes = []
#     # labels = []
#     # for doc, code in doc_codes.items():
#     #     y = set(doc_labels[doc])
#     #     x = list(class_names.intersection(y))
#     #     if x:
#     #         codes.append(code)
#     #         labels.append(x[0])
#     # x = 0
#     # pairs = []
#     # for each in labels:
#     #     if len(class_names.intersection(set(each))) > 1:
#     #         x += 1
#     #         pairs.append(class_names.intersection(set(each)))
#     # print x
# 
# 
#     X = np.r_[list(codes)]
#     X = PCA(n_components=3).fit_transform(X)
#     plt.figure(figsize=(10, 10), facecolor='white')
# 
#     x_pc, y_pc = 0, 1
# 
#     for c in class_names:
#         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 reuters_visualize_tsne(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_names = classes_to_visual.keys()
#     class_ids = dict(zip(class_names, range(C)))
#     class_names = set(class_names)
#     codes, labels = zip(*[(code, doc_labels[doc]) for doc, code in doc_codes.items() if class_names.intersection(set(doc_labels[doc]))])
# 
#     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 = 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_json(file):
#     try:
#         with open(file, 'r') as datafile:
#             data = json.load(datafile)
#     except Exception as e:
#         raise e
# 
#     return data


# Path: plot_reuters.py
import argparse
from autoencoder.testing.visualize import reuters_visualize_pca_2d, reuters_visualize_tsne
from autoencoder.utils.io_utils import load_json

'''
Created on Dec, 2016

@author: hugo

'''
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()
    classes_to_visual = {'ECAT': 'ECONOMICS', 'MCAT': 'MARKETS',
                        'CCAT': 'CORPORATE/INDUSTRIAL', 'GCAT': 'GOVERNMENT/SOCIAL'}

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

        reuters_visualize_tsne(load_json(args.doc_codes_file), load_json(args.doc_labels_file), classes_to_visual, args.output)