def setUp(self): self.asiga0 = AnalogSignalArray(np.array( [np.sin(np.arange(0, 20 * math.pi, 0.1))]).T, units='mV', sampling_rate=10 / ms) self.asiga1 = AnalogSignalArray(np.array([ np.sin(np.arange(0, 20 * math.pi, 0.1)), np.cos(np.arange(0, 20 * math.pi, 0.1)) ]).T, units='mV', sampling_rate=10 / ms) self.asiga2 = AnalogSignalArray(np.array([ np.sin(np.arange(0, 20 * math.pi, 0.1)), np.cos(np.arange(0, 20 * math.pi, 0.1)), np.tan(np.arange(0, 20 * math.pi, 0.1)) ]).T, units='mV', sampling_rate=10 / ms) self.st0 = SpikeTrain( [9 * math.pi, 10 * math.pi, 11 * math.pi, 12 * math.pi], units='ms', t_stop=self.asiga0.t_stop) self.lst = [ SpikeTrain([9 * math.pi, 10 * math.pi, 11 * math.pi, 12 * math.pi], units='ms', t_stop=self.asiga1.t_stop), SpikeTrain([30, 35, 40], units='ms', t_stop=self.asiga1.t_stop) ]
def test_one_spiketrain_empty(self): '''Test for one empty SpikeTrain, but existing spikes in other''' st = [SpikeTrain( [9 * math.pi, 10 * math.pi, 11 * math.pi, 12 * math.pi], units='ms', t_stop=self.asiga1.t_stop), SpikeTrain([], units='ms', t_stop=self.asiga1.t_stop)] STA = sta.spike_triggered_average(self.asiga1, st, (-1 * ms, 1 * ms)) cmp_array = AnalogSignalArray(np.array([np.zeros(20, dtype=float)]).T, units='mV', sampling_rate=10 / ms) cmp_array = cmp_array / 0. cmp_array.t_start = -1 * ms assert_array_equal(STA[:, 1], cmp_array[:, 0])
def setUp(self): # standard testsignals tlen0 = 100 * pq.s f0 = 20. * pq.Hz fs0 = 1 * pq.ms t0 = np.arange( 0, tlen0.rescale(pq.s).magnitude, fs0.rescale(pq.s).magnitude) * pq.s self.anasig0 = AnalogSignalArray( np.sin(2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) self.st0 = SpikeTrain( np.arange(0, tlen0.rescale(pq.ms).magnitude, 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst0 = BinnedSpikeTrain(self.st0, binsize=fs0) # shortened analogsignals self.anasig1 = self.anasig0.time_slice(1 * pq.s, None) self.anasig2 = self.anasig0.time_slice(None, 99 * pq.s) # increased sampling frequency fs1 = 0.1 * pq.ms self.anasig3 = AnalogSignalArray( np.sin(2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs1) self.bst1 = BinnedSpikeTrain( self.st0.time_slice(self.anasig3.t_start, self.anasig3.t_stop), binsize=fs1) # analogsignal containing multiple traces self.anasig4 = AnalogSignalArray( np.array([ np.sin(2 * np.pi * (f0 * t0).simplified.magnitude), np.sin(4 * np.pi * (f0 * t0).simplified.magnitude)]). transpose(), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) # shortened spike train self.st3 = SpikeTrain( np.arange( (tlen0.rescale(pq.ms).magnitude * .25), (tlen0.rescale(pq.ms).magnitude * .75), 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst3 = BinnedSpikeTrain(self.st3, binsize=fs0) self.st4 = SpikeTrain(np.arange( (tlen0.rescale(pq.ms).magnitude * .25), (tlen0.rescale(pq.ms).magnitude * .75), 50) * pq.ms, t_start=5 * fs0, t_stop=tlen0 - 5 * fs0) self.bst4 = BinnedSpikeTrain(self.st4, binsize=fs0) # spike train with incompatible binsize self.bst5 = BinnedSpikeTrain(self.st3, binsize=fs0 * 2.) # spike train with same binsize as the analog signal, but with # bin edges not aligned to the time axis of the analog signal self.bst6 = BinnedSpikeTrain( self.st3, binsize=fs0, t_start=4.5 * fs0, t_stop=tlen0 - 4.5 * fs0)
def test_all_spiketrains_empty(self): st = SpikeTrain([], units='ms', t_stop=self.asiga1.t_stop) with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger warnings. STA = sta.spike_triggered_average( self.asiga1, st, (-1 * ms, 1 * ms)) self.assertEqual("No spike at all was either found or used " "for averaging", str(w[-1].message)) nan_array = np.empty(20) nan_array.fill(np.nan) cmp_array = AnalogSignalArray(np.array([nan_array, nan_array]).T, units='mV', sampling_rate=10 / ms) assert_array_equal(STA, cmp_array)
def test_spike_triggered_average_with_n_spikes_on_constant_function(self): '''Signal should average to the input''' const = 13.8 x = const * np.ones(201) asiga = AnalogSignalArray( np.array([x]).T, units='mV', sampling_rate=10 / ms) st = SpikeTrain([3, 5.6, 7, 7.1, 16, 16.3], units='ms', t_stop=20) window_starttime = -2 * ms window_endtime = 2 * ms STA = sta.spike_triggered_average( asiga, st, (window_starttime, window_endtime)) a = int(((window_endtime - window_starttime) * asiga.sampling_rate).simplified) cutout = asiga[0: a] cutout.t_start = window_starttime assert_array_almost_equal(STA, cutout, 12)
def test_only_one_spike(self): '''The output should be the same as the input''' x = np.arange(0, 20, 0.1) y = x**2 sr = 10 / ms z = AnalogSignalArray(np.array([y]).T, units='mV', sampling_rate=sr) spiketime = 8 * ms spiketime_in_ms = int((spiketime / ms).simplified) st = SpikeTrain([spiketime_in_ms], units='ms', t_stop=20) window_starttime = -3 * ms window_endtime = 5 * ms STA = sta.spike_triggered_average( z, st, (window_starttime, window_endtime)) cutout = z[int(((spiketime + window_starttime) * sr).simplified): int(((spiketime + window_endtime) * sr).simplified)] cutout.t_start = window_starttime assert_array_equal(STA, cutout)
def setUp(self): # standard testsignals tlen0 = 100 * pq.s f0 = 20. * pq.Hz fs0 = 1 * pq.ms t0 = np.arange(0, tlen0.rescale(pq.s).magnitude, fs0.rescale(pq.s).magnitude) * pq.s self.anasig0 = AnalogSignalArray(np.sin( 2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) self.st0 = SpikeTrain(np.arange(0, tlen0.rescale(pq.ms).magnitude, 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst0 = BinnedSpikeTrain(self.st0, binsize=fs0) def test_old_scipy_version(self): self.assertRaises(AttributeError, sta.spike_field_coherence, self.anasig0, self.bst0)
class sfc_TestCase_new_scipy(unittest.TestCase): def setUp(self): # standard testsignals tlen0 = 100 * pq.s f0 = 20. * pq.Hz fs0 = 1 * pq.ms t0 = np.arange( 0, tlen0.rescale(pq.s).magnitude, fs0.rescale(pq.s).magnitude) * pq.s self.anasig0 = AnalogSignalArray( np.sin(2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) self.st0 = SpikeTrain( np.arange(0, tlen0.rescale(pq.ms).magnitude, 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst0 = BinnedSpikeTrain(self.st0, binsize=fs0) # shortened analogsignals self.anasig1 = self.anasig0.time_slice(1 * pq.s, None) self.anasig2 = self.anasig0.time_slice(None, 99 * pq.s) # increased sampling frequency fs1 = 0.1 * pq.ms self.anasig3 = AnalogSignalArray( np.sin(2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs1) self.bst1 = BinnedSpikeTrain( self.st0.time_slice(self.anasig3.t_start, self.anasig3.t_stop), binsize=fs1) # analogsignal containing multiple traces self.anasig4 = AnalogSignalArray( np.array([ np.sin(2 * np.pi * (f0 * t0).simplified.magnitude), np.sin(4 * np.pi * (f0 * t0).simplified.magnitude)]). transpose(), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) # shortened spike train self.st3 = SpikeTrain( np.arange( (tlen0.rescale(pq.ms).magnitude * .25), (tlen0.rescale(pq.ms).magnitude * .75), 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst3 = BinnedSpikeTrain(self.st3, binsize=fs0) self.st4 = SpikeTrain(np.arange( (tlen0.rescale(pq.ms).magnitude * .25), (tlen0.rescale(pq.ms).magnitude * .75), 50) * pq.ms, t_start=5 * fs0, t_stop=tlen0 - 5 * fs0) self.bst4 = BinnedSpikeTrain(self.st4, binsize=fs0) # spike train with incompatible binsize self.bst5 = BinnedSpikeTrain(self.st3, binsize=fs0 * 2.) # spike train with same binsize as the analog signal, but with # bin edges not aligned to the time axis of the analog signal self.bst6 = BinnedSpikeTrain( self.st3, binsize=fs0, t_start=4.5 * fs0, t_stop=tlen0 - 4.5 * fs0) # ========================================================================= # Tests for correct input handling # ========================================================================= def test_wrong_input_type(self): self.assertRaises(TypeError, sta.spike_field_coherence, np.array([1, 2, 3]), self.bst0) self.assertRaises(TypeError, sta.spike_field_coherence, self.anasig0, [1, 2, 3]) self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig0.duplicate_with_new_array([]), self.bst0) def test_start_stop_times_out_of_range(self): self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig1, self.bst0) self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig2, self.bst0) def test_non_matching_input_binning(self): self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig0, self.bst1) def test_incompatible_spiketrain_analogsignal(self): # These spike trains have incompatible binning (binsize or alignment to # time axis of analog signal) self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig0, self.bst5) self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig0, self.bst6) def test_signal_dimensions(self): # single analogsignal trace and single spike train s_single, f_single = sta.spike_field_coherence(self.anasig0, self.bst0) self.assertEqual(len(f_single.shape), 1) self.assertEqual(len(s_single.shape), 1) # multiple analogsignal traces and single spike train s_multi, f_multi = sta.spike_field_coherence(self.anasig4, self.bst0) self.assertEqual(len(f_multi.shape), 1) self.assertEqual(len(s_multi.shape), 2) # frequencies are identical since same sampling frequency was used # in both cases and data length is the same assert_array_equal(f_single, f_multi) # coherences of s_single and first signal in s_multi are identical, # since first analogsignal trace in anasig4 is same as in anasig0 assert_array_equal(s_single, s_multi[:, 0]) def test_non_binned_spiketrain_input(self): s, f = sta.spike_field_coherence(self.anasig0, self.st0) f_ind = np.where(f >= 19.)[0][0] max_ind = np.argmax(s[1:]) + 1 self.assertEqual(f_ind, max_ind) self.assertAlmostEqual(s[f_ind], 1., delta=0.01) # ========================================================================= # Tests for correct return values # ========================================================================= def test_spike_field_coherence_perfect_coherence(self): # check for detection of 20Hz peak in anasig0/bst0 s, f = sta.spike_field_coherence( self.anasig0, self.bst0, window='boxcar') f_ind = np.where(f >= 19.)[0][0] max_ind = np.argmax(s[1:]) + 1 self.assertEqual(f_ind, max_ind) self.assertAlmostEqual(s[f_ind], 1., delta=0.01) def test_output_frequencies(self): nfft = 256 _, f = sta.spike_field_coherence(self.anasig3, self.bst1, nfft=nfft) # check number of frequency samples self.assertEqual(len(f), nfft / 2 + 1) # check values of frequency samples assert_array_almost_equal( f, np.linspace( 0, self.anasig3.sampling_rate.rescale('Hz').magnitude / 2, nfft / 2 + 1) * pq.Hz) def test_short_spiketrain(self): # this spike train has the same length as anasig0 s1, f1 = sta.spike_field_coherence( self.anasig0, self.bst3, window='boxcar') # this spike train has the same spikes as above, but is shorter than # anasig0 s2, f2 = sta.spike_field_coherence( self.anasig0, self.bst4, window='boxcar') # the results above should be the same, nevertheless assert_array_equal(s1.magnitude, s2.magnitude) assert_array_equal(f1.magnitude, f2.magnitude)
def test_empty_analogsignal(self): asiga = AnalogSignalArray([], units='mV', sampling_rate=10 / ms) st = SpikeTrain([5], units='ms', t_stop=10) self.assertRaises(ValueError, sta.spike_triggered_average, asiga, st, (-1 * ms, 1 * ms))
def setUp(self): # standard testsignals tlen0 = 100 * pq.s f0 = 20. * pq.Hz fs0 = 1 * pq.ms t0 = np.arange(0, tlen0.rescale(pq.s).magnitude, fs0.rescale(pq.s).magnitude) * pq.s self.anasig0 = AnalogSignalArray(np.sin( 2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) self.st0 = SpikeTrain(np.arange(0, tlen0.rescale(pq.ms).magnitude, 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst0 = BinnedSpikeTrain(self.st0, binsize=fs0) # shortened analogsignals self.anasig1 = self.anasig0.time_slice(1 * pq.s, None) self.anasig2 = self.anasig0.time_slice(None, 99 * pq.s) # increased sampling frequency fs1 = 0.1 * pq.ms self.anasig3 = AnalogSignalArray(np.sin( 2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs1) self.bst1 = BinnedSpikeTrain(self.st0.time_slice( self.anasig3.t_start, self.anasig3.t_stop), binsize=fs1) # analogsignal containing multiple traces self.anasig4 = AnalogSignalArray(np.array([ np.sin(2 * np.pi * (f0 * t0).simplified.magnitude), np.sin(4 * np.pi * (f0 * t0).simplified.magnitude) ]).transpose(), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) # shortened spike train self.st3 = SpikeTrain(np.arange( (tlen0.rescale(pq.ms).magnitude * .25), (tlen0.rescale(pq.ms).magnitude * .75), 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst3 = BinnedSpikeTrain(self.st3, binsize=fs0) self.st4 = SpikeTrain(np.arange( (tlen0.rescale(pq.ms).magnitude * .25), (tlen0.rescale(pq.ms).magnitude * .75), 50) * pq.ms, t_start=5 * fs0, t_stop=tlen0 - 5 * fs0) self.bst4 = BinnedSpikeTrain(self.st4, binsize=fs0) # spike train with incompatible binsize self.bst5 = BinnedSpikeTrain(self.st3, binsize=fs0 * 2.) # spike train with same binsize as the analog signal, but with # bin edges not aligned to the time axis of the analog signal self.bst6 = BinnedSpikeTrain(self.st3, binsize=fs0, t_start=4.5 * fs0, t_stop=tlen0 - 4.5 * fs0)
class sfc_TestCase_new_scipy(unittest.TestCase): def setUp(self): # standard testsignals tlen0 = 100 * pq.s f0 = 20. * pq.Hz fs0 = 1 * pq.ms t0 = np.arange(0, tlen0.rescale(pq.s).magnitude, fs0.rescale(pq.s).magnitude) * pq.s self.anasig0 = AnalogSignalArray(np.sin( 2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) self.st0 = SpikeTrain(np.arange(0, tlen0.rescale(pq.ms).magnitude, 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst0 = BinnedSpikeTrain(self.st0, binsize=fs0) # shortened analogsignals self.anasig1 = self.anasig0.time_slice(1 * pq.s, None) self.anasig2 = self.anasig0.time_slice(None, 99 * pq.s) # increased sampling frequency fs1 = 0.1 * pq.ms self.anasig3 = AnalogSignalArray(np.sin( 2 * np.pi * (f0 * t0).simplified.magnitude), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs1) self.bst1 = BinnedSpikeTrain(self.st0.time_slice( self.anasig3.t_start, self.anasig3.t_stop), binsize=fs1) # analogsignal containing multiple traces self.anasig4 = AnalogSignalArray(np.array([ np.sin(2 * np.pi * (f0 * t0).simplified.magnitude), np.sin(4 * np.pi * (f0 * t0).simplified.magnitude) ]).transpose(), units=pq.mV, t_start=0 * pq.ms, sampling_period=fs0) # shortened spike train self.st3 = SpikeTrain(np.arange( (tlen0.rescale(pq.ms).magnitude * .25), (tlen0.rescale(pq.ms).magnitude * .75), 50) * pq.ms, t_start=0 * pq.ms, t_stop=tlen0) self.bst3 = BinnedSpikeTrain(self.st3, binsize=fs0) self.st4 = SpikeTrain(np.arange( (tlen0.rescale(pq.ms).magnitude * .25), (tlen0.rescale(pq.ms).magnitude * .75), 50) * pq.ms, t_start=5 * fs0, t_stop=tlen0 - 5 * fs0) self.bst4 = BinnedSpikeTrain(self.st4, binsize=fs0) # spike train with incompatible binsize self.bst5 = BinnedSpikeTrain(self.st3, binsize=fs0 * 2.) # spike train with same binsize as the analog signal, but with # bin edges not aligned to the time axis of the analog signal self.bst6 = BinnedSpikeTrain(self.st3, binsize=fs0, t_start=4.5 * fs0, t_stop=tlen0 - 4.5 * fs0) # ========================================================================= # Tests for correct input handling # ========================================================================= def test_wrong_input_type(self): self.assertRaises(TypeError, sta.spike_field_coherence, np.array([1, 2, 3]), self.bst0) self.assertRaises(TypeError, sta.spike_field_coherence, self.anasig0, [1, 2, 3]) self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig0.duplicate_with_new_array([]), self.bst0) def test_start_stop_times_out_of_range(self): self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig1, self.bst0) self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig2, self.bst0) def test_non_matching_input_binning(self): self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig0, self.bst1) def test_incompatible_spiketrain_analogsignal(self): # These spike trains have incompatible binning (binsize or alignment to # time axis of analog signal) self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig0, self.bst5) self.assertRaises(ValueError, sta.spike_field_coherence, self.anasig0, self.bst6) def test_signal_dimensions(self): # single analogsignal trace and single spike train s_single, f_single = sta.spike_field_coherence(self.anasig0, self.bst0) self.assertEqual(len(f_single.shape), 1) self.assertEqual(len(s_single.shape), 1) # multiple analogsignal traces and single spike train s_multi, f_multi = sta.spike_field_coherence(self.anasig4, self.bst0) self.assertEqual(len(f_multi.shape), 1) self.assertEqual(len(s_multi.shape), 2) # frequencies are identical since same sampling frequency was used # in both cases and data length is the same assert_array_equal(f_single, f_multi) # coherences of s_single and first signal in s_multi are identical, # since first analogsignal trace in anasig4 is same as in anasig0 assert_array_equal(s_single, s_multi[:, 0]) def test_non_binned_spiketrain_input(self): s, f = sta.spike_field_coherence(self.anasig0, self.st0) f_ind = np.where(f >= 19.)[0][0] max_ind = np.argmax(s[1:]) + 1 self.assertEqual(f_ind, max_ind) self.assertAlmostEqual(s[f_ind], 1., delta=0.01) # ========================================================================= # Tests for correct return values # ========================================================================= def test_spike_field_coherence_perfect_coherence(self): # check for detection of 20Hz peak in anasig0/bst0 s, f = sta.spike_field_coherence(self.anasig0, self.bst0, window='boxcar') f_ind = np.where(f >= 19.)[0][0] max_ind = np.argmax(s[1:]) + 1 self.assertEqual(f_ind, max_ind) self.assertAlmostEqual(s[f_ind], 1., delta=0.01) def test_output_frequencies(self): nfft = 256 _, f = sta.spike_field_coherence(self.anasig3, self.bst1, nfft=nfft) # check number of frequency samples self.assertEqual(len(f), nfft / 2 + 1) # check values of frequency samples assert_array_almost_equal( f, np.linspace(0, self.anasig3.sampling_rate.rescale('Hz').magnitude / 2, nfft / 2 + 1) * pq.Hz) def test_short_spiketrain(self): # this spike train has the same length as anasig0 s1, f1 = sta.spike_field_coherence(self.anasig0, self.bst3, window='boxcar') # this spike train has the same spikes as above, but is shorter than # anasig0 s2, f2 = sta.spike_field_coherence(self.anasig0, self.bst4, window='boxcar') # the results above should be the same, nevertheless assert_array_equal(s1.magnitude, s2.magnitude) assert_array_equal(f1.magnitude, f2.magnitude)