Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

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

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

revision 3915, Fri May 27 21:32:46 2016 UTC revision 3939, Tue Jun 7 18:06:23 2016 UTC
# Line 2  Line 2
2  from obj_ty import *  from obj_ty import *
3  from obj_operator import *  from obj_operator import *
4
5    def toStr(name,var, maxdiff,maxerr):
6        f = open("results_tmp.txt", 'a+')
7        f.write("\n\n **********")
8        #print (name)
9        x=(" max diff: "+str( maxdiff))
10        #print "x",x
11        f.write(name)
12        if (maxdiff<=0.01):
13            rtn = "Results: "+var+": good"
14            f.write("\n\t\t"+rtn)
15            f.close()
16            print rtn
17            return rtn
18        elif (maxdiff<=0.1):
19            rtn = "Results: "+var+": eh "+x
20            f.write("\n\t\t"+rtn)
21            f.close()
22            print rtn
23            return rtn
24        else:
25            rtn = "Results: "+var+": terrible "+x
26            f.write("\n\t\t"+rtn)
27            f.close()
28            print rtn
29            return rtn
30  #difference and error between observed data and correct data  #difference and error between observed data and correct data
31  #output is length 1  #output is length 1
32  def checkdiff_1(obv, cor):  def checkdiff_1(name, obv, cor):
33      maxdiff = 0      maxdiff = 0
34      maxerr = 0      maxerr = 0
35      for (o,c) in zip(obv,cor):      for (o,c) in zip(obv,cor):
36          print ("observed:",o,"correct:",c)          #print ("observed:",o,"correct:",c)
37          if (o==0):          if (o==0):
38              break              break
39          else:          else:
40              diff=abs(c-o)              diff=abs(c-o)
41              if(diff>maxdiff):              if(diff>maxdiff):
42                  maxdiff=diff                  maxdiff=diff
43            #print("current maximal difference", maxdiff, "c: ", c, " o:",o)
44          if (c==0):          if (c==0):
45              break              break
46          else:          else:
47              err=(diff/c)*100              err=(diff/c)*100
48              if(err>maxerr):              if(err>maxerr):
49                  maxerr=err                  maxerr=err
51
52          #print("diff", diff,"err", err)  def checkdiff(name, obv, cor):
53      print("maximal difference", maxdiff, "maximal percent error", maxerr)      no = len(obv)
54        nc = len(cor)
55  #output is length 2      if(no!=nc):
56  def checkdiff_2(obv, cor):          raise ("different size for data- observed: "+str(n0)+"correct: "+str(nc))
57      maxdiffx = 0      maxdiff=0
58      maxerrx = 0      maxerr=0
59      maxdiffy = 0      size =len(obv[0])
60      maxerry = 0      #print "no ",no," size: ",size
61      for ((o1,o2),(c1,c2)) in zip(obv,cor):      for i in range(no):
62          if (o1==0):          for j in range(size):
63              break              o1=obv[i][j]
64          diffx=abs(c1-o1)              #c1=round(cor[i][j], 4)
65          if(diffx>maxdiffx):              c1= cor[i][j]
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):
66          if (o1==0):          if (o1==0):
67              break              break
68          diffx=abs(c1-o1)              diff=abs(o1-c1)
69          if(diffx>maxdiffx):              if(diff > maxdiff):
70              maxdiffx=diffx                  maxdiff = diff
71          diffy=abs(c2-o2)              #print("current maximal difference", maxdiff, "c: ", c1, " o:",o1)
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
72          if(c1==0):          if(c1==0):
73              break              break
74          errx=(diffx/c1)*100              err=(diff/c1)*100
75          if(errx>maxerrx):              if(err>maxerr):
76              maxerrx=errx                  maxerr=err
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)

78
79  #chose function based on length of output  #chose function based on length of output
80  def compare(app, obv, cor):  def compare(app, obv, cor):
81      ex_otype=app.oty      ex_otype = fty.get_tensorType(app.oty)
82      if(ty_scalarT==ex_otype):      if(ty_scalarT==ex_otype):
83          return checkdiff_1(obv, cor)          return checkdiff_1(app.name, obv, cor)
elif(ty_vec2T==ex_otype):
return checkdiff_2(obv, cor)
elif(ty_mat2x2T==ex_otype):
return checkdiff_4(obv, cor)
84      else:      else:
raise Exception("return value  more than 2 or less than 1 not supported")
85            return checkdiff(app.name, obv, cor)

Legend:
 Removed from v.3915 changed lines Added in v.3939