def test_set_Ih_values_to_target(test_sg): """Test the setting of Ih values for targeted scaling.""" """Generate input for testing joint_Ih_table.""" Ih_table = IhTable( [generated_refl_for_splitting_1(), generated_refl_for_splitting_2()], test_sg, nblocks=2, ) Ih_table.calc_Ih() # First check that values are set up correctly. block_list = Ih_table.blocked_data_list assert list(block_list[0].Ih_values) == pytest.approx( [6.0, 17.0 / 3.0, 17.0 / 3.0, 6.0, 17.0 / 3.0]) assert list(block_list[1].Ih_values) == pytest.approx( [16.0 / 3.0, 16.0 / 3.0, 7.0, 16.0 / 3.0, 7.0, 7.0]) # set some values in the target # change the (2, 0, 0) reflections to (4, 0, 0) to test if they are removed # from the blocks t1 = generated_refl_for_splitting_1() t2 = generated_refl_for_splitting_2() t1["miller_index"][1] = (4, 0, 0) t1["miller_index"][5] = (4, 0, 0) t2["miller_index"][1] = (4, 0, 0) target = IhTable([t1, t2], test_sg, nblocks=1) target.blocked_data_list[0].Ih_table["Ih_values"] = np.array( [0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.1, 0.2, 0.3, 0.4, 0.4]) for block in block_list: block.match_Ih_values_to_target(target) assert list(block_list[0].Ih_values) == [0.1, 0.2, 0.2, 0.1, 0.2] assert list(block_list[1].Ih_values) == [0.4, 0.4, 0.4]
def test_set_Ih_values_to_target(test_sg): """Test the setting of Ih values for targeted scaling.""" """Generate input for testing joint_Ih_table.""" Ih_table = IhTable( [generated_refl_for_splitting_1(), generated_refl_for_splitting_2()], test_sg, nblocks=2, ) Ih_table.calc_Ih() # First check that values are set up correctly. block_list = Ih_table.blocked_data_list assert list(block_list[0].Ih_values) == [ 6.0, 17.0 / 3.0, 17.0 / 3.0, 6.0, 17.0 / 3.0, ] assert list(block_list[1].Ih_values) == [ 16.0 / 3.0, 16.0 / 3.0, 7.0, 16.0 / 3.0, 7.0, 7.0, ] target = IhTable( [generated_refl_for_splitting_1(), generated_refl_for_splitting_2()], test_sg, nblocks=1, ) # set some values in the target # change the (2, 0, 0) reflections to (4, 0, 0) to test if they are removed # from the blocks vals = target.blocked_data_list[0].asu_miller_index vals[3] = (4, 0, 0) vals[4] = (4, 0, 0) vals[8] = (4, 0, 0) target.blocked_data_list[0].Ih_table["Ih_values"] = flex.double( [0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.1, 0.2, 0.3, 0.4, 0.4]) for block in block_list: block.match_Ih_values_to_target(target) assert list(block_list[0].Ih_values) == [0.1, 0.2, 0.2, 0.1, 0.2] assert list(block_list[1].Ih_values) == [0.4, 0.4, 0.4]
def test_IhTable_freework(large_reflection_table, small_reflection_table, test_sg): sel1 = flex.bool(7, True) sel1[6] = False sel2 = flex.bool(4, True) sel2[1] = False Ih_table = IhTable( reflection_tables=[ large_reflection_table.select(sel1), small_reflection_table.select(sel2), ], indices_lists=[sel1.iselection(), sel2.iselection()], space_group=test_sg, nblocks=2, free_set_percentage=50.0, ) assert len(Ih_table.blocked_data_list) == 3 assert Ih_table.n_datasets == 2 assert Ih_table.n_work_blocks == 2 block_list = Ih_table.Ih_table_blocks # two standard blocks assert block_list[0].h_index_matrix[0, 0] == 1 assert block_list[0].h_index_matrix.non_zeroes == 1 assert block_list[1].h_index_matrix[0, 0] == 1 assert block_list[1].h_index_matrix[1, 0] == 1 assert block_list[1].h_index_matrix[2, 0] == 1 assert block_list[1].h_index_matrix.non_zeroes == 3 # free set block assert block_list[2].h_index_matrix[0, 0] == 1 assert block_list[2].h_index_matrix[1, 1] == 1 assert block_list[2].h_index_matrix[2, 2] == 1 assert block_list[2].h_index_matrix[3, 2] == 1 assert block_list[2].h_index_matrix[4, 3] == 1 assert block_list[2].h_index_matrix.non_zeroes == 5 assert list(block_list[0].block_selections[0]) == [5] assert list(block_list[0].block_selections[1]) == [] assert list(block_list[1].block_selections[0]) == [0, 2] assert list(block_list[1].block_selections[1]) == [0] assert list(block_list[2].block_selections[0]) == [1, 3, 4] assert list(block_list[2].block_selections[1]) == [3, 2] # test get_block_selections_for_dataset block_sels_0 = Ih_table.get_block_selections_for_dataset(0) assert len(block_sels_0) == 3 assert list(block_sels_0[0]) == [5] assert list(block_sels_0[1]) == [0, 2] assert list(block_sels_0[2]) == [1, 3, 4] block_sels_1 = Ih_table.get_block_selections_for_dataset(1) assert len(block_sels_1) == 3 assert list(block_sels_1[0]) == [] assert list(block_sels_1[1]) == [0] assert list(block_sels_1[2]) == [3, 2] with pytest.raises(AssertionError): _ = Ih_table.get_block_selections_for_dataset(2) Ih_table.calc_Ih() # test setting data # set scale factors new_s_block_2 = flex.double(range(1, 6)) Ih_table.set_inverse_scale_factors(new_s_block_2, 2) assert list(Ih_table.Ih_table_blocks[2].inverse_scale_factors) == list( new_s_block_2 ) # set derivatives derivs = Mock() Ih_table.set_derivatives(derivs, 0) assert Ih_table.Ih_table_blocks[0].derivatives is derivs def update_vars_side_effect(*args): return flex.double([0.5] * len(args[0])) # test setting an error model em = Mock() em.update_variances.side_effect = update_vars_side_effect Ih_table.update_weights(em) for block in Ih_table.Ih_table_blocks: assert list(block.weights) == pytest.approx([2.0] * block.size) Ih_table.calc_Ih(1) # now test free set with offset Ih_table = IhTable( reflection_tables=[ large_reflection_table.select(sel1), small_reflection_table.select(sel2), ], indices_lists=[sel1.iselection(), sel2.iselection()], space_group=test_sg, nblocks=2, free_set_percentage=50.0, free_set_offset=1, ) assert len(Ih_table.blocked_data_list) == 3 assert Ih_table.n_datasets == 2 assert Ih_table.n_work_blocks == 2 block_list = Ih_table.Ih_table_blocks # two standard blocks assert block_list[0].h_index_matrix[0, 0] == 1 assert block_list[0].h_index_matrix[1, 1] == 1 assert block_list[0].h_index_matrix.non_zeroes == 2 assert block_list[1].h_index_matrix[0, 0] == 1 assert block_list[1].h_index_matrix[1, 0] == 1 assert block_list[1].h_index_matrix[2, 1] == 1 assert block_list[1].h_index_matrix.non_zeroes == 3 # free set block assert block_list[2].h_index_matrix[0, 0] == 1 assert block_list[2].h_index_matrix[1, 1] == 1 assert block_list[2].h_index_matrix[2, 1] == 1 assert block_list[2].h_index_matrix[3, 1] == 1 assert block_list[2].h_index_matrix.non_zeroes == 4 assert list(block_list[0].block_selections[0]) == [1, 3] assert list(block_list[0].block_selections[1]) == [] assert list(block_list[1].block_selections[0]) == [4] assert list(block_list[1].block_selections[1]) == [3, 2] assert list(block_list[2].block_selections[0]) == [5, 0, 2] assert list(block_list[2].block_selections[1]) == [0] Ih_table.calc_Ih() # Test the 'as_miller_array' method. unit_cell = uctbx.unit_cell((1.0, 1.0, 1.0, 90.0, 90.0, 90.0)) arr = Ih_table.as_miller_array(unit_cell) assert arr.size() == 5 assert list(arr.indices()) == [ (0, 0, 1), (0, 2, 0), (0, 4, 0), (0, 4, 0), (10, 0, 0), ] assert list(arr.data()) == pytest.approx( [x / 2.0 for x in [100.0, 60.0, 30.0, 30.0, 10.0]] ) assert list(arr.sigmas()) == pytest.approx( [(x / 4.0) ** 0.5 for x in [100.0, 60.0, 30.0, 30.0, 10.0]] ) arr = Ih_table.as_miller_array(unit_cell, return_free_set_data=True) assert arr.size() == 4 assert list(arr.indices()) == [(0, 0, 2), (1, 0, 0), (1, 0, 0), (1, 0, 0)] assert list(arr.data()) == pytest.approx( [x / 2.0 for x in [40.0, 100.0, 80.0, 60.0]] ) assert list(arr.sigmas()) == pytest.approx( [(x / 4.0) ** 0.5 for x in [50.0, 90.0, 90.0, 60.0]] )