def test_stim_playlist_chunker_chunks_for_csv_explanation( monkeypatch, chunksize, tmpdir): import pandas as pd # reset the produce_instance to 0 monkeypatch.setattr(SignalProducer, 'instances_created', 0) stimplaylist = AudioStimPlaylist.from_playlist_definition( copy.deepcopy(_STIMPLAYLIST_PL), basedirs=[], paused_fallback=False, default_repeat=1) gen = chunker(stimplaylist.data_generator(), chunksize) recs = [] for i, chunk in enumerate(gen): if chunk is None: break recs.append({ 'sample_id': i, 'sample': (i + 1) * chunksize, 'producer_identifier': chunk.producer_identifier, 'producer_instance_n': chunk.producer_instance_n, 'producer_playlist_n': chunk.producer_playlist_n, 'chunk_n': chunk.chunk_n, 'mixed_producer': chunk.mixed_producer, 'mixed_start_offset': chunk.mixed_start_offset }) _path = tmpdir.join('%d.csv' % chunksize).strpath pd.DataFrame(recs).to_csv(_path, index=False) print(_path)
def check_chunker(test_gen, chunk_size): # Get a generator iterator data_gen_iter = test_gen() # Run the data iterator 10 times to get some data. Put it # all in one array. num_data_to_test = 10 test_data = next(data_gen_iter).data for i in range(num_data_to_test - 1): test_data = np.concatenate((test_data, next(data_gen_iter).data), axis=0) # Reset generator data_gen_iter = test_gen() # Make a chunk generator iterator out of it chunk_gen = chunker(data_gen_iter, chunk_size) # Now walk the chunk generator enough to get enough chunks to compare to # test data that we generated above num_chunks = int(math.ceil(float(test_data.shape[0]) / float(chunk_size))) chunk_test_data = next(chunk_gen).data for i in range(num_chunks - 1): chunk_test_data = np.concatenate( (chunk_test_data, next(chunk_gen).data), axis=0) # Truncate the data since the chunk size will probably not be a factor # of test data size. if test_data.ndim == 1: chunk_test_data = chunk_test_data[0:test_data.shape[0]] elif test_data.ndim == 2: chunk_test_data = chunk_test_data[0:test_data.shape[0], :] # Compare the data, make sure it is equal assert ((chunk_test_data == test_data).all())
def set_signal_producer(self, stim: SignalProducer): stim.initialize(BACKEND_DAQ) self._signal_producer = stim self._log.info('playing AudioStim object: %r' % stim) data_generator = stim.data_generator() with self._data_lock: chunked_gen = chunker(data_generator, chunk_size=self.num_samples_per_event) self._data_generator = chunked_gen
def play_signal_producer_item(self, item): if isinstance(self._signal_producer, AudioStimPlaylist): try: data_generator = self._signal_producer.play_item(item) self._log.info('playing playlist item identifier: %s' % item) with self._data_lock: chunked_gen = chunker( data_generator, chunk_size=self.num_samples_per_event) self._data_generator = chunked_gen except ValueError as _exc: self._log.warning('error playing playlist item: %s' % _exc)
def test_legacy_multi_channel_playlist(tmpdir): import os.path PATH = 'tests/test_data/opto_control_playlist.txt' stimList = AudioStimPlaylist.from_legacy_filename(PATH) gen = chunker(stimList.data_generator(), 1000) chunk = next(gen).data assert (chunk.shape[1] == 4) assert (stimList.num_channels == 4) with pytest.raises(IOError): with open(PATH, 'rt') as f: legacy_factory(f.readlines()[1:], basedirs=[tmpdir.strpath]) with open(PATH, 'rt') as f: stims = legacy_factory(f.readlines()[1:], basedirs=[os.path.dirname(PATH)]) stimList = AudioStimPlaylist(stims) gen = chunker(stimList.data_generator(), 1000) chunk = next(gen).data assert (chunk.shape[1] == 4) assert (stimList.num_channels == 4)
def test_single_stim_chunker_chunks(sinstim): assert sinstim.data.shape == (1200, ) gen = chunker(sinstim.data_generator(), 500) c0 = next(gen) assert c0.chunk_n == 0 assert c0.producer_identifier == sinstim.identifier assert c0.producer_instance_n == sinstim.producer_instance_n assert c0.mixed_producer is False assert c0.producer_playlist_n == -1 # not from playlist c1 = next(gen) assert c1.chunk_n == 1 assert c1.producer_identifier == sinstim.identifier assert c1.producer_instance_n == sinstim.producer_instance_n assert c1.mixed_producer is False assert c1.producer_playlist_n == -1 c2 = next(gen) assert c2.chunk_n == 2 assert c2.producer_identifier == sinstim.identifier assert c2.producer_instance_n == sinstim.producer_instance_n assert c2.mixed_producer is True assert c2.producer_playlist_n == -1 assert chunk_producers_differ(c1, c2) c3 = next(gen) assert c3.chunk_n == 3 assert c3.producer_identifier == sinstim.identifier assert c3.producer_instance_n == sinstim.producer_instance_n assert c3.mixed_producer is False assert c3.producer_playlist_n == -1 assert not chunk_producers_differ(c2, c3) c4 = next(gen) assert c4.chunk_n == 4 assert c4.producer_identifier == sinstim.identifier assert c4.producer_instance_n == sinstim.producer_instance_n assert c4.mixed_producer is True assert c4.producer_playlist_n == -1 ids = {_chunk_uid(c) for c in (c0, c1, c2, c3, c4)} assert len(ids) == 5, 'chunk uids not all different'
def data_generator(self, data_generator): """ Set the generator that will yield the next sample of data. :param data_generator: A generator instance. """ # If the stream has been setup and # If the generator the user is passing is None, then just set it. Otherwise, we need to set it but make sure # it is chunked up into the appropriate blocksize. if self._stream is not None: if data_generator is None: self._log.info('playing silence') self._data_generator = None else: self._data_generator = chunker( data_generator, chunk_size=self._stream.blocksize)
def test_stim_playlist_chunker_chunks_same_sized(stimplaylist): # extract the underlying identifiers and instance_n for the stimulus items that # will be played. stims_in_order = [ s for s in stimplaylist._iter_stims_with_randomization(True) ] assert [s.identifier for s in stims_in_order ] == ['constant1', 'sin10hz', 'constant1', 'sin10hz'] sconstantid, ssind = stims_in_order[0].identifier, stims_in_order[ 1].identifier sconstantn, ssinn = stims_in_order[0].producer_instance_n, stims_in_order[ 1].producer_instance_n assert sconstantid == 'constant1' assert ssind == 'sin10hz' # chunker is same size as stim, so no mixng, but still check # chunk_producers_differ does the right thing gen = chunker(stimplaylist.data_generator(), 1200) c0 = next(gen) assert c0.chunk_n == 0 assert c0.producer_identifier == sconstantid assert c0.producer_instance_n == sconstantn assert c0.mixed_producer is False assert chunk_producers_differ(None, c0) c1 = next(gen) assert c1.chunk_n == 1 assert c1.producer_identifier == ssind assert c1.producer_instance_n == ssinn assert c1.mixed_producer is False assert chunk_producers_differ(c0, c1) c2 = next(gen) assert c2.chunk_n == 2 assert c2.producer_identifier == sconstantid assert c2.producer_instance_n == sconstantn assert c2.mixed_producer is False assert chunk_producers_differ(c1, c2)
def test_stim_playlist_chunker_chunks_1000(stimplaylist): # extract the underlying identifiers and instance_n for the stimulus items that # will be played. stims_in_order = [ s for s in stimplaylist._iter_stims_with_randomization(True) ] assert [s.identifier for s in stims_in_order ] == ['constant1', 'sin10hz', 'constant1', 'sin10hz'] sconstantid, ssind = stims_in_order[0].identifier, stims_in_order[ 1].identifier sconstantn, ssinn = stims_in_order[0].producer_instance_n, stims_in_order[ 1].producer_instance_n assert sconstantid == 'constant1' assert ssind == 'sin10hz' # a chunk length of 1000 for stims of 1200 does not break evenly, so we get some mixed chunks gen = chunker(stimplaylist.data_generator(), 1000) c0 = next(gen) assert c0.chunk_n == 0 assert c0.producer_playlist_n == 0 assert c0.producer_identifier == sconstantid assert c0.producer_instance_n == sconstantn assert c0.mixed_producer is False assert chunk_producers_differ(None, c0) c1 = next(gen) assert c1.chunk_n == 1 assert c1.producer_playlist_n == 1 assert c1.producer_identifier == ssind assert c1.producer_instance_n == ssinn assert c1.mixed_producer is True assert c1.mixed_start_offset == 200 assert chunk_producers_differ(c0, c1) c2 = next(gen) assert c2.chunk_n == 2 assert c2.producer_playlist_n == 2 assert c2.producer_identifier == sconstantid # i.e. play the constant1 again assert c2.producer_instance_n == sconstantn assert c2.mixed_producer is True assert c2.mixed_start_offset == 400 assert chunk_producers_differ(c1, c2) c3 = next(gen) assert c3.chunk_n == 3 assert c3.producer_playlist_n == 3 assert c3.producer_identifier == ssind # i.e. play the sin10hz again assert c3.producer_instance_n == ssinn assert c3.mixed_producer is True assert c3.mixed_start_offset == 600 assert chunk_producers_differ(c2, c3) # because the chunk can't evenly fill the buffer size c4 = next(gen) assert c4 is None