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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3946 - (download) (as text) (annotate)
Sat Jun 11 00:39:19 2016 UTC (2 years, 9 months ago) by cchiw
File size: 15423 byte(s)
 added outer prod
import sys
import re
import os
from test_createField import createField
from test_writeDiderot import writeDiderot
from test_eval import eval
from test_compare import compare
from test_observed import observed
from obj_apply import *
from obj_ex import  *
from obj_field import *
from set import  *

# unu grid -i inputfile.nrrd | unu save -f text
# ^ where all the points are.

opr_max = 11
todo = sys.argv[1]
vispath = "/Users/chariseechiw/diderot/vis15/bin/diderotc --exec "
einpath =  "/Users/chariseechiw/diderot/ein16/bin/diderotc "
runtimepath  =  ""
isVis15 = false
if isVis15:
    runtimepath  = vispath
else:
    runtimepath  =  einpath


def clean():
    # Clean up
    os.system("rm *.o")
    os.system("rm *.pyc")
    os.system("rm *.h")
    os.system("rm *.nrrd")

def write(e):
    print (e)
# if it works continue
# f = open("results_tmp.txt", 'a+')
#f.write(e)
#f.close()
def write2(e):
    print (e)
    # if it works continue
    f = open("results_Final.txt", 'a+')
    f.write(e)
    f.close()
def pnt1(pre, cnt, total,rst_good,  rst_eh, rst_terrible, rst_compile):
    x= "\n"+pre+" good:"+str(rst_good)+" eh:"+str( rst_eh)+" terrible:"+str(rst_terrible)+" didn't compile:"+str(rst_compile)
    print (x)
    write2(x)
def pnt2(pre, cnt, total,rst_good,  rst_eh, rst_terrible, rst_compile):
    x= "\n"+pre+" good:"+str(rst_good)+" eh:"+str( rst_eh)+" terrible:"+str(rst_terrible)+" didn't compile:"+str(rst_compile)
    x+= " Number of tests ran: "+str(cnt)+" Total iterations attempted: " +str(total)
    print (x)
    write2(x)
def pnt3(pre, cnt, total,rst_good,  rst_eh, rst_terrible, rst_compile):
    x= "\n"+pre+" good:"+str(rst_good)+" eh:"+str( rst_eh)+" terrible:"+str(rst_terrible)+" didn't compile:"+str(rst_compile)
    x+= " Number of tests ran: "+str(cnt)+" Total iterations attempted:" +str(total)
    print (x)
    write2(x)

#get all the names
def get_names():
    for ex_opr in range(opr_max+1):
        ex = get_ex(ex_opr)
        n = len(ex.tys)
        print ("cmd:(1 "+str(ex_opr)+") opr "+example.get_oprname(ex))
        for ex_num  in range(n):
            print ("\tcmd:(-1 "+str(ex_opr)+","+str(ex_num)+")"+example.toStr(ex,ex_num))

# already created app object
def central(app, coeffs, dimF):
    print "inside central"
    os.system("rm data/*.txt")
    print app.name
    # testing positions
    positions = get_positions(dimF)
    #print "positions",positions
    # samples
    outSize = get_size(dimF)
    #create synthetic field data with diderot
    createField(app, outSize, coeffs)
    #create diderot program with operator
    writeDiderot(p_Observ, app, positions, output, runtimepath, isVis15)
    if(isVis15):
        return "compiled"
    else:
        #read observed data
        observed_data = observed(app, output)
        #print "observed data from python",observed_data
        if (len(observed_data)<2):
            return   ("diderot could not handle program",0,0,0,1)
        #correct values from python
        correct_data = eval(app , positions)
        #print "correct data from python", correct_data
        #--------------------Comparison
        #compare observed and correct data
        rtn = compare(app, observed_data, correct_data)
        (rtn_1, rst_good, rst_eh, rst_terrible, rst_compile) =  rtn
        if (rst_terrible==1):
            print "terrible-data"
            print "observed data from python",observed_data
            print "correct data from python", correct_data
        clean()
        return rtn

# get example from list of examples
def single1_specific_ex(ex_opr, ex_num):
    #print "single specific ex"
    (name,opr,ishape)= get_single_example(int(ex_opr), int(ex_num))
    #print name
    #create app object
    (app, coeffs, dimF,_) = mkApply_fld(name, opr, ishape, inputfile)
    # main
    (rtn,_,_,_,_) = central(app, coeffs, dimF)
    return rtn

