Example #1
0
 def gencode(self, env, opt):
     if opt.bp is None:
         g.r.addReport(
             m.Report('error', self.tok, 'break in unbreakable point'))
         return m.Insts()
     else:
         return m.Insts(m.Type(), [m.Inst(opc.JUMP, opt.bp)])
Example #2
0
class Post_dec(AST):
    def __init__(self, tok, left):
        self.tok = tok
        self.left = left

    def gencode(self, env, opt):

        if (result := self.assertOnlyRValue(env, opt)) is not None:
            return result

        left = self.left.gencode(env, newopt(opt, 1, 'r'))
        codes = left.bytecodes
        typ = copy(left.typ)

        if (opt.popc == 1):
            codes.append(m.Inst(opc.DUP, self.nullarg))
        else:
            typ = m.Type()

        if typ.isPointer():
            codes.append(m.Inst(opc.DEC, env.calcPointeredSize(typ)))
        else:
            codes.append(m.Inst(opc.DEC, 1))

        codes.extend(self.left.gencode(env, newopt(opt, 0, 'l')).bytecodes)

        return m.Insts(typ, codes)
Example #3
0
 def gencode(self, env, opt):
     if self.body:
         codes = self.body.gencode(env, newopt(opt, 1)).bytecodes
     else:
         codes = [m.Inst(opc.PUSH, 0)]
     codes.append(m.Inst(opc.RET, self.nullarg))
     return m.Insts(m.Type(), codes)
Example #4
0
class Funccall(AST):
    def __init__(self, tok, name, args):
        self.tok = tok
        self.name = name
        self.args = args

    def gencode(self, env, opt):

        if (result := self.assertOnlyRValue(env, opt)) is not None:
            return result

        try:
            define = env.functionLookup(self.name)
        except m.SymbolNotFoundException:
            g.r.addReport(
                m.Report('error', self.tok,
                         f"function '{self.name}' not defined"))
            return m.Insts()

        if self.args is None:
            self.args = []

        selfArgLength = len(self.args)
        definedArgLength = len(define.args)

        if selfArgLength != definedArgLength:
            g.r.addReport(
                m.Report(
                    'error', self.tok,
                    f'too {"many" if selfArgLength > definedArgLength else "few"} arguments to function call, expected {definedArgLength}, have {selfArgLength}'
                ))
            return m.Insts()

        mytype = define.typ
        codes = []
        argtypes = []
        for elem in reversed(self.args):
            arg = elem.gencode(env, newopt(opt, 1))
            codes.extend(arg.bytecodes)
            argtypes.append(arg.typ)

        argtypes = list(reversed(argtypes))

        for i in range(selfArgLength):
            if argtypes[i] != define.args[i].typ:
                g.r.addReport(
                    m.Report(
                        'error', self.tok,
                        f'{"%d%s" % (i+1,"tsnrhtdd"[(i+1//10%10!=1)*((i+1)%10<4)*(i+1)%10::4])} parameter type mismatches'
                    ))
                return m.Insts()

        codes.append(m.Inst(opc.CALL, self.name))
        codes.append(m.Inst(opc.POPR, len(self.args)))
        if (opt.popc == 0):
            codes.append(m.Inst(opc.POP, self.nullarg))

        env.markCalledFunc(self.name)

        return m.Insts(mytype, codes)
Example #5
0
 def assertOnlyRValue(self, env, opt):
     if (opt.lr != 'r'):
         g.r.addReport(
             m.Report(
                 'error', self.tok,
                 f'expression \'{self.__class__.__name__}\' is not assignable'
             ))
         return m.Insts()
Example #6
0
    def gencode(self, env, opt):
        label = env.currentFuncName + self.name
        expr = self.expr.gencode(env, newopt(opt, 0))

        codes = [m.Inst(opc.LABEL, label)]
        codes.extend(expr.bytecodes)

        return m.Insts(expr.typ, codes)
Example #7
0
    def gencode(self, env, opt):

        body = self.body.gencode(env, newopt(opt, 1))
        codes = body.bytecodes
        if opt.lr == 'r':
            if (result := self.assertOnlyPop1(env, opt)) is not None:
                return result
            codes.append(m.Inst(opc.LOADP, self.nullarg))
            return m.Insts(copy(body.typ).addRefcount(-1), codes)
Example #8
0
    def gencode(self, env, opt):
        env.pushScope()

        insts = []
        for elem in self.body:
            insts.extend(elem.gencode(env, newopt(opt, 0)).bytecodes)

        env.popScope()
        return m.Insts(m.Type(), insts)
