Monday 8 March 2021

Measure total count and avg distance between points - python

I'm aiming to return the total count and average distance of points within a certain radius. Using below, the centre of radius is 2 and the centre is determined by X2, Y2.

There will always be one point that is located at the same position as X2, Y2. I'm hoping to disregard this point from the analysis.

Note: I'm hoping the function will be able to handle numerous points in time.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({        
    'Time' : [1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3],                               
    'Item' : ['A','B','C','D','E','F','A','B','C','D','E','F','A','B','C','D'],       
    'x' : [4,5,8,3,6,2,6,4,3.5,2,4,6,6,2,4,4],
    'y' : [-2,0,-2,0,0,4,-1,-2,-2,4,-3,2,-2,0,-2.5,4],     
    'x_ref' : [4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4],
    'y_ref' : [-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2],                          
    })

sq_dist = (df['X2'] - df['x']) ** 2 + (df['Y2'] -  df['y']) ** 2    

# count of points within radius
count = ((sq_dist <= 2 ** 2).astype(int)
                             .groupby([df['Time']])
                             .sum()
                             .reset_index()
                             .fillna(0) 

# avg_distance between points within radius
df['dist'] = np.sqrt((df['X2'] - df['x']) ** 2 + (df['Y2'] -  df['y']) ** 2)
inside = df[sq_dist <= 2 ** 2].copy()

avg_dist = (inside.groupby(['Time'])['dist']
                              .mean()
                              .reset_index()
                              .fillna(0) 
                             )

If I merge count and avg_dist the output should be:

   Time  count dist
0     1  0     0.0
1     2  2     0.75
2     3  1     0.5


from Measure total count and avg distance between points - python

No comments:

Post a Comment