<commit_before>#!/usr/bin/env python2
## -*- coding: utf-8 -*-
##
##

import  sys
from    triton import *

trace = {
                                              #   <serial> function
  0x40056d: "\x55",                           #   push    rbp
  0x40056e: "\x48\x89\xe5",                   #   mov     rbp,rsp
  0x400571: "\x48\x89\x7d\xe8",               #   mov     QWORD PTR [rbp-0x18],rdi
  0x400575: "\xc7\x45\xfc\x00\x00\x00\x00",   #   mov     DWORD PTR [rbp-0x4],0x0
  0x40057c: "\xeb\x3f",                       #   jmp     4005bd <check+0x50>
  0x40057e: "\x8b\x45\xfc",                   #   mov     eax,DWORD PTR [rbp-0x4]
  0x400581: "\x48\x63\xd0",                   #   movsxd  rdx,eax
  0x400584: "\x48\x8b\x45\xe8",               #   mov     rax,QWORD PTR [rbp-0x18]
  0x400588: "\x48\x01\xd0",                   #   add     rax,rdx
  0x40058b: "\x0f\xb6\x00",                   #   movzx   eax,BYTE PTR [rax]
  0x40058e: "\x0f\xbe\xc0",                   #   movsx   eax,al
  0x400591: "\x83\xe8\x01",                   #   sub     eax,0x1
  0x400594: "\x83\xf0\x55",                   #   xor     eax,0x55
  0x400597: "\x89\xc1",                       #   mov     ecx,eax
  0x400599: "\x48\x8b\x15\xa0\x0a\x20\x00",   #   mov     rdx,QWORD PTR [rip+0x200aa0]        # 601040 <serial>
  0x4005a0: "\x8b\x45\xfc",                   #   mov     eax,DWORD PTR [rbp-0x4]
  0x4005a3: "\x48\x98",                       #   cdqe
  0x4005a5: "\x48\x01\xd0",                   #   add     rax,rdx
  0x4005a8: "\x0f\xb6\x00",                   #   movzx   eax,BYTE PTR [rax]
  0x4005ab: "\x0f\xbe\xc0",                   #   movsx   eax,al
  0x4005ae: "\x39\xc1",                       #   cmp     ecx,eax
  0x4005b0: "\x74\x07",                       #   je      4005b9 <check+0x4c>
  0x4005b2: "\xb8\x01\x00\x00\x00",           #   mov     eax,0x1
  0x4005b7: "\xeb\x0f",                       #   jmp     4005c8 <check+0x5b>
  0x4005b9: "\x83\x45\xfc\x01",               #   add     DWORD PTR [rbp-0x4],0x1
  0x4005bd: "\x83\x7d\xfc\x04",               #   cmp     DWORD PTR [rbp-0x4],0x4
  0x4005c1: "\x7e\xbb",                       #   jle     40057e <check+0x11>
  0x4005c3: "\xb8\x00\x00\x00\x00",           #   mov     eax,0x0
  0x4005c8: "\x5d",                           #   pop     rbp
  0x4005c9: "\xc3",                           #   ret
}

if __name__ == '__main__':

    # Set the architecture
    setArchitecture(ARCH.X86_64)

    # Define that we perform emulation
    enableSymbolicEmulation(True)

    # Define entry point
    pc = 0x40056d

    # Define our input context
    setLastMemoryValue(0x1000, ord('e'))
    setLastMemoryValue(0x1001, ord('l'))
    setLastMemoryValue(0x1002, ord('i'))
    setLastMemoryValue(0x1003, ord('t'))
    setLastMemoryValue(0x1004, ord('e'))

    # Define the serial pointer
    setLastMemoryValue(0x601040, 0x00)
    setLastMemoryValue(0x601041, 0x00)
    setLastMemoryValue(0x601042, 0x90)

    # Define the serial context
    setLastMemoryValue(0x900000, 0x31)
    setLastMemoryValue(0x900001, 0x3e)
    setLastMemoryValue(0x900002, 0x3d)
    setLastMemoryValue(0x900003, 0x26)
    setLastMemoryValue(0x900004, 0x31)

    # point rdi on our buffer
    setLastRegisterValue(Register(REG.RDI, 0x1000))

    # Setup stack
    setLastRegisterValue(Register(REG.RSP, 0x7fffffff))
    setLastRegisterValue(Register(REG.RBP, 0x7fffffff))

    while pc in trace:

        # Build an instruction
        inst = Instruction()

        # Setup opcodes
        inst.setOpcodes(trace[pc])

        # Setup Address
        inst.setAddress(pc)

        # Process everything
        processing(inst)

        # Display instruction
        print inst

        # Next instruction
        pc = evaluateAst(buildSymbolicRegister(REG.RIP))

    sys.exit(0)

<commit_msg>Add a simplification passe in emaluation example<commit_after>#!/usr/bin/env python2
## -*- coding: utf-8 -*-
##
##

import  sys
from    triton  import *
from    smt2lib import *

trace = {
                                              #   <serial> function
  0x40056d: "\x55",                           #   push    rbp
  0x40056e: "\x48\x89\xe5",                   #   mov     rbp,rsp
  0x400571: "\x48\x89\x7d\xe8",               #   mov     QWORD PTR [rbp-0x18],rdi
  0x400575: "\xc7\x45\xfc\x00\x00\x00\x00",   #   mov     DWORD PTR [rbp-0x4],0x0
  0x40057c: "\xeb\x3f",                       #   jmp     4005bd <check+0x50>
  0x40057e: "\x8b\x45\xfc",                   #   mov     eax,DWORD PTR [rbp-0x4]
  0x400581: "\x48\x63\xd0",                   #   movsxd  rdx,eax
  0x400584: "\x48\x8b\x45\xe8",               #   mov     rax,QWORD PTR [rbp-0x18]
  0x400588: "\x48\x01\xd0",                   #   add     rax,rdx
  0x40058b: "\x0f\xb6\x00",                   #   movzx   eax,BYTE PTR [rax]
  0x40058e: "\x0f\xbe\xc0",                   #   movsx   eax,al
  0x400591: "\x83\xe8\x01",                   #   sub     eax,0x1
  0x400594: "\x83\xf0\x55",                   #   xor     eax,0x55
  0x400597: "\x89\xc1",                       #   mov     ecx,eax
  0x400599: "\x48\x8b\x15\xa0\x0a\x20\x00",   #   mov     rdx,QWORD PTR [rip+0x200aa0]        # 601040 <serial>
  0x4005a0: "\x8b\x45\xfc",                   #   mov     eax,DWORD PTR [rbp-0x4]
  0x4005a3: "\x48\x98",                       #   cdqe
  0x4005a5: "\x48\x01\xd0",                   #   add     rax,rdx
  0x4005a8: "\x0f\xb6\x00",                   #   movzx   eax,BYTE PTR [rax]
  0x4005ab: "\x0f\xbe\xc0",                   #   movsx   eax,al
  0x4005ae: "\x39\xc1",                       #   cmp     ecx,eax
  0x4005b0: "\x74\x07",                       #   je      4005b9 <check+0x4c>
  0x4005b2: "\xb8\x01\x00\x00\x00",           #   mov     eax,0x1
  0x4005b7: "\xeb\x0f",                       #   jmp     4005c8 <check+0x5b>
  0x4005b9: "\x83\x45\xfc\x01",               #   add     DWORD PTR [rbp-0x4],0x1
  0x4005bd: "\x83\x7d\xfc\x04",               #   cmp     DWORD PTR [rbp-0x4],0x4
  0x4005c1: "\x7e\xbb",                       #   jle     40057e <check+0x11>
  0x4005c3: "\xb8\x00\x00\x00\x00",           #   mov     eax,0x0
  0x4005c8: "\x5d",                           #   pop     rbp
  0x4005c9: "\xc3",                           #   ret
}

# Rule (?* (concat ? ? ? ?) ? ?) -> (?* (evaluate) ? ?)
def concat(node):

    if len(node.getChilds()) and node.getChilds()[0].getKind() == SMT_AST_NODE.CONCAT:
        c = node.getChilds()[0]
        n = bv(evaluateAst(c), c.getBitvectorSize())
        node.setChild(0, n)

    for n in node.getChilds():
        concat(n)

    if node.getKind() == SMT_AST_NODE.CONCAT:
        n = bv(evaluateAst(node), node.getBitvectorSize())
        return n

    return node

