05-getscore.py 3.29 KB
import numpy as np 
import shelve 
import sys
import glob
from collections import defaultdict
from tinydb import TinyDB, Query
from mako.template import Template
import time 

def get_best(x):                                                                               
    argbest=np.argmax(x[1])                                                                    
    maxdev=x[1][argbest]
    maxtrain=np.max(x[0])
    maxtest=np.max(x[2])
    besttest=x[2][argbest]
    return ( maxtrain,maxdev,maxtest,besttest) 


template = '''
${name}
========================

LDA likelihood : ${lb}
-----------------------

MLP scores : 
-------------------

| LDA RAW   | train    | dev       |max test| best test|
| ASR       | - | ${mlp_lda_asr[0]}  | - | ${mlp_lda_asr[1]} |
| TRS       | - | ${mlp_lda_trs[0]}  | - | ${mlp_lda_trs[1]} |
| AE ASR   | train    | dev       |max test| best test|
% for cpt,line in enumerate(models["AE"]["ASR"]):
| ${cpt}      | ${line[0]} | ${line[1]}  |${line[2]} | ${line[3]} |
% endfor
| AE TRS   | train    | dev       |max test| best test|
% for cpt,line in enumerate(models["AE"]["TRS"]):
| ${cpt}      | ${line[0]} | ${line[1]}  |${line[2]} | ${line[3]} |
% endfor
| AE SPE   | train    | dev       |max test| best test|
% for cpt,line in enumerate(models["AE"]["SPE"]):
| ${cpt}      | ${line[0]} | ${line[1]}  |${line[2]} | ${line[3]} |
% endfor
| SAE ARS   | train    | dev       |max test| best test|
% for cpt,line in enumerate(models["SAE"]['ASR']):
| ${cpt}      | ${line[0]} | ${line[1]}  |${line[2]} | ${line[3]} |
% endfor
| SAE TRS   | train    | dev       |max test| best test|
% for cpt,line in enumerate(models["SAE"]["TRS"]):
| ${cpt}      | ${line[0]} | ${line[1]}  |${line[2]} | ${line[3]} |
% endfor
| SAE SPE   | train    | dev       |max test| best test|
% for cpt,line in enumerate(models["SAE"]["SPE"]):
| ${cpt}      | ${line[0]} | ${line[1]}  |${line[2]} | ${line[3]} |
% endfor

'''

# ae_model.shelve
def get_folder_file(x):
    folder=x.split("/")[1]
    shelve_file = ".".join(x.split(".")[:-1])
    return(folder,shelve_file)

in_folder = sys.argv[1]
folder_list = [ x.strip() for x in open("{}/ordered_model.txt".format(in_folder)).readlines() ][0:51]


models = defaultdict(dict)
db = TinyDB("{}/t4.db".format(in_folder))
for  folder in folder_list :
    try :
        q = Query()
        lb = db.search(q.name == folder)[0]["asr"]

        mlp_shelve = shelve.open("{}/{}/mlp_scores.shelve".format(in_folder,folder))
        mlp_lda_asr=mlp_shelve["ASR"][1]

        mlp_lda_trs=mlp_shelve["TRS"][1]

        ae_model_list = glob.glob("{}/{}/*/ae_model.shelve.dir".format(in_folder,folder))
        ae_model_list= map(get_folder_file,ae_model_list)
        for name , shelve_file in ae_model_list :
            opened_shelve = shelve.open(shelve_file)
            working_key = None 
            if "AE" in opened_shelve.keys():
                working_key = "AE"
            elif "SAE" in opened_shelve.keys():
                working_key = "SAE"

            for key in opened_shelve[working_key].keys():
                models[working_key][key] = [ get_best(x) for x in opened_shelve[working_key][key] ]
        print(Template(template).render(name=folder,lb=lb,mlp_lda_asr=mlp_lda_asr,mlp_lda_trs=mlp_lda_trs,models=models))
    except :
        print >>sys.stderr, "Erreur model {} ".format(folder)