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 4210 - (download) (as text) (annotate)
Thu Jul 14 18:01:19 2016 UTC (3 years, 8 months ago) by cchiw
File size: 15911 byte(s)
build on testing
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 = 17
layer = int(sys.argv[1])
args = int(sys.argv[2])

cnnt=0
vispath = "/Users/chariseechiw/diderot/vis15/bin/diderotc --exec "
einpath =  "/Users/chariseechiw/diderot/ein16/bin/diderotc "
runtimepath  =  ""
fld=""
isVis15 = false
def pathToString():
    if isVis15:
        return "vis15"
    else:
        return "ein16"

branch = pathToString()
if isVis15:
    runtimepath  = vispath
    fld="weds"
else:
    runtimepath  = einpath
    fld="weds"

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

def write(e):
    print (e)
# if it works continue
# f = open("results_tmp.txt", 'a+')
#f.write(e)
#f.close()
def writeall(e):
    print (e)
    # if it works continue
    f = open("results_Final.txt", 'a+')
    f.write(e)
    f.close()
def writesummary(e):
    print (e)
    # if it works continue
    f = open("results_summary.txt", 'a+')
    f.write(e)
    f.close()

def writetys(e):
    print (e)
    # if it works continue
    f = open("results_tys.txt", 'a+')
    f.write(e)
    f.close()

def pnt2(pre, cnt, total,rst_good,  rst_eh, rst_terrible, rst_compile):
    x= "\n"
    if(rst_good==cnt):
        x+="PASS:"
    else:
        x+="CHECK:"
    x+= pre+" "+branch
    if(rst_good>0):
        x+=" A:"+str(rst_good)+"|"+str(cnt)
    if(rst_eh>0):
        x+=" B:"+str( rst_eh)+"|"+str(cnt)
    if(rst_terrible>0):
        x+=" D:"+str( rst_terrible)+"|"+str(cnt)
    if(rst_compile>0):
        x+=" NA:"+str( rst_compile)+"|"+str(cnt)
    x+= " #ran: "+str(cnt)+"|" +str(total)
    x+="\n***************************************************\n"
    print (x)
    writeall(x)
    writesummary(x)

##################################################################################################
# iterate over ty2
def is_unary():
    if(layer==2):
        return true
    else:
        return false
# get outer operator
def get_outer(ex_outer):
    if(is_unary()):
        return op_unary[ex_outer]
    else:
        return op_binary[ex_outer]
# length of operators
def getN():
    if(is_unary()):
        return len(op_unary)
    else:
        return 6 #len(op_binary)

# get specific outer operator
def get_fty(ex_ty2):
    if(is_unary()):
        print "found unary"
        return []
    else:
        print "not unary"
        fty =l_all_F[ex_ty2]
        return [fty]

def idToOpr_outer(ex_outer):
    return get_outer(ex_outer).name

def idToOpr_inner(ex_opr):
    print "idToOpr_inner ex_opr",ex_opr
    return get_single_exampleName(ex_opr)

