<commit_before>import unittest
import numpy as np
import sys
from ikpy import chain
from ikpy import plot_utils
import params

plot = params.interactive


class TestChain(unittest.TestCase):
    def setUp(self):
        if plot:
            self.ax = plot_utils.init_3d_figure()
        self.chain1 = chain.Chain.from_urdf_file(params.resources_path + "/poppy_torso.URDF", base_elements=["base", "abs_z", "spine", "bust_y", "bust_motors", "bust_x", "chest", "r_shoulder_y"], last_link_vector=[0, 0.18, 0], active_links_mask=[False, False, False, False, True, True, True, True, True])
        self.joints = [0] * len(self.chain1.links)
        self.joints[-4] = 0

    def test_chain(self):
        self.chain1 = chain.Chain.from_urdf_file(params.resources_path + "/poppy_torso.URDF", base_elements=["base", "abs_z", "spine", "bust_y", "bust_motors", "bust_x", "chest", "r_shoulder_y"], last_link_vector=[0, 0.18, 0], active_links_mask=[False, False, False, False, True, True, True, True, True])
        self.chain2 = chain.Chain.from_urdf_file(params.resources_path + "/poppy_torso.URDF", base_elements=["base", "abs_z", "spine", "bust_y", "bust_motors", "bust_x", "chest", "l_shoulder_y"], last_link_vector=[0, 0.18, 0], active_links_mask=[False, False, False, False, True, True, True, True, True])

        if plot:
            self.chain1.plot(self.joints, self.ax)
            self.chain2.plot(self.joints, self.ax)

    def test_ik(self):
        target = [0.1, -0.2, 0.1]
        frame_target = np.eye(4)
        frame_target[:3, 3] = target
        ik = self.chain1.inverse_kinematics(frame_target, initial_position=self.joints)

        if plot:
            self.chain1.plot(ik, self.ax, target=target)
            plot_utils.show_figure()

        np.testing.assert_almost_equal(self.chain1.forward_kinematics(ik)[:3, 3], target, decimal=3)


if __name__ == '__main__':
    unittest.main(verbosity=2, argv=[sys.argv[0]])
<commit_msg>Add new test : IK_optimization_method<commit_after>import unittest
import numpy as np
import sys
from ikpy import chain
from ikpy import plot_utils
import params

plot = params.interactive


class TestChain(unittest.TestCase):
    def setUp(self):
        if plot:
            self.ax = plot_utils.init_3d_figure()
        self.chain1 = chain.Chain.from_urdf_file(params.resources_path + "/poppy_torso.URDF", base_elements=["base", "abs_z", "spine", "bust_y", "bust_motors", "bust_x", "chest", "r_shoulder_y"], last_link_vector=[0, 0.18, 0], active_links_mask=[False, False, False, False, True, True, True, True, True])
        self.joints = [0] * len(self.chain1.links)
        self.joints[-4] = 0
        self.target = [0.1, -0.2, 0.1]
        self.frame_target = np.eye(4)
        self.frame_target[:3, 3] = self.target

    def test_chain(self):
        self.chain1 = chain.Chain.from_urdf_file(params.resources_path + "/poppy_torso.URDF", base_elements=["base", "abs_z", "spine", "bust_y", "bust_motors", "bust_x", "chest", "r_shoulder_y"], last_link_vector=[0, 0.18, 0], active_links_mask=[False, False, False, False, True, True, True, True, True])
        self.chain2 = chain.Chain.from_urdf_file(params.resources_path + "/poppy_torso.URDF", base_elements=["base", "abs_z", "spine", "bust_y", "bust_motors", "bust_x", "chest", "l_shoulder_y"], last_link_vector=[0, 0.18, 0], active_links_mask=[False, False, False, False, True, True, True, True, True])

        if plot:
            self.chain1.plot(self.joints, self.ax)
            self.chain2.plot(self.joints, self.ax)

    def test_ik(self):

        ik = self.chain1.inverse_kinematics(self.frame_target, initial_position=self.joints)

        if plot:
            self.chain1.plot(ik, self.ax, target=self.target)
            plot_utils.show_figure()

        np.testing.assert_almost_equal(self.chain1.forward_kinematics(ik)[:3, 3], self.target, decimal=3)

    def test_ik_optimization(self):
        """Tests the IK optimization-based method"""
        args = {"max_iter": 3}
        ik = self.chain1.inverse_kinematics(self.frame_target, initial_position=self.joints, **args)
        # Check whether the results are almost equal
        np.testing.assert_almost_equal(self.chain1.forward_kinematics(ik)[:3, 3], self.target, decimal=1)


if __name__ == '__main__':
    unittest.main(verbosity=2, argv=[sys.argv[0]])

{'fraction_non_alphanumeric': 0.14913294797687862, 'fraction_numerical': 0.020809248554913295, 'mean_word_length': 3.8173719376391984, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '206480', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1552, 'n_words': 306}