Exemple #1
0
class TestFormAndfForm(unittest.TestCase):

    fform_filename = 'tests/fform/T_d-S_2-S_2(110).p'
    fform = shgpy.load_fform(fform_filename)
    fform.apply_phase_shift(S.psi)

    def test_fform_to_form(self):
        _logger.debug(shgpy.fform_to_form(self.fform).get_items())

    def test_form_to_dat(self):
        form = shgpy.fform_to_form(self.fform)
        dat = shgpy.form_to_dat(
            form,
            {
                k: random.uniform(-1, 1)
                for k in form.get_free_symbols() if k != S.phi
            },
            1000,
        )
        if MANUAL:
            easy_plot(
                [dat],
                [{
                    'linestyle': '-',
                    'color': 'blue'
                }],
                dat.get_keys(),
            )
    def test_fform(self):
        fform1 = shgpy.load_fform(self.fform_filename)
        fform2 = shgpy.load_fform(self.fform_filename)

        def mapping(key, index):
            return str(index) + key

        fform3 = shgpy.merge_containers([fform1, fform2], mapping)
        iterable = {}
        for k, v in fform1.get_items():
            iterable['0' + k] = np.copy(v)
        for k, v in fform2.get_items():
            iterable['1' + k] = np.copy(v)
        fform3_compare = shgpy.fFormContainer(iterable)

        for k in fform3.get_keys():
            a = fform3.get_pc(k)
            b = fform3_compare.get_pc(k)
            self.assertEqual(str(a), str(b))
Exemple #3
0
    def test_convert(self):

        fform_filename = 'tests/fform/T_d-S_2-S_2(110).p'
        fform = shgpy.load_fform(fform_filename)
        data_filenames_dict = {
            'PP': 'tests/Data/dataPP.csv',
            'PS': 'tests/Data/dataPS.csv',
            'SP': 'tests/Data/dataSP.csv',
            'SS': 'tests/Data/dataSS.csv',
        }
        dat, fdat = shgpy.load_data_and_fourier_transform(
            data_filenames_dict,
            'degrees',
        )
        form = shgpy.fform_to_form(fform)

        t1 = shgpy.dat_to_fdat(dat)
        self.assertIsInstance(t1, type(fdat))
        t2 = shgpy.fdat_to_dat(fdat, 1000)
        self.assertIsInstance(t2, type(dat))
        t3 = shgpy.form_to_dat(
            form,
            self.random_subs_dict(fform.get_free_symbols()),
            1000,
        )
        self.assertIsInstance(t3, type(dat))
        t4 = shgpy.form_to_fform(form)
        self.assertIsInstance(t4, type(fform))
        t5 = shgpy.form_to_fdat(
            form,
            self.random_subs_dict(fform.get_free_symbols()),
        )
        self.assertIsInstance(t5, type(fdat))
        t6 = shgpy.fform_to_fdat(
            fform,
            self.random_subs_dict(fform.get_free_symbols()),
        )
        self.assertIsInstance(t6, type(fdat))
        t7 = shgpy.fform_to_dat(
            fform,
            self.random_subs_dict(fform.get_free_symbols()),
            1000,
        )
        self.assertIsInstance(t7, type(dat))
        t8 = shgpy.fform_to_form(fform)
        self.assertIsInstance(t8, type(form))
Exemple #4
0
    def test_generate_contracted_fourier_transforms(self):

        t_dip = shgpy.make_tensor_real(
            shgpy.transform(
                shgpy.particularize(td.dipole['T_d']),
                shgpy.rotation_matrix_from_two_vectors(
                    np.array([1, 1, 0]),
                    np.array([0, 0, 1]),
                ),
            ), )
        t_quad = shgpy.make_tensor_real(
            np.zeros(shape=(3, 3, 3, 3), dtype=object), )
        save_filename = 'tests/fform/T_d-S_2-S_2(110)-particularized.p'
        shgpy.fformgen.generate_contracted_fourier_transforms(
            save_filename,
            'tests/fform/uft10deg',
            t_dip,
            t_quad,
            ndigits=4,
        )
        fform = shgpy.load_fform(save_filename)
        fform.apply_phase_shift(S.psi)
        form = shgpy.fform_to_form(fform)
        dat = shgpy.form_to_dat(
            form,
            {
                k: random.uniform(-1, 1)
                for k in form.get_free_symbols() if k != S.phi
            },
            1000,
        )
        if MANUAL:
            easy_plot(
                [dat],
                [{
                    'linestyle': '-',
                    'color': 'blue'
                }],
                dat.get_keys(),
            )
import shgpy
import sympy as sp

fform_filename = 'fform/T_d-S_2-S_2(110)-particularized.p'

fform = shgpy.load_fform(fform_filename)

for pc in fform.get_keys():
    print(pc)
    print('-----------------')
    for i, v in enumerate(fform.get_pc(pc)):
        print(i - 16, sp.simplify(sp.expand(v)))
    input('Press enter to continue >>> ')
