Beispiel #1
0
 def test_usage_of_spikes(self):
     st = SpikeTrain([16.5 * math.pi, 17.5 * math.pi, 
         18.5 * math.pi, 19.5 * math.pi], units='ms', t_stop=20 * math.pi)
     STA = sta.spike_triggered_average(
         self.asiga0, st, (-math.pi * ms, math.pi * ms))
     self.assertEqual(STA.annotations['used_spikes'], 3)
     self.assertEqual(STA.annotations['unused_spikes'], 1)
Beispiel #2
0
 def test_usage_of_spikes(self):
     st = SpikeTrain([16.5 * math.pi, 17.5 * math.pi,
         18.5 * math.pi, 19.5 * math.pi], units='ms', t_stop=20 * math.pi)
     STA = sta.spike_triggered_average(
         self.asiga0, st, (-math.pi * ms, math.pi * ms))
     self.assertEqual(STA.annotations['used_spikes'], 3)
     self.assertEqual(STA.annotations['unused_spikes'], 1)
Beispiel #3
0
 def test_spike_triggered_average_with_shifted_sin_wave(self):
     """Signal should average to zero"""
     STA = sta.spike_triggered_average(
         self.asiga0, self.st0, (-4 * ms, 4 * ms))
     target = 5e-2 * mV
     self.assertEqual(np.abs(STA).max().dimensionality.simplified,
                      pq.Quantity(1, "V").dimensionality.simplified)
     self.assertLess(np.abs(STA).max(), target)
Beispiel #4
0
 def test_spike_triggered_average_with_shifted_sin_wave(self):
     '''Signal should average to zero'''
     STA = sta.spike_triggered_average(
         self.asiga0, self.st0, (-4 * ms, 4 * ms))
     target = 5e-2 * mV
     self.assertEqual(np.abs(STA).max().dimensionality.simplified, 
                      pq.Quantity(1, "V").dimensionality.simplified)
     self.assertLess(np.abs(STA).max(), target)
Beispiel #5
0
 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 = AnalogSignal(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.magnitude[:, 1], cmp_array.magnitude[:, 0])
Beispiel #6
0
 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])
Beispiel #7
0
 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 = AnalogSignal(np.array([nan_array, nan_array]).T,
                                  units='mV', sampling_rate=10 / ms)
         assert_array_equal(STA.magnitude, cmp_array.magnitude)
Beispiel #8
0
 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 = AnalogSignal(np.array([nan_array, nan_array]).T,
             units='mV', sampling_rate=10 / ms)
         assert_array_equal(STA.magnitude, cmp_array.magnitude)
Beispiel #9
0
 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 = AnalogSignal(
         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)
Beispiel #10
0
 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)]
     with warnings.catch_warnings():
         warnings.simplefilter("ignore")
         """
         Ignore the RuntimeWarning: invalid value encountered in true_divide
         new_signal = f(other, *args) for the empty SpikeTrain.
         """
         STA = sta.spike_triggered_average(self.asiga1,
                                           spiketrains=st,
                                           window=(-1 * ms, 1 * ms))
     assert np.isnan(STA.magnitude[:, 1]).all()
Beispiel #11
0
 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 = AnalogSignal(
         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)
Beispiel #12
0
 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 = AnalogSignal(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)
Beispiel #13
0
 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 = AnalogSignal(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)
Beispiel #14
0
                        t_start=650.0*pq.ms)
    wns_list.append(wns2)

#%% Set up STA excluding spikes in first 500 ms
os.chdir('/home/ngg1/[email protected]/Data_Urban/NEURON/Analysis/'
         'AlmogAndKorngreen2014/ModCell_5_thrdsafe/figures')