if __name__ == '__main__':

    # Set the architecture
    setArchitecture(ARCH.X86_64)

    # Define that we perform emulation
    enableSymbolicEmulation(True)

    # Record simplification passes
    recordSimplificationCallback(concat)

    # Define entry point
    pc = 0x40056d

    # Define our input context
    setLastMemoryValue(0x1000, ord('e'))
    setLastMemoryValue(0x1001, ord('l'))
    setLastMemoryValue(0x1002, ord('i'))
    setLastMemoryValue(0x1003, ord('t'))
    setLastMemoryValue(0x1004, ord('e'))

    # Define the serial pointer
    setLastMemoryValue(0x601040, 0x00)
    setLastMemoryValue(0x601041, 0x00)
    setLastMemoryValue(0x601042, 0x90)

    # Define the serial context
    setLastMemoryValue(0x900000, 0x31)
    setLastMemoryValue(0x900001, 0x3e)
    setLastMemoryValue(0x900002, 0x3d)
    setLastMemoryValue(0x900003, 0x26)
    setLastMemoryValue(0x900004, 0x31)

    # point rdi on our buffer
    setLastRegisterValue(Register(REG.RDI, 0x1000))

    # Setup stack
    setLastRegisterValue(Register(REG.RSP, 0x7fffffff))
    setLastRegisterValue(Register(REG.RBP, 0x7fffffff))

    while pc in trace:

        # Build an instruction
        inst = Instruction()

        # Setup opcodes
        inst.setOpcodes(trace[pc])

        # Setup Address
        inst.setAddress(pc)

        # Process everything
        processing(inst)

        # Display instruction
        print inst

        # Next instruction
        pc = evaluateAst(buildSymbolicRegister(REG.RIP))

    sys.exit(0)

<|endoftext|>


<commit_before># coding: utf-8
from __future__ import unicode_literals

import collections

ANY = object()

class Stub(object):
    # valid attributes for this stub
    # can by `ANY` special value or iterable of strings.
    valid_attribures = ANY

    def __init__(self, *args, **kwargs):
        for key, value in kwargs.items():
            if self.valid_attribures == ANY or key in self.valid_attribures:
                kwargs.pop(key)
                setattr(self, key, value)
        super(Stub, self).__init__(*args, **kwargs)

    def __repr__(self):
        return '<%s: %s>' % (
            self.__class__.__name__,
            self._repr_data(),
        )

    def _repr_data(self):
        return str(self.__dict__)

class ControlData(Stub):
    def _repr_data(self):
        return str([
            package.id
            for package in self.packages
            if package.id is not None
        ])

    @property
    def source_package(self):
        for package in self.packages:
            if package.type == 'source':
                return package

    @property
    def binary_packages(self):
        return collections.OrderedDict([
            (package.id, package)
            for package in self.packages
            if package.type == 'binary'
        ])

class Package(Stub, collections.OrderedDict):
    """
    Case-insensitive read-only ordered mapping of fields in paragraph,
    representing package.
    """
    valid_attribures = (
        '_raw',
    )

    def __getitem__(self, item):
        if not isinstance(item, basestring):
            raise TypeError(item)
        for key in self:
            if item.lower() == key.lower():
                return super(Package, self).__getitem__(key)
        raise KeyError(item)

    def _repr_data(self):
        return str(self.keys())

    @property
    def type(self):
        if 'Source' in self:
            return 'source'
        elif 'Package' in self:
            return 'binary'
        return 'unknown'

    @property
    def id(self):
        if self.type == 'source':
            return self['Source'].text
        if self.type == 'binary':
            return self['Package'].text

# TODO:
#   * Every field value must be FieldValue inheritor, it
#   should have type, format or is_list at least. So we need to make
#   ListField. Also _raw must be in each fieldvalue.
#   * I guess not every fieldvalue must have meta, but only the first one
#   or maybe meta is not useful at all.
#   * canonical name could be in key object maybe
#   * all i can is enum of values need at some point.
#   if we want to make validation or something, but maybe we don't want
#   it by design

class FieldMeta(Stub):
    pass

class FieldValue(Stub):
    def _repr_data(self):
        return self._raw

class SimpleField(FieldValue):
    pass

class ListField(FieldValue, list):
    def __repr__(self):
        return list.__repr__(self)

class ContactField(FieldValue):
    pass

class DependencySimple(FieldValue):
    type = 'simple'

class DependencyAlternative(DependencySimple):
    type = 'alternative'

class DependencyPlaceholder(DependencySimple):
    type = 'placeholder'

class Restriction(Stub):
    pass
<commit_msg>__hash__ for Package class<commit_after># coding: utf-8
from __future__ import unicode_literals

import collections

ANY = object()

class Stub(object):
    # valid attributes for this stub
    # can by `ANY` special value or iterable of strings.
    valid_attribures = ANY

    def __init__(self, *args, **kwargs):
        for key, value in kwargs.items():
            if self.valid_attribures == ANY or key in self.valid_attribures:
                kwargs.pop(key)
                setattr(self, key, value)
        super(Stub, self).__init__(*args, **kwargs)

    def __repr__(self):
        return '<%s: %s>' % (
            self.__class__.__name__,
            self._repr_data(),
        )

    def _repr_data(self):
        return str(self.__dict__)

class ControlData(Stub):
    def _repr_data(self):
        return str([
            package.id
            for package in self.packages
            if package.id is not None
        ])

    @property
    def source_package(self):
        for package in self.packages:
            if package.type == 'source':
                return package

    @property
    def binary_packages(self):
        return collections.OrderedDict([
            (package.id, package)
            for package in self.packages
            if package.type == 'binary'
        ])

class Package(Stub, collections.OrderedDict):
    """
    Case-insensitive read-only ordered mapping of fields in paragraph,
    representing package.
    """
    valid_attribures = (
        '_raw',
    )

    def __getitem__(self, item):
        if not isinstance(item, basestring):
            raise TypeError(item)
        for key in self:
            if item.lower() == key.lower():
                return super(Package, self).__getitem__(key)
        raise KeyError(item)

    def _repr_data(self):
        return str(self.keys())

    @property
    def type(self):
        if 'Source' in self:
            return 'source'
        elif 'Package' in self:
            return 'binary'
        return 'unknown'

    @property
    def id(self):
        if self.type == 'source':
            return self['Source'].text
        if self.type == 'binary':
            return self['Package'].text

    def __hash__(self):
        return hash(self.type) ^ hash(self.id)

# TODO:
#   * Every field value must be FieldValue inheritor, it
#   should have type, format or is_list at least. So we need to make
#   ListField. Also _raw must be in each fieldvalue.
#   * I guess not every fieldvalue must have meta, but only the first one
#   or maybe meta is not useful at all.
#   * canonical name could be in key object maybe
#   * all i can is enum of values need at some point.
#   if we want to make validation or something, but maybe we don't want
#   it by design

class FieldMeta(Stub):
    pass

class FieldValue(Stub):
    def _repr_data(self):
        return self._raw

class SimpleField(FieldValue):
    pass

class ListField(FieldValue, list):
    def __repr__(self):
        return list.__repr__(self)

class ContactField(FieldValue):
    pass

class DependencySimple(FieldValue):
    type = 'simple'

class DependencyAlternative(DependencySimple):
    type = 'alternative'

class DependencyPlaceholder(DependencySimple):
    type = 'placeholder'

class Restriction(Stub):
    pass
<|endoftext|>


<commit_before>import unittest
import random
import json
import numpy as np
import gym
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import sgd
from keras.optimizers import Adam

class DeepQLearner:
    # initialize replay memory D
    # initialize action-value function Q with random weights
    # observe initial state s
    # repeat
    #     select an action a
    #         with probability ε select a random action
    #         otherwise select a = argmaxa’Q(s,a’)
    #     carry out action a
    #     observe reward r and new state s’
    #     store experience <s, a, r, s’> in replay memory D
    #
    #     sample random transitions <ss, aa, rr, ss’> from replay memory D
    #     calculate target for each minibatch transition
    #         if ss’ is terminal state then tt = rr
    #         otherwise tt = rr + γmaxa’Q(ss’, aa’)
    #     train the Q network using (tt - Q(ss, aa))^2 as loss
    #
    #     s = s'
    # until terminated

    def __init__(self, original_model, environment, input_shape, max_memory_size=500, epsilon=.1, epsilon_decay=.95, discount=.9, sample_size=50):
        self.Q_model = original_model
        self.action_space = environment.action_space
        self.environment = environment

        self.input_shape = input_shape

        self.memories = list()
        self.max_memory_size = max_memory_size

        self.state = None

        self.epsilon = epsilon
        self.epsilon_decay = epsilon_decay
        self.discount = discount
        self.sample_size = sample_size

    def select_action(self, state):
        if np.random.rand() <= self.epsilon:
            return self.action_space.sample()
        else:
            return np.argmax(self.predict_reward(state))

    def act(self, action):
        return self.environment.step(action)

    def store_memory(self, state, action, reward, next_state, done):
        self.memories.append((state, action, reward, next_state, done))
        if len(self.memories) > self.max_memory_size:
            del self.memories[0]

    def sample_actions(self):
        x_batch, y_batch = [], []
        mini_batch = random.sample(self.memories, min(len(self.memories), self.sample_size))

        for state, action, reward, next_state, done in mini_batch:
            y_target = self.predict_reward(state)
            y_target[action] = reward if done else reward + self.discount * np.max(self.predict_reward(next_state))
            x_batch.append(state)
            y_batch.append(y_target)

        return np.array(x_batch), np.array(y_batch)

    def predict_reward(self, state):
        return self.Q_model.predict(np.reshape(state, self.input_shape))[0]

    def train_network(self):
        inputs, targets = self.sample_actions()
        return self.Q_model.train_on_batch(inputs, targets)

    def run(self, num_epochs):
        # Train
        for e in range(num_epochs):
            loss = 0.

            game_over = False
            # get initial input
            input_t = self.environment.reset()

            cumulative_reward = 0.
            true_reward = 0.

            num_steps = 0

            while not game_over:
                input_tm1 = input_t
                # get next action
                action = self.select_action(input_tm1)
                # print("Selected action:{}", action)

                # apply action, get rewards and new state
                input_t, reward, game_over, _ = self.act(action)
                self.environment.render()

                true_reward += reward

                if e > 50:
                    time_punishment = np.floor(np.power(num_steps / 200, 1.2))
                    reward -= time_punishment

                # store experience
                self.store_memory(input_tm1, action, reward, input_t, game_over)

                self.train_network()

                cumulative_reward += reward

                num_steps += 1

            print("Epoch {:03d}/{} | Steps {} | Reward {:4f} | True Reward {:4f}"
                  .format(e, num_epochs - 1, num_steps, cumulative_reward, true_reward))
            self.epsilon *= self.epsilon_decay

