Ejemplo n.º 1
0
def test_ReflectionModelState(test_experiment):

    experiments = [test_experiment]

    S1 = Simple1MosaicityParameterisation()
    S6 = Simple6MosaicityParameterisation()
    W = WavelengthSpreadParameterisation()

    check_reflection_model_state_with_fixed(experiments[0],
                                            S1,
                                            None,
                                            fix_wavelength_spread=True)
    check_reflection_model_state_with_fixed(experiments[0],
                                            S1,
                                            W,
                                            fix_mosaic_spread=True)
    check_reflection_model_state_with_fixed(experiments[0],
                                            S1,
                                            W,
                                            fix_wavelength_spread=True)
    check_reflection_model_state_with_fixed(experiments[0],
                                            S1,
                                            W,
                                            fix_unit_cell=True)
    check_reflection_model_state_with_fixed(experiments[0],
                                            S1,
                                            W,
                                            fix_orientation=True)

    check_reflection_model_state_with_fixed(experiments[0],
                                            S6,
                                            None,
                                            fix_wavelength_spread=True)
    check_reflection_model_state_with_fixed(experiments[0],
                                            S6,
                                            W,
                                            fix_mosaic_spread=True)
    check_reflection_model_state_with_fixed(experiments[0],
                                            S6,
                                            W,
                                            fix_wavelength_spread=True)
    check_reflection_model_state_with_fixed(experiments[0],
                                            S6,
                                            W,
                                            fix_unit_cell=True)
    check_reflection_model_state_with_fixed(experiments[0],
                                            S6,
                                            W,
                                            fix_orientation=True)
Ejemplo n.º 2
0
def test_Simple1MosaicityParameterisation():

    p = Simple1MosaicityParameterisation(params=np.array([1e-3]))

    assert p.is_angular() is False
    assert p.num_parameters() == 1
    assert p.parameters == (1e-3, )
    p.parameters = np.array([2e-3])
    assert p.parameters == (2e-3, )
    psq = p.parameters[0]**2
    assert list(p.sigma().flatten()) == pytest.approx(
        [psq, 0, 0, 0, psq, 0, 0, 0, psq])
    d = p.first_derivatives()
    assert d.shape[0] == 1
    d1 = 2 * p.parameters[0]
    assert list(d[0, :, :].flatten()) == pytest.approx(
        [d1, 0, 0, 0, d1, 0, 0, 0, d1])
Ejemplo n.º 3
0
def simple1_model_state(test_experiment):

    state = ModelState(test_experiment, Simple1MosaicityParameterisation())

    return state
Ejemplo n.º 4
0
def test_ReflectionModelState_derivatives(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 = testdata.s0
        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 = np.array(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

        model = ReflectionModelState(state, s0, h)

        dr_dp = model.get_dr_dp()
        dS_dp = model.get_dS_dp()
        dL_dp = model.get_dL_dp()

        def compute_sigma(parameters):
            state.active_parameters = parameters
            model = ReflectionModelState(state, s0, h)
            return model.mosaicity_covariance_matrix

        def compute_r(parameters):
            state.active_parameters = parameters
            model = ReflectionModelState(state, s0, h)
            return model.get_r()

        def compute_sigma_lambda(parameters):
            state.active_parameters = parameters
            model = ReflectionModelState(state, s0, h)
            return model.wavelength_spread

        step = 1e-6

        parameters = copy(state.active_parameters)

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

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

            dr_num.append(
                first_derivative(f, parameters[i], step).reshape(3, 1))
        dr_num = np.concatenate(dr_num, axis=1)

        for n, c in zip(dr_num, dr_dp):
            for nn, cc in zip(n, c):
                print(nn)
                print(cc)
                assert abs(nn - cc) < 1e-7

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

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

            fd = first_derivative(f, parameters[i], step)
            print(fd)
            ds_num.append(fd.reshape(3, 3, 1))
        ds_num = np.concatenate(ds_num, axis=2)

        for i in range(len(parameters)):
            for n, c in zip(ds_num[:, :, i], dS_dp[:, :, i]):
                for nn, cc in zip(n.flatten(), c.flatten()):
                    print(nn)
                    print(cc)
                    assert abs(nn - cc) < 1e-5

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

            def f(x):
                p = copy(parameters)
                p[i] = x
                return compute_sigma_lambda(p)**2

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

        for n, c in zip(dl_num, dL_dp):
            assert abs(n - c) < 1e-7

    S1 = Simple1MosaicityParameterisation()
    S6 = Simple6MosaicityParameterisation()
    W = WavelengthSpreadParameterisation()

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

    check(S6, None, fix_wavelength_spread=True)
    check(S6, W, fix_mosaic_spread=True)
    check(S6, W, fix_wavelength_spread=True)
    check(S6, W, fix_unit_cell=True)
    check(S6, W, fix_orientation=True)
Ejemplo n.º 5
0
def test_ModelState(test_experiment):

    experiments = [test_experiment]

    S1 = Simple1MosaicityParameterisation()
    S6 = Simple6MosaicityParameterisation()
    W = WavelengthSpreadParameterisation()

    with pytest.raises(AssertionError):
        check_model_state_with_fixed(experiments[0],
                                     S1,
                                     None,
                                     fix_mosaic_spread=True)
    with pytest.raises(AssertionError):
        check_model_state_with_fixed(experiments[0],
                                     S1,
                                     None,
                                     fix_unit_cell=True)
    with pytest.raises(AssertionError):
        check_model_state_with_fixed(experiments[0],
                                     S1,
                                     None,
                                     fix_orientation=True)
    check_model_state_with_fixed(experiments[0],
                                 S1,
                                 None,
                                 fix_wavelength_spread=True)
    check_model_state_with_fixed(experiments[0], S1, W, fix_mosaic_spread=True)
    check_model_state_with_fixed(experiments[0],
                                 S1,
                                 W,
                                 fix_wavelength_spread=True)
    check_model_state_with_fixed(experiments[0], S1, W, fix_unit_cell=True)
    check_model_state_with_fixed(experiments[0], S1, W, fix_orientation=True)

    with pytest.raises(AssertionError):
        check_model_state_with_fixed(experiments[0],
                                     S6,
                                     None,
                                     fix_mosaic_spread=True)
    with pytest.raises(AssertionError):
        check_model_state_with_fixed(experiments[0],
                                     S6,
                                     None,
                                     fix_unit_cell=True)
    with pytest.raises(AssertionError):
        check_model_state_with_fixed(experiments[0],
                                     S6,
                                     None,
                                     fix_orientation=True)
    check_model_state_with_fixed(experiments[0],
                                 S6,
                                 None,
                                 fix_wavelength_spread=True)
    check_model_state_with_fixed(experiments[0], S6, W, fix_mosaic_spread=True)
    check_model_state_with_fixed(experiments[0],
                                 S6,
                                 W,
                                 fix_wavelength_spread=True)
    check_model_state_with_fixed(experiments[0], S6, W, fix_unit_cell=True)
    check_model_state_with_fixed(experiments[0], S6, W, fix_orientation=True)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
    def parameterisation(self):
        """
        Get the parameterisation

        """
        return Simple1MosaicityParameterisation(self.params)