Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/ein16/synth/d2/test_compare.py
ViewVC logotype

View of /branches/ein16/synth/d2/test_compare.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4459 - (download) (as text) (annotate)
Mon Aug 29 15:57:31 2016 UTC (2 years, 10 months ago) by cchiw
File size: 5635 byte(s)
add d-4 type
 #needs output tensor types
from obj_ty import *
from obj_operator import *
from sympy import *
import math
import numbers
divConstant=7.2

def toStr(name, var, maxdiff, sumdiff, per, s):
    # f = open("results_tmp.txt", 'a+')
    #f.write("\n\n **********")
    #print (name)
    x= " max diff: "+str(round(maxdiff,4))+" sumdiff: "+str(round(sumdiff,4))
    #print "x",x
    #f.write(name)
    rst_good = 0
    rst_eh = 0
    rst_check = 0
    rst_terrible= 0
    rst_compile = 0
    rst_NA = 0
    if (maxdiff<=0.0001):
        rtn = "Results: "+var+": V-0 RA"
        rst_good = 1
    elif (maxdiff<=0.001):
        if(per>10):
            rtn = "Results: "+var+": W+3 RC"+s
            rst_check = 1
        elif(per>1):
            rtn = "Results: "+var+": W-2 RB"+s
            rst_eh = 1
        elif(per>0.1):
            rtn = "Results: "+var+": W-1 RA"+s
            rst_good = 1
        else:
            rtn = "Results: "+var+": W-0 RA"
            rst_good = 1
    elif (maxdiff<=0.01):
        if(per>10):
            rtn = "Results: "+var+": X+3 RC"+s
            rst_check = 1
        elif(per>1):
            rtn = "Results: "+var+": X-2 RB"+s
            rst_eh = 1
        elif(per>0.1):
            rtn = "Results: "+var+": X-1 RB"+s
            rst_eh = 1
        else:
            rtn = "Results: "+var+": X-0 RA"
            rst_good = 1
    elif(maxdiff<=0.1):
        if(per>10):
            rtn = "Results: "+var+": Y-3 RD "+s
            rst_terrible = 1
        elif(per>1):
            rtn = "Results: "+var+": Y-2 RC"+s
            rst_check = 1
        elif(per>0.1):
            rtn = "Results: "+var+": Y-1 RB"+s
            rst_eh = 1
        else:
            rtn = "Results: "+var+": Y-0 RB"+s
            rst_eh = 1
    else:
        if(per>10):
            rtn = "Results: "+var+": Z-3 RD "+s
            rst_terrible = 1
        elif(per>1):
            rtn = "Results: "+var+": Z-2 RD "+s
            rst_terrible = 1
        elif(per>0.1):
            rtn = "Results: "+var+": Z-1 RC"+s
            rst_check = 1
        else:
            rtn = "Results: "+var+": Z-0 RB "+s
            rst_eh = 1
    return (rtn, rst_good, rst_eh, rst_check, rst_terrible, rst_compile, rst_NA)

# check is observed and correct is a float
def check_float(o1, c1):
    if(o1 == "nan" or o1 == "inf" or o1 == "-inf"):
        return false
    elif (float('nan')==o1):
        return false
    elif(math.isnan(o1)):
        return false
    elif(isinstance(o1, float)):
        if (float('-inf') < float(o1) < float('inf')):
            if(abs(o1-divConstant)<0.01) :
               return false
            else:
                if(not (c1.compare(zoo))):
                    return false
                if(not (c1.compare(I))):
                    return false
                if(c1=="nan" or c1=="inf" or c1=="-inf"):
                    return false
                elif (float('nan')==c1):
                    return false
                elif(math.isnan(c1)):
                    return false
                elif (float('-inf') < float(c1) < float('inf')):
                    return true
                else:
                    return false
    return false

#difference and error between observed data and correct data
#output is length 1
def checkdiff_1(name, obv, cor):
    maxdiff = 0
    sumdiff = 0
    length = 0
    obs=0.0
    o6 = 0.0
    c6 = 0.0
    per = 0.0
    t = 0
    for (o1,c1) in zip(obv,cor):
        #print ("observed:",o,"correct:",c)
        length+=1
        if(check_float(o1, c1)):
            t+=1
            diff=abs(o1-c1)
            sumdiff += diff
            if(diff > maxdiff):
                maxdiff = diff
                o6 = o1
                c6 = c1
        else:
            continue

    if(t==0):
        rtn ="NAN-single"
        return  (rtn, 0, 0, 0, 0, 0, 1)
    else:
        if(o6==0):
            per = 0.0
        else:
            per  = abs(100*(maxdiff/o6))
        avg = sumdiff/length
        s=" max diff: "+str(round(maxdiff,4))+" sumdiff: "+str(round(avg,4))+" "+ str(round(per,4))+"% c:"+str( c6)+ " o:"+str(o6)
            #print("current maximal difference", maxdiff, "c: ", c, " o:",o)
        return toStr(name,"x0", maxdiff,avg, per ,s)

def checkdiff(name, obv, cor):
    no = len(obv)
    nc = len(cor)
    if(no!=nc):
        raise ("different size for data- observed: "+str(n0)+"correct: "+str(nc))
    maxdiff=0
    sumdiff = 0
    length = 0
    s=""
    o6 = 0.0
    c6 = 0.0
    per= 0.0
    size =len(obv[0])
    #print "no ",no," size: ",size
    pre =""
    t= 0

    for i in range(no):
        for j in range(size):
            length+=1
            o1 = obv[i][j]
            c1 = cor[i][j]
            if(check_float(o1, c1)):
                t+=1
                diff=abs(o1-c1)
                sumdiff += diff
                if(diff > maxdiff):
                    maxdiff = diff
                    o6 = o1
                    c6 = c1
            else:
                continue
    if(o6==0):
        per = 0.0
    else:
        per  = abs(100*(maxdiff/o6))

    if(t==0):
        rtn ="NAN-multiple"
        return  (rtn, 0, 0, 0, 0, 0, 1)
    else:
        avg = sumdiff/length
        s=" max diff: "+str(round(maxdiff,4))+" sumdiff: "+str(round(avg,4))+" "+ str(round(per,4))+"% c:"+str( c6)+ " o:"+str(o6)+pre
        return toStr(name,"x0", maxdiff,avg, per ,s)

#chose function based on length of output
def compare(app, obv, cor):
    ex_otype = fty.get_tensorType(app.oty)
    if(ty_scalarT==ex_otype):
        return checkdiff_1(app.name, obv, cor)
    else:
        return checkdiff(app.name, obv, cor)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0