if __name__ == '__main__':
    epsilon = .7  # exploration
    num_actions = 3  # [move_left, stay, move_right]
    epoch = 1000
    max_memory = 500000
    hidden_size = 100
    batch_size = 50
    grid_size = 10
    discount = .9

    env = gym.make('LunarLander-v2')

    tmp_state = env.reset()

    model = Sequential()
    model.add(Dense(24, input_shape=env.observation_space.shape, activation='tanh'))
    model.add(Dense(48, activation='tanh'))
    model.add(Dense(4, activation='linear'))
    model.compile(loss='mse', optimizer=Adam(lr=0.001))

    # Define environment/game
    # env = Catch(grid_size)

    learner = DeepQLearner(model, env, [1, 8], epsilon=epsilon, max_memory_size=max_memory
                           , sample_size=batch_size, discount=discount)

    learner.run(num_epochs=epoch)

    # Save trained model weights and architecture, this will be used by the visualization code
    model.save_weights("model.h5", overwrite=True)
    with open("model.json", "w") as outfile:
        json.dump(model.to_json(), outfile)

    print("Saved models")
<commit_msg>Changed training to be only once per epoch<commit_after>import unittest
import random
import json
import numpy as np
import gym
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import sgd
from keras.optimizers import Adam
import _thread as thread

class DeepQLearner:
    # initialize replay memory D
    # initialize action-value function Q with random weights
    # observe initial state s
    # repeat
    #     select an action a
    #         with probability ε select a random action
    #         otherwise select a = argmaxa’Q(s,a’)
    #     carry out action a
    #     observe reward r and new state s’
    #     store experience <s, a, r, s’> in replay memory D
    #
    #     sample random transitions <ss, aa, rr, ss’> from replay memory D
    #     calculate target for each minibatch transition
    #         if ss’ is terminal state then tt = rr
    #         otherwise tt = rr + γmaxa’Q(ss’, aa’)
    #     train the Q network using (tt - Q(ss, aa))^2 as loss
    #
    #     s = s'
    # until terminated

    def __init__(self, original_model, environment, input_shape, max_memory_size=500, epsilon=.1, epsilon_decay=.95, discount=.9, sample_size=50):
        self.Q_model = original_model
        self.action_space = environment.action_space
        self.environment = environment

        self.input_shape = input_shape

        self.memories = list()
        self.max_memory_size = max_memory_size

        self.best_run = None
        self.best_score = -10000

        self.state = None
        self.epoch = 0

        self.epsilon = epsilon
        self.epsilon_decay = epsilon_decay
        self.discount = discount
        self.sample_size = sample_size

    def select_action(self, state):
        if np.random.rand() <= self.epsilon:
            return self.action_space.sample()
        else:
            return np.argmax(self.predict_reward(state))

    def act(self, action):
        return self.environment.step(action)

    def store_best_run(self, full_run):
        self.best_run = np.asarray(full_run)

    def store_memory(self, state, action, reward, next_state, done):
        self.memories.append((state, action, reward, next_state, done))
        if len(self.memories) > self.max_memory_size:
            del self.memories[0]

    def store_memories(self, memory_list):
        self.memories.extend(memory_list)
        while len(self.memories) > self.max_memory_size:
            del self.memories[0]

    def propogate_q(self, memory_list):
        index = len(memory_list) - 2  # skip the last element

        while index >= 0:
            reward = memory_list[index][2] + self.discount * memory_list[index+1][2]
            memory_list[index] = (memory_list[index][0], memory_list[index][1], reward, memory_list[index][3], memory_list[index][4])
            index = index - 1

        return memory_list

    def sample_actions(self):
        if np.random.rand() <= .005 and self.best_run is not None:
            mini_batch = self.best_run
            print("training from best run (Score: {:4f})".format(self.best_score))
        else:
            mini_batch = random.sample(self.memories, min(len(self.memories), self.sample_size))

        return self.prepare_batch(mini_batch)

    def prepare_batch(self, batch):
        x_batch, y_batch = [], []
        for state, action, reward, next_state, done in batch:
            y_target = self.predict_reward(state)
            y_target[action] = reward if done else reward + self.discount * np.max(self.predict_reward(next_state))
            x_batch.append(state)
            y_batch.append(y_target)

        return np.array(x_batch), np.array(y_batch)

    def predict_reward(self, state):
        return self.Q_model.predict(np.reshape(state, self.input_shape))[0]

    def train_network(self):
        if len(self.memories) == 0:
            return

        inputs = list()
        targets = list()
        for i in range(min((self.epoch + 1) * 10, 1000)):
            inputs_sample, targets_sample = self.sample_actions()
            inputs.extend(inputs_sample)
            targets.extend(targets_sample)

        self.Q_model.train_on_batch(np.array(inputs), np.array(targets))

    def run(self, num_epochs):
        # Train
        for self.epoch in range(num_epochs):
            loss = 0.

            game_over = False
            # get initial input
            input_t = self.environment.reset()

            cumulative_reward = 0.
            true_reward = 0.

            num_steps = 0

            full_run_memory = list()

            while not game_over:
                input_tm1 = input_t
                # get next action
                action = self.select_action(input_tm1)
                # print("Selected action:{}", action)

                # apply action, get rewards and new state
                input_t, reward, game_over, _ = self.act(action)
                self.environment.render()

                true_reward += reward

                # time_punishment = np.floor(np.power(num_steps / 200, 1.01))
                # reward -= time_punishment

                # store experience
                full_run_memory.append((input_tm1, action, reward, input_t, game_over))
                self.store_memory(input_tm1, action, reward, input_t, game_over)

                # self.train_network()

                cumulative_reward += reward

                num_steps += 1

            print("Epoch {:03d}/{} | Steps {} | Reward {:4f} | True Reward {:4f}"
                  .format(self.epoch, num_epochs - 1, num_steps, cumulative_reward, true_reward))

            self.train_network()

            if cumulative_reward > self.best_score:
                self.store_best_run(full_run_memory)
                self.best_score = cumulative_reward

            if self.epsilon > .005:
                self.epsilon *= self.epsilon_decay
            else:
                self.epsilon = 0.

if __name__ == '__main__':
    epsilon = .7  # exploration
    epsilon_decay = .99
    num_actions = 3  # [move_left, stay, move_right]
    epoch = 1000
    max_memory = 5000
    hidden_size = 100
    batch_size = 50
    grid_size = 10
    discount = .95

    env = gym.make('LunarLander-v2')

    tmp_state = env.reset()

    model = Sequential()
    model.add(Dense(24, input_shape=env.observation_space.shape, activation='tanh'))
    model.add(Dense(48, activation='tanh'))
    model.add(Dense(4, activation='linear'))
    model.compile(loss='mse', optimizer=Adam(lr=0.001))

    # Define environment/game
    # env = Catch(grid_size)

    learner = DeepQLearner(model, env, [1, 8], epsilon=epsilon, epsilon_decay=epsilon_decay, max_memory_size=max_memory
                           , sample_size=batch_size, discount=discount)

    learner.run(num_epochs=epoch)

    # Save trained model weights and architecture, this will be used by the visualization code
    model.save_weights("model.h5", overwrite=True)
    with open("model.json", "w") as outfile:
        json.dump(model.to_json(), outfile)

    print("Saved models")
<|endoftext|>


<commit_before>from deepchem.feat import Featurizer
from typing import List
try:
  from transformers import BertTokenizerFast
except ModuleNotFoundError:
  raise ImportError(
      'Transformers must be installed for BertFeaturizer to be used!')
  pass

