def test_preprocess_spike_train(self): stimulus = arange(20).T.reshape(-1, 2).T spike_train = arange(10).reshape(1, -1) spike_history = 3 stimulus_history = 2 stimuli = extract_windows(stimulus, stimulus_history) spikes = extract_windows(spike_train, spike_history) stimuli = stimuli[:, -spikes.shape[1]:] spikes = spikes[:, -stimuli.shape[1]:] self.assertEqual(stimuli.shape[0], stimulus.shape[0] * stimulus_history) self.assertEqual( stimuli.shape[1], stimulus.shape[1] - max([stimulus_history, spike_history]) + 1) self.assertEqual(spikes.shape[0], spike_train.shape[0] * spike_history) self.assertEqual( spikes.shape[1], spike_train.shape[1] - max([stimulus_history, spike_history]) + 1) self.assertLess( max(abs(spikes[:, -1] - spike_train[0, -spike_history:])), 1e-8) self.assertLess( max(abs(stimuli[:, -1] - stimulus[:, -stimulus_history:].T.ravel())), 1e-8)
def generate_data_from_spike_train(stimulus, stimulus_history, spike_train=None, spike_history=0): """ Extracts windows from a stimulus time-series and a spike train. @type stimulus: C{ndarray} @param stimulus: time-series (NxT) where the second dimension is time @type stimulus_history: C{int} @param stimulus_history: length of extracted stimulus windows @type spike_train: C{ndarray} @param spike_train: spikes corresponding to stimulus (1xT) @type spike_history: C{int} @param spike_history: length of extracted spike windows @rtype: C{tuple}/C{ndarray} @return: stimulus windows, spike histories, and spikes """ if stimulus.ndim == 1: stimulus = stimulus.reshape(1, -1) if spike_train is None: return extract_windows(stimulus, stimulus_history) if spike_train.ndim == 1: spike_train = spike_train.reshape(1, -1) if stimulus.shape[1] != spike_train.shape[1]: raise ValueError('Stimulus and spike train should have the same length.') # extract stimulus and spike history windows spikes = extract_windows(spike_train, spike_history + 1) stimuli = extract_windows(stimulus, stimulus_history) # make sure stimuli and spikes are aligned spikes = spikes[:, -stimuli.shape[1]:] stimuli = stimuli[:, -spikes.shape[1]:] # separate last spike of each window outputs = spikes[[-1]] spikes = spikes[:-1] if spike_history > 0: return stimuli, spikes, outputs else: return stimuli, outputs
def test_preprocess_spike_train(self): stimulus = arange(20).T.reshape(-1, 2).T spike_train = arange(10).reshape(1, -1) spike_history = 3 stimulus_history = 2 stimuli = extract_windows(stimulus, stimulus_history) spikes = extract_windows(spike_train, spike_history) stimuli = stimuli[:, -spikes.shape[1]:] spikes = spikes[:, -stimuli.shape[1]:] self.assertEqual(stimuli.shape[0], stimulus.shape[0] * stimulus_history) self.assertEqual(stimuli.shape[1], stimulus.shape[1] - max([stimulus_history, spike_history]) + 1) self.assertEqual(spikes.shape[0], spike_train.shape[0] * spike_history) self.assertEqual(spikes.shape[1], spike_train.shape[1] - max([stimulus_history, spike_history]) + 1) self.assertLess(max(abs(spikes[:, -1] - spike_train[0, -spike_history:])), 1e-8) self.assertLess(max(abs(stimuli[:, -1] - stimulus[:, -stimulus_history:].T.ravel())), 1e-8)