I am looking for some help speeding up some code that I have written in Numpy. Here is the code:
def TimeChunks(timevalues, num):
avg = len(timevalues) / float(num)
out = []
last = 0.0
while last < len(timevalues):
out.append(timevalues[int(last):int(last + avg)])
last += avg
return out
### chunk i can be called by out[i] ###
NumChunks = 100000
t1chunks = TimeChunks(t1, NumChunks)
t2chunks = TimeChunks(t2, NumChunks)
NumofBins = 2000
CoincAllChunks = 0
for i in range(NumChunks):
CoincOneChunk = 0
Hist1, something1 = np.histogram(t1chunks[i], NumofBins)
Hist2, something2 = np.histogram(t2chunks[i], NumofBins)
Mask1 = (Hist1>0)
Mask2 = (Hist2>0)
MaskCoinc = Mask1*Mask2
CoincOneChunk = np.sum(MaskCoinc)
CoincAllChunks = CoincAllChunks + CoincOneChunk
Is there anything that can be done to improve this to make it more efficient for large arrays?
To explain the point of the code in a nutshell, the purpose of the code is simply to find the average "coincidences" between two NumPy arrays, representing time values of two channels (divided by some normalisation constant). This "coincidence" occurs when there is at least one time value in each of the two channels in a certain time interval.
For example:
t1 = [.4, .7, 1.1]
t2 = [0.8, .9, 1.5]
There is a coincidence in the window [0,1] and one coincidence in the interval [1, 2].
I want to find the average number of these "coincidences" when I break down my time array into a number of equally distributed bins. So for example if:
t1 = [.4, .7, 1.1, 2.1, 3, 3.3]
t2 = [0.8, .9, 1.5, 2.2, 3.1, 4]
And I want 4 bins, the intervals I'll consider are ([0,1], [1,2], [2,3], [3,4]). Therefore the total coincidences will be 4 (because there is a coincidence in each bin), and therefore the average coincidences will be 4.
This code is an attempt to do this for large time arrays for very small bin sizes, and as a result, to make it work I had to break down my time arrays into smaller chunks, and then for-loop through each of these chunks.
I've tried making this as vectorized as I can, but it still is very slow... Any ideas what can be done to speed it up further?
Any suggestions/hints will be appreciated. Thanks!.