def SOP(alpha, teta, nbBasket, nbReco):
	
	data = load()
	###############################################################
	# CREATE MODELS
	###############################################################
	print 'Create the model based on the training set'
	
	modelSOP = processing.SOPRecoModel(data.getUserItemMatrix(), alpha, teta)
	modelSOP.launch()
		
	###############################################################
	# SET RECOMMENDATION
	###############################################################
	if nbBasket == -1:
		evalSOP = processing.Evaluation(modelSOP, data.getBasketItemList(), nbReco)
	else :
		evalSOP = processing.Evaluation(modelSOP, data.getBasketItemList()[:nbBasket], nbReco)
	
	###############################################################
	# LAUNCH RECOMMENDATION + SAVE RESULTS
	###############################################################	
	t = time.time()
	evalSOP.newEval()
	SOPTime = time.time()-t
	mmwrite('SOPPerf_a%s_t%s_nb%s_nr%s'%(alpha,teta,nbBasket,nbReco),evalSOP.perf) 
	
	print 'SOP Execution time:', SOPTime
	print 'Performances : '
	print evalSOP.testNames
	print evalSOP.meanPerf()
	evalSOP.savePerf('SOPPerf_a%s_t%s_nb%s_nr%s.txt'%(alpha,teta,nbBasket,nbReco))
	return evalSOP
def RWWR(alpha, nbBasket, nbReco):
	data = load()
	###############################################################
	# CREATE MODELS
	###############################################################
	print 'Create the model based on the training set'
	
	modelRWWR = processing.RandomWalkWithRestartRecoModel(data.getUserItemMatrix(), alpha)
		
	###############################################################
	# SET RECOMMENDATION
	###############################################################
	if nbBasket == -1:
		evalRWWR = processing.Evaluation(modelRWWR, data.getBasketItemList(), nbReco)
	else :
		evalRWWR = processing.Evaluation(modelRWWR, data.getBasketItemList()[:nbBasket], nbReco)
	
	###############################################################
	# LAUNCH RECOMMENDATION + SAVE RESULTS
	###############################################################	
	t = time.time()
	evalRWWR.newEval()
	RWWRTime = time.time()-t
	mmwrite('RWWR_a%s_nb%s'%(alpha, nbBasket),evalRWWR.perf) 
	
	print 'RWWR Execution time:', RWWRTime
	print 'Performances :'
	print evalRWWR.testNames
	print evalRWWR.meanPerf()
	evalRWWR.savePerf('RWWR_a%s_nb%s'%(alpha, nbBasket))
	return evalRWWR
def Pop(nbBasket, nbReco):
	data = load()
	###############################################################
	# CREATE MODELS
	###############################################################
	print 'Create the model based on the training set'
	
	modelPop = processing.PopRecoModel(data.getUserItemMatrix())
		
	###############################################################
	# SET RECOMMENDATION
	###############################################################
	if nbBasket == -1:
		evalPop = processing.Evaluation(modelPop, data.getBasketItemList(), nbReco)
	else :
		evalPop = processing.Evaluation(modelPop, data.getBasketItemList()[:nbBasket], nbReco)
	
	###############################################################
	# LAUNCH RECOMMENDATION + SAVE RESULTS
	###############################################################	
	t = time.time()
	evalPop.newEval()
	PopTime = time.time()-t
	mmwrite('Pop',evalPop.perf) 
	
	print 'Pop Execution time:', PopTime
	print 'Performances :'
	print evalPop.testNames
	print evalPop.meanPerf()
	evalPop.savePerf('Pop.txt')
	return evalPop
def RW_POP(alpha, nbBasket, nbReco):
    data = load()
    ###############################################################
    # CREATE MODELS
    ###############################################################
    print 'Create the model based on the training set'

    modelRW = processing.BasketRandomWalk_POP(data.getUserItemMatrix(), alpha)

    ###############################################################
    # SET RECOMMENDATION
    ###############################################################
    if nbBasket == -1:
        evalRW = processing.Evaluation(modelRW, data.getBasketItemList(),
                                       nbReco)
    else:
        evalRW = processing.Evaluation(modelRW,
                                       data.getBasketItemList()[:nbBasket],
                                       nbReco)

    ###############################################################
    # LAUNCH RECOMMENDATION + SAVE RESULTS
    ###############################################################
    t = time.time()
    evalRW.newEval()
    RWTime = time.time() - t
    mmwrite(resultFolder + 'RW_POP_a%s_nb%s' % (alpha, nbBasket), evalRW.perf)

    print 'RW_POP Execution time:', RWTime
    print 'Performances :'
    print evalRW.testNames
    print evalRW.computePerf()
    evalRW.savePerf(resultFolder + 'RW_POP_a%s_nb%s.txt' % (alpha, nbBasket))
    return evalRW