Exemple #6
0
class TestFit(unittest.TestCase):

    fform_filename = 'tests/fform/T_d-S_2-S_2(110)-particularized.p'
    fform = shgpy.load_fform(fform_filename)
    data_filenames_dict = {
        'PP': 'tests/Data/dataPP.csv',
        'PS': 'tests/Data/dataPS.csv',
        'SP': 'tests/Data/dataSP.csv',
        'SS': 'tests/Data/dataSS.csv',
    }
    dat, fdat = shgpy.load_data_and_fourier_transform(
        data_filenames_dict,
        'degrees',
    )
    fform.apply_phase_shift(S.psi)
    guess_dict = {k: 1 for k in fform.get_free_symbols()}
    bounds_dict = {
        k: ((-2, 2) if k != S.psi else (-np.pi, np.pi))
        for k in fform.get_free_symbols()
    }
    free_symbols = fform.get_free_symbols()

    def test_minimal(self):
        shgpy.fformfit._make_energy_func_wrapper(
            self.fform,
            self.fdat,
            self.free_symbols,
            False,
            'tests/Data/myfilename.so',
        )
        shgpy.fformfit._load_func('tests/Data/myfilename.so')
        shgpy.fformfit._make_energy_func_wrapper(
            self.fform,
            self.fdat,
            self.free_symbols,
            False,
            'tests/Data/myfilename.so',
        )
        shgpy.fformfit._load_func('tests/Data/myfilename.so')

    def test_least_squares(self):
        ret1 = least_squares_fit(self.fform, self.fdat, self.guess_dict)
        ret2 = least_squares_fit_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
        )
        for ret in [ret1, ret2]:
            self.assertAlmostEqual(abs(ret.xdict[S.psi]), 1.59, delta=0.1)
            self.assertAlmostEqual(
                abs(ret.xdict[shgpy.map_to_real(S.zyx)]),
                1.23,
                delta=0.1,
            )

        fit_dat = shgpy.fform_to_dat(self.fform, ret1.xdict, 1000)
        if MANUAL:
            easy_plot(
                [self.dat, fit_dat],
                [
                    {
                        'linestyle': ' ',
                        'markerfacecolor': 'none',
                        'color': 'blue',
                        'marker': 'o',
                    },
                    {
                        'linestyle': '-',
                        'color': 'blue',
                    },
                ],
                ['PP', 'PS', 'SP', 'SS'],
                show_plot=True,
                filename=None,
                show_legend=False,
            )
            easy_polar_plot(
                [self.dat, fit_dat],
                [
                    {
                        'linestyle': ' ',
                        'markerfacecolor': 'none',
                        'color': 'blue',
                        'marker': 'o'
                    },
                    {
                        'linestyle': '-',
                        'color': 'blue',
                    },
                ],
                ['PP', 'PS', 'SP', 'SS'],
                show_plot=True,
                filename=None,
                show_legend=False,
            )

    def test_basinhopping(self):
        niter = 100
        ret1 = basinhopping_fit(
            self.fform,
            self.fdat,
            self.guess_dict,
            niter,
        )
        ret2 = basinhopping_fit_jac(
            self.fform,
            self.fdat,
            self.guess_dict,
            niter,
        )
        ret3 = basinhopping_fit_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            niter,
        )
        ret4 = basinhopping_fit_jac_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            niter,
        )
        for ret in [ret1, ret2, ret3, ret4]:
            self.assertAlmostEqual(abs(ret.xdict[S.psi]), 1.59, delta=0.1)
            self.assertAlmostEqual(
                abs(ret.xdict[shgpy.map_to_real(S.zyx)]),
                1.23,
                delta=0.1,
            )

    def test_annealing(self):
        maxiter = 100
        ret1 = dual_annealing_fit_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            maxiter,
        )
        for ret in [ret1]:
            self.assertAlmostEqual(abs(ret.xdict[S.psi]), 1.59, delta=0.1)
            self.assertAlmostEqual(
                abs(ret.xdict[shgpy.map_to_real(S.zyx)]),
                1.23,
                delta=0.1,
            )

    def test_save(self):

        ret1 = dual_annealing_fit_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            maxiter=100,
            save_cost_func_filename='tests/Data/costfunc.so',
        )

        ret2 = dual_annealing_fit_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            maxiter=100,
            load_cost_func_filename='tests/Data/costfunc.so',
        )

        ret3 = basinhopping_fit_jac_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            100,
            save_cost_func_filename='tests/Data/costfunc.so',
            grad_save_cost_func_filename_prefix='tests/Data/grad_costfunc',
        )
        ret4 = basinhopping_fit_jac_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            100,
            load_cost_func_filename='tests/Data/costfunc.so',
            load_grad_cost_func_filename_prefix='tests/Data/grad_costfunc',
        )
        ret5 = basinhopping_fit_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            100,
            save_cost_func_filename='tests/Data/costfunc.so',
            chunk_cost_func=True,
        )
        ret6 = basinhopping_fit_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            100,
            load_cost_func_filename='tests/Data/costfunc.so',
            chunk_cost_func=True,
        )
        gen_cost_func(
            self.fform,
            self.fdat,
            save_filename='tests/Data/generated_costfunc.so',
        )
        ret7 = dual_annealing_fit_with_bounds(
            self.fform,
            self.fdat,
            self.guess_dict,
            self.bounds_dict,
            maxiter=100,
            load_cost_func_filename='tests/Data/generated_costfunc.so',
        )

        for ret in [ret1, ret2, ret3, ret4, ret5, ret6, ret7]:
            self.assertAlmostEqual(abs(ret.xdict[S.psi]), 1.59, delta=0.1)
            self.assertAlmostEqual(
                abs(ret.xdict[shgpy.map_to_real(S.zyx)]),
                1.23,
                delta=0.1,
            )