##################################################################################################
# already created app object
def central(app, coeffs, dimF,ex_opr, ex_num ,id):
    print "inside central"
    os.system("rm data/*.txt")
    names= "t_"+str(ex_opr)+"_"+str(id)+"_"+str(ex_num)#+"_"+str(app.oty.id)
    #apply.oprToStr(app)
    writetys("\n\t-"+apply.get_all_FieldTys(app)+"|"+  names)
    # 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(4<1):
        return "compiled"
    else:
        #read observed data
        observed_data = observed(app, output)
        #print "observed data from python",observed_data
        if (len(observed_data)<2):
            #print "fail did not find data"
            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
        x = ("\n\t-"+rtn_1+" "+names)
        writeall(x)
        if (rst_terrible==1):
            writesummary(x)
            path = fld+"/"+names
            os.system("mkdir "+path)
            os.system("cp data/output5_p_observ.diderot "+path+"/"+names+".diderot")
            os.system("cp output5_p_observ.diderot "+path+"/"+names+".diderot")
            os.system("cp data/inputfile_0.nrrd "+path+"/inputfile_0.nrrd")
            os.system("cp data/inputfile_1.nrrd "+path+"/inputfile_1.nrrd")
            os.system("cp data/inputfilecat_2.nrrd "+path+"/inputfilecat_2.nrrd")
            os.system("cp data/inputfile_2.nrrd "+path+"/inputfile_2.nrrd")
            a= "\n\nterrible-data"
            b= "\n\nobserved data from python"+str(observed_data)
            c= "\n\ncorrect data from python"+str(correct_data)
            d="\n\n coeffs"+str(coeffs)
            f = open(path+"/rst.txt", 'w+')
            f.write(a+b+c+d+"\n\n"+rtn_1)
            f.close()
        clean()
        return rtn

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

# checks to see if specific ex works
def embed_base_specific_ex(ex_opr, ex_outer, ex_num, ex_ty2):
    print ("embed_base_specific_ex: ex_opr,",ex_opr, " ex_outer:",ex_outer,"  ex_num: ", ex_num, " ex_ty2:", ex_ty2)
    opr_outer = get_outer(ex_outer)
    fty= get_fty(ex_ty2)
    id = ex_outer
    print "tree-A"
    # get result after applying operators
    def get_tshape(opr1, ishape):
        print "tshape-A"
        arity=opr1.arity
        #print "checking for first time in rum.py"
        #print ("checking second layers", opr_inner.name,"ishape",ishape[0].name)
        print "tshape-B"
        if(arity==1):
            print "tshape-C"
            print (opr1.name,ishape[0])
            return applyUnaryOp(opr1,ishape)
        elif(arity==2):
            print "tshape-D"
            #print ("-", ishape[1].name)
            return applyBinaryOp(opr1, ishape)
    # current example
    print "tree-B"
    (name,opr_inner,ishape)= get_single_example(ex_opr, ex_num)
    #print ("name:", name, "outer",opr_outer.name)
    # first layer
    print "tree-C"
    (tf, tshape) = get_tshape(opr_inner,ishape)
    if(tf==false):
        return (0, "", 0, 0, 0,0)
    #second layer, adds second field type
    print "tf",tf, "tshape",tshape
    print "tree-D"
    ishape = ishape+fty
    print "tree-E"
    (tf, _) = get_tshape(opr_outer,[tshape]+fty)
    print "tree-F"
    # checks to see if specific ex works
    if(tf==true):# if it works continue
        print "tf-T"
        write("\n\t\t typechecks ")
        #create app object
        print "mark -a"
        name  = opr_outer.name+"("+opr_inner.name+")"
        print "mark -b"
        #print "before mkapply twice"
        (app, coeffs, dimF) = mkApply_twice(name, opr_inner,opr_outer, ishape, inputfile)
        print "mark -c"
        #print "\nafter mk apply twice:\n",apply.toStr(app,0 )
        # main part
        (rtn0, rst_good, rst_eh, rst_terrible, rst_compile) =  central(app, coeffs, dimF,ex_opr, ex_num,id)
        print "mark d"
        rtn = str(ex_opr)+"-"+str( ex_num)+"-"+str(  opr_outer.name)+"-"+name+":"+rtn0
        print "mark e"
        return (1, rtn, rst_good, rst_eh, rst_terrible, rst_compile)
    else:
        print "tf-F"
        x = "\n\t\tskipped did not apply "+opr_outer.name+" to ("+tshape.name+","
        write(x)
        return (0, x, 0, 0, 0,0)

