<commit_before>import random
from functools import reduce
import time


import numpy as np
import numpy.testing as npt
from numpy import exp
import pytest
import numba

from clifford import Cl
from clifford.g3c import *
from clifford import general_exp
from clifford.tools.g3c import *
from clifford.tools.g3c.rotor_parameterisation import ga_log, ga_exp, general_logarithm, \
    interpolate_rotors
from clifford.tools.g3c.rotor_estimation import *
from clifford.tools.g3c.object_clustering import *
from clifford.tools.g3c.scene_simplification import *
from clifford.tools.g3c.object_fitting import *
from clifford.tools.g3c.model_matching import *
from clifford.tools.g3 import random_euc_mv
from clifford.tools.g3c.GAOnline import draw_objects, GAScene, GanjaScene


too_slow_without_jit = pytest.mark.skipif(
    numba.config.DISABLE_JIT, reason="test is too slow without JIT"
)


@too_slow_without_jit
class TestRotorGeneration:
    def test_generate_translation_rotor(self):
        for i in range(10000):
            euc_vector_a = random_euc_mv()
            res = generate_translation_rotor(euc_vector_a)
            res2 = (1 + ninf * euc_vector_a / 2)
            npt.assert_almost_equal(res.value, res2.value)


@too_slow_without_jit
class TestFitObjects:
    def test_fit_circle(self):
        noise = 0.1
        trueP = random_circle()
        point_list = project_points_to_circle([random_conformal_point() for i in range(100)], trueP)
        point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list]
        print(trueP)
        circle = fit_circle(point_list)
        print(circle)
        # draw(point_list + [circle], static=False, scale=0.1)

    def test_fit_line(self):
        noise = 0.1
        trueP = random_line()
        point_list = project_points_to_line([random_conformal_point() for i in range(100)], trueP)
        point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list]
        print(trueP)
        line = fit_line(point_list)
        print(line)
        # draw(point_list + [line], static=False, scale=0.1)

    def test_fit_sphere(self):
        noise = 0.1
        trueP = random_sphere()
        point_list = project_points_to_sphere([random_conformal_point() for i in range(100)], trueP)
        point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list]
        print(trueP)
        sphere = fit_sphere(point_list)
        print(sphere)
        # draw([sphere] + point_list, static=False, scale=0.1)

    def test_fit_plane(self):
        noise = 0.1
        trueP = random_plane()
        point_list = project_points_to_plane([random_conformal_point() for i in range(100)], trueP)
        point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list]
        print(trueP)
        plane = fit_plane(point_list)
        print(plane)
        # draw(point_list + [plane], static=False, scale=0.1)


@too_slow_without_jit
class TestGeneralLogarithm:

    def test_general_logarithm_rotation(self):
        # Check we can reverse rotations
        for i in range(50):
            R = random_rotation_rotor()
            biv_2 = general_logarithm(R)
            biv_3 = ga_log(R)
            npt.assert_almost_equal(biv_2.value, biv_3.value, 3)

    def test_general_logarithm_translation(self):
        # Check we can reverse translation
        for i in range(50):
            t = random_euc_mv()
            biv = ninf * t / 2
            R = general_exp(biv).normal()
            biv_2 = general_logarithm(R)
            npt.assert_almost_equal(biv.value, biv_2.value)

    def test_general_logarithm_scaling(self):
        # Check we can reverse scaling
        for i in range(50):
            scale = 0.5 + np.random.rand()
            biv = -np.log(scale) * e45 / 2
            R = general_exp(biv).normal()
            biv_2 = general_logarithm(R)
            npt.assert_almost_equal(biv.value, biv_2.value)

    def test_general_logarithm_RS(self):
        for i in range(5):
            scale = 0.5 + np.random.rand()

            S = generate_dilation_rotor(scale).normal()
            R = generate_rotation_rotor(0.5, e1, e2).normal()
            V = (R * S).normal()

            biv_test = general_logarithm(R) + general_logarithm(S)
            biv = general_logarithm(V)

            biv_alt = ga_log(R) + general_logarithm(S)

            npt.assert_almost_equal(biv.value, biv_test.value, 5)
            npt.assert_almost_equal(biv.value, biv_alt.value, 5)

    def test_general_logarithm_TR(self):
        for i in range(5):
            # R = generate_rotation_rotor(0.5, e1, e2).normal()
            # T = generate_translation_rotor(e3 + 7 * e2 - e1).normal()
            # V = (T*R).normal()
            biv_true = random_bivector()
            V = general_exp(biv_true).normal()
            biv = general_logarithm(V)

            V_rebuilt = (general_exp(biv)).normal()

            C1 = random_point_pair()
            C2 = (V * C1 * ~V).normal()
            C3 = (V_rebuilt * C1 * ~V_rebuilt).normal()
            npt.assert_almost_equal(C2.value, C3.value, 2)

    def test_general_logarithm_TS(self):
        for i in range(5):
            scale = 0.5 + np.random.rand()
            t = random_euc_mv()
            S = generate_dilation_rotor(scale)
            T = generate_translation_rotor(t)
            V = (T * S).normal()
            biv = general_logarithm(V)
            V_rebuilt = (general_exp(biv)).normal()

            C1 = random_point_pair()
            C2 = (V * C1 * ~V).normal()
            C3 = (V_rebuilt * C1 * ~V_rebuilt).normal()
            npt.assert_almost_equal(C2.value, C3.value, 5)

    def test_general_logarithm_TRS(self):
        for i in range(5):
            scale = 0.5 + np.random.rand()
            S = generate_dilation_rotor(scale)
            R = generate_rotation_rotor(0.5, e1, e2)
            T = generate_translation_rotor(e3 + 7 * e2 - e1)
            V = (T * R * S).normal()
            biv = general_logarithm(V)
            V_rebuilt = general_exp(biv).normal()
            biv2 = general_logarithm(V)

            C1 = random_point_pair()
            C2 = (V * C1 * ~V).normal()
            C3 = (V_rebuilt * C1 * ~V_rebuilt).normal()
            npt.assert_almost_equal(C2.value, C3.value)

    @pytest.mark.parametrize('obj_gen', [
        random_point_pair, random_line, random_circle, random_plane
    ])
    def test_general_logarithm_conformal(self, obj_gen):
        for i in range(10000):
            X = obj_gen()
            Y = obj_gen()
            R = rotor_between_objects(X, Y)
            biv = general_logarithm(R)
            R_recon = general_exp(biv).normal()
            npt.assert_almost_equal(R.value, R_recon.value, 4)


