# Path: autoencoder/preprocessing/preprocessing.py
# def load_corpus(corpus_path):
#     corpus = load_json(corpus_path)
# 
#     return corpus
# 
# Path: autoencoder/utils/io_utils.py
# def dump_json(data, file):
#     try:
#         with open(file, 'w') as datafile:
#             json.dump(data, datafile)
#     except Exception as e:
#         raise e
# 
# def write_file(data, file):
#     try:
#         with open(file, 'w') as datafile:
#             for line in data:
#                 datafile.write(' '.join(line) + '\n')
#     except Exception as e:
#         raise e
# 
# Path: autoencoder/baseline/lda.py
# def train_lda(corpus, vocab_dict, n_topics, n_iter, save_model):
#     lda = LdaModel(corpus, num_topics=n_topics, id2word=vocab_dict, \
#         passes=n_iter, minimum_probability=1e-3)
#     lda.save(save_model)
# 
#     return lda
# 
# def generate_doc_codes(model, corpus, output):
#     model.minimum_probability = 1e-3
#     n_topics = model.num_topics
#     doc_codes = {}
#     for key, doc_bow in corpus.iteritems():
#         code = np.zeros(n_topics)
#         for idx, val in model[doc_bow]:
#             code[idx] = val
#         doc_codes[key] = code.tolist()
#     dump_json(doc_codes, output)
# 
#     return doc_codes
# 
# def load_model(model_file):
#     return LdaModel.load(model_file)
# 
# def show_topics(model, n_words_per_topic=10):
#     n_topics = model.num_topics
#     topics = [zip(*model.show_topic(i, n_words_per_topic))[0] for i in range(n_topics)]
# 
#     return topics
# 
# def show_topics_prob(model, n_words_per_topic=10):
#     n_topics = model.num_topics
#     topics = [model.show_topic(i, n_words_per_topic) for i in range(n_topics)]
# 
#     return topics
# 
# def calc_pairwise_cosine(model):
#     n = model.num_topics
#     weights = model.state.get_lambda()
#     weights = np.apply_along_axis(lambda x: x / x.sum(), 1, weights) # get dist.
#     weights = unitmatrix(weights) # normalize
#     score = []
#     for i in range(n):
#         for j in range(i + 1, n):
#             score.append(np.arccos(weights[i].dot(weights[j])))
# 
#     return np.mean(score), np.std(score)
# 
# def calc_pairwise_dev(model):
#     # the average squared deviation from 0 (90 degree)
#     n = model.num_topics
#     weights = model.state.get_lambda()
#     weights = np.apply_along_axis(lambda x: x / x.sum(), 1, weights) # get dist.
#     weights = unitmatrix(weights) # normalize
#     score = 0.
#     for i in range(n):
#         for j in range(i + 1, n):
#             score += (weights[i].dot(weights[j]))**2
# 
#     return np.sqrt(2. * score / n / (n - 1))
# 
# Path: autoencoder/testing/visualize.py
# def word_cloud(word_embedding_matrix, vocab, s, save_file='scatter.png'):
#     words = [(i, vocab[i]) for i in s]
#     model = TSNE(n_components=2, random_state=0)
#     #Note that the following line might use a good chunk of RAM
#     tsne_embedding = model.fit_transform(word_embedding_matrix)
#     words_vectors = tsne_embedding[np.array([item[1] for item in words])]
# 
#     plt.subplots_adjust(bottom = 0.1)
#     plt.scatter(
#         words_vectors[:, 0], words_vectors[:, 1], marker='o', cmap=plt.get_cmap('Spectral'))
# 
#     for label, x, y in zip(s, words_vectors[:, 0], words_vectors[:, 1]):
#         plt.annotate(
#             label,
#             xy=(x, y), xytext=(-20, 20),
#             textcoords='offset points', ha='right', va='bottom',
#             fontsize=20,
#             # bbox=dict(boxstyle='round,pad=1.', fc='yellow', alpha=0.5),
#             arrowprops=dict(arrowstyle = '<-', connectionstyle='arc3,rad=0')
#             )
#     plt.show()
#     # plt.savefig(save_file)
# 
# 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 argparse
import timeit
import math
import numpy as np
from os import path
from autoencoder.preprocessing.preprocessing import load_corpus
from autoencoder.utils.io_utils import dump_json, write_file
from autoencoder.baseline.lda import train_lda, generate_doc_codes, load_model, show_topics, show_topics_prob, calc_pairwise_cosine, calc_pairwise_dev
from autoencoder.testing.visualize import word_cloud
from autoencoder.utils.op_utils import unitmatrix
'''
Created on Nov, 2016

@author: hugo

'''
from __future__ import absolute_import



def train(args):

