Esempio n. 1
0
def initialize():
    anotacoes = parser.parseAnnotation()
    dominios = parser.parseDominio()
    participantes = {}
    atributos = ["type", "size", "colour", "hpos", "vpos", "near", "left", "right", "below", "above", "in-front-of"]
    targets = {"01f-t1n":"h", "01f-t1r":"h", "01f-t2n":"h", "01f-t2r":"h", "01o-t1n":"h", "01o-t1r":"h", "01o-t2n":"h", "01o-t2r":"h", "02f-t1n":"o", "02f-t1r":"o", "02f-t2n":"o", "02f-t2r":"o", "02o-t1n":"o", "02o-t1r":"o", "02o-t2n":"o", "02o-t2r":"o", "03f-t1n":"m", "03f-t1r":"m", "03f-t2n":"m", "03f-t2r":"m", "03o-t1n":"m", "03o-t1r":"m", "03o-t2n":"m", "03o-t2r":"m", "04f-t1n":"a", "04f-t1r":"a", "04f-t2n":"a", "04f-t2r":"a", "04o-t1n":"a", "04o-t1r":"a", "04o-t2n":"a", "04o-t2r":"a", "05f-t1n":"m", "05f-t2n":"m", "05f-t1r":"m", "05f-t2r":"m", "05o-t1n":"m", "05o-t1r":"m", "05o-t2n":"m", "05o-t2r":"m", "06f-t1n":"h", "06f-t1r":"h", "06f-t2n":"h", "06f-t2r":"h", "06o-t1n":"h", "06o-t1r":"h", "06o-t2n":"h", "06o-t2r":"h", "07f-t1n":"i", "07f-t1r":"i", "07f-t2n":"i", "07f-t2r":"i", "07o-t1n":"i", "07o-t1r":"i", "07o-t2n":"i", "07o-t2r":"i", "08f-t1n":"a", "08f-t1r":"a", "08f-t2n":"a", "08f-t2r":"a", "08o-t1n":"a", "08o-t1r":"a", "08o-t2n":"a", "08o-t2r":"a" }
    return dominios, targets, anotacoes, atributos, participantes
Esempio n. 2
0
def initialize():
    anotacoes = parser.parseAnnotation()
    dominios = parser.parseDominio()
    participantes = {}
    atributos = ["type", "colour", "hpos", "vpos", "near", "left", "right", "below", "above"]
    targets = {"abs1":"e2", "abs3":"e2", "abs5":"e3", "proj1":"e2", "proj3":"e4", "proj5":"e2" }
    return dominios, targets, anotacoes, atributos, participantes
def run_validation(dominios, folds, featureVector, participantesInput, incluirParticipante):
    [inputs, frequencias, participantes] = [{}, {}, {}]
    
    # Inicializa Vetor de Caracteristicas
    for fold in folds:
        frequencias[fold] = utils.countAttributeFrequencyIndividual(folds, fold)
        participantes[fold] = parser.descriptionsMeans(folds, fold, dominios)
        inputs[fold] = parser.parseSVMInput(folds[fold], featureVector, participantes[fold], frequencias[fold], incluirParticipante)
    
    [acertos, total] = [{}, {}]
    
    keys = folds.keys()
    keys.sort()
    for fold in folds:
#             print "Fold: ", fold
#             print 50 * "-"
        resultados = svm.run(inputs, fold)
        
        for combination in resultados.keys():
            if combination not in acertos.keys():
                acertos[combination] = {}
                total[combination] = {}
            
            for svm1 in resultados[combination][0].keys():
                if svm1 not in acertos[combination].keys():
                    acertos[combination][svm1] = 0.0
                    total[combination][svm1] = 0.0
                
                acertos[combination][svm1] = acertos[combination][svm1] + resultados[combination][0][svm1]
                total[combination][svm1] = total[combination][svm1] + resultados[combination][1][svm1]
    
    combinacoes = {}
    acuracias = {}
    for combination in acertos.keys():
        aux = combination.split(",")
        C = float(aux[0])
        gamma = float(aux[1])
        
