# 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


EPSILON = 1e-8

def load_data(filename='result/qid.img.lable.feature.txt'):
    res = []
    qid = None
    labels = []
    vecs = []
    
    for line in open(filename):
        elems = line.strip().split()
        if qid != elems[0]: # meet a new query
            if len(labels)>0:
                nr_of_images = len(labels)
                feat_dim = len(vecs[0])
                scores = np.zeros((nr_of_images, feat_dim))
                for i in range(nr_of_images):
                    scores[i,:] = vecs[i]
                res.append((qid, labels, scores))

# 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


EPSILON = 1e-8

def load_data(filename='result/qid.img.lable.feature.txt'):
    res = []
    qid = None
    labels = []
    vecs = []
    
    for line in open(filename):
        elems = line.strip().split()
        if qid != elems[0]: # meet a new query
            if len(labels)>0:
                nr_of_images = len(labels)
                feat_dim = len(vecs[0])
                scores = np.zeros((nr_of_images, feat_dim))
                for i in range(nr_of_images):
                    scores[i,:] = vecs[i]
                res.append((qid, labels, scores))

                labels = []
