Пример #1
0
def test_ScalingModelObserver():
    """Test that the observer correctly logs data when passed a scaler."""

    KB_dict = {
        "__id__": "KB",
        "is_scaled": True,
        "scale": {
            "n_parameters": 1,
            "parameters": [0.5],
            "est_standard_devs": [0.05],
            "null_parameter_value": 1,
        },
        "configuration_parameters": {
            "corrections": ["scale"]
        },
    }

    KBmodel = KBScalingModel.from_dict(KB_dict)
    experiment = Experiment()
    experiment.scaling_model = KBmodel
    experiment.identifier = "0"

    scaler1 = mock.Mock()
    scaler1.experiment = experiment
    scaler1.active_scalers = None

    observer = ScalingModelObserver()
    observer.update(scaler1)
    assert observer.data["0"] == KB_dict

    msg = observer.return_model_error_summary()
    assert msg != ""

    mock_func = mock.Mock()
    mock_func.return_value = {"plot": {"layout": {"title": ""}}}

    with mock.patch("dials.algorithms.scaling.observers.plot_scaling_models",
                    new=mock_func):
        observer.make_plots()
        assert mock_func.call_count == 1
        assert mock_func.call_args_list == [mock.call(KB_dict)]

    experiment2 = Experiment()
    experiment2.scaling_model = KBmodel
    experiment2.identifier = "1"
    scaler2 = mock.Mock()
    scaler2.experiment = experiment2
    scaler2.active_scalers = None

    multiscaler = mock.Mock()
    multiscaler.active_scalers = [scaler1, scaler2]
    observer.data = {}
    observer.update(multiscaler)
    assert observer.data["0"] == KB_dict
    assert observer.data["1"] == KB_dict

    mock_func = mock.Mock()
    mock_func.return_value = {"plot": {"layout": {"title": ""}}}

    with mock.patch("dials.algorithms.scaling.observers.plot_scaling_models",
                    new=mock_func):
        r = observer.make_plots()
        assert mock_func.call_count == 2
        assert mock_func.call_args_list == [
            mock.call(KB_dict), mock.call(KB_dict)
        ]
        assert all(i in r["scaling_model"] for i in ["plot_0", "plot_1"])
Пример #2
0
def test_KBScalingModel():
    """Test for the KB Scaling Model."""

    # Test standard initialisation method.
    configdict = {"corrections": ["scale", "decay"]}
    parameters_dict = {
        "scale": {
            "parameters": flex.double([1.2]),
            "parameter_esds": flex.double([0.1]),
        },
        "decay": {
            "parameters": flex.double([0.01]),
            "parameter_esds": flex.double([0.02]),
        },
    }
    KBmodel = KBScalingModel(parameters_dict, configdict)
    assert KBmodel.id_ == "KB"
    assert "scale" in KBmodel.components
    assert "decay" in KBmodel.components
    assert list(KBmodel.components["scale"].parameters) == [1.2]
    assert list(KBmodel.components["decay"].parameters) == [0.01]
    assert list(KBmodel.components["scale"].parameter_esds) == [0.1]
    assert list(KBmodel.components["decay"].parameter_esds) == [0.02]

    # Test from_dict initialisation method.
    KB_dict = {
        "__id__": "KB",
        "is_scaled": True,
        "scale": {
            "n_parameters": 1,
            "parameters": [0.5],
            "est_standard_devs": [0.05],
            "null_parameter_value": 1,
        },
        "configuration_parameters": {
            "corrections": ["scale"]
        },
    }
    KBmodel = KBScalingModel.from_dict(KB_dict)
    assert KBmodel.is_scaled is True
    assert "scale" in KBmodel.components
    assert "decay" not in KBmodel.components
    assert list(KBmodel.components["scale"].parameters) == [0.5]
    assert list(KBmodel.components["scale"].parameter_esds) == [0.05]

    new_dict = KBmodel.to_dict()
    assert new_dict == KB_dict

    # Test again with all parameters
    KB_dict = {
        "__id__": "KB",
        "is_scaled": True,
        "scale": {
            "n_parameters": 1,
            "parameters": [0.5],
            "est_standard_devs": [0.05],
            "null_parameter_value": 1,
        },
        "decay": {
            "n_parameters": 1,
            "parameters": [0.2],
            "est_standard_devs": [0.02],
            "null_parameter_value": 0,
        },
        "configuration_parameters": {
            "corrections": ["scale", "decay"]
        },
    }
    KBmodel = KBScalingModel.from_dict(KB_dict)
    assert KBmodel.is_scaled is True
    assert "scale" in KBmodel.components
    assert "decay" in KBmodel.components
    assert list(KBmodel.components["scale"].parameters) == [0.5]
    assert list(KBmodel.components["scale"].parameter_esds) == [0.05]
    assert list(KBmodel.components["decay"].parameters) == [0.2]
    assert list(KBmodel.components["decay"].parameter_esds) == [0.02]

    new_dict = KBmodel.to_dict()
    assert new_dict == KB_dict

    with pytest.raises(RuntimeError):
        KB_dict["__id__"] = "physical"
        KBmodel = KBScalingModel.from_dict(KB_dict)

    assert KBmodel.consecutive_refinement_order == [["scale", "decay"]]
    assert "Decay component" in str(KBmodel)
Пример #3
0
    def from_dict(d):
        """creates a scaling model from a dict"""
        from dials.algorithms.scaling.model.model import KBScalingModel

        return KBScalingModel.from_dict(d)