Exemple #1
0
def _prepare_merged_reflection_table(experiments, reflections, d_min=None):
    """Filter the data and prepare a reflection table with merged data."""
    if ("inverse_scale_factor" in reflections[0]
            and "intensity.scale.value" in reflections[0]):
        logger.info("Attempting to perform absence checks on scaled data")
        reflections = filter_reflection_table(reflections[0],
                                              intensity_choice=["scale"],
                                              d_min=d_min)
        reflections["intensity"] = reflections["intensity.scale.value"]
        reflections["variance"] = reflections["intensity.scale.variance"]
    else:
        logger.info(
            "Attempting to perform absence checks on unscaled profile-integrated data"
        )
        reflections = filter_reflection_table(reflections[0],
                                              intensity_choice=["profile"],
                                              d_min=d_min)
        reflections["intensity"] = reflections["intensity.prf.value"]
        reflections["variance"] = reflections["intensity.prf.variance"]

    # now merge
    space_group = experiments[0].crystal.get_space_group()
    reflections["asu_miller_index"] = map_indices_to_asu(
        reflections["miller_index"], space_group)
    reflections["inverse_scale_factor"] = flex.double(reflections.size(), 1.0)
    merged = (_reflection_table_to_iobs(
        reflections, experiments[0].crystal.get_unit_cell(),
        space_group).merge_equivalents(use_internal_variance=False).array())
    merged_reflections = flex.reflection_table()
    merged_reflections["intensity"] = merged.data()
    merged_reflections["variance"] = merged.sigmas()**2
    merged_reflections["miller_index"] = merged.indices()
    return merged_reflections
Exemple #2
0
def run_sys_abs_checks(experiments, reflections, d_min=None, significance_level=0.95):
    """Check for systematic absences in the data for the laue group.

    Select the good data, merge, test screw axes and score possible space
    groups. The crystals are updated with the most likely space group.
    """

    if (
        "inverse_scale_factor" in reflections[0]
        and "intensity.scale.value" in reflections[0]
    ):
        logger.info("Attempting to perform absence checks on scaled data")
        reflections = filter_reflection_table(
            reflections[0], intensity_choice=["scale"], d_min=d_min
        )
        reflections["intensity"] = reflections["intensity.scale.value"]
        reflections["variance"] = reflections["intensity.scale.variance"]
    else:
        logger.info(
            "Attempting to perform absence checks on unscaled profile-integrated data"
        )
        reflections = filter_reflection_table(
            reflections[0], intensity_choice=["profile"], d_min=d_min
        )
        reflections["intensity"] = reflections["intensity.prf.value"]
        reflections["variance"] = reflections["intensity.prf.variance"]

    # now merge
    space_group = experiments[0].crystal.get_space_group()
    reflections["asu_miller_index"] = map_indices_to_asu(
        reflections["miller_index"], space_group
    )
    reflections["inverse_scale_factor"] = flex.double(reflections.size(), 1.0)
    merged = (
        _reflection_table_to_iobs(
            reflections, experiments[0].crystal.get_unit_cell(), space_group
        )
        .merge_equivalents(use_internal_variance=False)
        .array()
    )
    merged_reflections = flex.reflection_table()
    merged_reflections["intensity"] = merged.data()
    merged_reflections["variance"] = merged.sigmas() ** 2
    merged_reflections["miller_index"] = merged.indices()

    # Get the laue class from the space group.
    laue_group = str(space_group.build_derived_patterson_group().info())
    logger.info("Laue group: %s", laue_group)
    if laue_group not in laue_groups:
        logger.info("No absences to check for this laue group")
        return

    # Score the screw axes.
    screw_axes, screw_axis_scores = score_screw_axes(
        laue_groups[laue_group], merged_reflections, significance_level
    )

    logger.info(
        simple_table(
            [
                [
                    a.name,
                    "%.3f" % score,
                    str(a.n_refl_used[0]),
                    str(a.n_refl_used[1]),
                    "%.3f" % a.mean_I,
                    "%.3f" % a.mean_I_abs,
                    "%.3f" % a.mean_I_sigma,
                    "%.3f" % a.mean_I_sigma_abs,
                ]
                for a, score in zip(screw_axes, screw_axis_scores)
            ],
            column_headers=[
                "Screw axis",
                "Score",
                "No. present",
                "No. absent",
                "<I> present",
                "<I> absent",
                "<I/sig> present",
                "<I/sig> absent",
            ],
        ).format()
    )

    # Score the space groups from the screw axis scores.
    space_groups, scores = score_space_groups(
        screw_axis_scores, laue_groups[laue_group]
    )

    logger.info(
        simple_table(
            [[sg, "%.4f" % score] for sg, score in zip(space_groups, scores)],
            column_headers=["Space group", "score"],
        ).format()
    )

    # Find the best space group and update the experiments.
    best_sg = space_groups[scores.index(max(scores))]
    logger.info("Recommended space group: %s", best_sg)
    if "enantiomorphic pairs" in laue_groups[laue_group]:
        if best_sg in laue_groups[laue_group]["enantiomorphic pairs"]:
            logger.info(
                "Space group with equivalent score (enantiomorphic pair): %s",
                laue_groups[laue_group]["enantiomorphic pairs"][best_sg],
            )

    new_sg = sgtbx.space_group_info(symbol=best_sg).group()
    for experiment in experiments:
        experiment.crystal.set_space_group(new_sg)
