Esempio n. 1
0
def test_sort_tables_to_experiments_order_single_dataset_files():
    """Test reflection table sorting when tables contain a single dataset."""
    # Reflection tables in the wrong order
    reflection_tables = [
        mock_reflection_file_object(id_=1).data,
        mock_reflection_file_object(id_=0).data,
    ]
    experiments = ExperimentList()
    experiments.append(Experiment(identifier=str(0)))
    experiments.append(Experiment(identifier=str(1)))
    refls = sort_tables_to_experiments_order(reflection_tables, experiments)

    # Check that reflection tables are rearranged
    assert refls[0] is reflection_tables[1]
    assert refls[1] is reflection_tables[0]
    assert list(refls[0].experiment_identifiers().values()) == ["0"]
    assert list(refls[1].experiment_identifiers().values()) == ["1"]

    # Reflection tables in correct order
    reflection_tables = [
        mock_reflection_file_object(id_=0).data,
        mock_reflection_file_object(id_=1).data,
    ]
    experiments = ExperimentList()
    experiments.append(Experiment(identifier=str(0)))
    experiments.append(Experiment(identifier=str(1)))
    refls = sort_tables_to_experiments_order(reflection_tables, experiments)

    # Check that nothing has been changed
    assert refls[0] is reflection_tables[0]
    assert refls[1] is reflection_tables[1]
    assert list(refls[0].experiment_identifiers().values()) == ["0"]
    assert list(refls[1].experiment_identifiers().values()) == ["1"]
Esempio n. 2
0
def reflections_and_experiments_from_files(reflection_file_object_list,
                                           experiment_file_object_list):
    """Extract reflection tables and an experiment list from the files.
    If experiment identifiers are set, the order of the reflection tables is
    changed to match the order of experiments.
    """
    tables = flatten_reflections(reflection_file_object_list)

    experiments = flatten_experiments(experiment_file_object_list)

    if tables and experiments:
        tables = sort_tables_to_experiments_order(tables, experiments)

    return tables, experiments
Esempio n. 3
0
def test_sort_tables_to_experiments_order_multi_dataset_files():
    """Test reflection table sorting when a table contains multiple datasets."""
    # Reflection tables in the wrong order
    reflection_tables = [
        mock_two_reflection_file_object(ids=[1, 2]).data,
        mock_reflection_file_object(id_=0).data,
    ]
    experiments = ExperimentList()
    experiments.append(Experiment(identifier=str(0)))
    experiments.append(Experiment(identifier=str(1)))
    experiments.append(Experiment(identifier=str(2)))

    refls = sort_tables_to_experiments_order(reflection_tables, experiments)

    # Check that reflection tables are rearranged
    assert refls[0] is reflection_tables[1]
    assert refls[1] is reflection_tables[0]
    assert list(refls[0].experiment_identifiers().values()) == ["0"]
    assert list(refls[1].experiment_identifiers().values()) == ["1", "2"]
Esempio n. 4
0
def run(args: List[str] = None, phil: libtbx.phil.scope = phil_scope) -> None:
    """
    Run dials.anvil_correction as from the command line.

    Take integrated experiment lists and reflection tables and correct the all the
    integrated intensities for the estimated attenuation by the diamond anvils.

    Args:
        args: The arguments supplied by the user (default: sys.argv[1:]).
        phil: The PHIL scope definition (default: phil_scope, the master PHIL scope
              for this program).
    """
    usage = "dials.anvil_correction [options] integrated.expt integrated.refl"

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

    params, options = parser.parse_args(args=args, show_diff_phil=False)

    # Log the difference between the PHIL scope definition and the active PHIL scope,
    # which will include the parsed user inputs.
    diff_phil = parser.diff_phil.as_str()
    if diff_phil:
        logger.info("The following parameters have been modified:\n%s",
                    diff_phil)

    # Check that at least one reflection table and experiment list have been provided.
    input_errors = []
    if not params.input.experiments:
        input_errors.append(
            "Please provide at least one valid experiment list (.expt) file.")
    if not params.input.reflections:
        input_errors.append(
            "Please provide at least one valid reflection table (.refl) file.")
    if input_errors:
        sys.exit("\n".join([parser.format_help()] + input_errors))

    if not np.linalg.norm(params.anvil.normal):
        sys.exit(
            "It seems you have provided a surface normal vector with zero length."
        )

    # Check that the anvil surface normal really is normalised.
    dac_norm = params.anvil.normal / np.linalg.norm(params.anvil.normal)

    # Configure the logging.
    dials.util.log.config(options.verbose, logfile=params.output.log)

    # These functions are commonly used to collate the input.
    experiments = flatten_experiments(params.input.experiments)
    reflections_list = flatten_reflections(params.input.reflections)
    # Work around parse_multiple_datasets dropping unindexed reflections.
    unindexed = flex.reflection_table()
    for r_table in reflections_list:
        unindexed.extend(r_table.select(r_table["id"] == -1))
    # Get a single reflection table per experiment object.
    reflections_list = parse_multiple_datasets(reflections_list)
    reflections_list = sort_tables_to_experiments_order(
        reflections_list, experiments)

    # Record the density of diamond in g·cm⁻³ (for consistency with NIST tables,
    # https://doi.org/10.18434/T4D01F).
    density = params.anvil.density / 1000  # g·cm⁻³

    # Correct for the attenuation of the incident and diffracted beams by the diamond
    # anvil cell.
    logger.info(
        "Correcting integrated reflection intensities for attenuation by the diamond "
        "anvil cell.")
    for experiment, reflections in zip(experiments, reflections_list):
        correct_intensities_for_dac_attenuation(experiment, reflections,
                                                dac_norm,
                                                params.anvil.thickness,
                                                density)
    logger.info("Done.")

    # Do optional experiment list file output here.
    if params.output.experiments:
        logger.info("Writing the experiment list to %s",
                    params.output.experiments)
        experiments.as_file(params.output.experiments)
    logger.info("Writing the reflection table to %s",
                params.output.reflections)
    # Collate reflections into a single reflection table and save it to file.
    reflections = unindexed
    for r_table in reflections_list:
        reflections.extend(r_table)
    del reflections_list
    reflections.as_file(params.output.reflections)