Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add peak velocity and peak acceleration #253

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/reference/images/acceleration.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions docs/reference/images/velocity.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions docs/reference/statistics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,39 @@ Given pairs of :math:`(x, y)` cursor locations, the following statistics are cal

* Normalized Area
* (the area formed by paths) / (length of the paths)²

Velocity
--------

.. figure:: images/velocity.svg
:alt: Velocity is defined as the rate of change of position with respect to time

The cursor location at a timestamp is given by a pair of :math:`(x, y)` coordinates,
where :math:`(0, 0)` corresponds to the center of the screen, and 1 in these units is equal to the height of the screen.

Given pairs of :math:`(x, y)` cursor locations, and pairs of :math:`t` timestamp, the following statistics are calculated, all in units of screen height:
lkeegan marked this conversation as resolved.
Show resolved Hide resolved

* Velocity
* :math:`\frac{\sqrt{(x_{i+1}-x_i)^2+(y_{i+1}-y_i)^2}}{t_{i+1}-t_i}`
lkeegan marked this conversation as resolved.
Show resolved Hide resolved
* the rate of change of position with respect to time

* Peak Velocity
* maximum velocity

Acceleration
-----------

.. figure:: images/acceleration.png
lkeegan marked this conversation as resolved.
Show resolved Hide resolved
:alt: Acceleration is the rate of change of the velocity of an object with respect to time

The cursor location at a timestamp is given by a pair of :math:`(x, y)` coordinates,
where :math:`(0, 0)` corresponds to the center of the screen, and 1 in these units is equal to the height of the screen.

Given pairs of :math:`(x, y)` cursor locations, and pairs of :math:`t` timestamp, the following statistics are calculated, all in units of screen height:
lkeegan marked this conversation as resolved.
Show resolved Hide resolved

* Acceleration
* :math:`\frac{\sqrt{(\frac{x_{i+2}-x_{i+1}}{t_{i+2}-t_{i+1}}-\frac{x_{i+1}-x_{i}}{t_{i+1}-t_{i}})^2+(\frac{y_{i+2}-y_{i+1}}{t_{i+2}-t_{i+1}}-\frac{y_{i+1}-y`_{i}}{t_{i+1}-t_{i}})^2}}{t_{i+1}-t_i}`
lkeegan marked this conversation as resolved.
Show resolved Hide resolved
* the rate of change of the velocity of an object with respect to time

* Peak Acceleration
* maximum Acceleration
6 changes: 2 additions & 4 deletions src/vstt/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,13 +539,13 @@ def _peak_acceleration(mouse_times: np.ndarray, mouse_positions: np.ndarray) ->


def get_derivative(y: np.ndarray, x: np.ndarray) -> np.ndarray:
if x.size <= 1 or y.size <= 1:
return np.array([0])
dy_dx = np.diff(y) / np.diff(x)
return dy_dx


def get_velocity(mouse_times: np.ndarray, mouse_positions: np.ndarray) -> np.ndarray:
if mouse_times.size == 0 or mouse_positions.size == 0:
return np.array([0])
first_order_derivative = get_derivative(mouse_positions.transpose(), mouse_times)
velocity = LA.norm(first_order_derivative, axis=0)
return velocity
Expand All @@ -554,8 +554,6 @@ def get_velocity(mouse_times: np.ndarray, mouse_positions: np.ndarray) -> np.nda
def get_acceleration(
mouse_times: np.ndarray, mouse_positions: np.ndarray
) -> np.ndarray:
if mouse_times.size == 0 or mouse_positions.size == 0:
return np.array([0])
first_order_derivative = get_derivative(mouse_positions.transpose(), mouse_times)
second_order_derivative = get_derivative(first_order_derivative, mouse_times[:-1])
lkeegan marked this conversation as resolved.
Show resolved Hide resolved
acceleration = LA.norm(second_order_derivative, axis=0)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ def test_peak_velocity() -> None:
),
[10],
)
assert np.allclose(
vstt.stats._peak_velocity(np.array([0.5]), np.array([[0, 0]])),
[0],
)
assert np.allclose(
vstt.stats._peak_velocity(np.array([0.5]), np.array([])),
[0],
)
assert np.allclose(
vstt.stats._peak_velocity(np.array([]), np.array([[0, 0]])),
[0],
)


def test_peak_acceleration() -> None:
Expand All @@ -206,3 +218,15 @@ def test_peak_acceleration() -> None:
),
[103.07764],
)
assert np.allclose(
vstt.stats._peak_acceleration(np.array([0.5]), np.array([[0, 0]])),
[0],
)
assert np.allclose(
vstt.stats._peak_acceleration(np.array([0.5]), np.array([])),
[0],
)
assert np.allclose(
vstt.stats._peak_acceleration(np.array([]), np.array([[0, 0]])),
[0],
)