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.
if l1==0 or l2==0 or l3==0 or l4 ==0:
first = np.zeros((l4,l1,2))
fourth = np.zeros((l1,l4,2))
fourthc = cp.asarray(fourth)
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)
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)
second = cp.asnumpy(secondc)
third = cp.asnumpy(thirdc)