# 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 numpy as np
    import pdb;pdb.set_trace()
from collections import defaultdict, Counter
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from autoencoder.utils.op_utils import unitmatrix

'''
from __future__ import absolute_import
# from keras.optimizers import Adam



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

# 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: autoencoder/testing/retrieval.py
import numpy as np
    import pdb;pdb.set_trace()
from collections import defaultdict, Counter
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from autoencoder.utils.op_utils import unitmatrix


'''
from __future__ import absolute_import
# from keras.optimizers import Adam



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)