################################################## iterating ###############################################
#iterating ex_num from 0...n
def embed_base_iter_num(ex_opr, ex_outer, ex_ty2, n):
    print ("embed_base_iter_num: ex_opr,",ex_opr, " ex_outer:",ex_outer, " ex_ty2:", ex_ty2, " n:", n)
    rtn = []
    cnt = 0
    total = 0
    rst_good = 0
    rst_eh = 0
    rst_terrible = 0
    rst_compile = 0
    for ex_num  in range(n):
        print ("embed_base_iter_num_inside loop: ex_opr,",ex_opr, " ex_outer:",ex_outer, " ex_ty2:", ex_ty2, " n:", n,"ex_num:",ex_num)
        (cnt1, rtn1, rst_good1, rst_eh1, rst_terrible1, rst_compile1) =  embed_base_specific_ex(ex_opr, ex_outer, ex_num, ex_ty2)
        print "post embed_base_specific_ex"
        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)

def embed_base_iter_ty2(ex_opr, ex_outer):
    print "embed_base_iter_ty2: ex_opr",ex_opr, " ex_outer:",ex_outer
    if(is_unary()):
        gty=-1
        nn = len(get_ex(ex_opr).tys)
        return embed_base_iter_num(ex_opr, ex_outer, gty, nn)
    else:
        rtn=[]
        cnt = 0
        total = 0
        rst_good = 0
        rst_eh = 0
        rst_terrible = 0
        rst_compile = 0
        rst_total = 0
        ex = get_ex(ex_opr)
        n = len(ex.tys)
        m = len(l_all_F) # use all field types as extra types
        for ex_ty2 in range(m):  #extra type
            print "embed_base_iter_ty2: ex_opr",ex_opr, " ex_outer:",ex_outer," ex_ty2: ", ex_ty2
            (cnt1, _, rst_good1, rst_eh1, rst_terrible1, rst_compile1, rst_total1) = embed_base_iter_num(ex_opr, ex_outer, ex_ty2, n)
            cnt+=int(cnt1)
            total +=1
            rst_good +=rst_good1
            rst_eh +=  rst_eh1
            rst_terrible += rst_terrible1
            rst_compile += rst_compile1
            rst_total+=rst_total1
        return (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total)

#iterating ex_outer from 0...n
def embed_base_iter_outer(ex_opr):
    print "\nembed_base_iter_outer: ex_opr",ex_opr
    n = getN()
    rtn = []
    cnt = 0
    total = 0
    rst_good = 0
    rst_eh = 0
    rst_terrible = 0
    rst_compile = 0
    #have ex_opr iterating over ex_outer
    for  ex_outer in range(n):
        y = "\n\n"+idToOpr_outer(ex_outer)+ "_"+idToOpr_inner(ex_opr)+" |"+str(ex_opr)+"_"+str(ex_outer)
        writetys(y)
        (cnt1, rtn1, rst_good1, rst_eh1, rst_terrible1, rst_compile1, total1) = embed_base_iter_ty2(ex_opr, ex_outer)
        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():
    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) = embed_base_iter_outer(ex_opr)
        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 examples for a specific operator
def single_all_ops(ex_opr):
    ex = get_ex(ex_opr)
    #print(example.get_oprname(ex))
    n = len(ex.tys)
    if (n==0):
        return ("none", 0,0,0,0,0)
    rtn = [str(ex_opr)+": length "+str(n)]
    cnt = 0
    total = n
    rst_good = 0
    rst_eh = 0
    rst_terrible = 0
    rst_compile = 0
    y = "\n\n"+idToOpr_inner(ex_opr)+" |"+str(ex_opr)
    writetys(y)
    for ex_num  in range(n):
        (rtn1, rst_good1, rst_eh1, rst_terrible1, rst_compile1) = single_specific_ex(ex_opr, ex_num)
        rtn.append(rtn1)
        rst_good +=rst_good1
        rst_eh +=  rst_eh1
        rst_terrible += rst_terrible1
        rst_compile += rst_compile1
    return (rtn, rst_good, rst_eh, rst_terrible, rst_compile, n)