def CondProb(alpha, nbBasket, nbReco):
	data = load()
	###############################################################
	# CREATE MODELS
	###############################################################
	print 'Create the model based on the training set'
	
	modelCondProb = processing.CondProbRecoModel(data.getUserItemMatrix(), alpha)
		
	###############################################################
	# SET RECOMMENDATION
	###############################################################
	if nbBasket == -1:
		evalCondProb = processing.Evaluation(modelCondProb, data.getBasketItemList(), nbReco)
	else :
		evalCondProb = processing.Evaluation(modelCondProb, data.getBasketItemList()[:nbBasket], nbReco)
	
	###############################################################
	# LAUNCH RECOMMENDATION + SAVE RESULTS
	###############################################################	
	t = time.time()
	evalCondProb.newEval()
	CondProbTime = time.time()-t
	mmwrite('CondProb_a%s_nb%s'%(alpha, nbBasket),evalCondProb.perf) 
	
	print 'Cond Prob Execution time:', CondProbTime
	print 'Performances :'
	print evalCondProb.testNames
	print evalCondProb.meanPerf()
	evalCondProb.savePerf('CondProb_a%s_nb%s.txt'%(alpha, nbBasket))
	return evalCondProb
def pLSA(Z, nbBasket, nbReco):
    data = load()
    ###############################################################
    # CREATE MODELS
    ###############################################################
    print 'Create the model based on the training set'

    if not os.path.exists('pLSAModel_Z%s.dat' % Z):
        modelpLSA = processing.PLSARecoModel(data.getUserItemMatrix(), Z)
        modelpLSA.train()

        file = open('pLSAModel_Z%s.dat' % Z, 'w')
        pickle.dump(modelpLSA.pLSAmodel.get_model(), file)
        file.close()
    else:
        modelpLSA = processing.PLSARecoModel(data.getUserItemMatrix(), Z)
        file = open('pLSAModel_Z%s.dat' % Z, 'r')
        modelpLSA.pLSAmodel.set_model(pickle.load(file))
        file.close()

    ###############################################################
    # SET RECOMMENDATION
    ###############################################################
    if nbBasket == -1:
        evalpLSA = processing.Evaluation(modelpLSA, data.getBasketItemList(),
                                         nbReco)
    else:
        evalpLSA = processing.Evaluation(modelpLSA,
                                         data.getBasketItemList()[:nbBasket],
                                         nbReco)

    ###############################################################
    # LAUNCH RECOMMENDATION + SAVE RESULTS
    ###############################################################
    t = time.time()
    evalpLSA.newEval()
    pLSATime = time.time() - t
    mmwrite(resultFolder + 'PLSA_Z%s_nb%s' % (Z, nbBasket), evalpLSA.perf)

    print 'pLSA Execution time:', pLSATime
    print 'Performances :'
    print evalpLSA.testNames
    print evalpLSA.computePerf()

    pLSAPerf = dict()
    for performance in evalpLSA.testNames:
        pLSAPerf[(performance, modelpLSA.Z)] = evalpLSA.meanPerf[[
            i for i, j in enumerate(evalpLSA.testNames) if j == performance
        ]][0]
    print 'Writing Baskets to file'
    file = open(
        resultFolder + 'pLSAPerf_Z%s_nb%s_nr%s.txt' % (Z, nbBasket, nbReco),
        'w')
    pickle.dump(pLSAPerf, file)
    file.close()

    #evalpLSA.savePerf(resultFolder+'pLSA_Z%s_nb%s'%(Z, nbBasket))
    return evalpLSA
