def test_compute_delta_cchalf_returned_results(): """Test that delta cchalf return necessary values for scale_and_filter.""" # Check for correct recording of # results_summary['per_dataset_delta_cc_half_values']['delta_cc_half_values'] summary = {} delta_cc = {0: -4, 1: 2, 2: -3, 3: -5, 4: 1} sorted_data, sorted_ccs = DeltaCCHalf.sort_deltacchalf_values(delta_cc, summary) expected_data_order = [3, 0, 2, 4, 1] expected_cc_order = [-5, -4, -3, 1, 2] assert list(sorted_data) == expected_data_order assert list(sorted_ccs) == expected_cc_order assert ( summary["per_dataset_delta_cc_half_values"]["delta_cc_half_values"] == expected_cc_order ) # Check for correct recording for dataset mode exp = generate_test_experiments(2) refls = generate_test_reflections(2) ids_to_remove = [0] results_summary = {"dataset_removal": {}} _ = CCHalfFromDials.remove_datasets_below_cutoff( exp, refls, ids_to_remove, results_summary ) assert "experiments_fully_removed" in results_summary["dataset_removal"] assert "n_reflections_removed" in results_summary["dataset_removal"] assert results_summary["dataset_removal"]["experiments_fully_removed"] == ["0"] assert results_summary["dataset_removal"]["n_reflections_removed"] == 10 # Check for correct recording for image group mode. exp = generate_test_experiments(2) refls = generate_test_reflections(2) ids_to_remove = [0, 1] image_group_to_expid_and_range = { 0: ("0", (1, 5)), 1: ("0", (6, 10)), 2: ("1", (1, 5)), 3: ("1", (6, 10)), } expids_to_image_groups = {"0": [0, 1], "1": [2, 3]} results_summary = {"dataset_removal": {}} _ = CCHalfFromDials.remove_image_ranges_below_cutoff( exp, refls, ids_to_remove, image_group_to_expid_and_range, expids_to_image_groups, results_summary, ) assert "experiments_fully_removed" in results_summary["dataset_removal"] assert "n_reflections_removed" in results_summary["dataset_removal"] assert "image_ranges_removed" in results_summary["dataset_removal"] assert results_summary["dataset_removal"]["experiments_fully_removed"] == ["0"] assert results_summary["dataset_removal"]["n_reflections_removed"] == 10 assert [(6, 10), 0] in results_summary["dataset_removal"]["image_ranges_removed"] assert [(1, 5), 0] in results_summary["dataset_removal"]["image_ranges_removed"] assert len(results_summary["dataset_removal"]["image_ranges_removed"]) == 2
def test_exclusion_in_CCHalfFromDials(): """Test the exclusion of image groups.""" # Same input as above, but mock DeltaCCHalf algorithm to just test # interpretation of results and setting of excluded regions. With the # input, test that outlier edges are correctly removed. params = phil_scope.extract() params.mode = "image_group" expts = generated_exp(n=2) refls = generated_refl() def mock_algorithm(*_): """Mock a result from DeltaCCHalf""" algo = mock.Mock() algo.run.return_value = None algo.results_summary = { "per_dataset_delta_cc_half_values": { "delta_cc_half_values": [-5.0, -2.0, 4.0, -5.0], "datasets": [0, 1, 2, 3], }, "dataset_removal": { "cutoff_value": -1.0 }, } return algo with mock.patch( "dials.algorithms.statistics.cc_half_algorithm.DeltaCCHalf", side_effect=mock_algorithm, ): script = CCHalfFromDials(params, expts, refls) script.run() assert script.datasetid_to_groups == { "0": [], "1": [2] } # all but 3 removed expts = script.experiments assert list(expts.identifiers()) == ["1"] assert expts[0].scan.get_valid_image_ranges( expts.identifiers()[0]) == [(1, 10)] assert script.results_summary["dataset_removal"][ "experiment_ids_fully_removed"] == [0] assert script.results_summary["dataset_removal"][ "experiments_fully_removed"] == ["0"]
def run(args=None, phil=phil_scope): """Run the command-line script.""" usage = "dials.compute_delta_cchalf [options] scaled.expt scaled.refl" parser = OptionParser( usage=usage, phil=phil, epilog=help_message, read_experiments=True, read_reflections=True, check_format=False, ) params, _ = parser.parse_args(args=args, show_diff_phil=False) log.config(logfile=params.output.log) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments ) if not experiments and not reflections: if not params.input.mtzfile: parser.print_help() return else: try: script = CCHalfFromMTZ(params, params.input.mtzfile) except ValueError as e: sys.exit(f"Error: {e}") else: if not experiments or not reflections: parser.print_help() return else: if not len(reflections) == 1: exit("Only one reflection table can be provided") n_datasets = len(set(reflections[0]["id"]).difference({-1})) if n_datasets != len(experiments): exit( """ The number of experiments (%s) does not match the number of datasets in the reflection table (%s) """, len(experiments), n_datasets, ) try: script = CCHalfFromDials(params, experiments, reflections[0]) except ValueError as e: sys.exit(f"Error: {e}") script.run() script.output()
def test_setup_of_CCHalfFromDials(): """Test the correct setup in image group mode. Test for the case of outliers at the end of images, and image ranges not equaling a multiple of the grouping.""" params = phil_scope.extract() params.mode = "image_group" expts = generated_exp(n=2) refls = generated_refl() # Expected behaviour is that the outliers will not be included in the # image range, and that all groups will have at least 10 images in. script = CCHalfFromDials(params, expts, refls) assert script.group_to_datasetid_and_range == { 0: ("0", (5, 14)), 1: ("0", (15, 25)), 2: ("1", (1, 10)), 3: ("1", (11, 23)), } assert script.datasetid_to_groups == {"0": [0, 1], "1": [2, 3]}