Commit 7553ec53 authored by Olivier Bertrand's avatar Olivier Bertrand
Browse files

Test for markers in trajectory

add copy function for trajecotry keeping Trajectory class and not DataFrame.
parent 8304c2c7
......@@ -17,7 +17,6 @@ from functools import partial
import time
from scipy import signal
from scipy.interpolate import CubicSpline
import warnings
def posorient_columns(convention):
......@@ -64,7 +63,9 @@ def _markers2angles(x, kwargs):
def _markerstransform(index_i, trajectory,
homogeneous_markers, rotation_mode):
homogeneous_markers, rotation_mode=None):
if rotation_mode is None:
rotation_mode = trajectory.rotation_mode
row = trajectory.loc[index_i]
angles = row.loc[rotation_mode].values
translate = row.loc['location'].values
......@@ -82,7 +83,9 @@ def _markerstransform(index_i, trajectory,
def _invmarkerstransform(index_i, trajectory,
homogeneous_markers, rotation_mode):
homogeneous_markers, rotation_mode=None):
if rotation_mode is None:
rotation_mode = trajectory.rotation_mode
row = trajectory.loc[index_i]
angles = row.loc[rotation_mode].values
translate = row.loc['location'].values
......@@ -321,6 +324,9 @@ class Trajectory(pd.DataFrame):
df = pd.DataFrame(self)
df.to_csv(filename)
def copy(self, deep=True):
return Trajectory().from_dataframe(super().copy(deep=deep))
# -------------------------------------------
# ---------------- INITS FROM VAR------------
# -------------------------------------------
......@@ -574,10 +580,6 @@ class Trajectory(pd.DataFrame):
def world2body(self, markers, indeces=None):
""" Transform markers in world coordinate to body coordinate
"""
msg = 'Prior to 12/09/2018:\n'
msg += 'world2body was doing a reverse transformed\n'
msg += 'Please use body2world instead'
warnings.warn(msg)
if not isinstance(markers, pd.Series):
msg = 'markers should be of type pd.Series and not'
msg += ' {}'.format(type(markers))
......@@ -791,13 +793,6 @@ series.
self.loc[:, (rotconv, col)] = cs(time)
return self
else:
msg = 'Method {} is not supported.'
msg += 'please use method supported by pd.fillna'
msg += ' or one of the following methods {}'
msg = msg.format(method, customs_method)
raise NameError(msg)
# --------------------------------------------
# ---------------- EXTRACT -------------------
# --------------------------------------------
......
......@@ -3,21 +3,11 @@ import numpy as np
import pandas as pd
from navipy.maths import constants as htconst
from navipy.trajectories import Trajectory
from navipy.trajectories import _markerstransform, _invmarkerstransform
class TestTrajectoryTransform(unittest.TestCase):
def test_forwardreverse(self):
# Build an equilateral triangle
markers = pd.Series(data=0,
index=pd.MultiIndex.from_product([
[0, 1, 2],
['x', 'y', 'z']]))
markers.loc[(0, 'x')] = -1
markers.loc[(2, 'y')] = np.sin(np.pi / 3)
markers.loc[(1, 'y')] = -np.sin(np.pi / 3)
markers.loc[(1, 'x')] = np.cos(np.pi / 3)
markers.loc[(2, 'x')] = np.cos(np.pi / 3)
markers.index.name = None
def setUp(self):
# Create a random trajectory
trajectory = Trajectory(indeces=np.arange(10),
rotconv='xyz')
......@@ -30,6 +20,21 @@ class TestTrajectoryTransform(unittest.TestCase):
(np.random.rand(trajectory.shape[0]) - 0.5)
trajectory.alpha_2 = 2 * np.pi * \
(np.random.rand(trajectory.shape[0]) - 0.5)
self.trajectory = trajectory
def test_forwardreverse(self):
# Build an equilateral triangle
markers = pd.Series(data=0,
index=pd.MultiIndex.from_product([
[0, 1, 2],
['x', 'y', 'z']]))
markers.loc[(0, 'x')] = -1
markers.loc[(2, 'y')] = np.sin(np.pi / 3)
markers.loc[(1, 'y')] = -np.sin(np.pi / 3)
markers.loc[(1, 'x')] = np.cos(np.pi / 3)
markers.loc[(2, 'x')] = np.cos(np.pi / 3)
markers.index.name = None
trajectory = self.trajectory.copy()
# Create test trajectory
traj_test = Trajectory(indeces=np.arange(10),
rotconv='xyz')
......@@ -48,7 +53,70 @@ class TestTrajectoryTransform(unittest.TestCase):
triangle_mode)
np.testing.assert_array_almost_equal(
trajectory.astype(float),
traj_test.astype(float))
traj_test.astype(float),
err_msg='Trajectory and traj from markers')
# Test world2body
# We compare for two time points
# world2body will give world marker along traj in body coordinate
for idx in [trajectory.index[0], trajectory.index[-1]]:
markers_test = trajectory.world2body(
transformed_markers.loc[idx, :])
markers_test = markers_test.loc[idx, :]
np.testing.assert_array_almost_equal(
markers_test,
markers,
err_msg='body2world->world2body')
def test_raise_world2body(self):
trajectory = self.trajectory.copy()
with self.assertRaises(TypeError):
# Not a series
trajectory.body2world(markers=pd.DataFrame)
with self.assertRaises(TypeError):
# No multindex
trajectory.body2world(markers=pd.Series(index=[0]))
def test_markerstransform(self):
trajectory = self.trajectory.copy()
homogeneous_markers = pd.DataFrame(index=[0, 1],
columns=['x', 'y', 'z', 'w'],
data=np.random.rand(2, 4))
homogeneous_markers.loc[:, 'w'] = 1
# Make sure that columns are correctly ordered
homogeneous_markers = homogeneous_markers[['x', 'y', 'z', 'w']]
# Transpose because we apply homogeneous transformation
# on the marker, and thus a 4x4 matrix on a 4xN matrix
# here N is the number of markers
homogeneous_markers = homogeneous_markers.transpose()
for idx in [trajectory.index[0], trajectory.index[-1]]:
marker = _markerstransform(idx, trajectory,
homogeneous_markers,
rotation_mode=None)
marker = marker.unstack()
marker['w'] = 1
# Make sure that columns are correctly ordered
marker = marker[['x', 'y', 'z', 'w']]
# Transpose because we apply homogeneous transformation
# on the marker, and thus a 4x4 matrix on a 4xN matrix
# here N is the number of markers
marker = marker.transpose()
# Apply reverse
imarker = _invmarkerstransform(idx, trajectory,
marker,
rotation_mode=None)
imarker = imarker.unstack()
imarker['w'] = 1
# Make sure that columns are correctly ordered
imarker = imarker[['x', 'y', 'z', 'w']]
# Transpose because we apply homogeneous transformation
# on the marker, and thus a 4x4 matrix on a 4xN matrix
# here N is the number of markers
imarker = imarker.transpose()
# Compare
np.testing.assert_array_almost_equal(imarker, homogeneous_markers)
def test_velocity(self):
pass
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment