Best Python code snippet using unittest-xml-reporting_python
IAP_eval.py
Source:IAP_eval.py  
1#!/usr/bin/env python2"""3Animals with Attributes Dataset, http://attributes.kyb.tuebingen.mpg.de4Perform Multiclass Predicition from binary attributes and evaluates it.5(C) 2009 Christoph Lampert <chl@tuebingen.mpg.de>6"""7import os,sys8import numpy as np9import matplotlib.pyplot as plt10from sklearn.metrics import roc_curve, auc11from utils import bzPickle, bzUnpickle, get_class_attributes, get_attributes, create_data, autolabel12import warnings13warnings.filterwarnings('ignore')14def nameonly(x):15    return x.split('\t')[1]16def loadstr(filename,converter=str):17    return [converter(c.strip()) for c in open(filename).readlines()]18def loaddict(filename,converter=str):19    D={}20    for line in open(filename).readlines():21        line = line.split()22        D[line[0]] = converter(line[1].strip())23    return D24# adapt these paths and filenames to match local installation25classnames = loadstr('classes.txt',nameonly)26numexamples = loaddict('numexamples.txt',int)27def evaluate(split,C, attributepattern):28    global test_classnames29    if split == 0:30        test_classnames=loadstr('testclasses.txt')31        train_classnames=loadstr('trainclasses.txt')32    else:33        startid= (split-1)*1034        stopid = split*1035        test_classnames = classnames[startid:stopid]36        train_classnames = classnames[0:startid]+classnames[stopid:]37    test_classes = [ classnames.index(c) for c in test_classnames]38    train_classes = [ classnames.index(c) for c in train_classnames]39    M = np.loadtxt('predicate-matrix-binary.txt',dtype=float)40    L=[]41    for c in test_classes:42        L.extend( [c]*numexamples[classnames[c]] )43    L=np.array(L)  # (n,)44    P_prime = np.loadtxt(attributepattern)45    P = np.dot(P_prime, M[train_classes]) # (85,n)46    prior = np.mean(M[train_classes],axis=0)47    prior[prior==0.]=0.548    prior[prior==1.]=0.5    # disallow degenerated priors49    M = M[test_classes] # (10,85)50    prob=[]51    for p in P:52        prob.append( np.prod(M*p + (1-M)*(1-p),axis=1)/np.prod(M*prior+(1-M)*(1-prior), axis=1) )53    MCpred = np.argmax( prob, axis=1 )54    d = len(test_classes)55    confusion=np.zeros([d,d])56    for pl,nl in zip(MCpred,L):57        try:58            gt = test_classes.index(nl)59            confusion[gt,pl] += 1.60        except:61            pass62    for row in confusion:63        row /= sum(row)64    return confusion,np.asarray(prob),P65def plot_confusion(confusion, clf):66    fig=plt.figure(figsize=(10,9))67    plt.imshow(confusion,interpolation='nearest',origin='upper')68    plt.clim(0,1)69    plt.xticks(np.arange(0,10),[c.replace('+',' ') for c in test_classnames],rotation='vertical',fontsize=24)70    plt.yticks(np.arange(0,10),[c.replace('+',' ') for c in test_classnames],fontsize=24)71    plt.axis([-.5,9.5,9.5,-.5])72    plt.setp(plt.gca().xaxis.get_major_ticks(), pad=18)73    plt.setp(plt.gca().yaxis.get_major_ticks(), pad=12)74    fig.subplots_adjust(left=0.30)75    fig.subplots_adjust(top=0.98)76    fig.subplots_adjust(right=0.98)77    fig.subplots_adjust(bottom=0.22)78    plt.gray()79    plt.colorbar(shrink=0.79)80    plt.savefig('results/AwA-ROC-confusion-IAP-%s.pdf' %clf)81    return82def plot_roc(P,GT, clf):83    AUC=[]84    CURVE=[]85    for i,c in enumerate(test_classnames):86        class_id = classnames.index(c)87        fp, tp, _ = roc_curve(GT==class_id,  P[:,i])88        roc_auc = auc(fp, tp)89        print ("AUC: %s %5.3f" % (c,roc_auc))90        AUC.append(roc_auc)91        CURVE.append(np.array([fp,tp]))92    print ("----------------------------------")93    print ("Mean classAUC %g" % (np.mean(AUC)*100))94    order = np.argsort(AUC)[::-1]95    styles=['-','-','-','-','-','-','-','--','--','--']96    plt.figure(figsize=(9,5))97    for i in order:98        c = test_classnames[i]99        plt.plot(CURVE[i][0],CURVE[i][1],label='%s (AUC: %3.2f)' % (c,AUC[i]),linewidth=3,linestyle=styles[i])100    plt.legend(loc='lower right')101    plt.xticks([0.0,0.2,0.4,0.6,0.8,1.0], [r'$0$', r'$0.2$',r'$0.4$',r'$0.6$',r'$0.8$',r'$1.0$'],fontsize=18)102    plt.yticks([0.0,0.2,0.4,0.6,0.8,1.0], [r'$0$', r'$0.2$',r'$0.4$',r'$0.6$',r'$0.8$',r'$1.0$'],fontsize=18)103    plt.xlabel('false negative rate',fontsize=18)104    plt.ylabel('true positive rate',fontsize=18)105    plt.savefig('results/AwA-ROC-IAP-%s.pdf' %clf)106def plot_attAUC(P, attributepattern, clf):107    AUC=[]108    #P = np.loadtxt(attributepattern)109    attributes = get_attributes()110    # Loading ground truth111    test_index = bzUnpickle('./CreatedData/test_features_index.txt')112    test_attributes = get_class_attributes('./', name='test')113    _, y_true = create_data('./CreatedData/test_featuresVGG19.pic.bz2',test_index, test_attributes)114    print(y_true.shape, P.shape)115    for i in range(y_true.shape[1]):116        fp, tp, _ = roc_curve(y_true[:,i], P[:,i])117        roc_auc = auc(fp, tp)118        AUC.append(roc_auc)119    print ("Mean attrAUC %g" % (np.nanmean(AUC)) )120    xs = np.arange(y_true.shape[1])121    width = 0.5122    fig = plt.figure(figsize=(15,5))123    ax = fig.add_subplot(1,1,1)124    rects = ax.bar(xs, AUC, width, align='center')125    ax.set_xticks(xs)126    ax.set_xticklabels(attributes,  rotation=90)127    ax.set_ylabel("area under ROC curve")128    autolabel(rects, ax)129    plt.savefig('results/AwA-AttAUC-IAP-%s.pdf' %clf)130def main():131    list_clf = ['SVM', 'NN']132    try:133        clf = str(sys.argv[1])134    except IndexError:135        clf = 'SVM'136    if clf not in list_clf:137        print ("Non valid choice of classifier (SVM, NN)")138        raise SystemExit139    try:140        split = int(sys.argv[2])141    except IndexError:142        split = 0143    try:144        C = float(sys.argv[3])145    except IndexError:146        C = 10.147    attributepattern = 'IAP/probabilities_' + clf148    confusion,prob,L = evaluate(split,C, attributepattern)149    #plot_confusion(confusion, clf)150    #plot_roc(prob,L, clf)151    plot_attAUC(L, attributepattern, clf)152    print ("Mean class accuracy %g" % np.mean(np.diag(confusion)*100))153if __name__ == '__main__':...DAP_eval.py
Source:DAP_eval.py  
1#!/usr/bin/env python2"""3Animals with Attributes Dataset, http://attributes.kyb.tuebingen.mpg.de4Perform Multiclass Predicition from binary attributes and evaluates it.5(C) 2009 Christoph Lampert <chl@tuebingen.mpg.de>6"""7import os,sys8sys.path.append('/agbs/cluster/chl/libs/python2.5/site-packages/')9from numpy import *10def nameonly(x):11    return x.split('\t')[1]12def loadstr(filename,converter=str):13    return [converter(c.strip()) for c in file(filename).readlines()]14def loaddict(filename,converter=str):15    D={}16    for line in file(filename).readlines():17        line = line.split()18        D[line[0]] = converter(line[1].strip())19    20    return D21# adapt these paths and filenames to match local installation22classnames = loadstr('../classes.txt',nameonly)23numexamples = loaddict('numexamples.txt',int)24def evaluate(split,C):25    global test_classnames26    attributepattern = './DAP/cvfold%d_C%g_%%02d.prob' % (split,C)27    28    if split == 0:29        test_classnames=loadstr('/agbs/share/datasets/Animals_with_Attributes/testclasses.txt')30        train_classnames=loadstr('/agbs/share/datasets/Animals_with_Attributes/trainclasses.txt')31    else:32        startid= (split-1)*1033        stopid = split*1034        test_classnames = classnames[startid:stopid]35        train_classnames = classnames[0:startid]+classnames[stopid:]36    37    test_classes = [ classnames.index(c) for c in test_classnames]38    train_classes = [ classnames.index(c) for c in train_classnames]39    M = loadtxt('/agbs/share/datasets/Animals_with_Attributes/predicate-matrix-binary.txt',dtype=float)40    L=[]41    for c in test_classes:42        L.extend( [c]*numexamples[classnames[c]] )43    L=array(L)  # (n,)44    P = []45    for i in range(85):46        P.append(loadtxt(attributepattern % i,float))47    P = array(P).T   # (85,n)48    prior = mean(M[train_classes],axis=0)49    prior[prior==0.]=0.550    prior[prior==1.]=0.5    # disallow degenerated priors51    M = M[test_classes] # (10,85)52    prob=[]53    for p in P:54        prob.append( prod(M*p + (1-M)*(1-p),axis=1)/prod(M*prior+(1-M)*(1-prior), axis=1) )55    MCpred = argmax( prob, axis=1 )56    57    d = len(test_classes)58    confusion=zeros([d,d])59    for pl,nl in zip(MCpred,L):60        try:61            gt = test_classes.index(nl)62            confusion[gt,pl] += 1.63        except:64            pass65    for row in confusion:66        row /= sum(row)67    68    return confusion,asarray(prob),L69def plot_confusion(confusion):70    from pylab import figure,imshow,clim,xticks,yticks,axis,setp,gray,colorbar,savefig,gca71    fig=figure(figsize=(10,9))72    imshow(confusion,interpolation='nearest',origin='upper')73    clim(0,1)74    xticks(arange(0,10),[c.replace('+',' ') for c in test_classnames],rotation='vertical',fontsize=24)75    yticks(arange(0,10),[c.replace('+',' ') for c in test_classnames],fontsize=24)76    axis([-.5,9.5,9.5,-.5])77    setp(gca().xaxis.get_major_ticks(), pad=18)78    setp(gca().yaxis.get_major_ticks(), pad=12)79    fig.subplots_adjust(left=0.30)80    fig.subplots_adjust(top=0.98)81    fig.subplots_adjust(right=0.98)82    fig.subplots_adjust(bottom=0.22)83    gray()84    colorbar(shrink=0.79)85    savefig('AwA-ROC-confusion-DAP.pdf')86    return 87def plot_roc(P,GT):88    from pylab import figure,xticks,yticks,axis,setp,gray,colorbar,savefig,gca,clf,plot,legend,xlabel,ylabel89    from roc import roc90    AUC=[]91    CURVE=[]92    for i,c in enumerate(test_classnames):93        class_id = classnames.index(c)94        tp,fp,auc=roc(None,GT==class_id,  P[:,i] ) # larger is better95        print "AUC: %s %5.3f" % (c,auc)96        AUC.append(auc)97        CURVE.append(array([fp,tp]))98    order = argsort(AUC)[::-1]99    styles=['-','-','-','-','-','-','-','--','--','--']100    figure(figsize=(9,5))101    for i in order:102        c = test_classnames[i]103        plot(CURVE[i][0],CURVE[i][1],label='%s (AUC: %3.2f)' % (c,AUC[i]),linewidth=3,linestyle=styles[i])104    105    legend(loc='lower right')106    xticks([0.0,0.2,0.4,0.6,0.8,1.0], [r'$0$', r'$0.2$',r'$0.4$',r'$0.6$',r'$0.8$',r'$1.0$'],fontsize=18)107    yticks([0.0,0.2,0.4,0.6,0.8,1.0], [r'$0$', r'$0.2$',r'$0.4$',r'$0.6$',r'$0.8$',r'$1.0$'],fontsize=18)108    xlabel('false negative rate',fontsize=18)109    ylabel('true positive rate',fontsize=18)110    savefig('AwA-ROC-DAP.pdf')111def main():112    try:113        split = int(sys.argv[1])114    except IndexError:115        split = 0116    try:117        C = float(sys.argv[2])118    except IndexError:119        C = 10.120    confusion,prob,L = evaluate(split,C)121    print "Mean class accuracy %g" % mean(diag(confusion)*100)122    plot_confusion(confusion) 123    plot_roc(prob,L)124    125if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