#             print 20 * "-"
#             print str(C) + " - " + str(gamma)
#             print 20 * "-"
        
        for svm1 in acertos[combination].keys():
            if total[combination][svm1] == 0:
                acuracia = 0
            else:
                acuracia = acertos[combination][svm1] / total[combination][svm1]
#                 print str(svm1) + ": " + str(acuracia)
            if svm1 not in combinacoes.keys():
                acuracias[svm1] = acuracia
                combinacoes[svm1] = {}
                combinacoes[svm1]["C"] = C
                combinacoes[svm1]["gamma"] = gamma
            elif acuracia > acuracias[svm1]:
                acuracias[svm1] = acuracia
                combinacoes[svm1]["C"] = C
                combinacoes[svm1]["gamma"] = gamma
    return combinacoes           
Esempio n. 4
0
def crossValidation(anotacoes = {}):
    if len(anotacoes) == 0: 
        anotacoes = parser.parseAnnotation()
    folds = {}
    
    for i in range(1,7):
        folds[i] = {}
    
    for anotacao in anotacoes:
        contexto = anotacao["caracteristicas"]["context"]
        participante = anotacao["caracteristicas"]["trial"]
        
        for i in range(1,7):
            if participante not in folds[i]:
                folds[i][participante] = anotacao
                break
    return folds
        
Esempio n. 5
0
def crossValidation(fold = int, anotacoes = {}):
    if len(anotacoes) == 0: 
        anotacoes = parser.parseAnnotation()
    folds = {}
    
    nroParticipantes = contabilizaParticipantes(anotacoes)
    nroAnotacoes = len(anotacoes)
    
    expressoesParticipantePorFold = (nroAnotacoes / nroParticipantes) / fold
    
    nroExpressoes = {}
    for i in range(1,fold+1):
        folds[str(i)] = {}
        nroExpressoes[str(i)] = {}
    
    for anotacao in anotacoes:
        contexto = anotacao["caracteristicas"]["context"]
        participante = anotacao["caracteristicas"]["trial"]
        
        achou = False
        for i in range(1,fold+1):
            if participante not in folds[str(i)]:
                folds[str(i)][participante] = []
                nroExpressoes[str(i)][participante] = expressoesParticipantePorFold
            if nroExpressoes[str(i)][participante] > 0:
                folds[str(i)][participante].append(anotacao)
                nroExpressoes[str(i)][participante] = nroExpressoes[str(i)][participante] -1
                achou = True
                break
        
        if achou == False:
            for i in range(1,fold+1):
                if nroExpressoes[str(i)][participante] == 0:
                    folds[str(i)][participante].append(anotacao)
                    nroExpressoes[str(i)][participante] = nroExpressoes[str(i)][participante] -1
                    break
    return folds
Esempio n. 6
0
def run(dominios, targets, folds, atributos, participantes, incluirParticipante):
    featureVector = parser.parseFeatureVector(dominios, targets)
    
    inputs = {}
    
    for fold in folds:
        frequencias = countAttributeFrequencyIndividual(folds, fold)
        inputs[fold] = parser.parseSVMInput(folds[fold], featureVector, participantes, frequencias, incluirParticipante)
        
    
    resultadoTotal = []
    dice = []
    masi = []
    acuracia = 0.0
    
#     print 10 * "*"
#     if incluirParticipante == True:
#         print "SVM com o id do participante"
#     else:
#         print "SVM sem o id do participante"
#     print 10 * "*"
    
    for fold in ["1","2","3","4","5","6"]:
        print "FOLD: " + fold
        resultados = svm.run(inputs, fold)
        resultadoTotal.append(resultados)
        acertos = resultados[0]
        total = resultados[1]
        expressoes = resultados[2]
        
        tp = resultados[3]
        tn = resultados[4]
        fp = resultados[5]
        fn = resultados[6]
        
        for expressao in expressoes:
            contexto = expressao["anotacao"]["caracteristicas"]["context"]
            
            descricao = defineExpressao(expressao["previsoes"], dominios[contexto], targets[contexto])
            descricaoR = expressao["anotacao"]["descricao"]
            
            A = ass.parse(descricao)
            B = ass.parse(descricaoR)
            
            DICE = ass.dice(A, B)
            MASI = ass.masi(A, B)
            
            if DICE == 1.0:
                acuracia = acuracia + 1.0
            
            dice.append(DICE)
            masi.append(MASI)
