Skip to content

Commit

Permalink
improved performance and deleted commented lines (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alek050 authored Aug 7, 2023
1 parent 21f8e0e commit c3a8944
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions databallpy/features/velocity.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import pandas as pd


Expand All @@ -13,21 +15,17 @@ def get_velocity(
Returns:
pd.DataFrame: tracking data with the added velocity columns
"""
# for input_column in input_columns:
# output_column = input_column + "_v"
# v = df[input_column].diff() / (1 / framerate)
# df[output_column] = v

velocities = []
for input_column in input_columns:
v = df[input_column].diff() / (1 / framerate)
velocities.append(v)
velocity_df = pd.concat(velocities, axis=1)
velocity_df.columns = [f"{col}_v" for col in input_columns]
df = pd.concat([df, velocity_df], axis=1)

return df
Notes:
- Pandas PerformanceWarning is ignored. It suggests using df.copy() to
defragment the memory. However, this is not possible in this case, since
the dataframe is quite big, taking up a uneccessary memory, and the
df.copy makes the function about twice as slow.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=pd.errors.PerformanceWarning)
for input_column in input_columns:
velocity_column = f"{input_column}_v"
df[velocity_column] = df[input_column].diff() * framerate

return df

0 comments on commit c3a8944

Please sign in to comment.