class BertFeaturizer(Featurizer):
  """Bert Featurizer.

  Bert Featurizer.
  The Bert Featurizer is a wrapper class for HuggingFace's BertTokenizerFast.
  This class intends to allow users to use the BertTokenizer API while
  remaining inside the DeepChem ecosystem.

  Examples
  --------
  >>> from deepchem.feat import BertFeaturizer
  >>> from transformers import BertTokenizerFast
  >>> tokenizer = BertTokenizerFast.from_pretrained("Rostlab/prot_bert", do_lower_case=False)
  >>> featurizer = BertFeaturizer(tokenizer)
  >>> feats = featurizer.featurize('D L I P [MASK] L V T', return_tensors="pt")

  Notes
  -----
  This class may contain code and/or documentation taken from the
  RobertaFeaturizer pull request (#2581), which have been moved here due to
  code restructuring.
  """

  def __init__(self, tokenizer: BertTokenizerFast):
    if not isinstance(tokenizer, BertTokenizerFast):
      raise TypeError(f"""`tokenizer` must be a constructed `BertTokenizerFast`
                       object, not {type(tokenizer)}""")
    else:
      self.tokenizer = tokenizer

  def _featurize(self, datapoint: str, **kwargs) -> List[List[int]]:
    """
    Calculate encoding using HuggingFace's RobertaTokenizerFast

    Parameters
    ----------
    datapoint: str
      Arbitrary string sequence to be tokenized.

    Returns
    -------
    encoding: List
      List containing three lists: the `input_ids`, 'token_type_ids', and `attention_mask`.
    """

    # the encoding is natively a dictionary with keys 'input_ids', 'token_type_ids', and 'attention_mask'
    encoding = list(self.tokenizer(datapoint, **kwargs).values())
    return encoding
<commit_msg>fix doctest failure<commit_after>from deepchem.feat import Featurizer
from typing import List
try:
  from transformers import BertTokenizerFast
except ModuleNotFoundError:
  raise ImportError(
      'Transformers must be installed for BertFeaturizer to be used!')
  pass

class BertFeaturizer(Featurizer):
  """Bert Featurizer.

  Bert Featurizer.
  The Bert Featurizer is a wrapper class for HuggingFace's BertTokenizerFast.
  This class intends to allow users to use the BertTokenizer API while
  remaining inside the DeepChem ecosystem.

  Examples
  --------
  >>> from deepchem.feat import BertFeaturizer
  >>> from transformers import BertTokenizerFast
  >>> tokenizer = BertTokenizerFast.from_pretrained("Rostlab/prot_bert", do_lower_case=False)
  >>> featurizer = BertFeaturizer(tokenizer)
  >>> feats = featurizer.featurize('D L I P [MASK] L V T')

  Notes
  -----
  Examples are based on RostLab's ProtBert documentation.

  This class may contain code and/or documentation taken from the
  RobertaFeaturizer pull request (#2581), which have been moved here due to
  code restructuring.
  """

  def __init__(self, tokenizer: BertTokenizerFast):
    if not isinstance(tokenizer, BertTokenizerFast):
      raise TypeError(f"""`tokenizer` must be a constructed `BertTokenizerFast`
                       object, not {type(tokenizer)}""")
    else:
      self.tokenizer = tokenizer

  def _featurize(self, datapoint: str, **kwargs) -> List[List[int]]:
    """
    Calculate encoding using HuggingFace's RobertaTokenizerFast

    Parameters
    ----------
    datapoint: str
      Arbitrary string sequence to be tokenized.

    Returns
    -------
    encoding: List
      List containing three lists: the `input_ids`, 'token_type_ids', and `attention_mask`.
    """

    # the encoding is natively a dictionary with keys 'input_ids', 'token_type_ids', and 'attention_mask'
    encoding = list(self.tokenizer(datapoint, **kwargs).values())
    return encoding
<|endoftext|>


<commit_before>import sys
sys.path.insert(0, './src/main/python')

from contable import *
import numpy as np
import unittest
import contable.margins as margins
import contable.samplers as samplers
import contable.tabletools as tabletools

class TestBoundedExactRowsExpectedColumns(unittest.TestCase):

    def setUp(self):
        self.m = [margins.MarginsWithCellBounds([3,2,1],[2,2,1,1],1),
                  margins.MarginsWithCellBounds([2,2,2,2],[2,2,2,2],[[0,1,1,1],
                                                                     [1,0,1,1],
                                                                     [1,1,0,1],
                                                                     [1,1,1,0]]),
                  margins.MarginsWithCellBounds([2,1,3],[2,2,1,1],[[1,1,1,2],
                                                                   [1,4,1,0],
                                                                   [2,1,6,1]]),
                  margins.MarginsWithCellBounds([10],[1,2,3,4],[[1,2,3,4]])]
        self.sam = []
        for mii in self.m:
            self.sam.append(samplers.BoundedExactRowsExpectedColumns(mii))

    def test___init__(self):
        """Check that __init__ assigns what it should

        Correctness of the assigned values is checked elsewhere
        """

        def test_table_dimensions(table,m,n,h):
            """Test the dimensions of a Bounded...umns.table

            Args:
                table: sampler.Bou..umns.table to test
                m: number of subtables
                n: number of columns in each subtable
                h: vector with number of rows in subtable for each of the m
            """

            self.assertEqual(len(table),m)
            for ii in range(len(table)):
                self.assertEqual(len(table[ii]),n)
                for row in table[ii]:
                    self.assertEqual(len(row),h[ii])

        for (m,sam) in zip(self.m,self.sam):
            self.assertEqual(m,sam.margins)  #margins are correct
            test_table_dimensions(sam.table, #table has correct sizes
                              m.m,
                              m.n + 1,
                              np.array(m.r)+1)

    def test_table(self):
        """The table computed should be correct"""

        # Test the entire table for sam[1]
        sam = self.sam[1]
        w = sam.w[0]

        self.assertTrue(np.allclose(sam.w, w))

        c = [[1., 3 * w , 3 * w * w],
             [1., 2 * w , w * w],
             [1.,     w , 0.],
             [1.,     0., 0.]]

        for ii in range(4):
            tab = []
            for jj in range(4):
                tab.append(c[jj])
                if ii == jj:
                    tab.append(c[jj])
            self.assertTrue(np.allclose(tab, sam.table[ii]))

        #check table for sam[2], final row
        sam = self.sam[2]
        w = sam.w
        tab = [[1.,     sum(w), w[0] * sum(w) + w[1] * (w[2] + w[3]) + w[2]*w[2] + w[2]*w[3], w[0] * w[0] * sum(w[1:]) + w[0] * (w[1] * (w[2] + w[3]) + w[2]*w[2] + w[2]*w[3]) + w[1] * w[2] * (w[2] + w[3]) + w[2] * w[2] * w[3] + w[2] ** 3],
               [1., sum(w[1:]),                 w[1] * (w[2] + w[3]) + w[2]*w[2] + w[2]*w[3],                                                                                w[1] * w[2] * (w[2] + w[3]) + w[2] * w[2] * w[3] + w[2] ** 3],
               [1.,w[2] + w[3],                                        w[2]*w[2] + w[2]*w[3],                                                                                w[2] * w[2] * w[3] + w[2] ** 3],
               [1.,       w[3],                                                           0.,                                                                                0.],
               [1.,         0.,                                                           0.,                                                                                0.]]
        self.assertTrue(np.allclose(tab,sam.table[2]))

    def test_sample(self):
        """Sample many matrices and test that the distribution is correct"""

        print('')
        print('H0: sampling distribution is correct')
        print('HA: sampling distribution is not correct')
        for ii in [0,1,2]:
            print('Bounded table instance ' + str(ii) +
            ' chi-square test p-value: ' + '{:.3f}'.format(
                            tabletools.test_this_sampler( self.sam[ii], 10000)))
        print('')

        return 0

class TestBinaryExactRowsExpectedColumns(unittest.TestCase):

    def setUp(self):
        self.m = [margins.MarginsWithCellBounds([3,2,1],[2,2,1,1],1),
                  margins.MarginsWithCellBounds([2,2,2,2],[2,2,2,2],1)]
        self.sam = []
        for mii in self.m:
            self.sam.append(samplers.BinaryExactRowsExpectedColumns(mii))

    def test_sample(self):
        """Sample many matrices and test that the distribution is correct"""

        print('')
        print('H0: sampling distribution is correct')
        print('HA: sampling distribution is not correct')
        for ii in [0,1]:
            print('Binary table instance ' + str(ii) +
            ' chisquare test p-value: ' + '{:.3f}'.format(
                            tabletools.test_this_sampler( self.sam[ii], 100)))
        print('')

        return 0<commit_msg>more samples for testing the distribution<commit_after>import sys
sys.path.insert(0, './src/main/python')

from contable import *
import numpy as np
import unittest
import contable.margins as margins
import contable.samplers as samplers
import contable.tabletools as tabletools