#             if expressao["previsoes"]["relation"] > 0:
#                 print "Descricao Prevista : " + str(expressao["previsoes"])
#                 print "Descricao Prevista : " + str(descricao)
#                 print "Descricao Real : " + str(expressao["classes"])
#                 print "Descricao Real : " + str(descricaoR)
#                 print "Dice: " + str(DICE)
#                 print 30 * "-"
        
        for atributo in acertos.keys():
            print "Atributo: " + str(atributo)
            print "Acuracia: " + str(acertos[atributo] / total[atributo])
#             print "Verdadeiros Positivos: " + str(tp[atributo])
#             print "Falsos Positivos: " + str(fp[atributo])
#             print "Verdadeiros Negativos: " + str(tn[atributo])
#             print "Falsos Negativos: " + str(fn[atributo])
            print 10 * "-"
         
        print 50 * "*"
        
    print 50 * "*"
    print "Expressoes: "
    print "Dice Total: " + str(np.mean(dice))
    print "Masi Total: " + str(np.mean(masi))
    print "Acuracia: " + str(acuracia / len(dice))
    print 50 * "*"

    acertosT = {}
    totalT = {}
    
    for resultados in resultadoTotal:
        acertos = resultados[0]
        total = resultados[1]
        
        for atributo in acertos.keys():
            if atributo not in acertosT:
                acertosT[atributo] = 0.0
                totalT[atributo] = 0.0
            
            acertosT[atributo] = acertosT[atributo] + acertos[atributo]
            totalT[atributo] = totalT[atributo] + total[atributo]
            
    for atributo in acertosT.keys():
        print "Atributo: " + str(atributo)
        print "Acuracia: " + str(acertosT[atributo] / totalT[atributo])
        print 10 * "-"
        
    return resultadoTotal, dice, masi, acuracia
def run(dominios, targets, folds, atributos, participantesInput, incluirParticipante):
    featureVector = parser.parseFeatureVector(dominios, targets)
    
    inputs = {}
    frequencias = {}
    participantes = {}
    
    # Inicializa Vetor de Caracteristicas
    for fold in folds:
        frequencias[fold] = utils.countAttributeFrequencyIndividual(folds, fold)
        participantes[fold] = parser.descriptionsMeans(folds, fold, dominios)
        inputs[fold] = parser.parseSVMInput(folds[fold], featureVector, participantes[fold], frequencias[fold], incluirParticipante)
    
    resultadoTotal = []
    diceTotal = []
    masiTotal = []
    acuraciaTotal = 0.0
    
    for testeFold in folds:
        teste = folds[testeFold]
        inputTeste = inputs[testeFold]
        
        treinamentoFolds = {}
        inputsTreinamento = {}
        
        for foldAux in folds:
            if foldAux != testeFold:
                treinamentoFolds[foldAux] = folds[foldAux]
                inputsTreinamento[foldAux] = inputs[foldAux]
        
        combinacoes = run_validation(dominios, treinamentoFolds, featureVector, participantesInput, incluirParticipante)
         
        resultados = svm.run2(inputsTreinamento, inputTeste, combinacoes)
        resultadoTotal.append(resultados)
        acertos = resultados[0]
        total = resultados[1]
        expressoes = resultados[2]
         
        tp = resultados[3]
        tn = resultados[4]
        fp = resultados[5]
        fn = resultados[6]
         
        dice = []
        masi = []
        acuracia = 0
        
        for expressao in expressoes:
            contexto = expressao["anotacao"]["caracteristicas"]["context"]
             
            descricao = utils.defineExpressao(expressao["previsoes"], dominios[contexto], targets[contexto])
            descricaoR = expressao["anotacao"]["descricao"]
             
            A = ass.parse(descricao)
            B = ass.parse(descricaoR)
             
            DICE = ass.dice(A, B)
            MASI = ass.masi(A, B)
             