Example #9
0
    def gencode(self, env, opt):
        #        self.typ.resolve(env)
        insts = []
        for elem in reversed(self.bodys):
            insts.extend(elem.gencode(env, newopt(opt, 1)).bytecodes)
        #insts.append(m.Inst(opc[self.opc], self.arg.eval()))
        insts.append(m.Inst(opc[self.opc], self.arg))

        return m.Insts(self.typ, insts)
Example #10
0
    def gencode(self, env, opt):
        cond = self.cond.gencode(env, newopt(opt, 1)).bytecodes
        then = self.then.gencode(env, newopt(opt, 0)).bytecodes

        l0 = env.issueLabel()
        codes = cond
        codes.append(m.Inst(opc.JIF0, l0))
        codes.extend(then)
        codes.append(m.Inst(opc.LABEL, l0))
        return m.Insts(m.Type(), codes)
Example #11
0
 def gencode(self, env, opt):
     if opt.cp is None:
         g.r.addReport(
             m.Report(
                 'error', self.tok,
                 'to continue, you need extra gem! (you can\'t continue here.)'
             ))
         return m.Insts()
     else:
         return m.Insts(m.Type(), [m.Inst(opc.JUMP, opt.cp)])
Example #12
0
    def gencode(self, env, opt):
        left = self.left.gencode(env, newopt(opt, 1))
        codes = left.bytecodes

        field = env.getField(left.typ, self.fieldname)  # TODO handle exception
        #            g.r.addReport(m.Report('error', self.tok, f"cannot get field '{self.fieldname}' of type '{left.typ}'"))
        #            return m.Insts()
        codes.append(m.Inst(opc.PUSH, field[0]))
        codes.append(m.Inst(opc.ADDI, self.nullarg))

        return m.Insts(field[1], codes)
Example #13
0
    def gencode(self, env, opt):

        l0 = env.issueLabel()

        body = self.body.gencode(env, newopt(opt, cp=l0, bp=None)).bytecodes
        cond = self.cond.gencode(env, newopt(opt, 1)).bytecodes

        codes = [m.Inst(opc.LABEL, l0)]
        codes.extend(body)
        codes.extend(cond)
        codes.append(m.Inst(opc.INV, self.nullarg))
        codes.append(m.Inst(opc.JIF0, l0))

        return m.Insts(m.Type(), codes)
Example #14
0
    def gencode(self, env, opt):

        right = self.right.gencode(env, newopt(opt, 1))
        left = self.left.gencode(env, newopt(opt, 1, 'l'))

        typ = m.Type()

        codes = right.bytecodes
        if opt.popc == 1:
            codes.append(m.Inst(opc.DUP, 1))
            typ = right.typ
        codes.extend(left.bytecodes)

        return m.Insts(typ, codes)
Example #15
0
    def run_model(self, model: Model) -> dict:
        logging.warning("run_model(), attempting to run: " +
                        str(model.data["model_name"]))

        # TODO: error handling

        # import the model
        model_path: str = 'model_library/' + str(model.data["model_name"])

        # insert model scope
        sys.path.insert(0, model_path)
        import MODEL

        # execute model
        try:
            model_result: dict = MODEL.execute()
        except Exception as e:
            logging.error("Model Execution Failed: " +
                          str(model.data["model_name"]) + " Reason: " + str(e))

        # remove model scope from path list
        sys.path.remove(model_path)

        # delete model scope from sys.modules
        if 'MODEL' in sys.modules:
            del sys.modules["MODEL"]

        # return standard model result
        return model_result
Example #16
0
    def gencode(self, env, opt):

        try:
            var = env.variableLookup(self.symbolname)
        except m.SymbolNotFoundException as e:

            # 変数が見つからなかったら、Enumとして解決を試みる
            try:
                value = env.enumLookup(self.symbolname)
            except m.SymbolNotFoundException as e:
                g.r.addReport(m.Report('error', self.tok, f'{e} not found'))
                return m.Insts()

            if (result := self.assertOnlyRValue(env, opt)) is not None:
                return result
            if (result := self.assertOnlyPop1(env, opt)) is not None:
                return result
def setup_graph():
    graph_params = {}
    graph_params['graph'] = tf.Graph()

    with graph_params['graph'].as_default():

        model_params = MODEL.params()
        graph_params['target_image'] = tf.placeholder(
            tf.float32,
            shape=(1, MODEL.CNN_IN_HEIGHT, MODEL.CNN_IN_WIDTH,
                   MODEL.CNN_IN_CH))
        logits = MODEL.CNN(graph_params['target_image'],
                           model_params,
                           keep_prob=1.0)
        graph_params['pred'] = tf.nn.softmax(logits)
        graph_params['saver'] = tf.train.Saver()

    return graph_params
