Calculating velocities and acceleration in python for large numpy arrays

时间:2016-02-12 20:11:12

标签: python arrays numpy pandas

I am working on a particle tracking code where my array is stored like this:

x_pos   y_pos    path#   slice#
1       10       1       1
2       11       1       2
3       12       1       3
5       2        2       1
7       4        2       2
9       6        2       3
11      8        2       4
...
2       6        N       100

where each path number corresponds to a trajectory of a single particle and the slice number is the image number where the particle is contained.

I would like to calculate the velocity and acceleration for each path (for both each segment of the path and then for the overall path). Currently, I am using (assuming my time step is 1):

vel_x = np.diff(data['x_Pos']) 
acc = np.diff(vel_x)

This works well, but I then have to go through the array and take out incorrect values (ie. where the path# changes). Does anyone know of a fast way to do this since my array has a length close to 1 million? Or, a better way to calculate velocities and accelerations?

Any advice or suggestions would be greatly appreciated since I am relatively new to python!

1 个答案:

答案 0 :(得分:2)

Try func Uint8toString(array []uint8) []string { stringNoBrackets := string(array[1:len(array)-1]) return strings.Split(stringNoBrackets,",") } :

np.where

result ("inf" is just to show the data is not valid):

vel_x = np.where(np.diff(data['path#'])==0, np.diff(data['x_Pos']), np.inf)