示例#1
0
 def __init__(self):
     self.last5_left = RingBuffer(5)
     self.last5_right = RingBuffer(5)
     self.avg_left_fit = 0
     self.avg_right_fit = 0
     self.avg_left_curv = 0 
     self.avg_right_curv = 0 
     self.avg_left_dist = 0
     self.avg_right_dist = 0
示例#2
0
    def poll(self, poll_time, on_enter=None, on_exit=None):
        print('starting polling')
        history = RingBuffer(capacity=20, dtype=np.float)
        entered = False
        while True:
            dist = self.distance()
            history.append(dist)

            if len(history) < 10:
                continue

            avg = np.median(history)
            if DEBUG:
                sys.stdout.write('\rdist: {:06.10f} avg: {:06.10f}'.format(
                    dist, avg))

            if not entered and avg < THRESHOLD:
                entered = True
                if on_enter:
                    on_enter()
            elif entered and avg > THRESHOLD:
                entered = False
                if on_exit:
                    on_exit()
            time.sleep(poll_time)
 def test_size_zero_buffer(self):
     b = RingBuffer(0)
     self.assertEqual(b.bytes_total(), 0)
     self.assertEqual(b.bytes_free(), 0)
     self.assertEqual(b.bytes_used(), 0)
     with self.assertRaises(ValueError):
         b.write(b"a")
    def test_reading_exact_wrapping_full(self):
        b = RingBuffer(4)
        b.write(b"ab")
        self.assertEqual(b.read(), b"ab")

        b.write(b"defg")
        self.assertEqual(b.read_exactly(4), b"defg")
示例#5
0
 def __init__(self, capacity):
     # store as instance variable
     self._capacity = capacity
     # create list of size capacity
     self.RingBuffer = RingBuffer(self._capacity)
     # set other instance variable defaults
     self.top = -1
     self.size = 0
 def test_basic_wrap(self):
     b = RingBuffer(4)
     b.write(b"ab")
     self.assertEqual(b.read(), b"ab")
     self.assertEqual(b.bytes_used(), 0)
     b.write(b"cde")
     self.assertEqual(b.read(), b"cd")
     self.assertEqual(b.read(), b"e")
     self.assertEqual(b.read(), None)
    def test_reading_exact_wrapping_singles(self):
        b = RingBuffer(4)
        b.write(b"ab")
        self.assertEqual(b.read(), b"ab")

        b.write(b"defg")
        self.assertEqual(b.read_exactly(1), b"d")
        self.assertEqual(b.read_exactly(1), b"e")
        self.assertEqual(b.read_exactly(1), b"f")
示例#8
0
    def add_ip(self, ip):
        self.__lock.acquire()
        try:
            current_dt = datetime.datetime.now()

            if not self.dictionary.__contains__(ip):
                self.dictionary[ip] = RingBuffer(self.__buffer_size)
            self.dictionary[ip].append(current_dt)
        finally:
            self.__lock.release()
