I want to create a spike train with multiple trains on the y axis and this is the task: Plot of spike trains segmented into trials A more sophisticated way of illustrating neuronal responses to stimulus presentation is to plot, separately for each unit, segments of spike trains aligned to the timing of stimulus presentation. To draw this type of dot display, you need to cut out time-aligned segments from the original spike train. A convenient way to do such segmentation in NumPy is to use the advanced indexing as follows:
ts_spk = spikes[0] # spike train of the 0-th unit
t_on = stim_on[0][0] # the 0-th onset time of stimulus 0
ts_spk_trial = ts_spk[(t_on - 0.2 < ts_spk) & (ts_spk < t_on + 0.3)] - t_on
The indexing ts_spk[(t_on - 0.2 < ts_spk) & (ts_spk < t_on + 0.3)] in the last line picks up the elements of ts_spk that satisfy the conditions given in the square brackets. The conditions here are whether a spike time (i.e. an element of ts_spk) is between -0.2 sec (i.e. trial start) and 0.3 sec (i.e. trial end) from t_on (i.e. a stimulus onset). Note that t_on is subtracted from the spike times, so that ts_spk_trial stores the spike times relative to the stimulus-onset time.
Let’s use this indexing to implement a function, say segment_spike_train(), that cuts out segments from a given spike train around a given set of stimulus-onset times.'''
Taks 1c: complete the function segment_spike_train() below
def segment_spike_train(ts_spk, trig, t_pre, t_post):
Segment a given spike train around a given set of trigger times.
Arguments
---------
ts_spk : a list or array containing spike times
trig : a list or array containing trigger times for segmentation
t_pre : start time of a segment relative to trigger time
t_post : end time of a segment relative to trigger time
Returns
-------
segments : a list containing segments cut out from the spike train
around the trigger times
segments = []
for t_trig in trig:
t_on = stim_on[0][0]
ts_spk = ts_spk[(t_on + t_pre < ts_spk) & (ts_spk < t_on + t_post)] - t_on
segments.append(ts_spk)
return segments
# set parameter values
t_start = -0.2
t_end = 0.3
unitID = 0
# cut out spike train segments and draw a dot display for each stimulus type
for stimID in range(len(stim_on)):
# cut out spike train segments
spikes_trial = segment_spike_train(spikes[unitID],stim_on[unitID], t_start, t_end)
# give appropriate arguments
# plot spike train segments
plt.subplot(2, 2, stimID + 1)
dotdisplay(spikes_trial) # give an appropriate argument
# decorate the plot
plt.grid()
plt.xlabel("Time from stimulus onset (s)")
plt.ylabel("Trial index")
plt.title("Stimulus Type {0}".format(stimID))
plt.suptitle("Dot display: unit {0}".format(unitID))'''
Data format Data are saved in .npy format (i.e. the standard file format to save numpy arrays) spikes.npy contains spike times (in the unit of second) of 20 simultaneously-recorded neurons stim_on.npy contains the time stamps (in the unit of second) of the onsets of stimulus presentations, separately for each of the 4 stimulus types
I get only one train with this code at the Trial Index 0. I don't know how I can get multiple trains. Can someone help?