class TestVisualisation:
    def test_draw_objects(self):
        scene = ConformalMVArray([random_line() for i in range(100)])
        sc_a = str(draw_objects(scene))
        scene.save('test.ga')
        sc_b = str(draw_objects('test.ga'))
        assert sc_a == sc_b<commit_msg>Decimate the number of tests for TestGeneralLogarithm.test_general_logarithm_conformal<commit_after>import random
from functools import reduce
import time


import numpy as np
import numpy.testing as npt
from numpy import exp
import pytest
import numba

from clifford import Cl
from clifford.g3c import *
from clifford import general_exp
from clifford.tools.g3c import *
from clifford.tools.g3c.rotor_parameterisation import ga_log, ga_exp, general_logarithm, \
    interpolate_rotors
from clifford.tools.g3c.rotor_estimation import *
from clifford.tools.g3c.object_clustering import *
from clifford.tools.g3c.scene_simplification import *
from clifford.tools.g3c.object_fitting import *
from clifford.tools.g3c.model_matching import *
from clifford.tools.g3 import random_euc_mv
from clifford.tools.g3c.GAOnline import draw_objects, GAScene, GanjaScene


too_slow_without_jit = pytest.mark.skipif(
    numba.config.DISABLE_JIT, reason="test is too slow without JIT"
)


@too_slow_without_jit
class TestRotorGeneration:
    def test_generate_translation_rotor(self):
        for i in range(10000):
            euc_vector_a = random_euc_mv()
            res = generate_translation_rotor(euc_vector_a)
            res2 = (1 + ninf * euc_vector_a / 2)
            npt.assert_almost_equal(res.value, res2.value)


@too_slow_without_jit
class TestFitObjects:
    def test_fit_circle(self):
        noise = 0.1
        trueP = random_circle()
        point_list = project_points_to_circle([random_conformal_point() for i in range(100)], trueP)
        point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list]
        print(trueP)
        circle = fit_circle(point_list)
        print(circle)
        # draw(point_list + [circle], static=False, scale=0.1)

    def test_fit_line(self):
        noise = 0.1
        trueP = random_line()
        point_list = project_points_to_line([random_conformal_point() for i in range(100)], trueP)
        point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list]
        print(trueP)
        line = fit_line(point_list)
        print(line)
        # draw(point_list + [line], static=False, scale=0.1)

    def test_fit_sphere(self):
        noise = 0.1
        trueP = random_sphere()
        point_list = project_points_to_sphere([random_conformal_point() for i in range(100)], trueP)
        point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list]
        print(trueP)
        sphere = fit_sphere(point_list)
        print(sphere)
        # draw([sphere] + point_list, static=False, scale=0.1)

    def test_fit_plane(self):
        noise = 0.1
        trueP = random_plane()
        point_list = project_points_to_plane([random_conformal_point() for i in range(100)], trueP)
        point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list]
        print(trueP)
        plane = fit_plane(point_list)
        print(plane)
        # draw(point_list + [plane], static=False, scale=0.1)