for item in g_list:
    plt.figure()
    plt.xlabel('Time Before Spike (ms)')
    plt.ylabel('Current (nA)')
    plt.title(item+' Channel STA')
    for key in channels[item]:
        sta_ind = pd.DataFrame()                #starting time column to build on
        for i in range(50):                     #adding each columb of sta to df
            sta_i = spike_triggered_average(wns_list[i],
                                            channels[item][key]['neoSpkTrain500'][i],
                                            (-40*pq.ms,5.1*pq.ms))
            sta_i_df = pd.DataFrame(sta_i.as_array().flatten())
            sta_ind = pd.concat([sta_ind,sta_i_df],axis=1)
        sta_avg = sta_ind.apply(np.mean,axis=1) #average each row across columns
        t_sta = np.arange(-40,5.1,0.1)         #time from -50 to 10 ms inclusive by 0.1
        t_df = pd.DataFrame(t_sta)              #make a time data frame
        channels[item][key]['STA'] = sta_avg
        plt.plot(t_df,sta_avg,label='g'+item+'_bar*'+key) #easier to call as separate x,y
        sta_i = None
        sta_i_df = None
        sta_ind = None
        sta_avg = None
    plt.legend()
    #plt.show()
#    plt.savefig(item+'_sta.png',dpi=600,format='png')
Beispiel #15
0
        wns = item.iloc[:, i]
        wns2 = AnalogSignal(signal=wns.values * pq.nA,
                            sampling_rate=10 * pq.kHz,
                            t_start=0 * pq.ms)
        wns_list.append(wns2)
    wns_list_list.append(wns_list)
#%%
#stavg = spike_triggered_average(wns_list[0],st_list[0],(-50*pq.ms,10.1*pq.ms))

#Set up a for loop to do an STA for all trials
sta_list = []
for k in range(16):
    sta_ind = pd.DataFrame()  #starting time column to build on
    for i in range(50):  #adding each columb of sta to df
        sta_i = spike_triggered_average(wns_list_list[k][i],
                                        spktrain_list[k][i],
                                        (-50 * pq.ms, 10.1 * pq.ms))
        sta_i_df = pd.DataFrame(sta_i.as_array().flatten())
        sta_ind = pd.concat([sta_ind, sta_i_df], axis=1)
    sta_avg = sta_ind.apply(np.mean, axis=1)  #average each row across columns
    t_sta = np.arange(-50, 10.1, 0.1)  #time from -50 to 10 ms inclusive by 0.1
    t_df = pd.DataFrame(t_sta)  #make a time data frame
    sta_list.append(sta_avg)
    plt.figure()
    plt.plot(t_df, sta_avg)  #easier to call as separate x,y
#%%%
st_df_list = []
for item in binnedst_list:
    st_df = pd.DataFrame(item.to_array()).T
    st_df_list.append(st_df)
Beispiel #16
0
additional_inj0s = pd.DataFrame(
    np.zeros(5000))  # making noise file match length of voltage file
additional_injtime = pd.DataFrame(np.arange(35000, 40000, 1))
inj_vmdf = pd.concat([inj_vmdf, additional_inj0s], axis=0)
inj_vmdf.index = np.arange(0, 40000)
os.chdir('../..')

wns = inj_vmdf.iloc[7000:32000, :]
wns2 = AnalogSignal(signal=wns.values * pq.nA,
                    sampling_rate=10 * pq.kHz,
                    t_start=700.0 * pq.ms)

sta_ind = pd.DataFrame()  # starting time column to build on
for i in range(
        len(del_half_neospiketrain_list)):  # adding each columb of sta to df
    sta_i = spike_triggered_average(wns2, del_half_neospiketrain_list[i],
                                    (-50 * pq.ms, 10.1 * pq.ms))
    sta_i_df = pd.DataFrame(sta_i.as_array().flatten())
    sta_ind = pd.concat([sta_ind, sta_i_df], axis=1)
sta_avg = sta_ind.apply(np.mean, axis=1)  # average each row across columns
t_sta = np.arange(-50, 10.1, 0.1)  # time from -50 to 10 ms inclusive by 0.1
#sta_avg.index = t_sta
plt.figure()
plt.plot(t_sta, sta_avg)  # easier to call as separate x,y
plt.xlabel('Time Before Spike (ms)')
plt.ylabel('Current (pA)')
plt.title('STA')
plt.show()
os.chdir('Analysis/STA')
plt.savefig('STA_' + file_name + '.png', bbox_inches='tight')
plt.close()
os.chdir('../..')