Below is the code for the main engine of my program. This is where most of the time is taken up and where most memory problems occur.
To explain, my program looks at a sequence of say 120 images and initially identifies “stars” ie positions in each image that are brighter than their surrounds. These positions are put into lists, one for each image.
So the following subroutine takes 4 of these image lists in a row (not necessarily equally spaced in time). It makes an array out of the 1st and 4 lists, in other words it looks at every combination of stars in 1st image and stars in the 4th image and these combinations are the array. It then makes 2 other arrays where for each combination it calculates where theoretically a star should appear at the time of the 2nd image and the time of the 3rd image. It then compares those theoretically arrays with identified stars for the 2nd and 3rd images and returns true if there are “stars” at those positions in reality.
If the subroutine finds 4 stars in a straight line and moving at a constant speed (after taking into account the timing of the images) the the result is stored as a “sequence”.
I have used cupy in this which is the same as numpy but with the calculations done by the graphics card.
No doubt there are probably more efficient ways to do this but I was able to get decent speed boosts from vectorisation.
Two points to note.
-
The code if flexible so when comparing whether there is a real star where a theorised one is to be foudn I allow +/- 1 pixel>
-
When doing comparisons I divide the y positions value by 1000 and add to the x value so that I only need to compare one number not two.
cheers Peter
def seqfind(number,starttime,file1,file2,file3,file4,time1,time2,time3,time4,allsequence):
l1=len(file1)
l2=len(file2)
l3=len(file3)
l4=len(file4)
if l1==0 or l2==0 or l3==0 or l4 ==0:
return allsequence
perc= (time2-time1)/(time4-time1)
perc2= (time3-time1)/(time4-time1)
avetime=((time4-time1)/36)
e=len(file1)
f=len(file4)
first = np.zeros((l4,l1,2))
fourth = np.zeros((l1,l4,2))
first[:]=file1[:]
fourth[:]=file4[:]
first=first.transpose(1,0,2)
firstc= cp.asarray(first)
fourthc = cp.asarray(fourth)
p=fourthc-firstc
secondc=firstc+(p*perc)
thirdc=firstc+(p*perc2)
secondc =secondc.astype(int)
thirdc =thirdc.astype(int)
file2array=np.array(file2)
file2array=file2array.astype(float)
file2array1=file2array[:,0]+(file2array[:,1]/1000)
file2new=list(file2array1)
sc=secondc[:,:,0]
sc=sc.astype(float)
secondsplitc=sc+secondc[:,:,1]/1000
secondsplit1c=secondsplitc+0.001
secondsplit2c=secondsplitc-0.001
secondsplit3c=secondsplitc-1
secondsplit4c=secondsplitc-0.999
secondsplit5c=secondsplitc-1.001
secondsplit6c=secondsplitc+1
secondsplit7c=secondsplitc+1.001
secondsplit8c=secondsplitc+0.999
secondsplit = cp.asnumpy(secondsplitc)
secondsplit1 = cp.asnumpy(secondsplit1c)
secondsplit2 = cp.asnumpy(secondsplit2c)
secondsplit3 = cp.asnumpy(secondsplit3c)
secondsplit4 = cp.asnumpy(secondsplit4c)
secondsplit5 = cp.asnumpy(secondsplit5c)
secondsplit6 = cp.asnumpy(secondsplit6c)
secondsplit7 = cp.asnumpy(secondsplit7c)
secondsplit8 = cp.asnumpy(secondsplit8c)
s1=np.isin(secondsplit,file2new)
s2=np.isin(secondsplit1,file2new)
s3=np.isin(secondsplit2,file2new)
s4=np.isin(secondsplit3,file2new)
s5=np.isin(secondsplit4,file2new)
s6=np.isin(secondsplit5,file2new)
s7=np.isin(secondsplit6,file2new)
s8=np.isin(secondsplit7,file2new)
s9=np.isin(secondsplit8,file2new)
sall=np.logical_or.reduce((s1,s2,s3,s4,s5,s6,s7,s8,s9))
file3array=np.array(file3)
file3array=file3array.astype(float)
file3array1=file3array[:,0]+(file3array[:,1]/1000)
file3new=list(file3array1)
tc=thirdc[:,:,0]
tc=tc.astype(float)
thirdsplitc=tc+thirdc[:,:,1]/1000
thirdsplit1c=thirdsplitc+0.001
thirdsplit2c=thirdsplitc-0.001
thirdsplit3c=thirdsplitc-1
thirdsplit4c=thirdsplitc-0.999
thirdsplit5c=thirdsplitc-1.001
thirdsplit6c=thirdsplitc+1
thirdsplit7c=thirdsplitc+1.001
thirdsplit8c=thirdsplitc+0.999
thirdsplit = cp.asnumpy(thirdsplitc)
thirdsplit1 = cp.asnumpy(thirdsplit1c)
thirdsplit2 = cp.asnumpy(thirdsplit2c)
thirdsplit3 = cp.asnumpy(thirdsplit3c)
thirdsplit4 = cp.asnumpy(thirdsplit4c)
thirdsplit5 = cp.asnumpy(thirdsplit5c)
thirdsplit6 = cp.asnumpy(thirdsplit6c)
thirdsplit7 = cp.asnumpy(thirdsplit7c)
thirdsplit8 = cp.asnumpy(thirdsplit8c)
t1=np.isin(thirdsplit,file3new)
t2=np.isin(thirdsplit1,file3new)
t3=np.isin(thirdsplit2,file3new)
t4=np.isin(thirdsplit3,file3new)
t5=np.isin(thirdsplit4,file3new)
t6=np.isin(thirdsplit5,file3new)
t7=np.isin(thirdsplit6,file3new)
t8=np.isin(thirdsplit7,file3new)
t9=np.isin(thirdsplit8,file3new)
tall=np.logical_or.reduce((t1,t2,t3,t4,t5,t6,t7,t8,t9))
valid=(np.logical_and(sall,tall))
items=np.where(valid==True)
matrixpos=list(zip(items[0],items[1]))
p=len(matrixpos)
first=first.astype(int)
fourth=fourth.astype(int)
second = cp.asnumpy(secondc)
third = cp.asnumpy(thirdc)
storage(first,second,third,fourth,matrixpos,avetime,allsequence,p)
return allsequence