# Path: basic/metric.py
# def getScorer(name):
#     mapping = {"P":PrecisionScorer, "AP":APScorer, "RR":RRScorer,  "NDCG":NDCGScorer, "DCG":DCGScorer}
#     elems = name.split("@")
#     if len(elems) == 2:
#         k = int(elems[1])
#     else:
#         k = 0
#     return mapping[elems[0]](k)
# 
# Path: basic/common.py
# def checkToSkip(filename, overwrite):
#     if os.path.exists(filename):
#         print ("%s exists." % filename),
#         if overwrite:
#             print ("overwrite")
#             return 0
#         else:
#             print ("skip")
#             return 1
#     return 0    
# 
# def makedirsforfile(filename):
#     try:
#         os.makedirs(os.path.split(filename)[0])
#     except:
#         pass


import os
import sys
import random
import numpy as np
from basic.metric import getScorer
from basic.common import checkToSkip, makedirsforfile
    from optparse import OptionParser

            qid = elems[0]
            
        labels.append(int(elems[2]))
        vecs.append(map(float, elems[3:]))
    
    scores = np.zeros((len(labels), len(vecs[0])))
    for i in range(len(labels)):
        scores[i,:] = vecs[i]
    res.append((qid, labels, scores))
    return res    


def eval(data, weights):
    scorer = getScorer('DCG@25')
    feat_dim = data[0][-1].shape[1]
    nr_of_qry = len(data)

    mean_perf = [0] * feat_dim
    avg_perf = 0

    for i in range(nr_of_qry):
        qid, labels, scores = data[i]
        fusion = scores.dot(weights)
        sorted_idx = (0-fusion).argsort()
        sorted_labels = [labels[x] for x in sorted_idx]
        avg_perf += scorer.score(sorted_labels)
    return avg_perf / nr_of_qry


def eval_file(data, weights, file_name):

# Path: basic/metric.py
# def getScorer(name):
#     mapping = {"P":PrecisionScorer, "AP":APScorer, "RR":RRScorer,  "NDCG":NDCGScorer, "DCG":DCGScorer}
#     elems = name.split("@")
#     if len(elems) == 2:
#         k = int(elems[1])
#     else:
#         k = 0
#     return mapping[elems[0]](k)
# 
# Path: basic/common.py
# def checkToSkip(filename, overwrite):
#     if os.path.exists(filename):
#         print ("%s exists." % filename),
#         if overwrite:
#             print ("overwrite")
#             return 0
#         else:
#             print ("skip")
#             return 1
#     return 0    
# 
# def makedirsforfile(filename):
#     try:
#         os.makedirs(os.path.split(filename)[0])
#     except:
#         pass


# Path: weightOptimization.py
import os
import sys
import random
import numpy as np
from basic.metric import getScorer
from basic.common import checkToSkip, makedirsforfile
    from optparse import OptionParser

            qid = elems[0]
            
        labels.append(int(elems[2]))
        vecs.append(map(float, elems[3:]))
    
    scores = np.zeros((len(labels), len(vecs[0])))
    for i in range(len(labels)):
        scores[i,:] = vecs[i]
    res.append((qid, labels, scores))
    return res    


def eval(data, weights):
    scorer = getScorer('DCG@25')
    feat_dim = data[0][-1].shape[1]
    nr_of_qry = len(data)

    mean_perf = [0] * feat_dim
    avg_perf = 0

    for i in range(nr_of_qry):
        qid, labels, scores = data[i]
        fusion = scores.dot(weights)
        sorted_idx = (0-fusion).argsort()
        sorted_labels = [labels[x] for x in sorted_idx]
        avg_perf += scorer.score(sorted_labels)
    return avg_perf / nr_of_qry


def eval_file(data, weights, file_name):

    scorer = getScorer('DCG')