#run all examples for a specific operator
def single1_all_ops(ex_opr):
    #print "single all ops"
    ex = get_ex(ex_opr)
    #print(example.get_oprname(ex))
    n = len(ex.tys)
    rtn = [str(ex_opr)+": length "+str(n)]
    for ex_num  in range(n):
        #print (example.toStr(ex,ex_num))
        x =  example.toStr(ex,ex_num)+" "+str(ex_opr)+"-"+str(ex_num)+":"
        #print x
        y = single1_specific_ex(ex_opr, ex_num)
        rtn.append(x+y)
    return rtn

#run all possible examples
def depth1_all_exs():
    rtn = []
    for ex_opr in range(opr_max+1):
        rtn.append(single1_all_ops(ex_opr))
    for i in rtn:
        for j in i:
            print j

##################################################################################################
# get result after applying operators
def get_tshape(opr_inner,ishape):
    arity=opr_inner.arity
    #print "checking for first time in rum.py"
    #print ("checking second layers", opr_inner.name,"ishape",ishape[0].name)
    if(arity==1):
        return applyUnaryOp(opr_inner,ishape)
    elif(arity==2):
        #print ("-", ishape[1].name)
        return applyBinaryOp(opr_inner,ishape)

# checks to see if specific ex works
def embed2_specific_ex(ex_opr, ex_num, opr_outer, fty):
    # current example
    (name,opr_inner,ishape)= get_single_example(ex_opr, ex_num)
    #print ("name:", name, "outer",opr_outer.name)
    # first layer
    (_, tshape) = get_tshape(opr_inner,ishape)
    #second layer, adds second field type
    ishape = ishape+fty
    (tf, _) = get_tshape(opr_outer,[tshape]+fty)
    #print ("checking second layers", opr_outer.name,"ishape",ishape[0].name,ishape[1].name)
    if(tf==true):# if it works continue
        write("\n\t\tpassed ")
        #create app object
        name  = opr_outer.name+"("+opr_inner.name+")"
        #print "before mkapply twice"
        (app, coeffs, dimF) = mkApply_twice(name, opr_inner,opr_outer, ishape, inputfile)
        #print "\nafter mk apply twice:\n",apply.toStr(app,0 )
        # main part
        (rtn, rst_good, rst_eh, rst_terrible, rst_compile) =  central(app, coeffs, dimF)
        return (1, rtn, rst_good, rst_eh, rst_terrible, rst_compile)
    else:
        x = "\n\t\tskipped did not apply "+opr_outer.name+" to ("+tshape.name+","
        write(x)
        return (0, x, 0, 0, 0,0)

#apply all possible unary operators to a specific inside example
def embed2_outer(ex_opr, ex_num, opr_outer,ftyname, es):
    #write("\n*******:"+name+" len("+str(n)+")******")
    x = "-i_opr#("+idToStr(ex_opr)
    x +=")type#("+str(ex_num)+","+ftyname
    x+=")\no_opr#("+opr_outer.name
    x+=" between that and "+ftyname
    write("\n"+x)
    return embed2_specific_ex(ex_opr, ex_num, opr_outer, es)

def embed2_apply_outer(ex_opr, ex_num, ex_outer, gty):
    if(gty==-1):
        #apply all possible unary operators to a specific inside example
        opr_outer = op_unary[ex_outer]
        return  embed2_outer(ex_opr, ex_num, opr_outer, "_",[])
    else:
        #apply all possible binary operators to a specific inside example
        fty =l_all_F[gty]
        opr_outer= op_binary[ex_outer]
        return embed2_outer(ex_opr, ex_num, opr_outer, fty.name, [fty])

#apply all possible unary operators to a specific inside example
def embed2_iter_outer(ex_opr, ex_num, gty):
    def getN():
        if(gty==-1):
            return len(op_unary)
        else:
            return len(op_binary)
    n = getN()
    rtn = []
    cnt = 0
    total = n
    rst_good = 0
    rst_eh = 0
    rst_terrible = 0
    rst_compile = 0
    for  ex_outer in range(n):
        (cnt1, rtn1, rst_good1, rst_eh1, rst_terrible1, rst_compile1) = embed2_apply_outer(ex_opr, ex_num, ex_outer, gty)
        rtn.append(rtn1)
        cnt+=int(cnt1)
        rst_good +=rst_good1
        rst_eh +=  rst_eh1
        rst_terrible += rst_terrible1
        rst_compile += rst_compile1
    return (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total)

