Skip to content

Commit

Permalink
Merge pull request #95 from yupidevs/develop
Browse files Browse the repository at this point in the history
Upgrade velocity estimation methods
  • Loading branch information
jmorgadov authored Mar 22, 2022
2 parents 08dace1 + 9ecc5ea commit 95d8982
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 245 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
author = 'Gustavo Viera-López, Alfredo Reyes, Jorge Morgado, Ernesto Altshuler'

# The full version, including alpha/beta/rc tags
release = '0.8.7'
release = '0.8.8'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/example3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ The temporal evolution of the efficiency can be plotted by:
.. code-block:: python
import matplotlib.pyplot as plt
plt.plot(wheel.t[1:], eff)
plt.plot(wheel.t, eff)
plt.xlabel('time [s]')
plt.ylabel('efficiency')
plt.show()
Expand Down
Binary file modified docs/source/images/example3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/source/images/example6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "yupi"
version = "0.8.7"
version = "0.8.8"
description = "A package for tracking and analysing objects trajectories"
authors = [
"Gustavo Viera-López <[email protected]>",
Expand Down
Binary file removed resources/templates/ant_small.png
Binary file not shown.
Binary file removed resources/templates/pivot.png
Binary file not shown.
30 changes: 25 additions & 5 deletions tests/test_stats/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import pytest
from yupi import Trajectory, VelMethod
from yupi import Trajectory, VelocityMethod, WindowType
from yupi.stats import *

APPROX_REL_TOLERANCE = 1e-10
Expand All @@ -9,19 +9,39 @@
@pytest.fixture
def traj():
points = [[0, 0], [1, 0], [1, 1], [2, 1]]
return Trajectory(points=points, vel_est={"method": VelMethod.FORWARD})
return Trajectory(
points=points,
vel_est={
"method": VelocityMethod.LINEAR_DIFF,
"window_type": WindowType.FORWARD,
},
)


@pytest.fixture
def traj1():
x = [0, 8, 5, 11]
return Trajectory(x, dt=2, vel_est={"method": VelMethod.FORWARD})
return Trajectory(
x=x,
dt=2,
vel_est={
"method": VelocityMethod.LINEAR_DIFF,
"window_type": WindowType.FORWARD,
},
)


@pytest.fixture
def traj2():
x = [0, 8.5, 4.9, 10.5]
return Trajectory(x, dt=2, vel_est={"method": VelMethod.FORWARD})
return Trajectory(
x=x,
dt=2,
vel_est={
"method": VelocityMethod.LINEAR_DIFF,
"window_type": WindowType.FORWARD,
},
)


def test_turning_angles(traj):
Expand Down Expand Up @@ -75,7 +95,7 @@ def test_psd(traj1):
psd_o = psd([traj1], lag=lag, omega=True)
assert psd_o[0] == pytest.approx([8.5, 6.5])
assert psd_o[1] == pytest.approx([0, 0])
assert psd_o[2] == pytest.approx([-9.8696044, 0.])
assert psd_o[2] == pytest.approx([-9.8696044, 0.0])


def test_checkers():
Expand Down
40 changes: 14 additions & 26 deletions tests/test_trajectory/test_creation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from yupi import Trajectory, VelMethod, VelPadding
from yupi import Trajectory, VelocityMethod, WindowType

APPROX_REL_TOLERANCE = 1e-10

Expand Down Expand Up @@ -88,45 +88,33 @@ def test_creation_general():
def test_velocity_estimation_methods():
x = [1, 2, 4, 8, 16]

Trajectory.global_vel_method(VelMethod.FORWARD)
Trajectory.global_vel_method(VelocityMethod.LINEAR_DIFF, WindowType.FORWARD)
traj = Trajectory(x=x)

assert traj.v == pytest.approx([1, 2, 4, 8, 8], rel=APPROX_REL_TOLERANCE)

Trajectory.global_vel_method(VelMethod.CENTERED)
traj.set_vel_method(VelMethod.BACKWARD)
Trajectory.global_vel_method(VelocityMethod.LINEAR_DIFF)
traj.set_vel_method(VelocityMethod.LINEAR_DIFF, WindowType.BACKWARD)

assert traj.v == pytest.approx([1, 1, 2, 4, 8], rel=APPROX_REL_TOLERANCE)

traj = Trajectory(x=x)

assert traj.v == pytest.approx([3 / 2, 3 / 2, 3, 6, 6], rel=APPROX_REL_TOLERANCE)

traj = Trajectory(x=x, vel_est={"method": VelMethod.FORWARD, "h": 2})
vel_est = {
"method": VelocityMethod.FORNBERG_DIFF,
"window_type": WindowType.CENTRAL,
"accuracy": 2,
}

assert traj.v == pytest.approx([3 / 2, 3, 6, 6, 6], rel=APPROX_REL_TOLERANCE)
traj = Trajectory(x=x, vel_est=vel_est)

traj = Trajectory(
x=x,
vel_est={
"method": VelMethod.BACKWARD,
"h": 2,
"padding": VelPadding.VALUE,
"padding_val": 0,
},
)

assert traj.v == pytest.approx([0, 0, 3 / 2, 3, 6], rel=APPROX_REL_TOLERANCE)

traj.set_vel_method(VelMethod.CENTERED, h=2)

assert traj.v == pytest.approx(
[15 / 4, 15 / 4, 15 / 4, 15 / 4, 15 / 4], rel=APPROX_REL_TOLERANCE
)
vel_est["accuracy"] = 3

with pytest.raises(ValueError):
traj.set_vel_method(VelMethod.CENTERED, h=0)
traj.set_vel_method(**vel_est)

with pytest.raises(ValueError):
traj.set_vel_method(VelMethod.CENTERED, h=8)
vel_est["accuracy"] = 2

traj = Trajectory(x=x, y=[i**2 for i in x], vel_est=vel_est)
26 changes: 14 additions & 12 deletions tests/test_trajectory/test_operations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import numpy as np
from pytest import approx, fixture
import pytest
from yupi import Trajectory, VelMethod
from pytest import approx, fixture
from yupi import Trajectory, WindowType

APPROX_REL_TOLERANCE = 1e-12


@fixture
def points():
return np.array([[1, 2], [4, 3], [4, 1], [6, 8], [5, 7]], dtype=float)
Expand Down Expand Up @@ -32,7 +33,7 @@ def timed_traj(points, angles, time):

@fixture
def simple_traj():
return Trajectory(x=[0,1], y=[0,1])
return Trajectory(x=[0, 1], y=[0, 1], vel_est={"window_type": WindowType.FORWARD})


def test_length(points, traj):
Expand All @@ -59,9 +60,10 @@ def test_iteration(points, angles, traj):
t = time[i]

assert point == approx(tp.r, APPROX_REL_TOLERANCE) # Position
assert t == approx(tp.t, APPROX_REL_TOLERANCE) # Time
assert t == approx(tp.t, APPROX_REL_TOLERANCE) # Time
assert ang == approx(tp.ang, APPROX_REL_TOLERANCE) # Angle


def test_rotation(simple_traj):
# 45 degrees
ang = np.pi / 4
Expand All @@ -70,12 +72,14 @@ def test_rotation(simple_traj):
# [1, 1] -> [0, sqrt(2)]
simple_traj.rotate2d(ang)

assert simple_traj.r[0] == approx([0,0], APPROX_REL_TOLERANCE)
assert simple_traj.r[1] == approx([0,np.sqrt(2)], APPROX_REL_TOLERANCE)
assert simple_traj.r[0] == approx([0, 0], APPROX_REL_TOLERANCE)
assert simple_traj.r[1] == approx([0, np.sqrt(2)], APPROX_REL_TOLERANCE)


def test_rotation_3d():
traj = Trajectory(x=[0,1], y=[0,0], z=[0,0])
traj = Trajectory(
x=[0, 1], y=[0, 0], z=[0, 0], vel_est={"window_type": WindowType.FORWARD}
)

traj.rotate3d(-np.pi / 2, [0, 0, 3])

Expand Down Expand Up @@ -114,7 +118,7 @@ def test_traj_addition(points, traj):

def test_wrong_addition(traj):
with pytest.raises(TypeError):
traj += 'wrong'
traj += "wrong"


def test_constant_substraction(points, traj):
Expand Down Expand Up @@ -144,7 +148,7 @@ def test_traj_substraction(points, traj):

def test_wrong_substraction(traj):
with pytest.raises(TypeError):
traj -= 'wrong'
traj -= "wrong"


def test_constant_multiplication(points, traj):
Expand All @@ -157,7 +161,7 @@ def test_constant_multiplication(points, traj):

def test_wrong_multiplication(traj):
with pytest.raises(TypeError):
traj *= 'wrong'
traj *= "wrong"
with pytest.raises(TypeError):
traj *= [1, 2]

Expand Down Expand Up @@ -205,5 +209,3 @@ def test_slicing(traj, timed_traj):
# Test dt
assert slice_5.dt == approx(traj.dt * 2, APPROX_REL_TOLERANCE)
assert slice_6.dt == approx(traj.dt * 2, APPROX_REL_TOLERANCE)


8 changes: 4 additions & 4 deletions yupi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from yupi.features import Features
from yupi.trajectory import Trajectory, TrajectoryPoint
from yupi.vector import Vector
from yupi._vel_estimators import VelMethod, VelPadding
from yupi._vel_estimators import VelocityMethod, WindowType

logging.basicConfig(
level=logging.INFO,
Expand All @@ -20,8 +20,8 @@
'TrajectoryPoint',
'Features',
'Vector',
'VelMethod',
'VelPadding'
'VelocityMethod',
'WindowType',
]

__version__ = '0.8.7'
__version__ = '0.8.8'
Loading

0 comments on commit 95d8982

Please sign in to comment.