def RWWR(alpha, sim, nbBasket, nbReco):

    data = load()
    #data = cvTaFengProcessing()
    ###############################################################
    # CREATE MODELS
    ###############################################################
    print 'Create the model based on the training set'

    modelRWWR = processing.RandomWalkWithRestartRecoModel(
        data.getUserItemMatrix(), alpha, sim)
    modelRWWR.train()
    ###############################################################
    # SET RECOMMENDATION
    ###############################################################
    if nbBasket == -1:
        evalRWWR = processing.Evaluation(modelRWWR, data.getBasketItemList(),
                                         nbReco)
    else:
        evalRWWR = processing.Evaluation(modelRWWR,
                                         data.getBasketItemList()[:nbBasket],
                                         nbReco)

    ###############################################################
    # LAUNCH RECOMMENDATION + SAVE RESULTS
    ###############################################################
    t = time.time()
    evalRWWR.newEval()
    RWWRTime = time.time() - t
    mmwrite(resultFolder + 'RWWR_a%s_nb%s' % (alpha, nbBasket), evalRWWR.perf)

    print 'RWWR Execution time:', RWWRTime
    print 'Performances :'
    print evalRWWR.testNames
    print evalRWWR.computePerf()

    RWWRPerf = dict()
    for performance in evalRWWR.testNames:
        RWWRPerf[(performance, modelRWWR.alpha,
                  modelRWWR.sim)] = evalRWWR.meanPerf[[
                      i for i, j in enumerate(evalRWWR.testNames)
                      if j == performance
                  ]][0]
    print 'Writing Baskets to file'
    file = open(
        resultFolder + 'RWWRPerf_a%s_sim%s_nb%s_nr%s.txt' %
        (alpha, sim, nbBasket, nbReco), 'w')
    pickle.dump(RWWRPerf, file)
    file.close()

    #evalRWWR.savePerf(resultFolder+'RWWR_a%s_nb%s'%(alpha, nbBasket))
    return evalRWWR
def Cosine(nbBasket, nbReco):
    data = load()
    ###############################################################
    # CREATE MODELS
    ###############################################################
    print 'Create the model based on the training set'

    modelCosine = processing.CosineRecoModel(data.getUserItemMatrix())

    ###############################################################
    # SET RECOMMENDATION
    ###############################################################
    if nbBasket == -1:
        evalCosine = processing.Evaluation(modelCosine,
                                           data.getBasketItemList(), nbReco)
    else:
        evalCosine = processing.Evaluation(modelCosine,
                                           data.getBasketItemList()[:nbBasket],
                                           nbReco)

    ###############################################################
    # LAUNCH RECOMMENDATION + SAVE RESULTS
    ###############################################################
    t = time.time()
    evalCosine.newEval()
    CosineTime = time.time() - t
    mmwrite(resultFolder + 'Cosine_nb%s' % nbBasket, evalCosine.perf)

    CosinePerf = dict()
    for performance in evalCosine.testNames:
        CosinePerf[performance] = evalCosine.meanPerf[[
            i for i, j in enumerate(evalCosine.testNames) if j == performance
        ]][0]
    print 'Writing Baskets to file'
    file = open(resultFolder + 'CosinePerf_nb%s_nr%s.txt' % (nbBasket, nbReco),
                'w')
    pickle.dump(CosinePerf, file)
    file.close()

    print 'Cosine Execution time:', CosineTime
    print 'Performances :'
    print evalCosine.testNames
    print evalCosine.computePerf()
    evalCosine.savePerf(resultFolder + 'Cosine_nb%s.txt' % nbBasket)
    return evalCosine
    #READ TEST DATASET PARAM
    dataPaths = ['../data/TaFengDataSet/D02']
    testData = preprocessor.readDataFile(dataPaths, delimiter, skiprows,
                                         usecols, dt)

    testBaskets = preprocessor.aggregateTransactionProcessing(testData)
    preprocessor.saveBasketData(valBasketcvFNAME, testBaskets)

#OPTIMIZE SOP
mf = core.ModelFactoryReco()
jobs = list()
for alpha, teta in [(1, 1)]:
    modelSOP = processing.SOPRecoModel(UI, alpha, teta)
    if nbBasket == -1:
        evalSOP = processing.Evaluation(modelSOP, testBaskets, nbReco)
    else:
        evalSOP = processing.Evaluation(modelSOP, testBaskets[:nbBasket],
                                        nbReco)
    jobs.append(mf.add(modelSOP, evalSOP))

