def test_constant_isi(self): ''' .. todo:: this will only work if dt = 2^x. For now it should be enough. ''' max_size = 1011 dt = 0.25 for train_size in range(2, max_size): senders = [0] * train_size times = np.arange(train_size, dtype=float) * dt sp = PopulationSpikes(1, senders, times) res = sp.isi(n=0) assert np.all(res[0] == dt)
def _create_test_sequence(train_size, n_trains): ''' Create a test sequence of ``n_trains`` spike trains with exactly ``train_size`` number of spikes. Spike times are random, but **time sorted**. ''' senders = np.repeat(np.arange(n_trains), train_size) np.random.shuffle(senders) times = np.random.rand(train_size * n_trains) times.sort() sp = PopulationSpikes(n_trains, senders, times) return senders, times, sp
def get_test_vector(self, nspikes): '''Generate test vector. Generates a test vector in which every time step spikes are generated according to ``nspikes``. All times are in units of ms, except firing rates, which are in Hz. ''' nspikes = np.asarray(nspikes) nneurons, nt = nspikes.shape spikes = np.array([], dtype=np.float) senders = np.array([], dtype=np.int) # Generate spikes and senders t = self.tstart for t_i in range(nt): for n_i in range(nneurons): sample = t + np.random.rand(nspikes[n_i, t_i]) * self.dt spikes = np.hstack((spikes, sample)) senders = np.hstack((senders, [n_i] * len(sample))) t += self.dt to_sort = np.array(list(zip(senders, spikes)), dtype=[('senders', 'f8'), ('spikes', 'f8')]) sorted_spikes = np.sort(to_sort, order='spikes') pop = PopulationSpikes(nneurons, sorted_spikes['senders'], sorted_spikes['spikes']) # Adjust nspikes to accomodate for winlen test_rates = np.array(nspikes, dtype=np.float, copy=True) ndt_winlen = int(self.winlen / self.dt) for t_i in range(nt): test_rates[:, t_i] = np.sum(nspikes[:, t_i:t_i + ndt_winlen], axis=1) / self.winlen tend = self.tstart + (nt - 1) * self.dt test_rates *= 1e3 # Adjust timing to Hz return self.PopulationRateTestItem(pop, nspikes, test_rates, tend)
def test_avg_firing_rate(self): '''Test computation of the average firing rate.''' # Test zero neurons pop = PopulationSpikes(0, [], []) rates = pop.avg_firing_rate(0, 1e3) assert rates.size == 0 # Test one neuron pop = PopulationSpikes(1, [0, 0, 0, 0], [0., 100, 200, 300]) rates = pop.avg_firing_rate(0, 1e3) assert len(rates.shape) == 1 assert rates[0] == 4. # Test two neurons pop = PopulationSpikes(2, [0, 1, 0, 1, 0, 1, 0], [0., 50, 100, 150, 200, 250, 300]) rates = pop.avg_firing_rate(0, 1e3) assert len(rates.shape) == 1 np.testing.assert_allclose(rates, [4., 3.]) # tend < tstart pop = PopulationSpikes(2, [0, 1, 0, 1, 0, 1, 0], [0., 50, 100, 150, 200, 250, 300]) with pytest.raises(ValueError): rates = pop.avg_firing_rate(1e3, 0)
def test_zero_n(self): PopulationSpikes(0, [], [])
def test_negative_n(self): with pytest.raises(ValueError): PopulationSpikes(-10, [], [])
def test_empty(self): for N in range(0, 4): pop = PopulationSpikes(N, [], []) r, rt = pop.sliding_firing_rate(0, 1, .1, .2) assert np.all(r == 0)
def test_lists(self): train_size = 100 n = 10 senders = list(np.random.randint(n, size=train_size * n)) times = list(np.random.rand(train_size * n)) sp = PopulationSpikes(n, senders, times) # try to retrieve spike trains for nIdx in range(n): train = sp[nIdx] assert train is not None # Try to run all the methods, None should raise an exception sp.avg_firing_rate(0, 1) sp.sliding_firing_rate(0, 1, 0.05, 0.1) sp.windowed((0, 1)) sp.raster_data() sp.spike_train_difference(list(range(n)))