1

I have a 3D array (8000,12000,2), the dimensions corresponds to 8000 frames, 12000 particles, 2 -> (x,y) position of each particle. I want to apply a 2D grid boxes on each frame so that I can count the number of particles in each box. I have done that, but I feel I can speed it up.

The code below works fine on 1 frame, but I have to do it for all frames and for different files, which makes things slow.

#Lx,Ly are my box dimensions, all particles positions are between
# -Lx/2,Lx/2 and -Ly/2,Ly/2
# Ly = Lx*3
ratio = 6 # control number of boxes
lx = Lx/ratio
ly = Ly/ratio/3
boxes = np.zeros((int(Ly/ly),int(Lx/lx),2)) 
for i in range(boxes.shape[0]):
    for j in range(boxes.shape[1]):
        x_l = -Lx/2 + lx*j
        y_l = -Ly/2 + ly*i
        x_h = x_l+lx
        y_h = y_l+ly
        temp_cord = coord [(x_l<= coord[:,0]) & (coord[:,0] <=x_h) & (y_l<= coord[:,1]) & (coord[:,1] <=y_h)]
        if ((temp_cord[:,2]).sum() - temp_cord[:,2].shape[0])/temp_cord[:,2].shape[0] < .1 :
              boxes[i,j,0] = 1 #set box type
              boxes[i,j,1] = temp_cord.shape[0]
Coldz
  • 33
  • 5

0 Answers0