Example #18
0
    def gencode(self, env, opt):
        cond = self.cond.gencode(env, newopt(opt, 1))
        then = self.then.gencode(env, newopt(opt, 1))
        elst = self.elst.gencode(env, newopt(opt, 1))

        # TODO check if then.typ != elst.typ

        l0 = env.issueLabel()
        l1 = env.issueLabel()

        codes = cond.bytecodes
        codes.append(m.Inst(opc.JIF0, l0))
        codes.extend(then.bytecodes)
        codes.append(m.Inst(opc.JUMP, l1))
        codes.append(m.Inst(opc.LABEL, l0))
        codes.extend(elst.bytecodes)
        codes.append(m.Inst(opc.LABEL, l1))

        return m.Insts(then.typ, codes)
Example #19
0
    def gencode(self, env, opt):
        if isinstance(self.body, m.Type):
            return m.Insts(m.Type('int'),
                           [m.Inst(opc.PUSH, env.calcTypeSize(self.body))])
        elif isinstance(self.body, Symbol):
            try:
                var = env.variableLookup(self.body.symbolname)
            except m.SymbolNotFoundException as e:
                g.r.addReport(
                    m.Report('fatal', self.tok,
                             f"cannot eval size of type '{type(self.body)}'"))
                return m.Insts()

            return m.Insts(m.Type('int'),
                           [m.Inst(opc.PUSH, env.calcTypeSize(var.typ))])
        else:
            g.r.addReport(
                m.Report('fatal', self.tok,
                         f"cannot eval size of type '{type(self.body)}'"))
        return m.Insts()
    def load(self, dir = './applet.json'):

        with open(dir) as f:
            self.jfile = json.load(f)

        self.ds = DATASET(self.jfile["dataset"]["link"])


        if LOAD_MODELS:
            self.wf = int(self.jfile["model1"]["w"])
            self.model_flt = MODEL(w=self.wf, param_dir=self.jfile["model1"]["link"])

            self.wa = int(self.jfile["model2"]["w"])
            self.model_ang = MODEL(w=self.wa, param_dir=self.jfile["model2"]["link"])


        ds = np.load(self.jfile["pmap"]["plink"])
        self.lnumber = int(self.jfile["pmap"]["lnumber"])
        self.matrix = ds['matrix']
        self.width = self.matrix.shape[0]
        self.height = self.matrix.shape[1]

        if int(self.jfile["pmap"]["trained"]) == 1:
            h = np.load(self.jfile["pmap"]["alink"])
            #self.angels = h['matrix']
            self.angels = np.zeros((self.width, self.height, 36))




        sz = MAX_WINDOW_SIZE
        if self.width > sz or self.height > sz:
            if self.width > sz:
                self.height2 = (sz * self.height) // self.width
                self.width2 = sz
            else:
                self.width2 = (sz * self.width) // self.height
                self.height2 = sz
        else:
            self.height2 = self.height
            self.width2 = self.width
Example #21
0
def main():
    # モデルの読み込み
    model = MODEL.L.Classifier(MODEL.CIFAR10_cifer())
    chainer.serializers.load_npz("model_final", model)
    # データの読み込み
    images, labels = chainer.datasets.get_cifar10()[1]._datasets
    # table 作成
    create_AE_file('AE.h5', model, images, labels, r=0.001, n_threads=32)
    # 評価
    df = evaluate_AE('AE.h5', model, epsilon=0.01, n_threads=32)
    # 書き出し
    df.to_csv('AE.csv')
Example #22
0
    def gencode(self, env, opt):

        env.calcTypeSize(self.typ, self.init)
        var = env.addLocal(m.Symbol(self.symbolname, self.typ))

        if self.init is None:
            return m.Insts()
        elif isinstance(self.init, list):
            codes = []
            for i, elem in enumerate(self.init):
                codes.extend(elem.gencode(env, newopt(opt, 1)).bytecodes)
                codes.append(var.genAddrCode())
                codes.append(m.Inst(opc.PUSH, i))
                codes.append(m.Inst(opc.ADDI, self.nullarg))
                codes.append(m.Inst(opc.STOREP, self.nullarg))
            return m.Insts(m.Type(), codes)
        else:
            codes = self.init.gencode(env, newopt(opt, 1)).bytecodes
            codes.append(m.Inst(opc.STOREL, var.id))

        return m.Insts(m.Type(), codes)