mf.start()

#Check result
results = list()
for job in jobs:
    results.append(mf.get(job))

#Get best params
perfTester = processing.Evaluation()
SOPPerf = dict()
def SOP(alpha, teta, nbBasket, nbReco):

    data = load()
    #data = loadPyPred()
    #data = cvTaFengProcessing()
    ###############################################################
    # CREATE MODELS
    ###############################################################
    print 'Create the model based on the training set'

    modelSOP = processing.SOPRecoModel(data.getUserItemMatrix(), alpha, teta)
    modelSOP.train()

    ###############################################################
    # SET RECOMMENDATION
    ###############################################################
    if nbBasket == -1:
        evalSOP = processing.Evaluation(modelSOP, data.getBasketItemList(),
                                        nbReco)
    else:
        evalSOP = processing.Evaluation(modelSOP,
                                        data.getBasketItemList()[:nbBasket],
                                        nbReco)

    ###############################################################
    # LAUNCH RECOMMENDATION + SAVE RESULTS
    ###############################################################
    t = time.time()
    evalSOP.newEval()
    SOPTime = time.time() - t

    #	session = getSession()
    #	model = Model('SOP')
    #	model.parameters.append(ModelParameters(getParameter('alpha'), alpha))
    #	model.parameters.append(ModelParameters(getParameter('teta'), teta))
    #	model.parameters.append(ModelParameters(getParameter('nbBasket'), nbBasket))
    #	model.parameters.append(ModelParameters(getParameter('nbReco'), nbReco))
    #	mmwrite(resultFolder+'SOPPerf_a%s_t%s_nb%s_nr%s'%(alpha,teta,nbBasket,nbReco),evalSOP.perf)

    print 'SOP Execution time:', SOPTime
    print 'Performances : '
    print evalSOP.testNames
    print evalSOP.computePerf()
    #	res = Result()
    #	res.model = model
    #	for i in range(len(evalSOP.testNames)):
    #		perf = getPerformanceLabel(evalSOP.testNames[i])
    #		session.add(perf)
    #		res.performances.append(ResultPerf(perf, evalSOP.meanPerf[i]))
    #	session.add(res)
    #	session.commit()
    #	closeSession(session)
    SOPPerf = dict()
    for performance in evalSOP.testNames:
        SOPPerf[(performance, modelSOP.alpha,
                 modelSOP.teta)] = evalSOP.meanPerf[[
                     i for i, j in enumerate(evalSOP.testNames)
                     if j == performance
                 ]][0]
    print 'Writing Baskets to file'
    file = open(
        resultFolder + 'SOPPerf_a%s_t%s_nb%s_nr%s.txt' %
        (alpha, teta, nbBasket, nbReco), 'w')
    pickle.dump(SOPPerf, file)
    file.close()
    #evalSOP.savePerf(resultFolder+'SOPPerf_a%s_t%s_nb%s_nr%s.txt'%(alpha,teta,nbBasket,nbReco))
    return evalSOP
        valTransDataCV = trainTransData[len(trainTransData) * 3 / 4:]

        UIcv = preprocessor.aggregateUserProcessing(trainTransDataCV)
        preprocessor.saveTrainData('UIcv', UIcv)
        valBaskets = preprocessor.aggregateTransactionProcessing(
            valTransDataCV)
        preprocessor.saveBasketData('valBaskets.txt', valBaskets)

    return preprocessor


data = cvTaFengProcessing()

modelRWWR = processing.RandomWalkWithRestartRecoModel(UIcv, alpha, sim)
if nbBasket == -1:
    eval = processing.Evaluation(modelRWWR, valBaskets, nbReco)
else:
    eval = processing.Evaluation(modelRWWR, valBaskets[:nbBasket], nbReco)

#Get best params
perfTester = processing.Evaluation()
Perf = dict()
for job in results:
    for performance in perfTester.testNames:
        Perf[('RWWR', performance, job.model.alpha,
              job.model.sim)] = job.evaluator.meanPerf[[
                  i for i, j in enumerate(perfTester.testNames)
                  if j == performance
              ]][0]

print 'Writing Baskets to file'