Ejemplo n.º 1
0
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]]
    )
Ejemplo n.º 2
0
def test_IhTable_split_into_blocks(
    large_reflection_table, small_reflection_table, test_sg
):
    """Test that the Ih_table datastructure correctly organises the data
    from two reflection tables into two IhTableBlocks."""

    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,
    )

    assert Ih_table.n_datasets == 2
    assert Ih_table.n_work_blocks == 2
    block_list = Ih_table.Ih_table_blocks
    assert list(block_list[0].Ih_table["asu_miller_index"]) == [
        (0, 0, 1),
        (0, 0, 2),
        (0, 2, 0),
    ]
    assert list(block_list[1].Ih_table["asu_miller_index"]) == [
        (0, 4, 0),
        (1, 0, 0),
        (1, 0, 0),
        (0, 4, 0),
        (1, 0, 0),
        (10, 0, 0),
    ]
    assert list(block_list[0].block_selections[0]) == [1, 5, 3]
    assert list(block_list[0].block_selections[1]) == []
    assert list(block_list[1].block_selections[0]) == [4, 0, 2]
    assert list(block_list[1].block_selections[1]) == [3, 0, 2]

    # test the 'get_block_selections_for_dataset' method
    block_sels_0 = Ih_table.get_block_selections_for_dataset(dataset=0)
    assert len(block_sels_0) == 2
    assert list(block_sels_0[0]) == [1, 5, 3]
    assert list(block_sels_0[1]) == [4, 0, 2]
    block_sels_1 = Ih_table.get_block_selections_for_dataset(dataset=1)
    assert len(block_sels_1) == 2
    assert list(block_sels_1[0]) == []
    assert list(block_sels_1[1]) == [3, 0, 2]

    # test the size method
    assert Ih_table.size == 9

    expected_h_idx_matrix = sparse.matrix(4, 3)
    expected_h_idx_matrix[0, 0] = 1
    expected_h_idx_matrix[1, 1] = 1
    expected_h_idx_matrix[2, 2] = 1
    assert block_list[0].h_index_matrix == expected_h_idx_matrix
    expected_h_idx_matrix = sparse.matrix(7, 3)
    expected_h_idx_matrix[0, 0] = 1
    expected_h_idx_matrix[1, 1] = 1
    expected_h_idx_matrix[2, 1] = 1
    expected_h_idx_matrix[3, 0] = 1
    expected_h_idx_matrix[4, 1] = 1
    expected_h_idx_matrix[5, 2] = 1
    assert block_list[1].h_index_matrix == expected_h_idx_matrix

    # check that only dataset 1 intensities are updated
    new_intensities = flex.double([60.0, 50.0, 40.0, 30.0, 20.0, 10.0])
    Ih_table.update_data_in_blocks(data=new_intensities, dataset_id=0)
    assert list(Ih_table.blocked_data_list[0].intensities) == [50.0, 10.0, 30.0]
    assert list(Ih_table.blocked_data_list[1].intensities) == [
        20.0,
        60.0,
        40.0,
        30.0,
        60.0,
        10.0,
    ]
    # try updating variances
    new_vars = flex.double([100.0, 200.0, 300.0, 400.0])
    Ih_table.update_data_in_blocks(data=new_vars, dataset_id=1, column="variance")
    assert list(Ih_table.blocked_data_list[0].variances) == [100.0, 50.0, 60.0]
    assert list(Ih_table.blocked_data_list[1].variances) == [
        30.0,
        90.0,
        90.0,
        400.0,
        100.0,
        300.0,
    ]