Exemple #3
0
def test_IhTableblock_twodatasets(large_reflection_table, test_sg):
    """Test direct initialisation of Ih_table block. Use the same reflection
    table as previously to make comparions easier"""
    asu_indices = map_indices_to_asu(large_reflection_table["miller_index"], test_sg)
    large_reflection_table["asu_miller_index"] = asu_indices
    n_groups = len(set(asu_indices))
    n_refl = large_reflection_table.size()

    sel_1 = flex.bool([True, True, False, False, True, True, True])
    dataset_1 = large_reflection_table.select(sel_1)
    dataset_2 = large_reflection_table.select(~sel_1)

    block = IhTableBlock(n_groups=n_groups, n_refl=n_refl, n_datasets=2)

    block.add_data(0, group_ids=flex.int([0, 1, 3, 4, 4]), reflections=dataset_1)
    block.add_data(1, group_ids=flex.int([0, 2]), reflections=dataset_2)

    assert list(block.block_selections[0]) == [0, 1, 2, 3, 4]
    assert list(block.block_selections[1]) == [0, 1]
    assert len(block.block_selections) == 2

    assert block.h_index_matrix.n_rows == n_refl
    assert block.h_index_matrix.n_cols == n_groups
    assert block.h_index_matrix.non_zeroes == n_refl
    # first dataset
    assert block.h_index_matrix[0, 0] == 1
    assert block.h_index_matrix[1, 1] == 1
    assert block.h_index_matrix[2, 3] == 1
    assert block.h_index_matrix[3, 4] == 1
    assert block.h_index_matrix[4, 4] == 1
    # then second dataset
    assert block.h_index_matrix[5, 0] == 1
    assert block.h_index_matrix[6, 2] == 1

    assert block.h_expand_matrix.n_cols == n_refl
    assert block.h_expand_matrix.n_rows == n_groups
    assert block.h_expand_matrix.non_zeroes == n_refl
    assert block.h_expand_matrix[0, 0] == 1
    assert block.h_expand_matrix[1, 1] == 1
    assert block.h_expand_matrix[3, 2] == 1
    assert block.h_expand_matrix[4, 3] == 1
    assert block.h_expand_matrix[4, 4] == 1
    assert block.h_expand_matrix[0, 5] == 1
    assert block.h_expand_matrix[2, 6] == 1

    # Test select method
    new_block = block.select(flex.bool([True, False, True, False, False, True, False]))

    assert list(new_block.block_selections[0]) == [0, 2]
    assert list(new_block.block_selections[1]) == [0]

    assert new_block.h_index_matrix.n_rows == 3
    assert new_block.h_index_matrix.n_cols == 2
    assert new_block.h_index_matrix.non_zeroes == 3
    assert new_block.h_index_matrix[0, 0] == 1
    assert new_block.h_index_matrix[1, 1] == 1
    assert new_block.h_index_matrix[2, 0] == 1

    assert new_block.h_expand_matrix.n_cols == 3
    assert new_block.h_expand_matrix.n_rows == 2
    assert new_block.h_expand_matrix.non_zeroes == 3
    assert new_block.h_expand_matrix[0, 0] == 1
    assert new_block.h_expand_matrix[1, 1] == 1
    assert new_block.h_expand_matrix[0, 2] == 1
