Skip to content
Snippets Groups Projects
Commit 798c1e73 authored by Olivier Bertrand's avatar Olivier Bertrand
Browse files

Add test for sinuosity and travelled dist

parent 02f10e3a
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -102,7 +102,7 @@ def _invmarkerstransform(index_i, trajectory, ...@@ -102,7 +102,7 @@ def _invmarkerstransform(index_i, trajectory,
class Trajectory(pd.DataFrame): class Trajectory(pd.DataFrame):
def __init__(self, rotconv='zyx', indeces=np.arange(1)): def __init__(self, rotconv='zyx', indeces=np.arange(1)):
columns = self.__build_columns(rotconv) columns = self.__build_columns(rotconv)
super().__init__(index=indeces, columns=columns) super().__init__(index=indeces, columns=columns, dtype=np.float)
self.__rotconv = rotconv self.__rotconv = rotconv
self.sampling_rate = 0 self.sampling_rate = 0
...@@ -678,12 +678,12 @@ class Trajectory(pd.DataFrame): ...@@ -678,12 +678,12 @@ class Trajectory(pd.DataFrame):
# We remove nans between section # We remove nans between section
# and then calculate the velocity # and then calculate the velocity
# it is equivalent to interpolate between non-nan blocks # it is equivalent to interpolate between non-nan blocks
subtraj = self.location.dropna().reset_index().drop('index', subtraj = self.location.dropna().reset_index().drop('index', axis=1)
axis=1, level=0)
if subtraj.dropna().shape[0] < 2: if subtraj.dropna().shape[0] < 2:
print('Trajectory has less than 2 non nans points') print('Trajectory has less than 2 non nans points')
return np.nan return np.nan
velocity = subtraj.diff() # only location is of relevance # only location is of relevance
velocity = subtraj.astype(float).diff()
speed = np.sqrt(velocity.x**2 + velocity.y**2 + velocity.z**2) speed = np.sqrt(velocity.x**2 + velocity.y**2 + velocity.z**2)
travel_dist = np.sum(speed) travel_dist = np.sum(speed)
return travel_dist return travel_dist
......
...@@ -54,38 +54,39 @@ class TestTrajectoryTransform(unittest.TestCase): ...@@ -54,38 +54,39 @@ class TestTrajectoryTransform(unittest.TestCase):
pass pass
def test_traveldist(self): def test_traveldist(self):
indeces = np.linspace(0, 2*np.pi, 1000) indeces = np.linspace(0, 2*np.pi, 2000)
radius = 5 radius = 5
mytraj = Trajectory(indeces=indeces, rotconv='zyx') mytraj = Trajectory(indeces=indeces, rotconv='zyx')
mytraj.x = indeces mytraj.x = radius*np.cos(indeces)
mytraj.y = radius*np.cos(indeces) mytraj.y = radius*np.sin(indeces)
mytraj.z = 0 mytraj.z = 0
# The length of a cos from 0 to 2pi # The length of function above
# is equal to perimeter of the circle # is equal to perimeter of the circle
# 2*pi*r # 2*pi*r
travel_dist_theo = 2*np.pi*radius travel_dist_theo = 2*np.pi*radius
travel_dist = mytraj.traveled_distance() travel_dist = mytraj.traveled_distance()
np.testing.assert_almost_equal(travel_dist, travel_dist_theo) np.testing.assert_almost_equal(
travel_dist, travel_dist_theo, decimal=4)
# Test with nans # Test with nans
mytraj.loc[[15, 50, 90], :] = np.nan mytraj.iloc[[15, 50, 90], :] = np.nan
travel_dist = mytraj.traveled_distance() travel_dist = mytraj.traveled_distance()
np.testing.assert_almost_equal(travel_dist, travel_dist_theo) np.testing.assert_almost_equal(
travel_dist, travel_dist_theo, decimal=4)
def test_sinuosity(self): def test_sinuosity(self):
indeces = np.linspace(0, 2*np.pi, 1000) indeces = np.linspace(0, np.pi, 1000)
radius = 5 radius = 5
mytraj = Trajectory(indeces=indeces, rotconv='zyx') mytraj = Trajectory(indeces=indeces, rotconv='zyx')
mytraj.x = indeces mytraj.x = radius*np.cos(indeces)
mytraj.y = radius*np.cos(indeces) mytraj.y = radius*np.sin(indeces)
mytraj.z = 0 mytraj.z = 0
# The length of a cos from 0 to 2pi # The length of function above
# is equal to perimeter of the circle # is equal to half the perimeter of the circle
# 2*pi*r # pi*r
# the sinuosity will be equal to the radius # the sinuosity will be equal to:
# because dist from start to end is 2*pi sinuosity_theo = np.pi*radius/(2*radius)
sinuosity_theo = radius
sinuosity = mytraj.sinuosity() sinuosity = mytraj.sinuosity()
np.testing.assert_almost_equal(sinuosity, sinuosity_theo) np.testing.assert_almost_equal(sinuosity, sinuosity_theo, decimal=4)
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment