diff --git a/.coverage b/.coverage index 16dd820174b832da58b7fb91761d86381b6321b0..b7af7d0a349374c2744d7891f0f7d07282fcd980 100644 Binary files a/.coverage and b/.coverage differ diff --git a/navipy/trajectories/__init__.py b/navipy/trajectories/__init__.py index 2a6ef2990c6d4eb031e6555cf9dc84d615d5fda3..9b024aa37b61b9fe4c1c720bc13c310121313740 100644 --- a/navipy/trajectories/__init__.py +++ b/navipy/trajectories/__init__.py @@ -102,7 +102,7 @@ def _invmarkerstransform(index_i, trajectory, class Trajectory(pd.DataFrame): def __init__(self, rotconv='zyx', indeces=np.arange(1)): columns = self.__build_columns(rotconv) - super().__init__(index=indeces, columns=columns) + super().__init__(index=indeces, columns=columns, dtype=np.float) self.__rotconv = rotconv self.sampling_rate = 0 @@ -678,12 +678,12 @@ class Trajectory(pd.DataFrame): # We remove nans between section # and then calculate the velocity # it is equivalent to interpolate between non-nan blocks - subtraj = self.location.dropna().reset_index().drop('index', - axis=1, level=0) + subtraj = self.location.dropna().reset_index().drop('index', axis=1) if subtraj.dropna().shape[0] < 2: print('Trajectory has less than 2 non nans points') 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) travel_dist = np.sum(speed) return travel_dist diff --git a/navipy/trajectories/test_trajectory.py b/navipy/trajectories/test_trajectory.py index 96ec2a725aa6ba04f83a9e5f4314e2d738e5586c..09acc044a70f021994823dacbafc30969674c6e5 100644 --- a/navipy/trajectories/test_trajectory.py +++ b/navipy/trajectories/test_trajectory.py @@ -54,38 +54,39 @@ class TestTrajectoryTransform(unittest.TestCase): pass def test_traveldist(self): - indeces = np.linspace(0, 2*np.pi, 1000) + indeces = np.linspace(0, 2*np.pi, 2000) radius = 5 mytraj = Trajectory(indeces=indeces, rotconv='zyx') - mytraj.x = indeces - mytraj.y = radius*np.cos(indeces) + mytraj.x = radius*np.cos(indeces) + mytraj.y = radius*np.sin(indeces) mytraj.z = 0 - # The length of a cos from 0 to 2pi + # The length of function above # is equal to perimeter of the circle # 2*pi*r travel_dist_theo = 2*np.pi*radius 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 - mytraj.loc[[15, 50, 90], :] = np.nan + mytraj.iloc[[15, 50, 90], :] = np.nan 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): - indeces = np.linspace(0, 2*np.pi, 1000) + indeces = np.linspace(0, np.pi, 1000) radius = 5 mytraj = Trajectory(indeces=indeces, rotconv='zyx') - mytraj.x = indeces - mytraj.y = radius*np.cos(indeces) + mytraj.x = radius*np.cos(indeces) + mytraj.y = radius*np.sin(indeces) mytraj.z = 0 - # The length of a cos from 0 to 2pi - # is equal to perimeter of the circle - # 2*pi*r - # the sinuosity will be equal to the radius - # because dist from start to end is 2*pi - sinuosity_theo = radius + # The length of function above + # is equal to half the perimeter of the circle + # pi*r + # the sinuosity will be equal to: + sinuosity_theo = np.pi*radius/(2*radius) sinuosity = mytraj.sinuosity() - np.testing.assert_almost_equal(sinuosity, sinuosity_theo) + np.testing.assert_almost_equal(sinuosity, sinuosity_theo, decimal=4) if __name__ == '__main__':