#run all examples for a specific operator
def embed2_iter_tys(ex_opr, gty):
    n = len(get_ex(ex_opr).tys)
    rtn = []
    cnt = 0
    total = 0
    rst_good = 0
    rst_eh = 0
    rst_terrible = 0
    rst_compile = 0
    for ex_num  in range(n):
        (cnt1, rtn1, rst_good1, rst_eh1, rst_terrible1, rst_compile1, total1) = embed2_iter_outer(ex_opr, ex_num, gty)
        rtn.append(rtn1)
        cnt+=int(cnt1)
        total +=total1
        rst_good +=rst_good1
        rst_eh +=  rst_eh1
        rst_terrible += rst_terrible1
        rst_compile += rst_compile1
    return  (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total)

#run all possible examples
def embed2_iter_inner(gty):
    n = opr_max+1
    rtn = []
    cnt = 0
    total = 0
    rst_good = 0
    rst_eh = 0
    rst_terrible = 0
    rst_compile = 0
    for ex_opr in range(n):
        (cnt1, rtn1, rst_good1, rst_eh1, rst_terrible1, rst_compile1, total1) = embed2_iter_tys(ex_opr, gty)
        rtn.append(rtn1)
        cnt+=int(cnt1)
        total +=total1
        rst_good +=rst_good1
        rst_eh +=  rst_eh1
        rst_terrible += rst_terrible1
        rst_compile += rst_compile1
    return (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total)


n = int(todo) #command
if (n==0):
    #write all the names
    get_names()
elif (n==1):
    #run all the programs
    depth1_all_exs()
elif (n==-1):
    #run all the programs
    gty = -1
    (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total) = embed2_iter_inner(gty)
    pnt2("-1 cmd:", cnt, total, rst_good,  rst_eh, rst_terrible, rst_compile)
    for i in rtn:
        for j in i:
            for k in j:
                print k
elif (n==2):
    #run specific operators
    ex_opr = int(sys.argv[2])
    rtn = single1_all_ops(ex_opr)
    for i in rtn:
        print i
    os.system("open results_tmp.txt")
elif(n==-2):
    ex_opr = int(sys.argv[2])
    gty = -1
    (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total) = embed2_iter_tys(ex_opr, gty)
    pnt2("-2 cmd:", cnt, total, rst_good,  rst_eh, rst_terrible, rst_compile)
    for i in rtn:
        for j in i:
            print j
elif (n==3):
    # run one example
    # with a specific operator
    # number from generated list of types (not type id).
    ex_opr = int(sys.argv[2])
    ex_num = int(sys.argv[3])
    rtn = single1_specific_ex(ex_opr, ex_num)
    print rtn
elif (n==-3):
    # run n examples
    # with a specific operator,
    # number from generated list of types (not type id.)
    # and all possible outer operators
    ex_opr = int(sys.argv[2])
    ex_num = int(sys.argv[3])
    gty = -1
    (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total) = embed2_iter_outer(ex_opr, ex_num, gty)
    for i in rtn:
        print i
elif (n==4): # run a specific single layer example with type and opr
    raise "unsupported"
elif(n==-4):
    # run a specific double layer example
    ex_opr = int(sys.argv[2])
    ex_num = int(sys.argv[3])
    ex_outer= int(sys.argv[4])
    opr_outer= op_unary[ex_outer]
    rtn = embed2_specific_ex(ex_opr, ex_num,  opr_outer,[])
    print rtn
elif (n==-5):
    #ex_opr)+","+str(ex_outer2)+","+str(ex_ty2)+","+str( ex_num
    # run n examples
    # with a specific operator,
    # number from generated list of types (not type id.)
    # and all possible outer operators
    ex_opr = int(sys.argv[2])   #inside operator
    ex_outer = int(sys.argv[3])#outer binary operator
    ex_num = int(sys.argv[5])#inside type on list
    ex_ty2 = int(sys.argv[4]) #extra type
    (cnt, rtn, rst_good1, rst_eh1, rst_terrible1, rst_compile1) = embed2_apply_outer(ex_opr, ex_num, ex_outer, ex_ty2)
    print rtn