@too_slow_without_jit
class TestGeneralLogarithm:

    def test_general_logarithm_rotation(self):
        # Check we can reverse rotations
        for i in range(50):
            R = random_rotation_rotor()
            biv_2 = general_logarithm(R)
            biv_3 = ga_log(R)
            npt.assert_almost_equal(biv_2.value, biv_3.value, 3)

    def test_general_logarithm_translation(self):
        # Check we can reverse translation
        for i in range(50):
            t = random_euc_mv()
            biv = ninf * t / 2
            R = general_exp(biv).normal()
            biv_2 = general_logarithm(R)
            npt.assert_almost_equal(biv.value, biv_2.value)

    def test_general_logarithm_scaling(self):
        # Check we can reverse scaling
        for i in range(50):
            scale = 0.5 + np.random.rand()
            biv = -np.log(scale) * e45 / 2
            R = general_exp(biv).normal()
            biv_2 = general_logarithm(R)
            npt.assert_almost_equal(biv.value, biv_2.value)

    def test_general_logarithm_RS(self):
        for i in range(5):
            scale = 0.5 + np.random.rand()

            S = generate_dilation_rotor(scale).normal()
            R = generate_rotation_rotor(0.5, e1, e2).normal()
            V = (R * S).normal()

            biv_test = general_logarithm(R) + general_logarithm(S)
            biv = general_logarithm(V)

            biv_alt = ga_log(R) + general_logarithm(S)

            npt.assert_almost_equal(biv.value, biv_test.value, 5)
            npt.assert_almost_equal(biv.value, biv_alt.value, 5)

    def test_general_logarithm_TR(self):
        for i in range(5):
            # R = generate_rotation_rotor(0.5, e1, e2).normal()
            # T = generate_translation_rotor(e3 + 7 * e2 - e1).normal()
            # V = (T*R).normal()
            biv_true = random_bivector()
            V = general_exp(biv_true).normal()
            biv = general_logarithm(V)

            V_rebuilt = (general_exp(biv)).normal()

            C1 = random_point_pair()
            C2 = (V * C1 * ~V).normal()
            C3 = (V_rebuilt * C1 * ~V_rebuilt).normal()
            npt.assert_almost_equal(C2.value, C3.value, 2)

    def test_general_logarithm_TS(self):
        for i in range(5):
            scale = 0.5 + np.random.rand()
            t = random_euc_mv()
            S = generate_dilation_rotor(scale)
            T = generate_translation_rotor(t)
            V = (T * S).normal()
            biv = general_logarithm(V)
            V_rebuilt = (general_exp(biv)).normal()

            C1 = random_point_pair()
            C2 = (V * C1 * ~V).normal()
            C3 = (V_rebuilt * C1 * ~V_rebuilt).normal()
            npt.assert_almost_equal(C2.value, C3.value, 5)

    def test_general_logarithm_TRS(self):
        for i in range(5):
            scale = 0.5 + np.random.rand()
            S = generate_dilation_rotor(scale)
            R = generate_rotation_rotor(0.5, e1, e2)
            T = generate_translation_rotor(e3 + 7 * e2 - e1)
            V = (T * R * S).normal()
            biv = general_logarithm(V)
            V_rebuilt = general_exp(biv).normal()
            biv2 = general_logarithm(V)

            C1 = random_point_pair()
            C2 = (V * C1 * ~V).normal()
            C3 = (V_rebuilt * C1 * ~V_rebuilt).normal()
            npt.assert_almost_equal(C2.value, C3.value)

    @pytest.mark.parametrize('obj_gen', [
        random_point_pair, random_line, random_circle, random_plane
    ])
    def test_general_logarithm_conformal(self, obj_gen):
        for i in range(1000):
            X = obj_gen()
            Y = obj_gen()
            R = rotor_between_objects(X, Y)
            biv = general_logarithm(R)
            R_recon = general_exp(biv).normal()
            npt.assert_almost_equal(R.value, R_recon.value, 4)


class TestVisualisation:
    def test_draw_objects(self):
        scene = ConformalMVArray([random_line() for i in range(100)])
        sc_a = str(draw_objects(scene))
        scene.save('test.ga')
        sc_b = str(draw_objects('test.ga'))
        assert sc_a == sc_b
{'fraction_non_alphanumeric': 0.10353894258101194, 'fraction_numerical': 0.018831722569641843, 'mean_word_length': 2.4190962099125364, 'pattern_counts': {'":': 0, '<': 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': '644204', 'n_tokens_mistral': 4991, 'n_tokens_neox': 4762, 'n_words': 963}