# Path: autoencoder/preprocessing/preprocessing.py
# def load_corpus(corpus_path):
#     corpus = load_json(corpus_path)
# 
#     return corpus
# 
# Path: autoencoder/utils/io_utils.py
# def dump_json(data, file):
#     try:
#         with open(file, 'w') as datafile:
#             json.dump(data, datafile)
#     except Exception as e:
#         raise e
# 
# def write_file(data, file):
#     try:
#         with open(file, 'w') as datafile:
#             for line in data:
#                 datafile.write(' '.join(line) + '\n')
#     except Exception as e:
#         raise e
# 
# Path: autoencoder/baseline/lda.py
# def train_lda(corpus, vocab_dict, n_topics, n_iter, save_model):
#     lda = LdaModel(corpus, num_topics=n_topics, id2word=vocab_dict, \
#         passes=n_iter, minimum_probability=1e-3)
#     lda.save(save_model)
# 
#     return lda
# 
# def generate_doc_codes(model, corpus, output):
#     model.minimum_probability = 1e-3
#     n_topics = model.num_topics
#     doc_codes = {}
#     for key, doc_bow in corpus.iteritems():
#         code = np.zeros(n_topics)
#         for idx, val in model[doc_bow]:
#             code[idx] = val
#         doc_codes[key] = code.tolist()
#     dump_json(doc_codes, output)
# 
#     return doc_codes
# 
# def load_model(model_file):
#     return LdaModel.load(model_file)
# 
# def show_topics(model, n_words_per_topic=10):
#     n_topics = model.num_topics
#     topics = [zip(*model.show_topic(i, n_words_per_topic))[0] for i in range(n_topics)]
# 
#     return topics
# 
# def show_topics_prob(model, n_words_per_topic=10):
#     n_topics = model.num_topics
#     topics = [model.show_topic(i, n_words_per_topic) for i in range(n_topics)]
# 
#     return topics
# 
# def calc_pairwise_cosine(model):
#     n = model.num_topics
#     weights = model.state.get_lambda()
#     weights = np.apply_along_axis(lambda x: x / x.sum(), 1, weights) # get dist.
#     weights = unitmatrix(weights) # normalize
#     score = []
#     for i in range(n):
#         for j in range(i + 1, n):
#             score.append(np.arccos(weights[i].dot(weights[j])))
# 
#     return np.mean(score), np.std(score)
# 
# def calc_pairwise_dev(model):
#     # the average squared deviation from 0 (90 degree)
#     n = model.num_topics
#     weights = model.state.get_lambda()
#     weights = np.apply_along_axis(lambda x: x / x.sum(), 1, weights) # get dist.
#     weights = unitmatrix(weights) # normalize
#     score = 0.
#     for i in range(n):
#         for j in range(i + 1, n):
#             score += (weights[i].dot(weights[j]))**2
# 
#     return np.sqrt(2. * score / n / (n - 1))
# 
# Path: autoencoder/testing/visualize.py
# def word_cloud(word_embedding_matrix, vocab, s, save_file='scatter.png'):
#     words = [(i, vocab[i]) for i in s]
#     model = TSNE(n_components=2, random_state=0)
#     #Note that the following line might use a good chunk of RAM
#     tsne_embedding = model.fit_transform(word_embedding_matrix)
#     words_vectors = tsne_embedding[np.array([item[1] for item in words])]
# 
#     plt.subplots_adjust(bottom = 0.1)
#     plt.scatter(
#         words_vectors[:, 0], words_vectors[:, 1], marker='o', cmap=plt.get_cmap('Spectral'))
# 
#     for label, x, y in zip(s, words_vectors[:, 0], words_vectors[:, 1]):
#         plt.annotate(
#             label,
#             xy=(x, y), xytext=(-20, 20),
#             textcoords='offset points', ha='right', va='bottom',
#             fontsize=20,
#             # bbox=dict(boxstyle='round,pad=1.', fc='yellow', alpha=0.5),
#             arrowprops=dict(arrowstyle = '<-', connectionstyle='arc3,rad=0')
#             )
#     plt.show()
#     # plt.savefig(save_file)
# 
# 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: run_lda.py
import argparse
import timeit
import math
import numpy as np
from os import path
from autoencoder.preprocessing.preprocessing import load_corpus
from autoencoder.utils.io_utils import dump_json, write_file
from autoencoder.baseline.lda import train_lda, generate_doc_codes, load_model, show_topics, show_topics_prob, calc_pairwise_cosine, calc_pairwise_dev
from autoencoder.testing.visualize import word_cloud
from autoencoder.utils.op_utils import unitmatrix

'''
Created on Nov, 2016

@author: hugo

'''
from __future__ import absolute_import



def train(args):

    corpus = load_corpus(args.corpus)