#             print 20 * "-"
#             print descricaoR
#             print descricao
#             print DICE
#             print 20 * "-"
             
            if DICE == 1.0:
                acuracia = acuracia + 1.0
                acuraciaTotal = acuraciaTotal + 1.0
             
            dice.append(DICE)
            masi.append(MASI)
            diceTotal.append(DICE)
            masiTotal.append(MASI)
         
#         for atributo in acertos.keys():
#             print "Attribute: " + str(atributo)
#             print "Accuracy: " + str(acertos[atributo] / total[atributo])
#             print 10 * "-"
#           
#         print 50 * "*"
#          
#         print "\n"
#         print "General:"
#         print 50 * "*"
#         print "Expressions: "
#         print "Dice: " + str(np.mean(dice))
#         print "Masi: " + str(np.mean(masi))
#         print "Accuracy: " + str(acuracia / len(dice))
#         print "\n"
     
#     print "\n"
#     print "General:"
#     print 50 * "*"
#     print "Expressions: "
#     print "Dice: " + str(np.mean(diceTotal))
#     print "Masi: " + str(np.mean(masiTotal))
#     print "Accuracy: " + str(acuraciaTotal / len(diceTotal))
#     print "\n"       
    acertosT = {}
    totalT = {}
    
    for resultados in resultadoTotal:
        acertos = resultados[0]
        total = resultados[1]
        
        for atributo in acertos.keys():
            if atributo not in acertosT:
                acertosT[atributo] = 0.0
                totalT[atributo] = 0.0
            
            acertosT[atributo] = acertosT[atributo] + acertos[atributo]
            totalT[atributo] = totalT[atributo] + total[atributo]
    
#     print "Attributes:"
#     print 15 * "-"     
#     for atributo in acertosT.keys():
#         print "Attribute: " + str(atributo)
#         print "Accuracy: " + str(acertosT[atributo] / totalT[atributo])
#         print 10 * "-"  
    
    return resultadoTotal, diceTotal, masiTotal, acuraciaTotal
def run(dominios, targets, folds, atributos, participantes, incluirParticipante):
    featureVector = parser.parseFeatureVector(dominios, targets)
    
    inputs = {}
    
    # Cross Validation
    #folds = cross.crossValidation(6, annotations)
    
    # Inicializa Vetor de Caracteristicas
    for fold in folds:
        frequencias = countAttributeFrequencyIndividual(folds, fold)
        inputs[fold] = parser.parseSVMInputSecondVersion(folds[fold], featureVector, participantes, frequencias, incluirParticipante)
    
    resultadoTotal = []
    diceTotal = []
    masiTotal = []
    acuraciaTotal = 0.0
    
    for testeFold in folds:
        teste = folds[testeFold]
        inputTeste = inputs[testeFold]
        
        treinamentoFolds = {}
        inputsTreinamento = {}
        
        for foldAux in folds:
            if foldAux != testeFold:
                treinamentoFolds[foldAux] = folds[foldAux]
                inputsTreinamento[foldAux] = inputs[foldAux]
        
        acertos = {}
        total = {}
        tp = {}
        tn = {}
        fp = {}
        fn = {}
        
        keys = folds.keys()
        keys.sort()
        for fold in treinamentoFolds:
#             print "Fold: ", fold
#             print 50 * "-"
            resultados = svm.run(inputsTreinamento, fold)
            
            for combination in resultados.keys():
                if combination not in acertos.keys():
                    acertos[combination] = {}
                    total[combination] = {}
                    tp[combination] = {}
                    tn[combination] = {}
                    fp[combination] = {}
                    fn[combination] = {}
                
                for svm1 in resultados[combination][0].keys():
                    if svm1 not in acertos[combination].keys():
                        acertos[combination][svm1] = 0.0
                        total[combination][svm1] = 0.0
                        tp[combination][svm1] = 0.0
                        tn[combination][svm1] = 0.0
                        fp[combination][svm1] = 0.0
                        fn[combination][svm1] = 0.0
                    
                    acertos[combination][svm1] = acertos[combination][svm1] + resultados[combination][0][svm1]
                    total[combination][svm1] = total[combination][svm1] + resultados[combination][1][svm1]
                    tp[combination][svm1] = tp[combination][svm1] + resultados[combination][3][svm1]
                    tn[combination][svm1] = tn[combination][svm1] + resultados[combination][4][svm1]
                    fp[combination][svm1] = fp[combination][svm1] + resultados[combination][5][svm1]
                    fn[combination][svm1] = fn[combination][svm1] + resultados[combination][6][svm1]
        
        combinacoes = {}
        acuracias = {}
        for combination in acertos.keys():
            aux = combination.split(",")
            C = float(aux[0])
            gamma = float(aux[1])
            
