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 4236 - (download) (as text) (annotate)
Wed Jul 20 03:02:00 2016 UTC (3 years, 6 months ago) by cchiw
File size: 16839 byte(s)
added generic cases for trace|det and added test cases
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])
isVis15 = false

fld="mon_ein"  #directory to save programs

#global vars
#rtn = []
rst_cnt = 0
rst_total = 0
rst_good = 0
rst_eh = 0
rst_terrible = 0
rst_compile = 0
rst_cumulative = 0

runtimepath  =  "" #path to branch testing

if isVis15:
    runtimepath  = "/Users/chariseechiw/diderot/vis15/bin/diderotc --exec "
else:
    runtimepath  = "/Users/chariseechiw/diderot/ein16/bin/diderotc "

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 *.diderot")
    os.system("rm fs3d-vec2")
    os.system("rm fs3d-vec2")

def write(e):
    print (e)
def writeall(e):
    #print (e)
    # if it works continue
    f = open("rst/results_Final_1.txt", 'a+')
    f.write(e)
    f.close()
def writesummary(e):
    #print (e)
    # if it works continue
    f = open("rst/results_summary_1.txt", 'a+')
    f.write(e)
    f.close()

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

branch=""
# branch
if isVis15:
    branch ="vis15"
else:
    branch = "ein16"

def write_results(pre):
    global rst_compile
    global rst_good
    global rst_eh
    global rst_terrible
    global rst_cnt
    global rst_total
    global layer
    global args
    #    global rst_cumulative
    x= "\n"
    if(rst_good==rst_cnt):
        x+="PASS:"
    else:
        x+="CHECK:"
    x+= "cmd: "+str(layer)+" "+str(args)+" "+"- "+pre+"- "+branch
    if(rst_good>0):
        x+=" A:"+str(rst_good)+"|"+str(rst_cnt)
    if(rst_eh>0):
        x+=" B:"+str( rst_eh)+"|"+str(rst_cnt)
    if(rst_terrible>0):
        x+=" D:"+str( rst_terrible)+"|"+str(rst_cnt)
    if(rst_compile>0):
        x+=" did not run :"+str( rst_compile)+"|"+str(rst_total)
    x+= " ran: "+str(rst_cnt)+"|" +str(rst_total)+" cumulative: "+str(rst_cumulative)
    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/output.txt")
    os.system("rm data/output.nrrd")
    os.system("rm "+output+".txt")
    os.system("rm *.nrrd")
    #print "out",output+".txt"
    names= "t_"+str(ex_opr)+"_"+str(id)+"_"+str(ex_num)#+"_"+str(app.oty.id)
    print "names",names
    #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
    print "about to write program"
    writeDiderot(p_Observ, app, positions, output, runtimepath, isVis15)
    #read observed data
    print "about to observe"
    observed_data = observed(app, output)
    global rst_compile
    global rst_good
    global rst_eh
    global rst_terrible
    global rst_cnt
    global rst_cumulative
    print "observed data from python",observed_data
    if (len(observed_data)<2):
        print "fail did not find data"
        rst_compile += 1
        rtn = "diderot could not handle program"
        x = ("\n\t-"+rtn+" "+names)
        # print ("rst_cnt",rst_cnt)
        writeall(x)
                 
        names= "c__"+str(ex_opr)+"_"+str(id)+"_"+str(ex_num)+"_"+str(app.oty.id)
        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 "+branch+" "+str(observed_data)
        # correct values from python
        correct_data = eval(app , positions)
        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")
        f.close()
        return
    else:
        #correct values from python
        correct_data = eval(app , positions)
        print "correct data from python", correct_data
        #--------------------Comparison
        #compare observed and correct data
        rtn_all = compare(app, observed_data, correct_data)
        (rtn_1, rst_good_1, rst_eh_1, rst_terrible_1, rst_compile_1) =  rtn_all
        # collect results
        rtn = []
        rtn += rtn_1
        rst_good +=  rst_good_1
        rst_eh += rst_eh_1
        rst_terrible += rst_terrible_1
        rst_compile += rst_compile_1
        rst_cnt += 1
        rst_cumulative +=1
        x = ("\n\t-"+rtn_1+" "+names)
        # print ("rst_cnt",rst_cnt)
        writeall(x)
        if (rst_terrible_1==10):
            writesummary(x)
            names= "t_"+str(ex_opr)+"_"+str(id)+"_"+str(ex_num)+"_"+str(app.oty.id)
            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 "+branch+" "+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
    return

########################################### 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
    global rst_total
    rst_total+= 1
    return central(app, coeffs, dimF,ex_opr, ex_num,0)

# checks to see if specific ex works
def embed_base_specific_ex(ex_opr, ex_outer, ex_num, ex_ty2):
    global rst_total
    rst_total+= 1
    # print ("rst_total",rst_total)
    #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
    # get result after applying operators
    def get_tshape(opr1, ishape):
        print ("inside get_shape",opr1.name, ishape)
    
        arity=opr1.arity
        #print "checking for first time in rum.py"
        print ("tshape- inner:",opr1.name,"ishape",ishape[0].name)
        if(len(ishape)>1):
           print (",",ishape[1].name)
        def get():
            if(arity==1):
                print ("arity 1")
                return applyUnaryOp(opr1,ishape)
            elif(arity==2):
                print ("arity 2")
                return applyBinaryOp(opr1, ishape)
        (tf0, shape0) = get()
        if(tf0):
                print "shape-rtn-name",shape0.name
        else:
                print "shape-rtn-err",shape0
        return  (tf0, shape0)
                
    # current example
    (name,opr_inner,ishape)= get_single_example(ex_opr, ex_num)
    # first layer
    (tf1, tshape1) = get_tshape(opr_inner,ishape)
    if(tf1==false):
        return
    else:
        #second layer, adds second field type
        ishape = ishape+fty
        (tf2, tshape2) = get_tshape(opr_outer,[tshape1]+fty)
        # checks to see if specific ex works
        if(tf2==true):# if it works continue
            write("\n\t\t typechecks ")
            #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
            central(app, coeffs, dimF,ex_opr, ex_num,id)
            return
        else:
            x = "\n\t\tskipped did not apply "+opr_outer.name+" to ("+tshape2+","
            write(x)
            return
        return
    return