Exemple #4
0
def test_IhTableblock_onedataset(large_reflection_table, test_sg):
    """Test direct initialisation of Ih_table block"""
    asu_indices = map_indices_to_asu(large_reflection_table["miller_index"], test_sg)
    large_reflection_table["asu_miller_index"] = asu_indices
    n_groups = len(set(asu_indices))
    n_refl = large_reflection_table.size()
    block = IhTableBlock(n_groups=n_groups, n_refl=n_refl)
    group_ids = flex.int([0, 1, 0, 2, 3, 4, 4])

    block.add_data(0, group_ids, large_reflection_table)

    # test properties
    assert block.inverse_scale_factors
    assert block.variances
    assert block.intensities
    assert block.weights
    assert block.asu_miller_index

    assert list(block.block_selections[0]) == [0, 1, 2, 3, 4, 5, 6]
    assert len(block.block_selections) == 1

    assert block.h_index_matrix.n_rows == n_refl
    assert block.h_index_matrix.n_cols == n_groups
    assert block.h_index_matrix.non_zeroes == n_refl
    assert block.h_index_matrix[0, 0] == 1
    assert block.h_index_matrix[1, 1] == 1
    assert block.h_index_matrix[2, 0] == 1
    assert block.h_index_matrix[3, 2] == 1
    assert block.h_index_matrix[4, 3] == 1
    assert block.h_index_matrix[5, 4] == 1
    assert block.h_index_matrix[6, 4] == 1

    assert block.h_expand_matrix.n_cols == n_refl
    assert block.h_expand_matrix.n_rows == n_groups
    assert block.h_expand_matrix.non_zeroes == n_refl
    assert block.h_expand_matrix[0, 0] == 1
    assert block.h_expand_matrix[1, 1] == 1
    assert block.h_expand_matrix[0, 2] == 1
    assert block.h_expand_matrix[2, 3] == 1
    assert block.h_expand_matrix[3, 4] == 1
    assert block.h_expand_matrix[4, 5] == 1
    assert block.h_expand_matrix[4, 6] == 1

    assert list(block.intensities) == list(large_reflection_table["intensity"])
    assert list(block.weights) == list(1.0 / large_reflection_table["variance"])
    assert "Ih_values" not in block.Ih_table
    block.calc_Ih()
    assert list(block.Ih_values) == pytest.approx(
        [x / 2.0 for x in [90.0, 100.0, 90.0, 60.0, 30.0, 50.0, 50.0]]
    )

    with pytest.raises(AssertionError):
        block.add_data(0, group_ids, large_reflection_table)

    assert list(block.calc_nh()) == [2, 1, 2, 1, 1, 2, 2]

    # Test update error model
    block.update_weights(mock_error_model())
    assert list(block.weights) == pytest.approx(
        [1.0, 1.0 / 2.0, 1.0 / 3.0, 1.0 / 4.0, 1.0 / 5.0, 1.0 / 6.0, 1.0 / 7.0]
    )

    # Test select method
    new_block = block.select(flex.bool([True, False, True, False, True, False, False]))

    assert list(new_block.block_selections[0]) == [0, 2, 4]

    assert new_block.h_index_matrix.n_rows == 3
    assert new_block.h_index_matrix.n_cols == 2
    assert new_block.h_index_matrix.non_zeroes == 3
    assert new_block.h_index_matrix[0, 0] == 1
    assert new_block.h_index_matrix[1, 0] == 1
    assert new_block.h_index_matrix[2, 1] == 1

    assert new_block.h_expand_matrix.n_cols == 3
    assert new_block.h_expand_matrix.n_rows == 2
    assert new_block.h_expand_matrix.non_zeroes == 3
    assert new_block.h_expand_matrix[0, 0] == 1
    assert new_block.h_expand_matrix[0, 1] == 1
    assert new_block.h_expand_matrix[1, 2] == 1

    assert list(new_block.Ih_values) == pytest.approx(
        [x / 2.0 for x in [90.0, 90.0, 30.0]]
    )

    # test setter methods
    new_block.inverse_scale_factors = flex.double([1.0, 2.0, 3.0])
    assert list(new_block.inverse_scale_factors) == [1.0, 2.0, 3.0]
    with pytest.raises(AssertionError):
        new_block.inverse_scale_factors = flex.double([1.0, 2.0, 3.0, 4.0])
    new_block.weights = flex.double([0.1, 0.2, 0.3])
    assert list(new_block.weights) == [0.1, 0.2, 0.3]
    with pytest.raises(AssertionError):
        new_block.weights = flex.double([1.0, 2.0, 3.0, 4.0])

    # test selecting initial table on groups, expecting same result as before
    new_block = block.select_on_groups(flex.bool([True, False, False, True, False]))
    assert list(new_block.block_selections[0]) == [0, 2, 4]

    assert new_block.h_index_matrix.n_rows == 3
    assert new_block.h_index_matrix.n_cols == 2
    assert new_block.h_index_matrix.non_zeroes == 3
    assert new_block.h_index_matrix[0, 0] == 1
    assert new_block.h_index_matrix[1, 0] == 1
    assert new_block.h_index_matrix[2, 1] == 1

    assert new_block.h_expand_matrix.n_cols == 3
    assert new_block.h_expand_matrix.n_rows == 2
    assert new_block.h_expand_matrix.non_zeroes == 3
    assert new_block.h_expand_matrix[0, 0] == 1
    assert new_block.h_expand_matrix[0, 1] == 1
    assert new_block.h_expand_matrix[1, 2] == 1

    assert list(new_block.Ih_values) == pytest.approx(
        [x / 2.0 for x in [90.0, 90.0, 30.0]]
    )