def test_SingleScaler_update_for_minimisation(): """Test the update_for_minimisation method of the singlescaler.""" # test_params.scaling_options.nproc = 1 p, e, r = (generated_param(), generated_exp(), generated_refl_2()) exp = create_scaling_model(p, e, r) p.reflection_selection.method = "use_all" single_scaler = SingleScaler(p, exp[0], r) pmg = ScalingParameterManagerGenerator( single_scaler.active_scalers, ScalingTarget(), single_scaler.params.scaling_refinery.refinement_order, ) single_scaler.components["scale"].parameters /= 2.0 apm = pmg.parameter_managers()[0] Ih_table = single_scaler.Ih_table.blocked_data_list[0] Ih_table.calc_Ih() assert list(Ih_table.inverse_scale_factors) == [1.0, 1.0] assert list(Ih_table.Ih_values) == [10.0, 1.0] single_scaler.update_for_minimisation(apm, 0) # Should set new scale factors, and calculate Ih and weights. bf = RefinerCalculator.calculate_scales_and_derivatives(apm.apm_list[0], 0) assert list(Ih_table.inverse_scale_factors) == list(bf[0]) assert list(Ih_table.Ih_values) != [1.0, 10.0] assert list(Ih_table.Ih_values) == pytest.approx( list(Ih_table.intensities / bf[0])) for i in range(Ih_table.derivatives.n_rows): for j in range(Ih_table.derivatives.n_cols): assert Ih_table.derivatives[i, j] == pytest.approx(bf[1][i, j]) assert Ih_table.derivatives.non_zeroes == bf[1].non_zeroes
def test_multiscaler_update_for_minimisation(): """Test the multiscaler update_for_minimisation method.""" p, e = (generated_param(), generated_exp(2)) p.reflection_selection.method = "use_all" r1 = generated_refl(id_=0) r1["intensity.sum.value"] = r1["intensity"] r1["intensity.sum.variance"] = r1["variance"] r2 = generated_refl(id_=1) r2["intensity.sum.value"] = r2["intensity"] r2["intensity.sum.variance"] = r2["variance"] p.scaling_options.nproc = 2 p.model = "physical" exp = create_scaling_model(p, e, [r1, r2]) singlescaler1 = create_scaler(p, [exp[0]], [r1]) singlescaler2 = create_scaler(p, [exp[1]], [r2]) multiscaler = MultiScaler([singlescaler1, singlescaler2]) pmg = ScalingParameterManagerGenerator( multiscaler.active_scalers, ScalingTarget, multiscaler.params.scaling_refinery.refinement_order, ) multiscaler.single_scalers[0].components["scale"].parameters /= 2.0 multiscaler.single_scalers[1].components["scale"].parameters *= 1.5 apm = pmg.parameter_managers()[0] multiscaler.update_for_minimisation(apm, 0) multiscaler.update_for_minimisation(apm, 1) # bf[0], bf[1] should be list of scales and derivatives s1, d1 = RefinerCalculator.calculate_scales_and_derivatives( apm.apm_list[0], 0) s2, d2 = RefinerCalculator.calculate_scales_and_derivatives( apm.apm_list[1], 0) s3, d3 = RefinerCalculator.calculate_scales_and_derivatives( apm.apm_list[0], 1) s4, d4 = RefinerCalculator.calculate_scales_and_derivatives( apm.apm_list[1], 1) expected_scales_for_block_1 = s1 expected_scales_for_block_1.extend(s2) expected_scales_for_block_2 = s3 expected_scales_for_block_2.extend(s4) expected_derivatives_for_block_1 = sparse.matrix( expected_scales_for_block_1.size(), apm.n_active_params) expected_derivatives_for_block_2 = sparse.matrix( expected_scales_for_block_2.size(), apm.n_active_params) expected_derivatives_for_block_1.assign_block(d1, 0, 0) expected_derivatives_for_block_1.assign_block(d2, d1.n_rows, apm.apm_data[1]["start_idx"]) expected_derivatives_for_block_2.assign_block(d3, 0, 0) expected_derivatives_for_block_2.assign_block(d4, d3.n_rows, apm.apm_data[1]["start_idx"]) block_list = multiscaler.Ih_table.blocked_data_list assert block_list[0].inverse_scale_factors == expected_scales_for_block_1 assert block_list[1].inverse_scale_factors == expected_scales_for_block_2 assert block_list[1].derivatives == expected_derivatives_for_block_2 assert block_list[0].derivatives == expected_derivatives_for_block_1
def test_scaling_active_parameter_manager(): """Test the scaling-specific parameter manager.""" components_2 = {"1": mock_scaling_component(2), "2": mock_scaling_component(2)} scaling_apm = scaling_active_parameter_manager(components_2, ["1"]) assert list(scaling_apm.constant_g_values[0]) == list( components_2["2"].calculate_scales() ) assert len(scaling_apm.constant_g_values) == 1 assert scaling_apm.n_obs == [2] # Test that no constant_g_values if both components selected scaling_apm = scaling_active_parameter_manager(components_2, ["1", "2"]) assert scaling_apm.constant_g_values is None # Check that one can't initialise with an unequal number of reflections, # either within the selection or overall. with pytest.raises(AssertionError): components_2 = {"1": mock_scaling_component(2), "2": mock_scaling_component(1)} scaling_apm = scaling_active_parameter_manager(components_2, ["1", "2"]) with pytest.raises(AssertionError): components_2 = {"1": mock_scaling_component(2), "2": mock_scaling_component(1)} scaling_apm = scaling_active_parameter_manager(components_2, ["1"]) data_manager = mock_data_manager(components_2) pmg = ScalingParameterManagerGenerator([data_manager], mode="concurrent") assert isinstance(pmg.apm_type, type(scaling_active_parameter_manager))