class TestBoundedExactRowsExpectedColumns(unittest.TestCase):

    def setUp(self):
        self.m = [margins.MarginsWithCellBounds([3,2,1],[2,2,1,1],1),
                  margins.MarginsWithCellBounds([2,2,2,2],[2,2,2,2],[[0,1,1,1],
                                                                     [1,0,1,1],
                                                                     [1,1,0,1],
                                                                     [1,1,1,0]]),
                  margins.MarginsWithCellBounds([2,1,3],[2,2,1,1],[[1,1,1,2],
                                                                   [1,4,1,0],
                                                                   [2,1,6,1]]),
                  margins.MarginsWithCellBounds([10],[1,2,3,4],[[1,2,3,4]])]
        self.sam = []
        for mii in self.m:
            self.sam.append(samplers.BoundedExactRowsExpectedColumns(mii))

    def test___init__(self):
        """Check that __init__ assigns what it should

        Correctness of the assigned values is checked elsewhere
        """

        def test_table_dimensions(table,m,n,h):
            """Test the dimensions of a Bounded...umns.table

            Args:
                table: sampler.Bou..umns.table to test
                m: number of subtables
                n: number of columns in each subtable
                h: vector with number of rows in subtable for each of the m
            """

            self.assertEqual(len(table),m)
            for ii in range(len(table)):
                self.assertEqual(len(table[ii]),n)
                for row in table[ii]:
                    self.assertEqual(len(row),h[ii])

        for (m,sam) in zip(self.m,self.sam):
            self.assertEqual(m,sam.margins)  #margins are correct
            test_table_dimensions(sam.table, #table has correct sizes
                              m.m,
                              m.n + 1,
                              np.array(m.r)+1)

    def test_table(self):
        """The table computed should be correct"""

        # Test the entire table for sam[1]
        sam = self.sam[1]
        w = sam.w[0]

        self.assertTrue(np.allclose(sam.w, w))

        c = [[1., 3 * w , 3 * w * w],
             [1., 2 * w , w * w],
             [1.,     w , 0.],
             [1.,     0., 0.]]

        for ii in range(4):
            tab = []
            for jj in range(4):
                tab.append(c[jj])
                if ii == jj:
                    tab.append(c[jj])
            self.assertTrue(np.allclose(tab, sam.table[ii]))

        #check table for sam[2], final row
        sam = self.sam[2]
        w = sam.w
        tab = [[1.,     sum(w), w[0] * sum(w) + w[1] * (w[2] + w[3]) + w[2]*w[2] + w[2]*w[3], w[0] * w[0] * sum(w[1:]) + w[0] * (w[1] * (w[2] + w[3]) + w[2]*w[2] + w[2]*w[3]) + w[1] * w[2] * (w[2] + w[3]) + w[2] * w[2] * w[3] + w[2] ** 3],
               [1., sum(w[1:]),                 w[1] * (w[2] + w[3]) + w[2]*w[2] + w[2]*w[3],                                                                                w[1] * w[2] * (w[2] + w[3]) + w[2] * w[2] * w[3] + w[2] ** 3],
               [1.,w[2] + w[3],                                        w[2]*w[2] + w[2]*w[3],                                                                                w[2] * w[2] * w[3] + w[2] ** 3],
               [1.,       w[3],                                                           0.,                                                                                0.],
               [1.,         0.,                                                           0.,                                                                                0.]]
        self.assertTrue(np.allclose(tab,sam.table[2]))

    def test_sample(self):
        """Sample many matrices and test that the distribution is correct"""

        print('')
        print('H0: sampling distribution is correct')
        print('HA: sampling distribution is not correct')
        for ii in [0,1,2]:
            print('Bounded table instance ' + str(ii) +
            ' chi-square test p-value: ' + '{:.3f}'.format(
                            tabletools.test_this_sampler( self.sam[ii], 10000)))
        print('')

        return 0

class TestBinaryExactRowsExpectedColumns(unittest.TestCase):

    def setUp(self):
        self.m = [margins.MarginsWithCellBounds([3,2,1],[2,2,1,1],1),
                  margins.MarginsWithCellBounds([2,2,2,2],[2,2,2,2],1)]
        self.sam = []
        for mii in self.m:
            self.sam.append(samplers.BinaryExactRowsExpectedColumns(mii))

    def test_sample(self):
        """Sample many matrices and test that the distribution is correct"""

        print('')
        print('H0: sampling distribution is correct')
        print('HA: sampling distribution is not correct')
        for ii in [0,1]:
            print('Binary table instance ' + str(ii) +
            ' chisquare test p-value: ' + '{:.3f}'.format(
                            tabletools.test_this_sampler( self.sam[ii], 10000)))
        print('')

        return 0<|endoftext|>


<commit_before>import io
import os
import time
import sys
import struct
import socket
import numpy as np
import torch as torch
from torch.multiprocessing import Process, Pipe, SimpleQueue
from spiketag.utils import Timer
from spiketag.utils import EventEmitter
from spiketag.view import scatter_3d_view
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QThread, QEventLoop
from PyQt5.QtWidgets import QMainWindow, QAction, QFileDialog, QWidget, QSplitter, QComboBox, QTextBrowser, QSlider, QPushButton, QTableWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QLabel, QGridLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from spiketag.view.grid_scatter3d import grid_scatter3d
from spiketag.fpga import xike_config
from spiketag.utils import Timer
from collections import deque

class bmi_recv(object):
    """docstring for FPGA"""
    def __init__(self, prb=None):
        self.prb = prb
        # self.group_idx = np.array(self.prb.grp_dict.keys())
        self.reset()

    def close(self):
        self.r32.close()

    def reset(self):
        self.r32 = io.open('/dev/xillybus_fet_clf_32', 'rb')
        # self.r32_buf = io.BufferedReader(r32)
        self.fd = os.open("./fet.bin", os.O_CREAT | os.O_WRONLY | os.O_NONBLOCK)
        self._size = 7*4  # 6 samples, 4 bytes/sample

    def shared_mem_init(self):
        n_spike_count_vector = len(self.prb.grp_dict.keys())
        # trigger task using frame counter
        self.spike_count_vector = torch.zeros(n_spike_count_vector,)
        self.spike_count_vector.share_memory_()

    def _fpga_process(self, queue):
        '''
        A daemon process dedicated on reading data from PCIE and update
        the shared memory with other processors: shared_arr
        '''
        while True:
            buf = self.r32.read(self._size)
            with Timer('real-time decoding', verbose=False):
                # ----- real-time processing the BMI output ------
                # ----- This section should cost < 100us -----
                bmi_output = struct.unpack('<7i', buf)
                timestamp, grp_id, fet0, fet1, fet2, fet3, spk_id = bmi_output

                ##### real-time decoder

                ##### queue for visualization
                queue.put(bmi_output)

                ##### file for visualization
                os.write(self.fd, buf)
                # ----- This section should cost < 100us -----

    def start(self):
        self.queue = SimpleQueue()
        self.fpga_process = Process(target=self._fpga_process, name='fpga', args=(self.queue,)) #, args=(self.pipe_jovian_side,)
        self.fpga_process.daemon = True
        self.fpga_process.start()

    def stop(self):
        self.fpga_process.terminate()
        self.fpga_process.join()

class BMI_GUI(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.fet_view_timer = QtCore.QTimer(self)
        self.fet_view_timer.timeout.connect(self.test_update)   # fet_view_update
        self.current_group = 0
        self.fpga = bmi_recv()
        self.fpga_setting = xike_config()
        self.track_groups()
        self.fresh_interval = 30
        self._fet = {}
        self._clu = {}
        for i in range(40):
            # self._fet[i] = deque()
            # self._clu[i] = deque()
            self._fet[i] = np.zeros((40, 4))
            self._clu[i] = np.zeros((40,), dtype=np.int64)

        self.init_UI()

    def init_UI(self, keys='interactive'):
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.darkGray)
        p.setColor(self.foregroundRole(), Qt.white)
        self.setPalette(p)

        self.fpgaBtn = QPushButton("FPGA Stream Off",self)
        self.fpgaBtn.setCheckable(True)
        self.fpgaBtn.setStyleSheet("background-color: darkgrey")
        self.fpgaBtn.toggled.connect(self.fpga_process_toggle)

        rows,cols=6,7
        self.fet_grid = grid_scatter3d(rows, cols)
        self.N = 6000
        for i in range(rows*cols):
            if i in self._group_on_track:
                self.fet_grid.fet_view[i].info = i
            self.fet_grid.fet_view[i].set_data(np.zeros((self.N,4), dtype=np.float32))

        layout = QVBoxLayout()
        layout.addWidget(self.fpgaBtn)
        layout.addWidget(self.fet_grid)

        self.setLayout(layout)

    def fpga_process_toggle(self, checked):
        if checked:
            self.fpga_process_start()
        else:
            self.fpga_process_stop()

    def fpga_process_start(self):
        self.fpgaBtn.setText('FPGA Stream ON')
        self.fpgaBtn.setStyleSheet("background-color: green")
        self.fpga.start()
        self.fet_view_timer.start(self.fresh_interval)

    def fpga_process_stop(self):
        self.fpgaBtn.setText('FPGA Stream Off')
        self.fpgaBtn.setStyleSheet("background-color: darkgrey")
        self.fpga.stop()
        self.fet_view_timer.stop()

    @property
    def group_on_track(self):
        return self._group_on_track

    def track_groups(self, grp_ids=None):
        if grp_ids is None:
            self._group_on_track = np.where(self.fpga_setting.transformer_status)[0]
        else:
            self._group_on_track = grp_ids
        print('groups on track:{}'.format(self.group_on_track))

    def test_update(self):
        group_need_update = []
        _nspk, limit = 0, 40
        while not self.fpga.queue.empty():
            with Timer('receive', verbose=False):
                _timestamp, _grp_id, _fet0, _fet1, _fet2, _fet3, _spk_id = self.fpga.queue.get()
                if _grp_id in self._group_on_track:
                    group_need_update.append(_grp_id)
                    incoming_fet = np.array([_fet0, _fet1, _fet2, _fet3])/float(2**16)
                    incoming_clu = int(_spk_id)
                    self._fet[_grp_id] = np.roll(self._fet[_grp_id], 1, axis=0)
                    self._fet[_grp_id][0] = incoming_fet
                    self._clu[_grp_id] = np.roll(self._clu[_grp_id], 1)
                    self._clu[_grp_id][0] = incoming_clu

                    _nspk += 1
                    if _nspk>limit:
                        while not self.fpga.queue.empty():
                            self.fpga.queue.get()
                        break

        with Timer('update', verbose=True):
            for grp_id in group_need_update:
                with Timer('render', verbose=False):
                    self.fet_grid.fet_view[grp_id].stream_in(self._fet[grp_id][:_nspk],
                                                             self._clu[grp_id][:_nspk],
                                                             rho=1, highlight_no=10)

    def fet_view_update(self):
        # with Timer('update fet', verbose=False):
        #
        # try:
            N = 5000
            fet = np.fromfile('./fet.bin', dtype=np.int32)
            # try:
                # fet = np.memmap('./fet.bin', dtype='int32', mode='r')
            if fet.shape[0] > 0:
                try:
                    fet = fet.reshape(-1, 7)
                    fet_info = fet[:,:2]
                    fet_val = fet[:,2:6]/float(2**16)
                    labels  = fet[:, -1]
                    # get idx of fet from current selected group
                    idx = np.where(fet_info[:,1]==self.current_group)[0]
                    if len(idx)>N:
                        idx = idx[-N:]
                        fet = fet_val[idx, :]
                        clu = np.zeros((fet.shape[0],), dtype=np.int32)
                        clu[-30:] = 1
                    # if self.current_group in self.fpga.vq_grp_idx:
                    #     fet = fet_val[idx, :]
                    #     clu = self.fpga.vq['labels'][self.current_group][labels[idx]]
                    #     # # self.log.info(clu)

                    else:
                        fet = fet_val[idx, :]
                        # if len(idx)>N:
                        #     fet = fet[-N:, :]
                        clu = np.zeros((fet.shape[0],), dtype=np.int32)
                        clu[-30:] = 1

                    # self.log.info('get_fet{}'.format(idx.shape))
                    if len(fet)>0:
                        try:
                            self.fet_view.stream_in(fet, clu, highlight_no=30)
                        except:
                            pass
                except:
                    pass

