# Path: autoencoder/testing/classifier.py
# def multiclass_classifier(X_train, Y_train, X_val, Y_val, X_test, Y_test, nb_epoch=200, batch_size=10, seed=7):
#     clf = softmax_network(X_train.shape[1], Y_train.shape[1])
#     clf.fit(X_train, Y_train,
#                         epochs=nb_epoch,
#                         batch_size=batch_size,
#                         shuffle=True,
#                         validation_data=(X_val, Y_val),
#                         callbacks=[
#                                     ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.01),
#                                     EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=5, verbose=0, mode='auto'),
#                         ]
#                         )
#     acc = clf.test_on_batch(X_test, Y_test)[1]
#     # confusion matrix and precision-recall
#     true = np.argmax(Y_test,axis=1)
#     pred = np.argmax(clf.predict(X_test), axis=1)
#     print confusion_matrix(true, pred)
#     print classification_report(true, pred)
#     return acc
# 
# def multilabel_classifier(X_train, Y_train, X_val, Y_val, X_test, Y_test, nb_epoch=200, batch_size=10, seed=7):
#     clf = sigmoid_network(X_train.shape[1], Y_train.shape[1])
#     clf.fit(X_train, Y_train,
#                         nb_epoch=nb_epoch,
#                         batch_size=batch_size,
#                         shuffle=True,
#                         validation_data=(X_val, Y_val),
#                         callbacks=[
#                                     ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.01),
#                                     EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=5, verbose=0, mode='auto'),
#                         ]
#                         )
#     pred = clf.predict(X_test)
#     pred = (pred > .5) * 1
#     macro_f1 = f1_score(Y_test, pred, average='macro')
#     micro_f1 = f1_score(Y_test, pred, average='micro')
# 
#     return [macro_f1, micro_f1]
# 
# 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_pickle(path_to_file):
#     try:
#         with open(path_to_file, 'r') as f:
#             data = pickle.load(f)
#     except Exception as e:
#         raise e
# 
#     return data


import argparse
import numpy as np
    import pdb;pdb.set_trace()
from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.model_selection import ShuffleSplit
from autoencoder.testing.classifier import multiclass_classifier, multilabel_classifier
from autoencoder.utils.io_utils import load_json, load_pickle
    # Y_train = load_pickle(args.train_doc_labels)
    # X_val = np.array(load_pickle(args.val_doc_codes))
    # Y_val = load_pickle(args.val_doc_labels)
    # X_test = np.array(load_pickle(args.test_doc_codes))
    # Y_test = load_pickle(args.test_doc_labels)

    if args.multilabel_clf:
        encoder = MultiLabelBinarizer()
        encoder.fit(Y_train + Y_val + Y_test)
        Y_train = encoder.transform(Y_train)
        Y_val = encoder.transform(Y_val)
        Y_test = encoder.transform(Y_test)
    else:
        Y = Y_train + Y_val + Y_test
        n_train = len(Y_train)
        n_val = len(Y_val)
        n_test = len(Y_test)
        encoder = LabelEncoder()
        Y = np_utils.to_categorical(encoder.fit_transform(Y))
        Y_train = Y[:n_train]
        Y_val = Y[n_train:n_train + n_val]
        Y_test = Y[-n_test:]

    seed = 7
    print 'train: %s, val: %s, test: %s' % (X_train.shape[0], X_val.shape[0], X_test.shape[0])
    if args.multilabel_clf:
        results = multilabel_classifier(X_train, Y_train, X_val, Y_val, \
                X_test, Y_test, nb_epoch=args.n_epoch, batch_size=args.batch_size, seed=seed)
        print 'f1 score on test set: macro_f1: %s, micro_f1: %s' % tuple(results)
    else:

# Path: autoencoder/testing/classifier.py
# def multiclass_classifier(X_train, Y_train, X_val, Y_val, X_test, Y_test, nb_epoch=200, batch_size=10, seed=7):
#     clf = softmax_network(X_train.shape[1], Y_train.shape[1])
#     clf.fit(X_train, Y_train,
#                         epochs=nb_epoch,
#                         batch_size=batch_size,
#                         shuffle=True,
#                         validation_data=(X_val, Y_val),
#                         callbacks=[
#                                     ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.01),
#                                     EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=5, verbose=0, mode='auto'),
#                         ]
#                         )
#     acc = clf.test_on_batch(X_test, Y_test)[1]
#     # confusion matrix and precision-recall
#     true = np.argmax(Y_test,axis=1)
#     pred = np.argmax(clf.predict(X_test), axis=1)
#     print confusion_matrix(true, pred)
#     print classification_report(true, pred)
#     return acc
# 
# def multilabel_classifier(X_train, Y_train, X_val, Y_val, X_test, Y_test, nb_epoch=200, batch_size=10, seed=7):
#     clf = sigmoid_network(X_train.shape[1], Y_train.shape[1])
#     clf.fit(X_train, Y_train,
#                         nb_epoch=nb_epoch,
#                         batch_size=batch_size,
#                         shuffle=True,
#                         validation_data=(X_val, Y_val),
#                         callbacks=[
#                                     ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.01),
#                                     EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=5, verbose=0, mode='auto'),
#                         ]
#                         )
#     pred = clf.predict(X_test)
#     pred = (pred > .5) * 1
#     macro_f1 = f1_score(Y_test, pred, average='macro')
#     micro_f1 = f1_score(Y_test, pred, average='micro')
# 
#     return [macro_f1, micro_f1]
# 
# 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_pickle(path_to_file):
#     try:
#         with open(path_to_file, 'r') as f:
#             data = pickle.load(f)
#     except Exception as e:
#         raise e
# 
#     return data


# Path: run_clf.py
import argparse
import numpy as np
    import pdb;pdb.set_trace()
from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.model_selection import ShuffleSplit
from autoencoder.testing.classifier import multiclass_classifier, multilabel_classifier
from autoencoder.utils.io_utils import load_json, load_pickle

    # Y_train = load_pickle(args.train_doc_labels)
    # X_val = np.array(load_pickle(args.val_doc_codes))
    # Y_val = load_pickle(args.val_doc_labels)
    # X_test = np.array(load_pickle(args.test_doc_codes))
    # Y_test = load_pickle(args.test_doc_labels)

    if args.multilabel_clf:
        encoder = MultiLabelBinarizer()
        encoder.fit(Y_train + Y_val + Y_test)
        Y_train = encoder.transform(Y_train)
        Y_val = encoder.transform(Y_val)
        Y_test = encoder.transform(Y_test)
    else:
        Y = Y_train + Y_val + Y_test
        n_train = len(Y_train)
        n_val = len(Y_val)
        n_test = len(Y_test)
        encoder = LabelEncoder()
        Y = np_utils.to_categorical(encoder.fit_transform(Y))
        Y_train = Y[:n_train]
        Y_val = Y[n_train:n_train + n_val]
        Y_test = Y[-n_test:]

    seed = 7
    print 'train: %s, val: %s, test: %s' % (X_train.shape[0], X_val.shape[0], X_test.shape[0])
    if args.multilabel_clf:
        results = multilabel_classifier(X_train, Y_train, X_val, Y_val, \
                X_test, Y_test, nb_epoch=args.n_epoch, batch_size=args.batch_size, seed=seed)
        print 'f1 score on test set: macro_f1: %s, micro_f1: %s' % tuple(results)
    else:

        results = multiclass_classifier(X_train, Y_train, X_val, Y_val, \