Example #23
0
def create(name):
    cfg = util.load_config_from_file(name)
    l=[ obj.Struct(**node.parsing(i.strip())) for i in cfg.split('\n')]
    cfg_new = node.gen_graph(l)
    node.show_node(cfg_new,debug=True)
    device = model.DEVICE()

    for i in cfg_new:
        if i.TYPE==ast.AST.hostname:
            device.hostname = i.RGX[1];
        elif i.TYPE==ast.AST.ip_default_gateway:
            device.default_gateway = i.RGX[1]
        elif i.TYPE==ast.AST.svi:
            mng = i.Node[0]
            device.mng_ip, device.mng_mask = mng.RGX[2], mng.RGX[3]
            device.mng_int_vlan = i.RGX[1]
        elif i.TYPE==ast.AST.interface:
            port = device.getport(int(i.RGX[1]))
            switchport_allowed_tagged, switchport_allowed_untagged, switchport_native, description = None, None, None, None
            for j in i.Node:
                if j.TYPE ==ast.AST.description:
                    port.setdescription(j.RGX[1])
                elif j.TYPE==ast.AST.switchport_allowed_tagged:
                    port.addtagged( j.VLAN )
                elif j.TYPE==ast.AST.switchport_allowed_untagged:
                    port.adduntagged( j.VLAN )
                elif j.TYPE==ast.AST.description:
                    port.setdescription( j.RGX[1] )
                elif j.TYPE==ast.AST.shutdown:
                    port.setdown();
        elif i.TYPE==ast.AST.vlan:
            if i.RGX[1] not in ['1','4093']:
                device.addvlan(model.VLAN(i.RGX[4],i.RGX[1]))
        elif i.TYPE==ast.AST.ip_igmp_snooping:
            if i.RGX[4] not in device.igmp_snooping:
                device.igmp_snooping.append(i.RGX[4]);
        elif i.TYPE==ast.AST.ip_dhcp_snooping:
            if i.RGX[4] not in device.dhcp_snooping:
                device.dhcp_snooping.append(i.RGX[4]);
Example #24
0
class Symbol(AST):
    def __init__(self, tok, symbolname):
        self.tok = tok
        self.symbolname = symbolname

    def gencode(self, env, opt):

        try:
            var = env.variableLookup(self.symbolname)
        except m.SymbolNotFoundException as e:

            # 変数が見つからなかったら、Enumとして解決を試みる
            try:
                value = env.enumLookup(self.symbolname)
            except m.SymbolNotFoundException as e:
                g.r.addReport(m.Report('error', self.tok, f'{e} not found'))
                return m.Insts()

            if (result := self.assertOnlyRValue(env, opt)) is not None:
                return result
            if (result := self.assertOnlyPop1(env, opt)) is not None:
                return result

            return m.Insts(m.Type('int'), [m.Inst(opc.PUSH, value)])
Example #25
0
    def gencode(self, env, opt):

        size = env.calcTypeSize(self.typ, self.init)

        init = [0] * size

        if isinstance(self.init, AST):
            init[0] = self.init.eval()

        elif isinstance(self.init, list):
            for i, elem in enumerate(self.init):
                init[i] = self.init[i].eval()

        env.addStatic(m.Symbol(self.symbolname, self.typ, init))
        return env
Example #26
0
    def gencode(self, env, opt):

        try:
            env.addFunction(
                m.Function(self.symbolname, self.typ, self.args, None))
        except m.SymbolRedefineException:
            g.r.addReport(
                m.Report('error', self.tok,
                         f"Redefinition of '{self.symbolname}'"))

        if self.body is None:  # 定義
            return env

        env.resetFrame(self.symbolname)

        for elem in self.args:
            env.addArg(elem)

        codes = self.body.gencode(env, newopt(opt, 0)).bytecodes

        insts = []
        insts.append(m.Inst(opc.ENTRY, self.symbolname))
        insts.append(m.Inst(opc.FRAME, env.getFrameSize()))
        insts.extend(codes)
        if (insts[-1].opc is not opc.RET):
            insts.append(m.Inst(opc.PUSH, 0))
            insts.append(m.Inst(opc.RET, self.nullarg))

        try:
            env.addFunction(
                m.Function(self.symbolname, self.typ, self.args, insts))
        except m.SymbolRedefineException:
            g.r.addReport(
                m.Report('error', self.tok,
                         f"Redefinition of '{self.symbolname}'"))
        except m.ConflictingTypesException:
            g.r.addReport(
                m.Report('error', self.tok,
                         f"Conflicting types for '{self.symbolname}'"))

        return env