################################################## iterating ###############################################
def embed_base_iter_ty2(ex_opr, ex_outer):
    #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)
        for ex_num  in range(n):
            embed_base_specific_ex(ex_opr, ex_outer, ex_num, ex_ty2)
        return
    #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)
        embed_base_iter_num(ex_opr, ex_outer, gty, nn)
    else:
        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
            embed_base_iter_num(ex_opr, ex_outer, ex_ty2, n)
    return

#iterating ex_outer from 0...length(potential outer_operators)
def embed_base_iter_outer(ex_opr):
    # print "\nembed_base_iter_outer: ex_opr",ex_opr
    n = getN()
    y = idToOpr_inner(ex_opr)+" |"+str(ex_opr)
    writeall(y)
    #have ex_opr iterating over ex_outer
    for  ex_outer in range(n):
        global rst_cnt
        rst_cnt = 0
        global rst_total
        rst_total = 0
        global rst_compile
        rst_compile = 0
        global rst_good
        rst_good = 0
        global rst_eh
        rst_eh = 0
        global rst_terrible
        rst_terrible = 0
        y = idToOpr_outer(ex_outer)+ "_"+idToOpr_inner(ex_opr)+" |"+str(ex_opr)+"_"+str(ex_outer)
        writetys("\n"+y)
        writeall(y)
        embed_base_iter_ty2(ex_opr, ex_outer)
        write_results(y)
    return

#run all possible examples from 0...n
def embed2_iter_inner(n):
    for ex_opr in range(n):
        embed_base_iter_outer(ex_opr)
    return

#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
    y = "\n\n"+idToOpr_inner(ex_opr)+" |"+str(ex_opr)
    writetys(y)
    writeall(y)
    global rst_cnt
    rst_cnt = 0
    global rst_total
    rst_total = 0
    global rst_compile
    rst_compile = 0
    global rst_good
    rst_good = 0
    global rst_eh
    rst_eh = 0
    global rst_terrible
    rst_terrible = 0
    for ex_num  in range(n):
        single_specific_ex(ex_opr, ex_num)
    return

##################################################################################################
##################################################################################################
if (layer==1):
    if(args==0):
        # iterate over single layer
        rtn = []
        for ex_opr in range(opr_max+1):
            single_all_ops(ex_opr)
            write_results(str(ex_opr)+"("+get_exname(ex_opr)+")--")
        x= "\n***************************************************\n #cumulative: "+str(rst_cumulative)
        print (x)
        writeall(x)
        writesummary(x)
    elif (args==1):
        # iterate over single layer
        ex_opr = int(sys.argv[3])
        single_all_ops(ex_opr)
        write_results(str(ex_opr)+"("+get_exname(ex_opr)+")--")
    elif (args==2):
        ex_opr = int(sys.argv[3])
        ex_num = int(sys.argv[4])
        ex = get_ex(ex_opr)
        n = len(ex.tys)
        if(ex_num>n):
            raise Exception ("unsupported num:"+str(ex_num)+"|"+str(n))
        single_specific_ex(ex_opr, ex_num)
        write_results(str(ex_opr)+"("+get_exname(ex_opr)+")--")
    else:
        raise Exception ("unsupported number of args")
elif (layer==2):
    if(args==0):
        #run all the programs
        embed2_iter_inner (opr_max+1)
        write_results("all")
        x= "\n***************************************************\n #cumulative: "+str(rst_cumulative)
        print (x)
        writeall(x)
        writesummary(x)
    elif(args==1):
        # iterate over double layer with unary operator
        ex_opr = int(sys.argv[3])
        embed_base_iter_outer(ex_opr)
        x= "\n***************************************************\n #cumulative: "+str(rst_cumulative)
        print (x)
        writeall(x)
        writesummary(x)
    elif (args==2):
        ex_opr = int(sys.argv[3])
        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)
        embed_base_iter_ty2(ex_opr, ex_outer)
        write_results(str(ex_opr)+" "+str(ex_outer)+":")
        x= "\n***************************************************\n #cumulative: "+str(rst_cumulative)
        print (x)
        writeall(x)
        writesummary(x)
    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
        embed_base_specific_ex(ex_opr, ex_outer, ex_num, ex_ty2)
        write_results(str(ex_opr)+"("+get_exname(ex_opr)+")--"+ str(ex_num))
elif (layer==3):
    # number from generated list of types (not type id.)
    ex_opr = int(sys.argv[3])   #inside operator
    if(ex_opr<12):
        raise Exception("not possible to iterate over all unary ops")
    if(args==0):
        raise Exception("not possible to iterate over all binary operators")
    elif (args==1):
        # and all possible outer operators
        embed_base_iter_outer(ex_opr)
    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)
        embed_base_iter_ty2(ex_opr, ex_outer)
        write_results(str(ex_opr)+" "+str(ex_outer)+":")
    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)
        embed_base_specific_ex(ex_opr, ex_outer, ex_num, ex_ty2)
        write_results(str(ex_opr)+" "+str(ex_outer)+" "+str(ex_num)+" "+str(ex_ty2)+":")
else:
  raise "unsupported"

clean()

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