if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = BMI_GUI()
    gui.show()
    # fpga.thres[:] = -300.
    # fpga.thres[:] = -150.
    # fpga.thres[80:84] = -590.
    # print(fpga.thres)
    sys.exit(app.exec_())
<commit_msg>demo of receiver_grid for real-time spike assignment visualization<commit_after>import time
import sys
import socket
import numpy as np
from spiketag.utils import Timer
from spiketag.view import scatter_3d_view
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QThread, QEventLoop
from PyQt5.QtWidgets import QMainWindow, QAction, QFileDialog, QWidget, QSplitter, QComboBox, QTextBrowser, QSlider, QPushButton, QTableWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QLabel, QGridLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from spiketag.view.grid_scatter3d import grid_scatter3d
from spiketag.fpga import xike_config
from spiketag.base import probe
from spiketag.realtime import bmi_recv
from spiketag.fpga import read_mem_16

class BMI_GUI(QWidget):
    def __init__(self, prb, fet_file, show=True):
        QWidget.__init__(self)
        self.fet_view_timer = QtCore.QTimer(self)
        self.fet_view_timer.timeout.connect(self.update_fet_views)   # fet_view_update
        self.current_group = 0
        self.init_FPGA(prb, fet_file)
        self.track_groups()
        self.fresh_interval = 30
        self._fet = {}
        self._clu = {}
        for i in range(40):
            self._fet[i] = np.zeros((40, 4))
            self._clu[i] = np.zeros((40,), dtype=np.int64)
        if show:
            self.init_UI()

    def init_FPGA(self, prb, fet_file):
        print('---init FPGA BMI---')
        print('---download probe file into FPGA---')
        try:
            self.fpga = bmi_recv(prb, fet_file)
            self.fpga_setting = xike_config(prb)
            print('---success---')
        except Exception as e:
            raise

    def init_UI(self, keys='interactive'):
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.darkGray)
        p.setColor(self.foregroundRole(), Qt.white)
        self.setPalette(p)

        self.fpgaBtn = QPushButton("FPGA Stream Off",self)
        self.fpgaBtn.setCheckable(True)
        self.fpgaBtn.setStyleSheet("background-color: darkgrey")
        self.fpgaBtn.toggled.connect(self.fpga_process_toggle)

        rows,cols=6,7
        self.fet_grid = grid_scatter3d(rows, cols)
        self.N = 2000
        for i in range(rows*cols):
            if i in self._group_on_track:
                self.fet_grid.fet_view[i].info = i
            self.fet_grid.fet_view[i].set_data(np.zeros((self.N,4), dtype=np.float32))

        layout = QVBoxLayout()
        layout.addWidget(self.fpgaBtn)
        layout.addWidget(self.fet_grid)

        self.setLayout(layout)

    def fpga_process_toggle(self, checked):
        if checked:
            self.fpga_process_start()
        else:
            self.fpga_process_stop()

    def fpga_process_start(self):
        self.fpgaBtn.setText('FPGA Stream ON')
        self.fpgaBtn.setStyleSheet("background-color: green")
        self.fpga.start()
        self.fet_view_timer.start(self.fresh_interval)

    def fpga_process_stop(self):
        self.fpgaBtn.setText('FPGA Stream Off')
        self.fpgaBtn.setStyleSheet("background-color: darkgrey")
        self.fpga.stop()
        self.fet_view_timer.stop()

    @property
    def group_on_track(self):
        return self._group_on_track

    def track_groups(self, grp_ids=None):
        if grp_ids is None:
            self._group_on_track = np.where(self.fpga_setting.transformer_status)[0]
        else:
            self._group_on_track = grp_ids
        print('{} groups on track:{}'.format(len(self.group_on_track), self.group_on_track))

    def update_fet_views(self):
        group_need_update = []
        _nspk, limit = 0, 40
        while not self.fpga.gui_queue.empty():
            with Timer('receive', verbose=False):
                _timestamp, _grp_id, _fet0, _fet1, _fet2, _fet3, _spk_id = self.fpga.gui_queue.get()
                if _grp_id in self._group_on_track:
                    group_need_update.append(_grp_id)
                    incoming_fet = np.array([_fet0, _fet1, _fet2, _fet3])/float(2**16)
                    incoming_clu = int(_spk_id)
                    self._fet[_grp_id] = np.roll(self._fet[_grp_id], 1, axis=0)
                    self._fet[_grp_id][0] = incoming_fet
                    self._clu[_grp_id] = np.roll(self._clu[_grp_id], 1)
                    self._clu[_grp_id][0] = incoming_clu

                    _nspk += 1
                    if _nspk>limit:
                        while not self.fpga.gui_queue.empty():
                            self.fpga.gui_queue.get()
                        break

        with Timer('update', verbose=False):
            for grp_id in group_need_update:
                with Timer('render', verbose=False):
                    self.fet_grid.fet_view[grp_id].stream_in(self._fet[grp_id][:_nspk],
                                                             self._clu[grp_id][:_nspk],
                                                             rho=1, highlight_no=10)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    prb = probe(prbfile='./dusty.json')
    gui = BMI_GUI(prb=prb, fet_file='./fet.bin')
    gui.show()
    n_id = read_mem_16(0)
    print(n_id)
    gui.fpga.set_binner(bin_size=33.33, n_id=n_id, n_bin=1)
    # fpga.thres[:] = -300.
    # fpga.thres[:] = -150.
    # fpga.thres[80:84] = -590.
    # print(fpga.thres)
    sys.exit(app.exec_())
<|endoftext|>


<commit_before>import argparse
import os
import time

import paddle
import paddle.fluid as fluid

import reader
from network_conf import ctr_dnn_model

def print_log(log_str):
    time_stamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print(str(time_stamp) + " " + log_str)

def parse_args():
    parser = argparse.ArgumentParser(description="PaddlePaddle CTR example")
    parser.add_argument(
        '--train_data_path',
        type=str,
        default='./data/train.txt',
        help="The path of training dataset")
    parser.add_argument(
        '--test_data_path',
        type=str,
        default='./data/valid.txt',
        help="The path of testing dataset")
    parser.add_argument(
        '--batch_size',
        type=int,
        default=1000,
        help="The size of mini-batch (default:1000)")
    parser.add_argument(
        '--embedding_size',
        type=int,
        default=10,
        help="The size for embedding layer (default:10)")
    parser.add_argument(
        '--num_passes',
        type=int,
        default=10,
        help="The number of passes to train (default: 10)")
    parser.add_argument(
        '--model_output_dir',
        type=str,
        default='models',
        help='The path for model to store (default: models)')

    return parser.parse_args()

