# devel.py
import os
import sys
import yaml
import pickle
import json
import time
import numpy as np
from collections import defaultdict
from sklearn.model_selection import train_test_split as tts
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score

from ensembled_svm import EnsembledSVM as eSVM

sys.path.append('../../utility')
import util
import yamanishi_data_util as yam

from devel_config import config as cfg

def main():
    if len(sys.argv)!=1:
        print 'USAGE:'
        print 'python -m scoop devel.py'
        print 'see devel_config.py'
        return

    outDir = os.path.join(cfg['xprmtDir'],
                          '-'.join(['imbalance',
                                    cfg['method']+'#'+str(cfg['nClone']),
                                    cfg['dataset'],util.tag()]))
    os.makedirs(outDir)

    ## Load data
    print 'loading data...'
    dParam = cfg['dataset'].split('#')
    disMat = None; iList = None
    if dParam[0]=='yamanishi':
        connFpath = os.path.join(cfg['clusterDir'],'calinskiharabaz_connDict.pkl')
        with open(connFpath,'r') as f:
            data = pickle.load(f)

        simDir = os.path.join(cfg['datasetDir'],dParam[0],'similarity-mat')
        comSimDict = yam.loadKernel2('compound',dParam[1],simDir)
        proSimDict = yam.loadKernel2('protein',dParam[1],simDir)
    else:
        assert False,'FATAL: unknown dataset'

    print 'getting devel data...'
    xraw = []; yraw = []
    for k,v in data.iteritems():
        for vv in v:
            xraw.append(vv)
            yraw.append(k)

    devIdx = [i for i in range(len(xraw)) if yraw[i]!=0]
    xdev = [xraw[i] for i in devIdx]
    ydev = [yraw[i] for i in devIdx]
    print 'nDevel: '+str(len(devIdx))+'/'+str(len(yraw))

    ## DEVEL
    results = []
    for i in range(cfg['nClone']):
        msg = 'devel clone: '+str(i+1)+'/'+str(cfg['nClone'])
        print msg
        xtr,xte,ytr,yte = tts(xdev,ydev,test_size=cfg['testSize'],
                              random_state=None,stratify=ydev)

        esvm = eSVM(cfg['maxTrainingSamplesPerBatch'],
                    cfg['maxTestingSamplesPerBatch'],
                    cfg['bootstrap'],
                    {'com':comSimDict,'pro':proSimDict},msg)

        ##
        print msg+': fitting nTr= '+str(len(ytr))
        esvm.fit(xtr,ytr)

        ##
        chosenIdx = np.random.randint(len(xte),size=cfg['maxTestingSamples'])
        xte = [xte[i] for i in chosenIdx]; yte = [yte[i] for i in chosenIdx]

        print msg+': predicting nTe= '+str(len(yte))
        ypred = esvm.predict(xte,cfg['mode'])

        results.append( {'xtr':xtr,'xte':xte,'ytr':ytr,'yte':yte,'ypred':ypred} )

    # devel perfs
    print 'getting perfs...'
    perfs = defaultdict(list)
    for r in results:
        coka = cohen_kappa_score(r['yte'],r['ypred'])
        aupr = average_precision_score(r['yte'],r['ypred'],average='micro')
        perfs['cohen_kappa_score'].append(coka)
        perfs['average_precision_score'].append(aupr)

    fpath = os.path.join(outDir,'perfs.json')
    with open(fpath,'w') as f: json.dump(perfs,f,indent=2,sort_keys=True)

if __name__ == '__main__':
    tic = time.time()
    main()
    print "main took: "+str(time.time()-tic)+' seconds'

@@ -2782,17 +2782,16 @@
 vel perf
-s
 %0A    pri
@@ -2806,17 +2806,16 @@
 ing perf
-s
 ...'%0A   
@@ -2819,17 +2819,16 @@
     perf
-s
  = defau
@@ -3004,17 +3004,16 @@
     perf
-s
 %5B'cohen_
@@ -3051,17 +3051,16 @@
     perf
-s
 %5B'averag
@@ -3134,16 +3134,221 @@
 perf
-s
 .json')
+%0A    with open(fpath,'w') as f: json.dump(perf,f,indent=2,sort_keys=True)%0A%0A    perfAvg = %7B%7D; fpath = os.path.join(outDir,'perf_avg.json')%0A    for k,v in perf.iteritems(): perfAvg%5Bk%5D = %5Bnp.mean(v),np.std(v)%5D
 %0A   
@@ -3389,17 +3389,19 @@
 ump(perf
-s
+Avg
 ,f,inden

write perf avg