# 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

    for i in range(1):
        perf, alpha = coordinate_ascent(data, sigma)
        if perf > best_perf:
            best_perf = perf
            best_alpha = alpha
        print '*'*70
    print 'optimized wights:', ' '.join(['%g'%x for x in best_alpha])
    print 'best tuned performance:', best_perf

    open(weightFile, 'w').write(' '.join(map(str,best_alpha)))
    print 'optimized wight parameters have written into %s' % weightFile



def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    parser = OptionParser(usage="""usage: %prog [options] """)

    parser.add_option("--overwrite", default=0, type="int", help="overwrite existing file (default: 0)")
    parser.add_option("--inputeFile", default='qid.img.lable.feature.txt', type="string", help="file stored all score from different methods")
    parser.add_option("--weightFile", default='optimized_wights.txt', type="string", help="optimized wight will be written in the file")
    
    (options, args) = parser.parse_args(argv)
           
    return process(options)


if __name__ == "__main__":

# 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

    for i in range(1):
        perf, alpha = coordinate_ascent(data, sigma)
        if perf > best_perf:
            best_perf = perf
            best_alpha = alpha
        print '*'*70
    print 'optimized wights:', ' '.join(['%g'%x for x in best_alpha])
    print 'best tuned performance:', best_perf

    open(weightFile, 'w').write(' '.join(map(str,best_alpha)))
    print 'optimized wight parameters have written into %s' % weightFile



def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    parser = OptionParser(usage="""usage: %prog [options] """)

    parser.add_option("--overwrite", default=0, type="int", help="overwrite existing file (default: 0)")
    parser.add_option("--inputeFile", default='qid.img.lable.feature.txt', type="string", help="file stored all score from different methods")
    parser.add_option("--weightFile", default='optimized_wights.txt', type="string", help="optimized wight will be written in the file")
    
    (options, args) = parser.parse_args(argv)
           
    return process(options)


if __name__ == "__main__":

    sys.exit(main())    
