00001 import unittest
00002 import os
00003 import rosbag
00004 import numpy as np
00005
00006 from grasp_synergy.grasp_synergy import GraspSynergy
00007
00008 """
00009 Author: Felix Duvallet <felixd@gmail.com>
00010 """
00011
00012
00013 class TestCase(unittest.TestCase):
00014
00015 def setUp(self):
00016 (_, self.messages, _) = zip(*self.data)
00017 self.synergy = GraspSynergy()
00018 pass
00019
00020 @classmethod
00021 def setUpClass(cls):
00022 fpath = os.path.join(os.path.dirname(__file__), 'data', 'allegro.bag')
00023 bag = rosbag.Bag(fpath)
00024 topics = ['/allegroHand_0/joint_states']
00025 cls.data = list(bag.read_messages(topics))
00026 bag.close()
00027 pass
00028
00029 def test_constructor(self):
00030 self.assertIsNotNone(self.synergy)
00031 self.assertEqual(0, self.synergy._D)
00032 self.assertEqual(0, self.synergy._N)
00033
00034 def test_trained_with_data(self):
00035 self.assertFalse(self.synergy.trained)
00036 joints = np.random.random((25, 5))
00037 self.synergy.fit_joint_values(joints)
00038
00039 self.assertTrue(self.synergy.trained)
00040
00041 def test_trained_no_data(self):
00042 joints = np.zeros((10, 0))
00043 self.synergy.fit_joint_values(joints)
00044 self.assertFalse(self.synergy.trained)
00045
00046 def test_fit_joint_values(self):
00047 joints = np.random.random((25, 5))
00048 ret = self.synergy.fit_joint_values(joints)
00049 self.assertTrue(ret)
00050 self.assertEqual(5, self.synergy._D)
00051 self.assertEqual(25, self.synergy._N)
00052 self.assertEqual(5, len(self.synergy._pca.components_))
00053
00054 def test_fit_joint_values_bad_type(self):
00055 joints = [[1, 2, 3], [4, 5, 6]]
00056 with self.assertRaisesRegexp(AssertionError, 'Must have'):
00057 self.synergy.fit_joint_values(joints)
00058
00059 def test_fit_joint_values_empty(self):
00060
00061
00062
00063 joints = np.zeros((10, 0))
00064
00065 ret = self.synergy.fit_joint_values(joints)
00066
00067 self.assertFalse(ret)
00068 self.assertEqual(0, self.synergy._D)
00069 self.assertEqual(0, self.synergy._N)
00070 self.assertFalse(self.synergy.trained)
00071
00072 def test_fit_joint_messages(self):
00073 ret = self.synergy.fit_joint_state_messages(self.messages)
00074 self.assertTrue(ret)
00075 self.assertEqual(16, self.synergy._D)
00076 self.assertEqual(82, self.synergy._N)
00077 self.assertEqual(16, len(self.synergy._pca.components_))
00078
00079 def test_fit_joint_messages_empty(self):
00080 messages = []
00081 ret = self.synergy.fit_joint_state_messages(messages)
00082 self.assertFalse(ret)
00083 self.assertEqual(0, self.synergy._D)
00084 self.assertEqual(0, self.synergy._N)
00085 self.assertFalse(self.synergy.trained)
00086
00087 def test_fit_bag_file(self):
00088 fpath = os.path.join(os.path.dirname(__file__), 'data', 'allegro.bag')
00089 ret = self.synergy.fit_bag_file(fpath)
00090 self.assertTrue(ret)
00091 self.assertEqual(16, self.synergy._D)
00092 self.assertEqual(82, self.synergy._N)
00093 self.assertEqual(16, len(self.synergy._pca.components_))
00094
00095 def test_bag_file_nonexistent(self):
00096 ret = self.synergy.fit_bag_file('/not/a/file')
00097 self.assertFalse(ret)
00098 self.assertEqual(0, self.synergy._D)
00099 self.assertEqual(0, self.synergy._N)
00100 self.assertFalse(self.synergy.trained)
00101
00102 def test_compute_grasp_no_data(self):
00103 ret = self.synergy.compute_grasp([0, 1, 3])
00104 self.assertIsNone(ret)
00105
00106 def test_compute_grasp_shape(self):
00107 self.synergy.fit_joint_state_messages(self.messages)
00108 ret = self.synergy.compute_grasp([1.0])
00109 self.assertIsNotNone(ret)
00110 self.assertEqual((16,), ret.shape)
00111
00112 def test_compute_grasp_zero_alphas(self):
00113 self.synergy.fit_joint_state_messages(self.messages)
00114 ret = self.synergy.compute_grasp([])
00115 ref = self.synergy._pca.mean_
00116 np.testing.assert_array_almost_equal(ref, ret)
00117
00118 def test_compute_grasp_sum_alphas(self):
00119 self.synergy.fit_joint_state_messages(self.messages)
00120 ret = self.synergy.compute_grasp([1.0, 1.0])
00121 ref = (self.synergy._pca.components_[0] +
00122 self.synergy._pca.components_[1] + self.synergy._pca.mean_)
00123 np.testing.assert_array_almost_equal(ref, ret)
00124
00125 def test_compute_grasp_many_alphas(self):
00126
00127
00128 self.synergy.fit_joint_state_messages(self.messages)
00129 alphas = np.ones((100,))
00130 ret = self.synergy.compute_grasp(alphas)
00131 ref = (np.sum(self.synergy._pca.components_, axis=0) +
00132 self.synergy._pca.mean_)
00133 np.testing.assert_array_almost_equal(ref, ret)
00134
00135 def test_component_ranges_0(self):
00136 self.synergy.fit_joint_state_messages(self.messages)
00137 (ret_min, ret_max) = self.synergy.synergy_range(0)
00138 self.assertAlmostEqual(-1.18130, ret_min, places=4)
00139 self.assertAlmostEqual(1.12507406, ret_max, places=4)
00140
00141 def test_component_ranges_1(self):
00142 self.synergy.fit_joint_state_messages(self.messages)
00143 (ret_min, ret_max) = self.synergy.synergy_range(1)
00144 self.assertAlmostEqual(-0.41370870, ret_min, places=4)
00145 self.assertAlmostEqual(0.4547809556, ret_max, places=4)
00146
00147 def test_component_ranges_untrained(self):
00148 (ret_min, ret_max) = self.synergy.synergy_range(1)
00149 self.assertEqual(0, ret_min)
00150 self.assertEqual(0, ret_max)
00151
00152 def test_component_ranges_negative(self):
00153 (ret_min, ret_max) = self.synergy.synergy_range(-1)
00154 self.assertEqual(0, ret_min)
00155 self.assertEqual(0, ret_max)
00156
00157 def test_component_ranges_too_big(self):
00158 (ret_min, ret_max) = self.synergy.synergy_range(100)
00159 self.assertEqual(0, ret_min)
00160 self.assertAlmostEqual(0, ret_max)
00161
00162
00163 if __name__ == '__main__':
00164 unittest.main()