# Path: autoencoder/testing/retrieval.py
# def retrieval(X_train, Y_train, X_test, Y_test, fractions=[0.01, 0.5, 1.0], multilabel=False):
#     db_size = len(X_train)
#     n_queries = len(X_test)
#     X_train = unitmatrix(X_train) # normalize
#     X_test = unitmatrix(X_test)
#     score = X_test.dot(X_train.T)
#     X_train = None
#     X_test = None
#     precisions = defaultdict(float)
# 
#     for idx in range(n_queries):
#         retrieval_idx = score[idx].argsort()[::-1]
#         target = Y_test[idx]
#         for fr in fractions:
#             ntop = int(fr * db_size)
#             pr = float(len([i for i in retrieval_idx[:ntop] if hit(Y_train[i], target, multilabel)])) / ntop
#             precisions[fr] += pr
#     precisions = dict([(x, y / n_queries) for x, y in precisions.iteritems()])
# 
#     return sorted(precisions.items(), key=lambda d:d[0])
# 
# def retrieval_by_doclength(X_train, Y_train, X_test, Y_test, len_test, fraction=0.001, len_bin=600, multilabel=False):
#     X_train = unitmatrix(X_train) # normalize
#     X_test = unitmatrix(X_test)
#     score = X_test.dot(X_train.T)
#     precisions = defaultdict(list)
#     n_queries = len(X_test)
#     ntop = int(fraction * len(X_train))
#     # bins = [50, 100, 200, 300, 500, 1000, 2000, 3000, 4000, 5000]
#     bins = [100, 120, 150, 200, 300, 1000, 1500, 2000, 4000]
# 
#     for idx in range(n_queries):
#         retrieval_idx = score[idx].argsort()[::-1]
#         pr = float(len([i for i in retrieval_idx[:ntop] if hit(Y_train[i], Y_test[idx], multilabel)])) / ntop
#         for each in bins:
#             if len_test[idx] < each:
#                 precisions[each].append(pr)
#                 break
#     import pdb;pdb.set_trace()
#     precisions = dict([(x, sum(y) / len(y)) for x, y in precisions.iteritems()])
# 
#     return sorted(precisions.items(), key=lambda d:d[0])
# 
# 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
# 
# 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: autoencoder/preprocessing/preprocessing.py
# def load_corpus(corpus_path):
#     corpus = load_json(corpus_path)
# 
#     return corpus


import argparse
import numpy as np
    import pdb;pdb.set_trace()
from keras.utils import np_utils
from autoencoder.testing.retrieval import retrieval, retrieval_by_doclength
from autoencoder.utils.io_utils import load_json, load_marshal
from autoencoder.preprocessing.preprocessing import load_corpus
    #     X_test.append([float(x) for x in each])

    # X_train = np.r_[X_train]
    # Y_train = np.array([train_doc_labels[i] for i in train_doc_codes])
    # X_test = np.r_[X_test]
    # Y_test = np.array([test_doc_labels[i] for i in test_doc_codes])


    # # DBN
    # X_train = np.array(load_marshal(args.train_doc_codes))
    # Y_train = np.array(load_marshal(args.train_doc_labels))
    # X_test = np.array(load_marshal(args.test_doc_codes))
    # Y_test = np.array(load_marshal(args.test_doc_labels))


    seed = 7
    np.random.seed(seed)
    val_idx = np.random.choice(range(X_train.shape[0]), args.n_val, replace=False)
    train_idx = list(set(range(X_train.shape[0])) - set(val_idx))
    X_new_train = X_train[train_idx]
    Y_new_train = Y_train[train_idx]
    X_new_val = X_train[val_idx]
    Y_new_val = Y_train[val_idx]
    print 'train: %s, val: %s, test: %s' % (X_new_train.shape[0], X_new_val.shape[0], X_test.shape[0])

    results = retrieval(X_new_train, Y_new_train, X_new_val, Y_new_val,\
                        fractions=[0.001], multilabel=args.multilabel)
    print 'precision on val set: %s' % results

    if not args.query_info:

# Path: autoencoder/testing/retrieval.py
# def retrieval(X_train, Y_train, X_test, Y_test, fractions=[0.01, 0.5, 1.0], multilabel=False):
#     db_size = len(X_train)
#     n_queries = len(X_test)
#     X_train = unitmatrix(X_train) # normalize
#     X_test = unitmatrix(X_test)
#     score = X_test.dot(X_train.T)
#     X_train = None
#     X_test = None
#     precisions = defaultdict(float)
# 
#     for idx in range(n_queries):
#         retrieval_idx = score[idx].argsort()[::-1]
#         target = Y_test[idx]
#         for fr in fractions:
#             ntop = int(fr * db_size)
#             pr = float(len([i for i in retrieval_idx[:ntop] if hit(Y_train[i], target, multilabel)])) / ntop
#             precisions[fr] += pr
#     precisions = dict([(x, y / n_queries) for x, y in precisions.iteritems()])
# 
#     return sorted(precisions.items(), key=lambda d:d[0])
# 
# def retrieval_by_doclength(X_train, Y_train, X_test, Y_test, len_test, fraction=0.001, len_bin=600, multilabel=False):
#     X_train = unitmatrix(X_train) # normalize
#     X_test = unitmatrix(X_test)
#     score = X_test.dot(X_train.T)
#     precisions = defaultdict(list)
#     n_queries = len(X_test)
#     ntop = int(fraction * len(X_train))
#     # bins = [50, 100, 200, 300, 500, 1000, 2000, 3000, 4000, 5000]
#     bins = [100, 120, 150, 200, 300, 1000, 1500, 2000, 4000]
# 
#     for idx in range(n_queries):
#         retrieval_idx = score[idx].argsort()[::-1]
#         pr = float(len([i for i in retrieval_idx[:ntop] if hit(Y_train[i], Y_test[idx], multilabel)])) / ntop
#         for each in bins:
#             if len_test[idx] < each:
#                 precisions[each].append(pr)
#                 break
#     import pdb;pdb.set_trace()
#     precisions = dict([(x, sum(y) / len(y)) for x, y in precisions.iteritems()])
# 
#     return sorted(precisions.items(), key=lambda d:d[0])
# 
# 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
# 
# 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: autoencoder/preprocessing/preprocessing.py
# def load_corpus(corpus_path):
#     corpus = load_json(corpus_path)
# 
#     return corpus


# Path: run_doc_retrieval.py
import argparse
import numpy as np
    import pdb;pdb.set_trace()
from keras.utils import np_utils
from autoencoder.testing.retrieval import retrieval, retrieval_by_doclength
from autoencoder.utils.io_utils import load_json, load_marshal
from autoencoder.preprocessing.preprocessing import load_corpus

    #     X_test.append([float(x) for x in each])

    # X_train = np.r_[X_train]
    # Y_train = np.array([train_doc_labels[i] for i in train_doc_codes])
    # X_test = np.r_[X_test]
    # Y_test = np.array([test_doc_labels[i] for i in test_doc_codes])


    # # DBN
    # X_train = np.array(load_marshal(args.train_doc_codes))
    # Y_train = np.array(load_marshal(args.train_doc_labels))
    # X_test = np.array(load_marshal(args.test_doc_codes))
    # Y_test = np.array(load_marshal(args.test_doc_labels))


    seed = 7
    np.random.seed(seed)
    val_idx = np.random.choice(range(X_train.shape[0]), args.n_val, replace=False)
    train_idx = list(set(range(X_train.shape[0])) - set(val_idx))
    X_new_train = X_train[train_idx]
    Y_new_train = Y_train[train_idx]
    X_new_val = X_train[val_idx]
    Y_new_val = Y_train[val_idx]
    print 'train: %s, val: %s, test: %s' % (X_new_train.shape[0], X_new_val.shape[0], X_test.shape[0])

    results = retrieval(X_new_train, Y_new_train, X_new_val, Y_new_val,\
                        fractions=[0.001], multilabel=args.multilabel)
    print 'precision on val set: %s' % results

    if not args.query_info:

        results = retrieval(X_train, Y_train, X_test, Y_test,\