Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

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

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

Wed May 18 16:45:54 2016 UTC (3 years, 10 months ago) by cchiw
File size: 3346 byte(s)
`added some binary operators for 2-d`
```from test_examples import value_scalarT
from test_examples import value_vec2T
from test_examples import value_mat2x2T

#needs output tensor types
ty_scalarT = value_scalarT()
ty_vec2T = value_vec2T()
ty_mat2x2T=value_mat2x2T()

#difference and error between observed data and correct data
#output is length 1
def checkdiff_1(obv, cor):
maxdiff = 0
maxerr = 0
for (o,c) in zip(obv,cor):
print ("observed:",o,"correct:",c)
if (o==0):
break
else:
diff=abs(c-o)
if(diff>maxdiff):
maxdiff=diff
if (c==0):
break
else:
err=(diff/c)*100
if(err>maxerr):
maxerr=err

#print("diff", diff,"err", err)
print("maximal difference", maxdiff, "maximal percent error", maxerr)

#output is length 2
def checkdiff_2(obv, cor):
maxdiffx = 0
maxerrx = 0
maxdiffy = 0
maxerry = 0
for ((o1,o2),(c1,c2)) in zip(obv,cor):
if (o1==0):
break
diffx=abs(c1-o1)
if(diffx>maxdiffx):
maxdiffx=diffx
diffy=abs(c2-o2)
if(diffy>maxdiffy):
maxdiffy=diffy
# percent error
if(c1==0):
break
errx=(diffx/c1)*100
if(errx>maxerrx):
maxerrx=errx
if(c2==0):
break
erry=(diffy/c2)*100
if(erry>maxerry):
maxerry=erry
print("x-axis: maximal difference", maxdiffx, "maximal % err", maxerrx)
print("y-axis: maximal difference", maxdiffy, "maximal % err", maxerry)

#output is length 4
def checkdiff_4(obv, cor):
maxdiffx = 0
maxerrx = 0
maxdiffy = 0
maxerry = 0
maxdiff3 = 0
maxerr3 = 0
maxdiff4 = 0
maxerr4 = 0
for ((o1,o2,o3,o4),(c1,c2,c3,c4)) in zip(obv,cor):
if (o1==0):
break
diffx=abs(c1-o1)
if(diffx>maxdiffx):
maxdiffx=diffx
diffy=abs(c2-o2)
if(diffy>maxdiffy):
maxdiffy=diffy
diff3=abs(c3-o3)
if(diff3>maxdiff3):
maxdiff3=diff3
diff4=abs(c4-o4)
if(diff4>maxdiff4):
maxdiff4=diff4
# percent error
if(c1==0):
break
errx=(diffx/c1)*100
if(errx>maxerrx):
maxerrx=errx
if(c2==0):
break
erry=(diffy/c2)*100
if(erry>maxerry):
maxerry=erry
if(c3==0):
break
err3=(diff3/c3)*100
if(err3>maxerr3):
maxerr3=err3
if(c4==0):
break
err4=(diff4/c4)*100
if(err4>maxerr4):
maxerr4=err4
print("x-axis: maximal difference", maxdiffx, "maximal % err", maxerrx)
print("y-axis: maximal difference", maxdiffy, "maximal % err", maxerry)
print("axis-3: maximal difference", maxdiff3, "maximal % err", maxerr3)
print("axis-4: maximal difference", maxdiff4, "maximal % err", maxerr4)

#chose function based on length of output
def compare(ex_otype, obv, cor):
if(ex_otype==ty_scalarT):
return checkdiff_1(obv, cor)
elif(ex_otype==ty_vec2T):
return checkdiff_2(obv, cor)
elif(ex_otype==ty_mat2x2T):
return checkdiff_4(obv, cor)
else:
raise Exception("return value  more than 2 or less than 1 not supported")```