def __init__(self, host, port, upstream, downstream_class, out_buf_size=BUF_SIZE, in_buf_size=BUF_SIZE): self._host = host self._port = port # an downstream node writes incoming data here self._in_ringbuf = ringbuffer.RingBuffer(in_buf_size) # an upstream node writes outgoing data here self._out_ringbuf = ringbuffer.RingBuffer(out_buf_size) self._upstream = upstream self._downstream = downstream_class(host, port, self)
def __init__(self, device_index, plot_results, dim, audio_length, sample_rate, size_limit): """Segment analysis. Splits audio buffers into segments called 'dims' before feature analysis. Each dim-type (samples classified by length) has cluster analysis run on it, samples outside of clusters are prunned over time.""" self.limit = size_limit * 100 # The limit is a value used to control how many files can exist at a time, it is approximately 100 mb per 10000 limit value. self.sample_rate = int(sample_rate) self.dim = dim # How many dims to create or how many different lengths of segementations of the audio to make. self.dataset,self.input_data,self.nearest, self.input_data = [],[],[],[] self.nearest = [[] for x in range(0, dim) ] # The nearest files to the input for each dim. self.input_data = [[] for x in range(0, dim)] self.distance_thresholds = [ 0.1, 0.4, 0.5, 0.7, 0.8, 0.8 ] # The inital distance threshold for different dims. self.num_neighbours = [ 4, 5, 8, 10, 11, 12, 13 ] # Number of neighbours for each dim to be checked against in dim-wise analysis. Should be set by users who want to use more than the default amont of dims. self.axs = [0 for x in range(0, dim)] # For subplotting. self.plot = plot_results # True or false to plot the data. self.removal_queue = [ ] # Filled with prunned samples, files saved to disk are deleted using this queue. if self.plot: fig = plt.figure() fig.subplots_adjust(hspace=0.4, wspace=0.4) for i in range(0, dim): self.axs[i] = fig.add_subplot(2, 3, i + 1) self.rb = ringbuffer.RingBuffer( audio_length * self.sample_rate ) # Ring buffer stores incoming audio from threader.py for segmentation and analysis.
def rb(request): with ringbuffer.RingBuffer(10, 4) as under_test: # Rotate the ring buffer position by various amounts, so we test behaviour when the buffer rolls around for i in range(0, request.param): assert under_test.try_write(b"0000") assert under_test.try_read() == b"0000" yield under_test
def __init__(self, host, port, upstream, buf_size=BUF_SIZE): # an upstream node writes outgoing data here self._host = host self._port = port self._upstream = upstream self._out_ringbuf = ringbuffer.RingBuffer(buf_size) self._state = ENDPOINT_STATE_NEW
def test_ringbuffer_write_multiple(): ring = ringbuffer.RingBuffer(c_type=TStruct, slot_count=10) ring.new_writer() reader = ring.new_reader() data = (TStruct * 8)() for i in range(0, 8): data[i] = TStruct(i=i) ring.try_write_multiple(data) expected_data_list = [{'i': index} for index in range(0, 6)] received_data = ring.try_read(reader, length=6) for i, expected_data in enumerate(expected_data_list): for k, v in expected_data.items(): value = getattr(received_data[i], k) assert value == v data = (TStruct * 6)() for i in range(8, 14): data[i - 8] = TStruct(i=i) ring.try_write_multiple(data) expected_data_list = [{'i': index} for index in range(6, 14)] received_data = ring.try_read(reader, length=8) for i, expected_data in enumerate(expected_data_list): for k, v in expected_data.items(): value = getattr(received_data[i], k) assert value == v
def __init__(self, num_episodes, model_name=None, carConfig=None, replay_freq=20): env = gym.make('CarRacingTrain-v1') env = wrappers.Monitor(env, 'monitor-folder', force=True) self.carConfig = carConfig self.env = env self.gamma = 0.99 self.K = 10 self.model_name = model_name self.model, self.init_weights, self.loss_fn, self.optimizer = create_nn( model_name ) # 4 consecutive steps, 111-element vector for each state self.target_model, _, _, _ = create_nn(model_name) self.past_weights = deque(maxlen=self.K) self.past_weights.append(self.init_weights) self.target_model.load_state_dict(self.init_weights) self.replay_freq = replay_freq if not model_name: MEMORY_SIZE = 10000 else: MEMORY_SIZE = 5000 # smaller memory for retraining self.memory = ringbuffer.RingBuffer(MEMORY_SIZE) self.num_episodes = num_episodes
def __init__(self): config = self.read_config() self.configs = config.get_map() self.sites = config.get_sites() self.global_config = self.configs['global'] self.downloads = [] self.setup_logging() self.already_downloaded = ringbuffer.RingBuffer()
def test_getting_ringbuffer_data_contains_all_added_strings (self): self.buffer = ringbuffer.RingBuffer (5) self.__add_times_number_of_test_strings (4) data = self.buffer.get_data () for i in range (0,4): name = 'test%d' %(i+1) self.assertEqual (name, data[i]) self.assertEqual (None, data[4])
def main(): ring = ringbuffer.RingBuffer(c_type=Record, slot_count=10) ring.new_writer() processes = [ multiprocessing.Process(target=reader, args=(ring, ring.new_reader())), multiprocessing.Process(target=reader, args=(ring, ring.new_reader())), multiprocessing.Process(target=writer, args=(ring, 1, 1000)), ] for p in processes: p.daemon = True p.start() for p in processes: p.join(timeout=20) assert not p.is_alive() assert p.exitcode == 0
def main(): ring = ringbuffer.RingBuffer(slot_bytes=50000, slot_count=100) ring.new_writer() processes = [ multiprocessing.Process(target=writer, args=(ring, )), ] for i in range(10): processes.append( multiprocessing.Process(target=reader, args=(ring, ring.new_reader()))) for p in processes: p.start() for p in processes: p.join(timeout=20) assert not p.is_alive() assert p.exitcode == 0
def __init__(self, err_threshold=0.5, request_volume_window=5, fail_max=5, reset_timeout=60, exclude=None, listeners=None, state_storage=None, name=None, buffer_reset_flag=False): """ Creates a new circuit breaker with the given parameters. """ self._lock = threading.RLock() self._state_storage = state_storage or CircuitMemoryStorage(STATE_CLOSED) self._state = self._create_new_state(self.current_state) self._err_threshold = err_threshold self._request_volume_window = request_volume_window self._fail_max = fail_max self._reset_timeout = reset_timeout self._excluded_exceptions = list(exclude or []) self._listeners = list(listeners or []) self._name = name self._success_fail_buffer = ringbuffer.RingBuffer(request_volume_window) self._buffer_reset_flag = buffer_reset_flag
def main(): # size of an entry (8 bytes per float), size of the circular buffer ring = ringbuffer.RingBuffer(slot_bytes=8, slot_count=50) ring.new_writer() processes = [ multiprocessing.Process(target=writer, args=(ring, )), ] for i in range(10): processes.append( multiprocessing.Process(target=reader, args=(ring, ring.new_reader()))) for p in processes: p.start() for p in processes: p.join(timeout=20) assert not p.is_alive() assert p.exitcode == 0
def main(): t = (ctypes.c_double * 100) * 25 ring = ringbuffer.RingBuffer(c_type=t, slot_count=100) ring.new_writer() processes = [ multiprocessing.Process(target=writer, args=(ring, )), ] for i in range(10): processes.append( multiprocessing.Process(target=reader, args=(ring, ring.new_reader()))) for p in processes: p.start() for p in processes: print('join') p.join(timeout=50) assert not p.is_alive() assert p.exitcode == 0
def __init__(self, format_in=pyaudio.paInt16, channels=1, rate=44100, block_time=0.05, buffer_time=0.25): global audio_data # setup for the audio card self.format = format_in self.channels = channels self.rate = rate self.input_block_time = block_time self.input_frames_per_block = int(self.rate * self.input_block_time) self.buffer_time = buffer_time self.buffer_length_in_frames = int(self.rate * self.buffer_time) # instantiate PyAudio self.p = pyaudio.PyAudio() # setup the vector we will add audio samples to audio_data = ringbuffer.RingBuffer(self.buffer_length_in_frames) self.stream = None
def setUp(self): self.ring = ringbuffer.RingBuffer(slot_bytes=100, slot_count=10) self.proxies = [] self.error_queue = self.new_queue()
def setUp(self): self.ring = ringbuffer.RingBuffer(c_type=TStruct, slot_count=10) self.proxies = [] self.error_queue = self.new_queue()
import serial import ringbuffer import numpy as np import time import threading import matplotlib #matplotlib.use('TKAgg') from matplotlib import animation from matplotlib.lines import Line2D from matplotlib import pyplot as plt BUFFER_SIZE = 500 data = ringbuffer.RingBuffer(BUFFER_SIZE) serial_port = serial.Serial('/dev/cu.usbmodem14322', baudrate=57600, timeout=1) serial_port.flush() fig = plt.figure() # setup the plot # ax1 = fig.add_subplot(2, 1, 1) # ax1.set_xlim(0, BUFFER_SIZE-1) # ax1.set_ylim(-5, 300) # line1 = Line2D([], [], color='red', linewidth=0.5) # ax1.add_line(line1) ax1 = plt.axes(xlim=(0, BUFFER_SIZE - 1), ylim=(-5, 300)) line1 = Line2D([], [], color='red', linewidth=0.5) ax1.add_line(line1) print("before stuff goes down")
def __init__(self, parent=None, name="", controller=None, unit=None, x=0, y=0, controlable=True, plotable=True, loggable=True, format="%f", bufferlen=20000, callback=None, **kwargs): QtGui.QWidget.__init__(self, parent) # Store passed variables self.controlable = controlable self.plotable = plotable self.loggable = loggable self.format = format self.emitCallback = callback if type(controller) == type([]): self.controller = controller else: self.controller = [controller] if type(unit) == type([]): self.unit = unit else: self.unit = [unit] self.name = name self.status = VacObject.ERROR self.statusMessage = "" self.value = 0 self.valueUnits = "None" self.yplot = 0 # Locate object self.move(x, y) # Setup ringbuffer self.ringbuffer = ringbuffer.RingBuffer(bufferlen) self.ringBufferUpdate = 10 self.lastRingBufferUpdate = 0 # Time of update self.lastUpdate = time.localtime() # Setup menu of actions for this control self.popup = QtGui.QMenu(self.name, self) self.popup.setTearOffEnabled(True) QtCore.QObject.connect(self.popup, QtCore.SIGNAL('triggered(QAction *)'), self.actionTriggered) self.setDefaultActions() # Setup standard labels self.nameLabel = QtGui.QLabel(self) self.nameLabel.hide() self.nameLabel.setText(self.name) self.statusLabel = QtGui.QLabel(self) self.statusLabel.hide() self.valuebox = QtGui.QLabel(self) self.valuebox.hide() self.noUpdate = False
import time import numpy as np import tools import ringbuffer as rb import dynamicplot as dp import serialreader as sr buffer_size = 240 # samples time_step = 0.5 # sec, plotting takes ~0.4 time_min = 0; time_max = buffer_size * time_step; temp_min = 18; temp_max = 28; buffer = rb.RingBuffer(buffer_size) serial = sr.SerialReader(9600, '/dev/ttyACM0') plot = dp.DynamicPlot(np.linspace(time_min, time_max, buffer_size), True, True) plot.format_x(time_min, time_max, 'Time [sec]') plot.format_y(temp_min, temp_max, 'Temperature [$^\circ$C]') t0 = time.time() while(plot.opened()): t1 = time.time() #temperature = tools.get_random() temperature = serial.read_line() buffer.append(temperature) plot.update_y(buffer.get_lifo()) print("\rElapsed time: %.1f sec, T = %.1f C" % (time.time() - t0, temperature), end='') #time.sleep(max(0, time_step - (time.time() - t1)))
def get_buffer(flags): return ringbuffer.RingBuffer(slot_bytes=flags.slot_bytes, slot_count=flags.slots)
def get_buffer(flags): t = ctypes.c_byte * flags.slot_bytes return ringbuffer.RingBuffer(c_type=t, slot_count=flags.slots)
__author__ = 'eclarson' import pyaudio import ringbuffer import numpy as np audio_data = ringbuffer.RingBuffer(100) def audio_callback(in_data, frame_count, time_info, status): # define callback for PyAudio--this is called in separate thread global audio_data tmp = np.fromstring(in_data, dtype=np.int16) audio_data.insert_new(tmp) # unpack the data, add to buffer return "", pyaudio.paContinue # return nothing to play on speaker, and keep streaming class MicAudio: """A class for sampling from the microphone""" def __init__(self, format_in=pyaudio.paInt16, channels=1, rate=44100, block_time=0.05, buffer_time=0.25): global audio_data # setup for the audio card self.format = format_in self.channels = channels self.rate = rate self.input_block_time = block_time
def setUp (self): self.buffer = ringbuffer.RingBuffer (2)
import serial import ringbuffer import numpy as np import time import threading import matplotlib #matplotlib.use('TKAgg') # need to use this on OSX for animate w/ blit=True from matplotlib import pyplot as plt from matplotlib import animation from matplotlib.lines import Line2D import globals from msvcrt import getch BUFFER_SIZE = 500 data = ringbuffer.RingBuffer(BUFFER_SIZE) CHAR_BUFFER_SIZE = 8 opening_sequence_checker = ringbuffer.RingBuffer(CHAR_BUFFER_SIZE) closing_sequence_checker = ringbuffer.RingBuffer(CHAR_BUFFER_SIZE) # setup the figure to plot with fig = plt.figure() # setup the plot ax1 = fig.add_subplot(1, 1, 1) ax1.set_xlim(0, BUFFER_SIZE - 1) ax1.set_ylim(0, 255) line1 = Line2D([], [], color='red', linewidth=0.5) ax1.add_line(line1)
inlet = pylsl.stream_inlet(streams[0]) inletInfo = inlet.info() inlet_sampleRate = int(inletInfo.nominal_srate()) inlet_numChannels = int(inletInfo.channel_count()) if verbose: print("Reported sample rate: %i , number of channels: %i" %(inlet_sampleRate, inlet_numChannels)) filterbank = lp.filterbank(lowcut,highcut,order,inlet_sampleRate) specGen = lp.specGen(output_width, output_height, output_stacks, lowcut, periods, overlapRatio, inlet_sampleRate) if verbose: print("window width: %i - overlap: %i - buffer size: %i" %(specGen.SFFTwindowWidth, specGen.SFFToverlap, specGen.smpPerSpec)) rbuffer = ringbuffer.RingBuffer(size_max=specGen.smpPerSpec) sendEverySmpl = math.ceil(inlet_sampleRate / outlet_sendRate) if verbose: print("Transmitting every %i samples" %sendEverySmpl) print("Creating outlet") outlet_numChannels = output_width*output_height*output_stacks info = pylsl.StreamInfo('ContinuousEEG', 'EEGPre', outlet_numChannels, outlet_sendRate, 'int8', 'transmissionTest') outlet = pylsl.StreamOutlet(info) samplesInBuffer = 0 samplesSent = 0 if verbose: fig = plt.figure(figsize=(10,8)) outer = gridspec.GridSpec(3, 1, wspace=0, hspace=0.2)
def record_and_process(stop_event, channel_data=[], time_stamps=[]): ### based on realtime.py # 0. General #verbose = False # 1. Output output_width = 32 output_height = 32 output_stacks = 3 # channels outlet_sendRate = 2 # [Hz] outlet_numChannels = output_width * output_height * output_stacks # 2. Filterbank lowcut = 2 # [Hz] highcut = 60 # [Hz] order = 3 # 3. Spectrogram Generation periods = 1.5 overlapRatio = 0.95 ### initialise data inlet and outlet inlet = find_gUSBamp_stream('EEG') inletInfo = inlet.info() inlet_sampleRate = int(inletInfo.nominal_srate()) inlet_numChannels = int(inletInfo.channel_count()) print("Reported sample rate: %i , number of channels: %i" % (inlet_sampleRate, inlet_numChannels)) outletInfo = pylsl.StreamInfo('PreprocessedEEG', 'EEGPre', outlet_numChannels, outlet_sendRate, 'int8', 'UB-2016.08.03') outlet = pylsl.StreamOutlet(outletInfo) ### initialise processing: Filterbank, Spectrogram generation filterbank = lp.filterbank(lowcut, highcut, order, inlet_sampleRate) specGen = lp.specGen(output_width, output_height, output_stacks, lowcut, periods, overlapRatio, inlet_sampleRate) ### initialise ringbuffer rbuffer = ringbuffer.RingBuffer(size_max=specGen.smpPerSpec) sendEverySmpl = math.ceil(inlet_sampleRate / outlet_sendRate) print("Transmitting every %i samples" % sendEverySmpl) samplesInBuffer = 0 samplesSent = 0 while not stop_event.is_set(): try: sample, time_stamp = inlet.pull_sample() time_stamp += inlet.time_correction() time_stamps.append(time_stamp) channel_data.append(sample) rbuffer.append(sample) samplesInBuffer += 1 except KeyboardInterrupt: ### save data and exit on KeybordInterrupt complete_samples = min(len(time_stamps), len(channel_data)) sio.savemat( "recording_" + time_str() + ".mat", { "time_stamps": time_stamps[:complete_samples], "channel_data": channel_data[:complete_samples], }) break if (rbuffer.full and samplesInBuffer >= sendEverySmpl): ### get from buffer, filter and generate spectrogram specs = specGen.process( filterbank.process(np.array(rbuffer.get())[:, 0:3])) ### convert to uint8 and flatten to send it to the LSL outlet.push_sample(lp.np_to_tn(specs).flatten()) samplesSent += 1 ### indicate that buffer content is used, such that it can be overwritten samplesInBuffer = 0
def __init__(self, downstream_class, buf_size=BUF_SIZE): self._downstream_class = downstream_class # a downstream node writes incoming data here self._in_ringbuf = ringbuffer.RingBuffer(buf_size) self._upstream_state = ENDPOINT_STATE_NEW