#             print 20 * "-"
#             print str(C) + " - " + str(gamma)
#             print 20 * "-"
            
            for svm1 in acertos[combination].keys():
                acuracia = acertos[combination][svm1] / total[combination][svm1]
#                 print str(svm1) + ": " + str(acuracia)
                if svm1 not in combinacoes.keys():
                    acuracias[svm1] = acuracia
                    combinacoes[svm1] = {}
                    combinacoes[svm1]["C"] = C
                    combinacoes[svm1]["gamma"] = gamma
                elif acuracia > acuracias[svm1]:
                    acuracias[svm1] = acuracia
                    combinacoes[svm1]["C"] = C
                    combinacoes[svm1]["gamma"] = gamma
        
#         print "\n"
#         print "Combinacao: " + str(combinacoes)
         
         
        resultados = svm.run2(inputsTreinamento, inputTeste, combinacoes)
        resultadoTotal.append(resultados)
        acertos = resultados[0]
        total = resultados[1]
        expressoes = resultados[2]
         
        tp = resultados[3]
        tn = resultados[4]
        fp = resultados[5]
        fn = resultados[6]
         
        dice = []
        masi = []
         
        for expressao in expressoes:
            contexto = expressao["anotacao"]["caracteristicas"]["context"]
             
            descricao = defineExpressao(expressao["previsoes"], dominios[contexto], targets[contexto])
            descricaoR = expressao["anotacao"]["descricao"]
             
            A = ass.parse(descricao)
            B = ass.parse(descricaoR)
             
            DICE = ass.dice(A, B)
            MASI = ass.masi(A, B)
             
#             print 20 * "-"
#             print descricaoR
#             print descricao
#             print DICE
#             print 20 * "-"
             
            if DICE == 1.0:
                acuracia = acuracia + 1.0
                acuraciaTotal = acuraciaTotal + 1.0
             
            dice.append(DICE)
            masi.append(MASI)
            diceTotal.append(DICE)
            masiTotal.append(MASI)
         
#         for atributo in acertos.keys():
#             print "Attribute: " + str(atributo)
#             print "Accuracy: " + str(acertos[atributo] / total[atributo])
#             print 10 * "-"
#           
#         print 50 * "*"
#          
#         print "\n"
#         print "General:"
#         print 50 * "*"
#         print "Expressions: "
#         print "Dice: " + str(np.mean(dice))
#         print "Masi: " + str(np.mean(masi))
#         print "Accuracy: " + str(acuracia / len(dice))
#         print "\n"
     
    print "\n"
    print "General:"
    print 50 * "*"
    print "Expressions: "
    print "Dice: " + str(np.mean(diceTotal))
    print "Masi: " + str(np.mean(masiTotal))
    print "Accuracy: " + str(acuraciaTotal / len(diceTotal))
    print "\n"       
    acertosT = {}
    totalT = {}
    
    for resultados in resultadoTotal:
        acertos = resultados[0]
        total = resultados[1]
        
        for atributo in acertos.keys():
            if atributo not in acertosT:
                acertosT[atributo] = 0.0
                totalT[atributo] = 0.0
            
            acertosT[atributo] = acertosT[atributo] + acertos[atributo]
            totalT[atributo] = totalT[atributo] + total[atributo]
    
    print "Attributes:"
    print 15 * "-"     
    for atributo in acertosT.keys():
        print "Attribute: " + str(atributo)
        print "Accuracy: " + str(acertosT[atributo] / totalT[atributo])
        print 10 * "-"