def firstTrial():
    #   create LocatedModels and add them into populationArray[]
    for i in range(sizeOfPopulation):
        locatedModel = MODEL.LocatedModel(
            numberOfNodes)  #   create default locatedModel

        #   define 1 chromosomes in DNA of current locatedModel randomly untill ones is equal to the pmedian
        counter = 0
        while counter < pmedian:
            randomIndex = randint(0, len(locatedModel.DNA) - 1)
            if (locatedModel.DNA[randomIndex] is not 1):
                locatedModel.DNA[randomIndex] = 1
                counter += 1

        FitnessLevelFinder(
            locatedModel
        )  #   calculate fitness value for the current locatedModel
        locatedModelList.append(
            locatedModel
        )  #   after all of properties of the current locatedModel have been defined add into locatedModelList[]
def newLocatedModel(dad, mom):

    baby = MODEL.LocatedModel(numberOfNodes)  # create a default  baby

    #   UNIFORM CROSSOVER chromosomes are chosen radomly form dad and mom
    for i in range(len(baby.DNA)):
        randomChromosome = randint(0, 1)
        if randomChromosome is 0:
            baby.DNA[i] = dad.DNA[i]
        else:
            baby.DNA[i] = mom.DNA[i]

    #   In this part fix amount of ones in DNA if there are more or less than pmedian
    onesIndexes = baby.getOnes()

    difference = pmedian - len(
        onesIndexes
    )  # difference<0 means number of ones more than pmedian so we need to remove some ones randomly
    if difference < 0:
        for i in range(abs(difference)):
            r = randint(0, len(onesIndexes) - 1)
            randomDnaIndex = onesIndexes[r]
            baby.DNA[randomDnaIndex] = 0
            onesIndexes.remove(onesIndexes[r])
    elif difference > 0:
        dnaLength = len(baby.DNA)
        i = 0
        while i is not difference:
            randomDnaIndex = randint(0, dnaLength - 1)
            if (baby.DNA[randomDnaIndex] is 0):
                baby.DNA[randomDnaIndex] = 1
                i += 1
    else:
        pass

    mutation(baby)  #   check mutation factor for baby

    FitnessLevelFinder(baby)  #   get fitness value of baby

    return baby
Example #29
0
    def gencode(self, env, opt):

        l0 = env.issueLabel()
        l1 = env.issueLabel()

        if self.init is None:  # TODO 1ライン化するか関数化して全部に適用
            init = []
        else:
            init = self.init.gencode(env, newopt(opt, 0)).bytecodes
        cond = self.cond.gencode(env, newopt(opt, 1)).bytecodes
        loop = self.loop.gencode(env, newopt(opt, 0)).bytecodes
        body = self.body.gencode(env, newopt(opt, 0, cp=l0, bp=l1)).bytecodes

        codes = init
        codes.append(m.Inst(opc.LABEL, l0))
        codes.extend(cond)
        codes.append(m.Inst(opc.JIF0, l1))
        codes.extend(body)
        codes.extend(loop)
        codes.append(m.Inst(opc.JUMP, l0))
        codes.append(m.Inst(opc.LABEL, l1))
        return m.Insts(m.Type(), codes)
Example #30
0
def a2t(ast):
    if (ast is not None and ast.coord is not None):
        return m.TokenInfo(ast.coord.line, ast.coord.column, ast.coord.file)
    else:
        return m.TokenInfo(0, 0, '<ERROR>')
Example #31
0
#--------------------------------------------------------------------------

dates = ["07/01/2004", "07/15/2004", "07/29/2004", "08/12/2004", "08/26/2004",
        "09/09/2004", "09/23/2004", "10/01/2004"]

for date in dates:

    starting_date = date

    the_date, the_bird, the_human = get_windows(starting_date)

    NUMPYPATH = get_numpy_filepath(r'C:/dev/data', date)
    print NUMPYPATH

    # this is the starting date,
    # and (t - 21) for the bird lower limit
    #lower_limit = wrap_date_for_SQL(the_bird)
    base_date = wrap_date_for_SQL(the_date)

    # human limits . lower limit is the starting day
    # upper limit is 12 days into the future (t + 12)
    # to test the predictive behavior of the model
    #lower_limit_human = wrap_date_for_SQL(the_date)
    #upper_limit_human = wrap_date_for_SQL(the_human)

    bird_window, human_window = generate_dummy_proof_windows(the_date, the_bird, the_human)

    print upper_limit, lower_limit, upper_limit_human

    MODEL.run((base_date, bird_window, human_window), NUMPYPATH)