コード例 #1
0
ファイル: test_refiner.py プロジェクト: kmdalton/dials
def test_ConditionalDistribution(testdata):
    def check(
        mosaicity_parameterisation,
        wavelength_parameterisation,
        fix_mosaic_spread=False,
        fix_wavelength_spread=False,
        fix_unit_cell=False,
        fix_orientation=False,
    ):
        experiment = testdata.experiment
        models = testdata.models
        h = testdata.h
        s0 = np.array(testdata.s0).reshape(3, 1)
        sp = np.array(testdata.sp).reshape(3, 1)
        # ctot = testdata.ctot
        # mobs = testdata.mobs
        # Sobs = testdata.Sobs

        U_params = models[1].get_param_vals()
        B_params = models[2].get_param_vals()
        M_params = models[0][:mosaicity_parameterisation.num_parameters()]
        L_params = models[3]

        state = ModelState(
            experiment,
            mosaicity_parameterisation,
            wavelength_parameterisation,
            fix_mosaic_spread=fix_mosaic_spread,
            fix_wavelength_spread=fix_wavelength_spread,
            fix_unit_cell=fix_unit_cell,
            fix_orientation=fix_orientation,
        )
        state.U_params = np.array(U_params, dtype=np.float64)
        state.B_params = np.array(B_params, dtype=np.float64)
        state.M_params = np.array(M_params, dtype=np.float64)
        state.L_params = L_params

        def get_conditional(state):
            conditional = ReflectionLikelihood(
                state,
                s0,
                sp,
                h,
                0,
                np.array([0.0, 0.0]),
                np.array([0.0, 0.0, 0.0, 0.0]).reshape(2, 2),
            ).conditional
            return conditional

        conditional = get_conditional(state)

        step = 1e-6

        dm_dp = conditional.first_derivatives_of_mean()
        dS_dp = conditional.first_derivatives_of_sigma()
        parameters = state.active_parameters

        def compute_sigma(parameters):
            state.active_parameters = parameters
            return get_conditional(state).sigma()

        def compute_mean(parameters):
            state.active_parameters = parameters
            return get_conditional(state).mean()

        dm_num = []
        for i in range(len(parameters)):

            def f(x):
                p = copy.copy(parameters)
                p[i] = x
                return compute_mean(p)

            dm_num.append(first_derivative(f, parameters[i], step))

        for n, c in zip(dm_num, dm_dp):
            for nn, cc in zip(n, c):
                print(nn)
                print(cc)
                assert abs(nn - cc) < 1e-7
            # assert all(abs(nn - cc) < 1e-7 for nn, cc in zip(n, c))

        ds_num = []
        for i in range(len(parameters)):

            def f(x):
                p = copy.copy(parameters)
                p[i] = x
                return compute_sigma(p)

            ds_num.append(first_derivative(f, parameters[i], step))

        for n, c in zip(ds_num, dS_dp):
            for nn, cc in zip(n.flatten(), c.flatten()):
                print(nn)
                print(cc)
                assert abs(nn - cc) < 1e-7
            # assert all(abs(nn - cc) < 1e-7 for nn, cc in zip(n, c))

    S1 = Simple1MosaicityParameterisation()
    S6 = Simple6MosaicityParameterisation()

    check(S1, None, fix_wavelength_spread=True)
    check(S1, None, fix_wavelength_spread=True, fix_mosaic_spread=True)
    check(S1, None, fix_wavelength_spread=True, fix_unit_cell=True)
    check(S1, None, fix_wavelength_spread=True, fix_orientation=True)

    check(S6, None, fix_wavelength_spread=True)
    check(S6, None, fix_wavelength_spread=True, fix_mosaic_spread=True)
    check(S6, None, fix_wavelength_spread=True, fix_unit_cell=True)
    check(S6, None, fix_wavelength_spread=True, fix_orientation=True)
コード例 #2
0
ファイル: test_refiner.py プロジェクト: kmdalton/dials
def test_ReflectionLikelihood(testdata):
    def check(
        mosaicity_parameterisation,
        wavelength_parameterisation,
        fix_mosaic_spread=False,
        fix_wavelength_spread=False,
        fix_unit_cell=False,
        fix_orientation=False,
    ):
        experiment = testdata.experiment
        models = testdata.models
        s0 = np.array(testdata.s0)
        sp = np.array(testdata.sp)
        h = testdata.h
        ctot = testdata.ctot
        mobs = testdata.mobs
        Sobs = testdata.Sobs

        U_params = models[1].get_param_vals()
        B_params = models[2].get_param_vals()
        M_params = np.array(
            models[0][:mosaicity_parameterisation.num_parameters()])
        L_params = flex.double(models[3])

        state = ModelState(
            experiment,
            mosaicity_parameterisation,
            wavelength_parameterisation,
            fix_mosaic_spread=fix_mosaic_spread,
            fix_wavelength_spread=fix_wavelength_spread,
            fix_unit_cell=fix_unit_cell,
            fix_orientation=fix_orientation,
        )
        state.U_params = U_params
        state.B_params = B_params
        state.M_params = M_params
        state.L_params = L_params

        def get_reflection_likelihood(state):
            return ReflectionLikelihood(state, s0, sp, h, ctot, mobs, Sobs)

        likelihood = get_reflection_likelihood(state)

        step = 1e-6

        dL_dp = likelihood.first_derivatives()

        parameters = state.active_parameters

        assert len(dL_dp) == len(parameters)

        def compute_likelihood(parameters):
            state.active_parameters = parameters
            likelihood = get_reflection_likelihood(state)
            return likelihood.log_likelihood()

        dL_num = []
        for i in range(len(parameters)):

            def f(x):
                p = copy.copy(parameters)
                p[i] = x
                return compute_likelihood(p)

            dL_num.append(first_derivative(f, parameters[i], step))

        assert len(dL_num) == len(parameters)
        print(dL_num)
        print(list(dL_dp))
        for n, c in zip(dL_num, dL_dp):
            print(n, c)
            assert n == pytest.approx(c, abs=1e-4)

    S1 = Simple1MosaicityParameterisation()
    S6 = Simple6MosaicityParameterisation()

    check(S1, None, fix_wavelength_spread=True)
    check(S1, None, fix_wavelength_spread=True, fix_mosaic_spread=True)
    check(S1, None, fix_wavelength_spread=True, fix_unit_cell=True)
    check(S1, None, fix_wavelength_spread=True, fix_orientation=True)

    check(S6, None, fix_wavelength_spread=True, fix_mosaic_spread=True)
    check(S6, None, fix_wavelength_spread=True, fix_unit_cell=True)
    check(S6, None, fix_wavelength_spread=True, fix_orientation=True)
    check(S6, None, fix_wavelength_spread=True)
コード例 #3
0
ファイル: model.py プロジェクト: kmdalton/dials
    def parameterisation(self):
        """
        Get the parameterisation

        """
        return Simple1MosaicityParameterisation(self.params)