Esempio n. 1
0
def run(args):
    from dials.util.options import OptionParser, reflections_and_experiments_from_files
    from dials.util import log

    usage = "dials.detect_blanks [options] models.expt observations.refl"

    parser = OptionParser(
        usage=usage,
        phil=phil_scope,
        read_experiments=True,
        read_reflections=True,
        check_format=False,
        epilog=help_message,
    )

    params, options = parser.parse_args(args)
    reflections, experiments = reflections_and_experiments_from_files(
        params.input.reflections, params.input.experiments
    )

    if len(experiments) == 0 or len(reflections) == 0:
        parser.print_help()
        exit(0)

    # Configure the logging
    log.config(logfile="dials.detect_blanks.log")

    # Log the diff phil
    diff_phil = parser.diff_phil.as_str()
    if diff_phil != "":
        logger.info("The following parameters have been modified:\n")
        logger.info(diff_phil)

    reflections = reflections[0]

    imagesets = experiments.imagesets()

    if any(experiment.is_still() for experiment in experiments):
        sys.exit("dials.detect_blanks can only be used with rotation data")

    assert len(imagesets) == 1
    imageset = imagesets[0]
    scan = imageset.get_scan()

    integrated_sel = reflections.get_flags(reflections.flags.integrated)
    indexed_sel = reflections.get_flags(reflections.flags.indexed)
    centroid_outlier_sel = reflections.get_flags(reflections.flags.centroid_outlier)
    strong_sel = reflections.get_flags(reflections.flags.strong)
    indexed_sel &= ~centroid_outlier_sel

    logger.info(f"Analysis of {strong_sel.count(True)} strong reflections:")
    strong_results = detect_blanks.blank_counts_analysis(
        reflections.select(strong_sel),
        scan,
        phi_step=params.phi_step,
        fractional_loss=params.counts_fractional_loss,
    )
    for blank_start, blank_end in strong_results["blank_regions"]:
        logger.info(f"Potential blank images: {blank_start + 1} -> {blank_end}")

    indexed_results = None
    if indexed_sel.count(True) > 0:
        logger.info(f"Analysis of {indexed_sel.count(True)} indexed reflections:")
        indexed_results = detect_blanks.blank_counts_analysis(
            reflections.select(indexed_sel),
            scan,
            phi_step=params.phi_step,
            fractional_loss=params.counts_fractional_loss,
        )
        for blank_start, blank_end in indexed_results["blank_regions"]:
            logger.info(f"Potential blank images: {blank_start + 1} -> {blank_end}")

    integrated_results = None
    if integrated_sel.count(True) > 0:
        logger.info(f"Analysis of {integrated_sel.count(True)} integrated reflections:")
        integrated_results = detect_blanks.blank_integrated_analysis(
            reflections.select(integrated_sel),
            scan,
            phi_step=params.phi_step,
            fractional_loss=params.misigma_fractional_loss,
        )
        for blank_start, blank_end in integrated_results["blank_regions"]:
            logger.info(f"Potential blank images: {blank_start + 1} -> {blank_end}")

    d = {
        "strong": strong_results,
        "indexed": indexed_results,
        "integrated": integrated_results,
    }

    if params.output.json is not None:
        with open(params.output.json, "w") as fh:
            json.dump(d, fh)

    if params.output.plot:
        from matplotlib import pyplot

        plots = [(strong_results, "-")]
        if indexed_results:
            plots.append((indexed_results, "--"))
        if integrated_results:
            plots.append((integrated_results, ":"))

        for results, linestyle in plots:
            xs = results["data"][0]["x"]
            ys = results["data"][0]["y"]
            xmax = max(xs)
            ymax = max(ys)
            xs = [x / xmax for x in xs]
            ys = [y / ymax for y in ys]
            blanks = results["data"][0]["blank"]
            pyplot.plot(xs, ys, color="blue", linestyle=linestyle)
            pyplot.plot(
                *zip(*[(x, y) for x, y, blank in zip(xs, ys, blanks) if blank]),
                color="red",
                linestyle=linestyle,
            )
        pyplot.ylim(0)
        pyplot.show()
        pyplot.clf()
Esempio n. 2
0
def test_blank_integrated_analysis(dials_data):
    expts = ExperimentList.from_file(dials_data("insulin_processed") /
                                     "integrated.expt",
                                     check_format=False)
    refl = flex.reflection_table.from_file(
        dials_data("insulin_processed") / "integrated.refl")
    results = detect_blanks.blank_integrated_analysis(refl,
                                                      expts[0].scan,
                                                      phi_step=5,
                                                      fractional_loss=0.1)
    assert results["data"][0]["x"] == [
        2.5,
        7.5,
        12.5,
        17.5,
        22.5,
        27.5,
        32.5,
        37.5,
        42.5,
    ]
    assert results["data"][0]["y"] == pytest.approx([
        27.903266149430973,
        25.832527090455052,
        26.9236206883069,
        26.50234804728626,
        26.41019377727383,
        25.810676090828185,
        24.844906790823064,
        25.89992001081651,
        25.580718362291474,
    ])
    assert not any(results["data"][0]["blank"])
    assert results["blank_regions"] == []

    # Now with some "blank" regions - make some of the reflections weak
    z = refl["xyzobs.px.value"].parts()[2]
    refl["intensity.prf.value"].set_selected(
        z < 10, refl["intensity.prf.value"] * 0.05)
    results = detect_blanks.blank_integrated_analysis(refl,
                                                      expts[0].scan,
                                                      phi_step=5,
                                                      fractional_loss=0.1)
    assert results["data"][0]["y"] == pytest.approx([
        1.3951633074715482,
        1.2916263545227527,
        26.9236206883069,
        26.50234804728626,
        26.41019377727383,
        25.810676090828185,
        24.844906790823064,
        25.89992001081651,
        25.580718362291474,
    ])
    assert results["data"][0]["blank"] == [
        True,
        True,
        False,
        False,
        False,
        False,
        False,
        False,
        False,
    ]
    assert results["blank_regions"] == [(0, 10)]

    # Unset the integrated_prf flags, so the analysis should instead use the umodified
    # intensity.sum.value instead
    refl.unset_flags(flex.bool(len(refl), True), refl.flags.integrated_prf)
    results = detect_blanks.blank_integrated_analysis(refl,
                                                      expts[0].scan,
                                                      phi_step=5,
                                                      fractional_loss=0.1)
    assert not any(results["data"][0]["blank"])
    assert results["blank_regions"] == []