示例#9
0
    def __init__(self, output_size, layers, memory_size=3000, batch_size=32,
                 use_ddqn=False, default_policy=False, model_filename=None, tb_dir="tb_log",
                 epsilon=1, epsilon_lower_bound=0.1, epsilon_decay_function=lambda e: e - (0.9 / 1000000),
                 gamma=0.95, optimizer=RMSprop(0.00025), learn_thresh=50000,
                 update_rate=10000):
        """
        Args:
            output_size: number of actions
            layers: list of Keras layers
            memory_size: size of replay memory
            batch_size: size of batch for replay memory
            use_ddqn: boolean for choosing between DQN/DDQN
            default_policy: boolean for loading a model from a file
            model_filename: name of file to load
            tb_dir: directory for tensorboard logging
            epsilon: annealing function upper bound
            epsilon_lower_bound: annealing function lower bound
            epsilon_decay_function: lambda annealing function 
            gamma: discount factor hyper parameter
            optimizer: Keras optimiser
            learn_thresh: number of steps to perform without learning
            update_rate: number of steps between network-target weights copy
        """
        self.output_size = output_size
        self.memory = RingBuffer(memory_size)
        self.use_ddqn = use_ddqn
        self.default_policy = default_policy
        # Tensorboard parameters
        self.tb_step = 0
        self.tb_gather = 500
        self.tb_dir = tb_dir
        if tb_dir is not None:
            self.tensorboard = TensorBoard(log_dir='./Monitoring/%s' % tb_dir, write_graph=False)
            print("Tensorboard Loaded! (log_dir: %s)" % self.tensorboard.log_dir)
        # Exploration/Exploitation parameters
        self.epsilon = epsilon
        self.epsilon_decay_function = epsilon_decay_function
        self.epsilon_lower_bound = epsilon_lower_bound
        self.total_steps = 0
        # Learning parameters
        self.gamma = gamma
        self.loss = huber_loss #'mean_squared_error'
        self.optimizer = optimizer
        self.batch_size = batch_size
        self.learn_thresh = learn_thresh # Number of steps from which the network starts learning
        self.update_rate = update_rate
        self.discrete_state = False

        if self.default_policy:
            self.evaluate_model = self.load_model(model_filename)
        else:
            self.evaluate_model = self.build_model(layers)

            if self.use_ddqn:
                self.target_model = self.build_model(layers)
    def test_size_wrap_and_full(self):
        b = RingBuffer(2)

        b.write(b"a")
        self.assertEqual(b.read(), b"a")
        self.assertEqual(b.read(), None)

        b.write(b"bc")
        self.assertEqual(b.read(), b"b")
        self.assertEqual(b.read(), b"c")
        self.assertEqual(b.read(), None)
示例#11
0
 def test_init(self):
     rb = RingBuffer(4)
     rs = RingStack(4)
     rq = RingQueue(4)
     ls = ListStack(4)
     lq = ListQueue(4)
     self.assertIsNotNone(rb.head)
     self.assertEqual(rb.size(), 0)
     self.assertEqual(rs.top, -1)
     self.assertEqual(rs.size, 0)
     self.assertEqual(ls.top, -1)
     self.assertEqual(ls.size, 0)
     self.assertEqual(rq.front, -1)
     self.assertEqual(rq.back, -1)
     self.assertEqual(rq.size, 0)
     self.assertEqual(lq.front, -1)
     self.assertEqual(lq.back, -1)
     self.assertEqual(lq.size, 0)
示例#12
0
    def __init__(self, queue):
        super().__init__(queue)
        self._name = "SoundEffectEngine"
        self._chunk = 1024
        self._player = pyaudio.PyAudio()
        self._wav_dir = config.SOUNDFILE_DIR
        self._wav_files = {}
        self._cur_wav_file = None
        self._stream = None
        self._dataFile = open("history.csv", "w")
        self._dataWriter = csv.writer(self._dataFile,
                                      delimiter=",",
                                      quotechar='"',
                                      quoting=csv.QUOTE_MINIMAL)
        self._currentBpms = RingBuffer(config.AVG_WINDOW)
        self._heartbeat = config.HEARTBEAT_TIMEOUT

        if config.BPM_RANGE_LOW >= config.BPM_RANGE_HIGH:
            raise ValueError(
                "BPM Ranges are not configured correctly in config")
    def test_size_one_buffer(self):
        b = RingBuffer(1)
        self.assertEqual(b.bytes_total(), 1)
        self.assertEqual(b.bytes_free(), 1)
        self.assertEqual(b.bytes_used(), 0)

        b.write(b"a")
        self.assertEqual(b.bytes_total(), 1)
        self.assertEqual(b.bytes_free(), 0)
        self.assertEqual(b.bytes_used(), 1)

        self.assertEqual(b.read(), b"a")
        self.assertEqual(b.bytes_total(), 1)
        self.assertEqual(b.bytes_free(), 1)
        self.assertEqual(b.bytes_used(), 0)

        self.assertEqual(b.read(), None)

        b.write(b"b")
        self.assertEqual(b.read(), b"b")
        self.assertEqual(b.read(), None)