elif (n==-6):
    # run n examples
    # with a specific operator,
    # number from generated list of types (not type id.)
    # and all possible outer operators
    ex_opr = int(sys.argv[2])   #inside operator
    #ex_num = int(sys.argv[2])#inside type on list
    ex_outer2 = int(sys.argv[3])#outer binary operator
    if(ex_outer2>6):
        raise "outer2 needs to be less than 6"
    #  ex_ty2 = int(sys.argv[4])
    rtn=[]
    #ex_opr=10
    cnt = 0
    total = 0
    rst_good = 0
    rst_eh = 0
    rst_terrible = 0
    rst_compile = 0
    #for ex_opr in range(len(op_binary)):
    ex = get_ex(ex_opr)
    n = len(ex.tys)
    m = len(l_all_F)
    write2("\n\n starting: " +str(ex_opr)+" outer range: 6, allF: "+str(m)+ " n: "+str(n))
    for ex_ty2 in range(m):  #extra type
        rst_good2 = 0
        rst_eh2 = 0
        rst_terrible2 = 0
        rst_compile2 = 0
        for  ex_num  in range(n):
            print("current : "+str(ex_opr)+" "+str(ex_outer2)+" "+str(ex_ty2)+" "+str( ex_num))
            #write2("\n"+str(ex_opr)+","+str(ex_outer2)+","+str(ex_ty2)+","+str( ex_num))
            (cnt1, _, rst_good1, rst_eh1, rst_terrible1, rst_compile1) = embed2_apply_outer(ex_opr, ex_num, ex_outer2, ex_ty2)
            # rtn.append(rtn1)
            cnt+=int(cnt1)
            total +=1
            rst_good +=rst_good1
            rst_eh +=  rst_eh1
            rst_terrible += rst_terrible1
            rst_compile += rst_compile1
            rst_good2 +=rst_good1
            rst_eh2 +=  rst_eh1
            rst_terrible2 += rst_terrible1
            rst_compile2 += rst_compile1
        pnt1("onerun:"+str(ex_opr)+","+str(ex_outer2)+","+str(ex_ty2),cnt, total,rst_good2,  rst_eh2, rst_terrible2, rst_compile2)
    pnt3("Final for " +str(ex_opr)+":", cnt, total,rst_good,  rst_eh, rst_terrible, rst_compile)
elif (n==-7):
    # run n examples
    # with a specific operator,
    # number from generated list of types (not type id.)
    # and all possible outer operators
    ex_opr = int(sys.argv[2])   #inside operator
    #ex_num = int(sys.argv[2])#inside type on list
    # ex_outer2 = int(sys.argv[3])#outer binary operator
    #  ex_ty2 = int(sys.argv[4])
    rtn=[]
    #ex_opr=10
    cnt = 0
    total = 0
    rst_good = 0
    rst_eh = 0
    rst_terrible = 0
    rst_compile = 0
    #for ex_opr in range(len(op_binary)):
    ex = get_ex(ex_opr)
    n = len(ex.tys)
    m = len(l_all_F)
    write2("\n\n starting: " +str(ex_opr)+" outer range: 6, allF: "+str(m)+ " n: "+str(n))
    for ex_outer2 in range(6):
        #write2("outer op"+str(ex_outer2)+"-"+idToStr(ex_outer2+6))
        for ex_ty2 in range(m):  #extra type
            rst_good2 = 0
            rst_eh2 = 0
            rst_terrible2 = 0
            rst_compile2 = 0
            for  ex_num  in range(n):
                print("current : "+str(ex_opr)+" "+str(ex_outer2)+" "+str(ex_ty2)+" "+str( ex_num))
                #write2("\n"+str(ex_opr)+","+str(ex_outer2)+","+str(ex_ty2)+","+str( ex_num))
                (cnt1, _, rst_good1, rst_eh1, rst_terrible1, rst_compile1) = embed2_apply_outer(ex_opr, ex_num, ex_outer2, ex_ty2)
                # rtn.append(rtn1)
                cnt+=int(cnt1)
                total +=1
                rst_good +=rst_good1
                rst_eh +=  rst_eh1
                rst_terrible += rst_terrible1
                rst_compile += rst_compile1
                rst_good2 +=rst_good1
                rst_eh2 +=  rst_eh1
                rst_terrible2 += rst_terrible1
                rst_compile2 += rst_compile1
            pnt1("onerun:"+str(ex_opr)+","+str(ex_outer2)+","+str(ex_ty2),cnt, total,rst_good2,  rst_eh2, rst_terrible2, rst_compile2)
        pnt2("cumulative:"+str(ex_opr)+","+sstr(ex_outer2),cnt, total,rst_good,  rst_eh, rst_terrible, rst_compile)
    pnt3("Final for " +str(ex_opr)+":", cnt, total,rst_good,  rst_eh, rst_terrible, rst_compile)
else:
  raise "unsupported"

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