##################################################################################################
##################################################################################################
cmd = "cmd: "+str(layer)+" "+str(args)+" "
if (layer==1):
    if(args==0):
        # iterate over single layer
        rtn = []
        for ex_opr in range(opr_max+1):
            (rtn, rst_good, rst_eh, rst_terrible, rst_compile, n) = single_all_ops(ex_opr)
            pnt2(cmd +str(ex_opr)+"("+get_exname(ex_opr)+")--" ,n, n, rst_good,  rst_eh, rst_terrible, rst_compile)
    elif (args==1):
        # iterate over single layer
        ex_opr = int(sys.argv[3])
        (rtn, rst_good, rst_eh, rst_terrible, rst_compile, n)  = single_all_ops(ex_opr)
        pnt2(cmd +str(ex_opr)+"("+get_exname(ex_opr)+")--" ,n, n, rst_good,  rst_eh, rst_terrible, rst_compile)
elif (layer==2):
    if(args==0):
        #run all the programs
        (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total) = embed2_iter_inner()
        pnt2(cmd, cnt, total, rst_good,  rst_eh, rst_terrible, rst_compile)
    elif(args==1):
        # iterate over double layer with unary operator
        ex_opr = int(sys.argv[3])
        (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total) = embed_base_iter_outer(ex_opr)
        pnt2(cmd+str(ex_opr)+"("+get_exname(ex_opr)+")--", cnt, total, rst_good,  rst_eh, rst_terrible, rst_compile)
    elif(args==3):
        #  run a specific iterate over double layer with unary operator
        ex_opr = int(sys.argv[3])
        ex_outer= int(sys.argv[4])
        if((ex_outer)> 10):
            raise Exception("not unary id:"+str(ex_outer))
        ex_num = int(sys.argv[5])
        ex_ty2 = None
        rtn = embed_base_specific_ex(ex_opr, ex_outer, ex_num, ex_ty2)
        print rtn
elif (layer==3):
    # number from generated list of types (not type id.)
    ex_opr = int(sys.argv[3])   #inside operator
    if(args==0):
        raise Exception("not possible to iterate over all binary operators")
    elif (args==1):
        # and all possible outer operators
        (cnt, rtn, rst_good, rst_eh, rst_terrible, rst_compile, total)=  embed_base_iter_outer(ex_opr)
        pnt2(cmd+str(ex_opr)+":", cnt, total,rst_good,  rst_eh, rst_terrible, rst_compile)
    elif (args==2):
        ex_outer = int(sys.argv[4])#outer binary operator
        if(ex_outer>len(op_binary )):
            raise Exception ("outer2 needs to be less than "+str(len(op_binary)))
        y = "\n\n"+idToOpr_outer(ex_outer)+ "_"+idToOpr_inner(ex_opr)+" |"+str(ex_opr)+"_"+str(ex_outer)
        writetys(y)
        writeall(y)
        (cnt, _, rst_good, rst_eh, rst_terrible, rst_compile, total) = embed_base_iter_ty2(ex_opr, ex_outer)
        pnt2(cmd+str(ex_opr)+" "+str(ex_outer)+":", cnt, total, rst_good,  rst_eh, rst_terrible, rst_compile)
    elif (args==4):
        ex_outer = int(sys.argv[4]) # outer binary operator
        ex_num = int(sys.argv[5])   # inside type on list
        ex_ty2 = int(sys.argv[6])   # extra type (not in text file)
        (cnt, rtn, rst_good1, rst_eh1, rst_terrible1, rst_compile1) = embed_base_specific_ex(ex_opr, ex_outer, ex_num, ex_ty2)
        pnt2(cmd+str(ex_opr)+" "+str(ex_outer)+" "+str(ex_num)+" "+str(ex_ty2)+":", cnt, cnt, rst_good1,  rst_eh1, rst_terrible1, rst_compile1)
else:
  raise "unsupported"

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