# Path: autoencoder/core/ae.py
# class AutoEncoder(object):
#     def __init__(self, input_size, dim, comp_topk=None, ctype=None, save_model='best_model'):
#     def build(self):
#     def fit(self, train_X, val_X, nb_epoch=50, batch_size=100, contractive=None):
# def save_ae_model(model, model_file):
# def load_ae_model(model_file):
# 
# Path: autoencoder/preprocessing/preprocessing.py
# def load_corpus(corpus_path):
#     corpus = load_json(corpus_path)
# 
#     return corpus
# 
# def doc2vec(doc, dim):
#     vec = np.zeros(dim)
#     for idx, val in doc.items():
#         vec[int(idx)] = val
# 
#     return vec
# 
# Path: autoencoder/utils/op_utils.py
# def vecnorm(vec, norm, epsilon=1e-3):
#     """
#     Scale a vector to unit length. The only exception is the zero vector, which
#     is returned back unchanged.
#     """
#     if norm not in ('prob', 'max1', 'logmax1'):
#         raise ValueError("'%s' is not a supported norm. Currently supported norms include 'prob',\
#              'max1' and 'logmax1'." % norm)
# 
#     if isinstance(vec, np.ndarray):
#         vec = np.asarray(vec, dtype=float)
#         if norm == 'prob':
#             veclen = np.sum(np.abs(vec)) + epsilon * len(vec) # smoothing
#         elif norm == 'max1':
#             veclen = np.max(vec) + epsilon
#         elif norm == 'logmax1':
#             vec = np.log10(1. + vec)
#             veclen = np.max(vec) + epsilon
#         if veclen > 0.0:
#             return (vec + epsilon) / veclen
#         else:
#             return vec
#     else:
#         raise ValueError('vec should be ndarray, found: %s' % type(vec))
# 
# def revdict(d):
#     """
#     Reverse a dictionary mapping.
#     When two keys map to the same value, only one of them will be kept in the
#     result (which one is kept is arbitrary).
#     """
#     return dict((v, k) for (k, v) in d.iteritems())
# 
# 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/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


import argparse
import math
import numpy as np
from autoencoder.core.ae import AutoEncoder, load_model
from autoencoder.preprocessing.preprocessing import load_corpus, doc2vec
from autoencoder.utils.op_utils import vecnorm, revdict, unitmatrix #, corrupted_matrix
from autoencoder.utils.io_utils import dump_json, write_file
        print

def test(args):
    corpus = load_corpus(args.input)
    vocab, docs = corpus['vocab'], corpus['docs']
    n_vocab = len(vocab)

    doc_keys = docs.keys()
    X_docs = []
    for k in doc_keys:
        X_docs.append(vecnorm(doc2vec(docs[k], n_vocab), 'logmax1', 0))
        del docs[k]
    X_docs = np.r_[X_docs]

    model = AutoEncoder
    # model = DeepAutoEncoder
    ae = load_model(model, args.load_arch, args.load_weights)

    doc_codes = ae.encoder.predict(X_docs)
    dump_json(dict(zip(doc_keys, doc_codes.tolist())), args.output)
    print 'Saved doc codes file to %s' % args.output

    if args.save_topics:
        topics_strength = get_topics_strength(ae, revdict(vocab), topn=10)
        save_topics_strength(topics_strength, args.save_topics)
        # topics = get_topics(ae, revdict(vocab), topn=10)
        # write_file(topics, args.save_topics)
        print 'Saved topics file to %s' % args.save_topics

    if args.sample_words:

# Path: autoencoder/core/ae.py
# class AutoEncoder(object):
#     def __init__(self, input_size, dim, comp_topk=None, ctype=None, save_model='best_model'):
#     def build(self):
#     def fit(self, train_X, val_X, nb_epoch=50, batch_size=100, contractive=None):
# def save_ae_model(model, model_file):
# def load_ae_model(model_file):
# 
# Path: autoencoder/preprocessing/preprocessing.py
# def load_corpus(corpus_path):
#     corpus = load_json(corpus_path)
# 
#     return corpus
# 
# def doc2vec(doc, dim):
#     vec = np.zeros(dim)
#     for idx, val in doc.items():
#         vec[int(idx)] = val
# 
#     return vec
# 
# Path: autoencoder/utils/op_utils.py
# def vecnorm(vec, norm, epsilon=1e-3):
#     """
#     Scale a vector to unit length. The only exception is the zero vector, which
#     is returned back unchanged.
#     """
#     if norm not in ('prob', 'max1', 'logmax1'):
#         raise ValueError("'%s' is not a supported norm. Currently supported norms include 'prob',\
#              'max1' and 'logmax1'." % norm)
# 
#     if isinstance(vec, np.ndarray):
#         vec = np.asarray(vec, dtype=float)
#         if norm == 'prob':
#             veclen = np.sum(np.abs(vec)) + epsilon * len(vec) # smoothing
#         elif norm == 'max1':
#             veclen = np.max(vec) + epsilon
#         elif norm == 'logmax1':
#             vec = np.log10(1. + vec)
#             veclen = np.max(vec) + epsilon
#         if veclen > 0.0:
#             return (vec + epsilon) / veclen
#         else:
#             return vec
#     else:
#         raise ValueError('vec should be ndarray, found: %s' % type(vec))
# 
# def revdict(d):
#     """
#     Reverse a dictionary mapping.
#     When two keys map to the same value, only one of them will be kept in the
#     result (which one is kept is arbitrary).
#     """
#     return dict((v, k) for (k, v) in d.iteritems())
# 
# 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/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: legacy_pred.py
import argparse
import math
import numpy as np
from autoencoder.core.ae import AutoEncoder, load_model
from autoencoder.preprocessing.preprocessing import load_corpus, doc2vec
from autoencoder.utils.op_utils import vecnorm, revdict, unitmatrix #, corrupted_matrix
from autoencoder.utils.io_utils import dump_json, write_file

        print

def test(args):
    corpus = load_corpus(args.input)
    vocab, docs = corpus['vocab'], corpus['docs']
    n_vocab = len(vocab)

    doc_keys = docs.keys()
    X_docs = []
    for k in doc_keys:
        X_docs.append(vecnorm(doc2vec(docs[k], n_vocab), 'logmax1', 0))
        del docs[k]
    X_docs = np.r_[X_docs]

    model = AutoEncoder
    # model = DeepAutoEncoder
    ae = load_model(model, args.load_arch, args.load_weights)

    doc_codes = ae.encoder.predict(X_docs)
    dump_json(dict(zip(doc_keys, doc_codes.tolist())), args.output)
    print 'Saved doc codes file to %s' % args.output

    if args.save_topics:
        topics_strength = get_topics_strength(ae, revdict(vocab), topn=10)
        save_topics_strength(topics_strength, args.save_topics)
        # topics = get_topics(ae, revdict(vocab), topn=10)
        # write_file(topics, args.save_topics)
        print 'Saved topics file to %s' % args.save_topics

    if args.sample_words:

        revocab = revdict(vocab)