I need to create a retangular matrix from timeseries data which is T time bins by L, the sum of the length of the set of events of interest occuring in the timeseries within certain times refered to as trials. So in this example my time bins are 0.1 seconds long I have two sets of event one cosistin of events lasting 0.3 seconds another consisting of events lasting 0.5 seconds so a matrix of a 1 second long trial would be 10 by 8. The matrix needs to be zeros everywhere except at time when an event occurs in which case a diagonal of ones covering the length of that event needs to be placed. My issue is kinda like have to insert identity matrices within a larger rectangular matrix of zeros.
I have already created a solution to this issue but I want to see how I could have done it better. My solution is far too long to post here so instead I will provide an example that captures all the key issues. For instance lets say we have a 1 second long trial with an event a which last 0.3s occurring at 0.1 seconds and an even b lasting 0.5s occurring at 0.2 second with a 0.1s bin the matrix would look like this...
#example predictor matrix
array([[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 1., 0., 1., 0.],
[0., 0., 0., 1., 0., 1., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]])
So as you can see the dimension L comes from the length of the duration of the events, giving them space in the matrix to travel down. Here is some code to represent the basic problem I am facing. Each of these diagonal is a Kernel.
event_A = np.array([[0.2, 0.5], [3.4,3.7], [4.0,4.3], [5.2, 5.5], [6.4, 6.7]])
event_B = np.array([[0.8, 1.3], [3.3, 3.8], [6.0, 6.5], [7.1, 7.6] ])
bin = 0.1 # 0.1 second time bins, 10Hz sampling frequency
#length of the events in time bins
# events A last 0.3s each so...
event_a_len = int(0.3/bin)
# and events B last 0.5s each...
event_b_len = int(0.5/bin)
# you will notice one event A occurs outside of the intervals
# its ok if in your solution
trial_intervals = np.array([[0.15, 5.15], [5.73,10.73]])
This is what the matrix for a single trial should start out like before adding the ones. For the first trial...
begin = trial_intervals[0,0]
end = trial_intervals[0,1]
mat = np.zeros([(end-begin)/bin, event_a_len+event_b_len])
And we would need to add ones along diagonals for the events occurring at...
A = event_A[(event_A[:,1]>begin)&(event_A[:,0]<end)]
B = event_B[(event_B[:,1]>begin)&(event_B[:,0]<end)]
In my solution I created each matrix for each trial separately but now I am not sure this was the best solution. Also feel like using pandas datetime may have made things easier. I feel like there was a solution that could be accomplished in just a few lines of code instead of the like 50 + lines it took me. if youa re interested this paper is where the issue came from: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6913580/#S7title