def train():
    args = parse_args()

    if not os.path.isdir(args.model_output_dir):
        os.mkdir(args.model_output_dir)

    loss, data_list, auc_var, batch_auc_var = ctr_dnn_model(args.embedding_size)
    optimizer = fluid.optimizer.Adam(learning_rate=1e-4)
    optimizer.minimize(loss)

    dataset = reader.Dataset()
    train_reader = paddle.batch(
        paddle.reader.shuffle(
            dataset.train([args.train_data_path]),
            buf_size=args.batch_size * 100),
        batch_size=args.batch_size)
    place = fluid.CPUPlace()

    feeder = fluid.DataFeeder(feed_list=data_list, place=place)
    data_name_list = [var.name for var in data_list]

    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())
    for pass_id in range(args.num_passes):
        for batch_id, data in enumerate(train_reader()):
            loss_val, auc_val, batch_auc_val = exe.run(
                fluid.default_main_program(),
                feed=feeder.feed(data),
                fetch_list=[loss, auc_var, batch_auc_var]
            )
            print_log("TRAIN --> pass: {} batch: {} loss: {} auc: {}, batch_auc: {}"
                      .format(pass_id, batch_id, loss_val, auc_val, batch_auc_val))
            if batch_id % 1000 == 0 and batch_id != 0:
                model_dir = args.model_output_dir + '/batch-' + str(batch_id)
                fluid.io.save_inference_model(model_dir, data_name_list, [loss, auc_var], exe)
        model_dir = args.model_output_dir + '/pass-' + str(pass_id)
        fluid.io.save_inference_model(model_dir, data_name_list, [loss, auc_var], exe)

if __name__ == '__main__':
    train()
<commit_msg>support dist train<commit_after>from __future__ import print_function
import argparse
import os
import time

import paddle
import paddle.fluid as fluid

import reader
from network_conf import ctr_dnn_model

def print_log(log_str):
    time_stamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print(str(time_stamp) + " " + log_str)

def parse_args():
    parser = argparse.ArgumentParser(description="PaddlePaddle CTR example")
    parser.add_argument(
        '--train_data_path',
        type=str,
        default='./data/train.txt',
        help="The path of training dataset")
    parser.add_argument(
        '--test_data_path',
        type=str,
        default='./data/valid.txt',
        help="The path of testing dataset")
    parser.add_argument(
        '--batch_size',
        type=int,
        default=1000,
        help="The size of mini-batch (default:1000)")
    parser.add_argument(
        '--embedding_size',
        type=int,
        default=10,
        help="The size for embedding layer (default:10)")
    parser.add_argument(
        '--num_passes',
        type=int,
        default=10,
        help="The number of passes to train (default: 10)")
    parser.add_argument(
        '--model_output_dir',
        type=str,
        default='models',
        help='The path for model to store (default: models)')

    parser.add_argument(
        '--is_local',
        type=bool,
        default=True,
        help='Local train or distributed train (default: True)')
    # the following arguments is used for distributed train, if is_local == false, then you should set them
    parser.add_argument(
        '--role',
        type=str,
        default='trainer', # trainer or pserver
        help='The path for model to store (default: models)')
    parser.add_argument(
        '--endpoints',
        type=str,
        default='127.0.0.1:6000',
        help='The pserver endpoints, like: 127.0.0.1:6000,127.0.0.1:6001')
    parser.add_argument(
        '--current_endpoint',
        type=str,
        default='127.0.0.1:6000',
        help='The path for model to store (default: 127.0.0.1:6000)')
    parser.add_argument(
        '--trainer_id',
        type=int,
        default=0,
        help='The path for model to store (default: models)')
    parser.add_argument(
        '--trainers',
        type=int,
        default=1,
        help='The num of trianers, (default: 1)')

    return parser.parse_args()

def train_loop(args, train_program, data_list, loss, auc_var, batch_auc_var):
    dataset = reader.Dataset()
    train_reader = paddle.batch(
        paddle.reader.shuffle(
            dataset.train([args.train_data_path]),
            buf_size=args.batch_size * 100),
        batch_size=args.batch_size)
    place = fluid.CPUPlace()

    feeder = fluid.DataFeeder(feed_list=data_list, place=place)
    data_name_list = [var.name for var in data_list]

    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())
    for pass_id in range(args.num_passes):
        for batch_id, data in enumerate(train_reader()):
            loss_val, auc_val, batch_auc_val = exe.run(
                train_program,
                feed=feeder.feed(data),
                fetch_list=[loss, auc_var, batch_auc_var]
            )
            print_log("TRAIN --> pass: {} batch: {} loss: {} auc: {}, batch_auc: {}"
                      .format(pass_id, batch_id, loss_val, auc_val, batch_auc_val))
            if batch_id % 1000 == 0 and batch_id != 0:
                model_dir = args.model_output_dir + '/batch-' + str(batch_id)
                fluid.io.save_inference_model(model_dir, data_name_list, [loss, auc_var], exe)
        model_dir = args.model_output_dir + '/pass-' + str(pass_id)
        fluid.io.save_inference_model(model_dir, data_name_list, [loss, auc_var], exe)

def train():
    args = parse_args()

    if not os.path.isdir(args.model_output_dir):
        os.mkdir(args.model_output_dir)

    loss, data_list, auc_var, batch_auc_var = ctr_dnn_model(args.embedding_size)
    optimizer = fluid.optimizer.Adam(learning_rate=1e-4)
    optimizer.minimize(loss)

    if args.is_local:
        main_program = fluid.default_main_program()
        train_loop(args, main_program, data_list, loss, auc_var, batch_auc_var)
    else:
        t = fluid.DistributeTranspiler()
        t.transpile(args.trainer_id, pservers=args.endpoints, trainers=args.trainers)
        if args.role == "pserver":
            prog = t.get_pserver_program(args.curargs.rent_endpoint)
            startup = t.get_startup_program(args.current_endpoint, pserver_program=prog)
            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup)
            exe.run(prog)
        elif args.role == "trainer":
            train_prog = t.get_trainer_program()
            train_loop(args, train_prog, data_list, loss, auc_var, batch_auc_var)

if __name__ == '__main__':
    train()
<|endoftext|>


<commit_before>from django import forms
from django.contrib import admin
from django.db import models as django
from django.utils.text import Truncator
from django.utils.html import mark_safe, format_html
from django.utils.translation import ugettext_lazy as _

from . import models

class CountryTabularInline(admin.TabularInline):
    fields = ('code', 'name', )
    model = models.Country

    class CountryInlineFormset(forms.models.BaseInlineFormSet):
        def clean(self):
            super(CountryTabularInline.CountryInlineFormset, self).clean()
            if not hasattr(self, 'cleaned_data'):
                return

            for form_data in self.cleaned_data:
                code = form_data.get('code', None)
                if code and len(code) < 2:
                    raise forms.ValidationError('One of the countries code is invalid')
                return self.cleaned_data

    formset = CountryInlineFormset

    def has_add_permission(self, request):
        return False

class CityStackedInline(admin.TabularInline):
    model = models.City
    readonly_fields = ('wiki', )

    wiki_link_template = "" \
                         " search" \
                         ""

    def wiki(self, city):
        if city.id:
            return mark_safe(self.wiki_link_template.format(city.name))
        return ""
    wiki.short_description = _('wiki')

class SeaStackedInline(admin.StackedInline):
    extra = 0
    fields = ('name', 'area', 'avg_depth', 'max_depth')
    model = models.Sea
    readonly_fields = ('avg_depth', 'max_depth')

    class SeaInlineFormset(forms.models.BaseInlineFormSet):
        def clean(self):
            super(SeaStackedInline.SeaInlineFormset, self).clean()
            if not hasattr(self, 'cleaned_data'):
                return

            for form_data in self.cleaned_data:
                name = form_data['name']
                if len(name) < 2:
                    raise forms.ValidationError('One of the seas name is too short')
                return self.cleaned_data

    formset = SeaInlineFormset

class ContinentTabularInline(admin.TabularInline):
    model = models.Continent.oceans.through
    extra = 1

@admin.register(models.Ocean)
class OceanAdmin(admin.ModelAdmin):
    icon = ''
    actions = None
    exclude = ('area', )
    readonly_fields = ('map', )
    inlines = [ContinentTabularInline, SeaStackedInline]
    list_display = ('name', 'area', 'short_description', 'map',)
    prepopulated_fields = {'slug': ('name', )}

    def map(self, ocean):
        if ocean.map_url:
            return format_html('

', ocean.map_url)
        return ""
    map.short_description = _('map')

    def short_description(self, ocean):
        return Truncator(ocean.description).words(100, truncate=' ...')
    short_description.short_description = _('short description')

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

@admin.register(models.Sea)
class SeaAdmin(admin.ModelAdmin):
    icon = 'bubble_chart'
    fields = (('name', 'parent'),
              'ocean',
              ('area', 'avg_depth', 'max_depth'),
              'basin_countries')
    filter_horizontal = ('basin_countries', )
    list_display = ('name', 'parent', 'ocean', 'sea_area', )
    list_filter = ('parent', 'ocean', )

    def sea_area(self, sea):
        return None if sea.area == 0 else sea.area
    sea_area.short_description = _('sea area')
    sea_area.empty_value_display = '?'

@admin.register(models.Continent)
class ContinentAdmin(admin.ModelAdmin):
    icon = ''
    actions_selection_counter = False
    fieldsets = (
        (None, {
            'fields': ('name',)}),
        (_('Details'), {
            'fields': ('area', ('oceans', 'hemisphere'),
                       ('population', 'population_density'))}),
        (_('Fun facts'), {
            'fields': ('largest_country', 'biggest_mountain',
                       ('biggest_city', 'longest_river', ))})
    )
    inlines = [CountryTabularInline]
    list_display = (
        'name', 'surrounded_oceans', 'countries_count',
        'area', 'population', )
    list_filter = ('oceans', )
    ordering = ['population']
    raw_id_fields = ('oceans', )
    readonly_fields = ('biggest_city', 'longest_river', )

    def surrounded_oceans(self, contintent):
        return ', '.join(ocean.name for ocean in contintent.oceans.all())
    surrounded_oceans.short_description = _('surrounded oceans')

    def countries_count(self, contintent):
        return contintent.countries.count()
    countries_count.short_description = _('countries count')

class CountryForm(forms.ModelForm):
    class Meta:
        model = models.Country
        fields = '__all__'

    def clean(self):
        cleaned_data = super(CountryForm, self).clean()
        code = cleaned_data.get('code')
        if code and len(code) < 2:
            raise forms.ValidationError('The country code is invalid')
        return self.cleaned_data

@admin.register(models.Country)
class CountryAdmin(admin.ModelAdmin):
    icon = 'flag'
    date_hierarchy = 'independence_day'
    form = CountryForm
    inlines = [CityStackedInline]
    list_display = (
        'tld', 'name', 'continent',
        'became_independent_in_20_century',
        'gay_friendly')
    list_display_links = ('tld', 'name', )
    list_filter = ('continent', )
    list_per_page = 50
    list_select_related = ('continent', )
    search_fields = ('code', 'name', )

    def tld(self, country):
        return '.' + country.code.lower()
    tld.short_description = 'TLD'
    tld.admin_order_field = 'code'

    def became_independent_in_20_century(self, country):
        if country.independence_day:
            return 1900 <= country.independence_day.year <= 2000
    became_independent_in_20_century.short_description = _('became independent in XX century')
    became_independent_in_20_century.boolean = True

@admin.register(models.City)
class CityAdmin(admin.ModelAdmin):
    icon = ''
    list_display = ('name', 'country', 'population')
    list_filter = ('is_capital', 'country', (
        'country__continent', admin.RelatedOnlyFieldListFilter))
    search_fields = ('name', )
    formfield_overrides = {
        django.IntegerField: {
            'widget': forms.NumberInput(attrs={'min': 0})},
    }
    show_full_result_count = False
    raw_id_fields = ('country', )
    readonly_fields = ('became_independent_in_20_century', )

    def became_independent_in_20_century(self, city):
        if city.country_id is not None and city.country.independence_day:
            return 1900 <= city.country.independence_day.year <= 2000
    became_independent_in_20_century.short_description = _('became independent in XX century')
    became_independent_in_20_century.boolean = True
<commit_msg>Fix admin demo<commit_after>from django import forms
from django.contrib import admin
from django.db import models as django
from django.utils.text import Truncator
from django.utils.html import mark_safe, format_html
from django.utils.translation import ugettext_lazy as _

from . import models

class CountryTabularInline(admin.TabularInline):
    fields = ('code', 'name', )
    model = models.Country

    class CountryInlineFormset(forms.models.BaseInlineFormSet):
        def clean(self):
            super(CountryTabularInline.CountryInlineFormset, self).clean()
            if not hasattr(self, 'cleaned_data'):
                return

            for form_data in self.cleaned_data:
                code = form_data.get('code', None)
                if code and len(code) < 2:
                    raise forms.ValidationError('One of the countries code is invalid')
                return self.cleaned_data

    formset = CountryInlineFormset

    def has_add_permission(self, request, obj=None):
        return False

class CityStackedInline(admin.TabularInline):
    model = models.City
    readonly_fields = ('wiki', )

    wiki_link_template = "" \
                         " search" \
                         ""

    def wiki(self, city):
        if city.id:
            return mark_safe(self.wiki_link_template.format(city.name))
        return ""
    wiki.short_description = _('wiki')

class SeaStackedInline(admin.StackedInline):
    extra = 0
    fields = ('name', 'area', 'avg_depth', 'max_depth')
    model = models.Sea
    readonly_fields = ('avg_depth', 'max_depth')

    class SeaInlineFormset(forms.models.BaseInlineFormSet):
        def clean(self):
            super(SeaStackedInline.SeaInlineFormset, self).clean()
            if not hasattr(self, 'cleaned_data'):
                return

            for form_data in self.cleaned_data:
                name = form_data['name']
                if len(name) < 2:
                    raise forms.ValidationError('One of the seas name is too short')
                return self.cleaned_data

    formset = SeaInlineFormset

class ContinentTabularInline(admin.TabularInline):
    model = models.Continent.oceans.through
    extra = 1

@admin.register(models.Ocean)
class OceanAdmin(admin.ModelAdmin):
    icon = ''
    actions = None
    exclude = ('area', )
    readonly_fields = ('map', )
    inlines = [ContinentTabularInline, SeaStackedInline]
    list_display = ('name', 'area', 'short_description', 'map',)
    prepopulated_fields = {'slug': ('name', )}

    def map(self, ocean):
        if ocean.map_url:
            return format_html('

', ocean.map_url)
        return ""
    map.short_description = _('map')

    def short_description(self, ocean):
        return Truncator(ocean.description).words(100, truncate=' ...')
    short_description.short_description = _('short description')

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

@admin.register(models.Sea)
class SeaAdmin(admin.ModelAdmin):
    icon = 'bubble_chart'
    fields = (('name', 'parent'),
              'ocean',
              ('area', 'avg_depth', 'max_depth'),
              'basin_countries')
    filter_horizontal = ('basin_countries', )
    list_display = ('name', 'parent', 'ocean', 'sea_area', )
    list_filter = ('parent', 'ocean', )

    def sea_area(self, sea):
        return None if sea.area == 0 else sea.area
    sea_area.short_description = _('sea area')
    sea_area.empty_value_display = '?'

@admin.register(models.Continent)
class ContinentAdmin(admin.ModelAdmin):
    icon = ''
    actions_selection_counter = False
    fieldsets = (
        (None, {
            'fields': ('name',)}),
        (_('Details'), {
            'fields': ('area', ('oceans', 'hemisphere'),
                       ('population', 'population_density'))}),
        (_('Fun facts'), {
            'fields': ('largest_country', 'biggest_mountain',
                       ('biggest_city', 'longest_river', ))})
    )
    inlines = [CountryTabularInline]
    list_display = (
        'name', 'surrounded_oceans', 'countries_count',
        'area', 'population', )
    list_filter = ('oceans', )
    ordering = ['population']
    raw_id_fields = ('oceans', )
    readonly_fields = ('biggest_city', 'longest_river', )

    def surrounded_oceans(self, contintent):
        return ', '.join(ocean.name for ocean in contintent.oceans.all())
    surrounded_oceans.short_description = _('surrounded oceans')

    def countries_count(self, contintent):
        return contintent.countries.count()
    countries_count.short_description = _('countries count')

class CountryForm(forms.ModelForm):
    class Meta:
        model = models.Country
        fields = '__all__'

    def clean(self):
        cleaned_data = super(CountryForm, self).clean()
        code = cleaned_data.get('code')
        if code and len(code) < 2:
            raise forms.ValidationError('The country code is invalid')
        return self.cleaned_data

@admin.register(models.Country)
class CountryAdmin(admin.ModelAdmin):
    icon = 'flag'
    date_hierarchy = 'independence_day'
    form = CountryForm
    inlines = [CityStackedInline]
    list_display = (
        'tld', 'name', 'continent',
        'became_independent_in_20_century',
        'gay_friendly')
    list_display_links = ('tld', 'name', )
    list_filter = ('continent', )
    list_per_page = 50
    list_select_related = ('continent', )
    search_fields = ('code', 'name', )

    def tld(self, country):
        return '.' + country.code.lower()
    tld.short_description = 'TLD'
    tld.admin_order_field = 'code'

    def became_independent_in_20_century(self, country):
        if country.independence_day:
            return 1900 <= country.independence_day.year <= 2000
    became_independent_in_20_century.short_description = _('became independent in XX century')
    became_independent_in_20_century.boolean = True

@admin.register(models.City)
class CityAdmin(admin.ModelAdmin):
    icon = ''
    list_display = ('name', 'country', 'population')
    list_filter = ('is_capital', 'country', (
        'country__continent', admin.RelatedOnlyFieldListFilter))
    search_fields = ('name', )
    formfield_overrides = {
        django.IntegerField: {
            'widget': forms.NumberInput(attrs={'min': 0})},
    }
    show_full_result_count = False
    raw_id_fields = ('country', )
    readonly_fields = ('became_independent_in_20_century', )

    def became_independent_in_20_century(self, city):
        if city.country_id is not None and city.country.independence_day:
            return 1900 <= city.country.independence_day.year <= 2000
    became_independent_in_20_century.short_description = _('became independent in XX century')
    became_independent_in_20_century.boolean = True
<|endoftext|>