def test_set_batch_offsets(): """Test for namesake function""" scan = Scan(image_range=[1, 200], oscillation=[0.0, 1.0]) exp1 = Experiment(scan=scan) exp2 = Experiment() set_batch_offsets([exp2, exp1], [0, 101]) assert exp1.scan.get_batch_offset() == 101
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"]
def generated_exp(n=1, space_group="P 2", assign_ids=False, id_=None): """Generate an experiment list with two experiments.""" experiments = ExperimentList() exp_dict = { "__id__": "crystal", "real_space_a": [15.0, 0.0, 0.0], "real_space_b": [0.0, 10.0, 0.0], "real_space_c": [0.0, 0.0, 20.0], "space_group_hall_symbol": space_group, } crystal = Crystal.from_dict(exp_dict) scan = Scan(image_range=[0, 90], oscillation=[0.0, 1.0]) beam = Beam(s0=(0.0, 0.0, 1.01)) if assign_ids: experiments.append( Experiment(identifier="0", beam=beam, scan=scan, crystal=crystal) ) elif id_: experiments.append( Experiment(identifier=str(id_), beam=beam, scan=scan, crystal=crystal) ) else: experiments.append(Experiment(beam=beam, scan=scan, crystal=crystal)) if n > 1: for i in range(1, n): if assign_ids: experiments.append( Experiment(identifier=str(i), beam=beam, scan=scan, crystal=crystal) ) else: experiments.append(Experiment(beam=beam, scan=scan, crystal=crystal)) return experiments
def generated_exp(n=1): """Generate an experiment list with two experiments.""" experiments = ExperimentList() exp_dict = { "__id__": "crystal", "real_space_a": [1.0, 0.0, 0.0], "real_space_b": [0.0, 1.0, 0.0], "real_space_c": [0.0, 0.0, 2.0], "space_group_hall_symbol": " C 2y", } crystal = Crystal.from_dict(exp_dict) scan = Scan(image_range=[0, 90], oscillation=[0.0, 1.0]) beam = Beam(s0=(0.0, 0.0, 1.01)) goniometer = Goniometer((1.0, 0.0, 0.0)) detector = Detector() experiments.append( Experiment( beam=beam, scan=scan, goniometer=goniometer, detector=detector, crystal=crystal, )) if n > 1: for _ in range(n - 1): experiments.append( Experiment( beam=beam, scan=scan, goniometer=goniometer, detector=detector, crystal=crystal, )) return experiments
def _make_input_for_exclude_tests(exclude_images=True): """Generate input data, that upon exclusion should leave only the first reflection.""" if exclude_images: exclude_images = [["0:360:720"], ["1:360:720"]] expt1 = Experiment(scan=Scan(image_range=(0, 720), oscillation=(0.0, 1.0))) expt2 = Experiment(scan=Scan(image_range=(0, 720), oscillation=(0.0, -1.0))) refls1 = flex.reflection_table() refls2 = flex.reflection_table() refls1["xyzobs.mm.value"] = flex.vec3_double([ (0.0, 0.0, 10.0 * math.pi / 180.0), (0.0, 0.0, 370.0 * math.pi / 180.0) ]) refls1["xyzobs.px.value"] = flex.vec3_double([(0.0, 0.0, 10.0), (0.0, 0.0, 370.0)]) refls1["i"] = flex.int([0, 1]) refls2["xyzobs.mm.value"] = flex.vec3_double([ (0.0, 0.0, -10.0 * math.pi / 180.0), (0.0, 0.0, -370.0 * math.pi / 180.0) ]) refls2["xyzobs.px.value"] = flex.vec3_double([(0.0, 0.0, 10.0), (0.0, 0.0, 370.0)]) refls2["i"] = flex.int([0, 1]) expts = ExperimentList([expt1, expt2]) tables = [refls1, refls2] expts, tables = assign_unique_identifiers(expts, tables) return exclude_images, expts, tables
def test_experimentlist_with_identifiers(): from dxtbx.model import Beam, Detector, Goniometer, Scan # Initialise a list of experiments experiments = ExperimentList() experiments.append( Experiment(beam=Beam(s0=(0, 0, -1)), detector=Detector(), identifier="bacon")) experiments.append( Experiment(beam=Beam(s0=(0, 0, -1)), detector=Detector(), identifier="sausage")) with pytest.raises(Exception): experiments.append( Experiment(beam=Beam(), detector=Detector(), identifier="bacon")) d = experiments.to_dict() e2 = ExperimentListDict(d).decode() assert experiments[0].identifier == e2[0].identifier assert experiments[1].identifier == e2[1].identifier assert tuple(experiments.identifiers()) == ("bacon", "sausage") experiments[0].identifier = "spam" assert tuple(experiments.identifiers()) == ("spam", "sausage")
def experiments(): """Make a list of three empty experiments""" experiments = ExperimentList() experiments.append(Experiment()) experiments.append(Experiment()) experiments.append(Experiment()) return experiments
def test_get_image_ranges(): """Test for namesake function""" scan = Scan(image_range=[1, 200], oscillation=[0.0, 1.0]) exp1 = Experiment(scan=scan) exp2 = Experiment() experiments = [exp1, exp2] image_ranges = get_image_ranges(experiments) assert image_ranges == [(1, 200), (0, 0)]
def test_calculate_batch_offsets(): """Test offset calculation. Offset is next number ending in 01 bigger than previous batch numbers which is not consecutive""" scan = Scan(image_range=[1, 200], oscillation=[0.0, 1.0]) exp1 = Experiment(scan=scan) exp2 = Experiment() offsets = calculate_batch_offsets([exp1, exp2]) assert offsets == [0, 301]
def test_get_batch_ranges(): """Test for namesake function""" scan = Scan(image_range=[1, 200], oscillation=[0.0, 1.0]) exp1 = Experiment(scan=scan) exp2 = Experiment(scan=scan) batch_offsets = [0, 300] experiments = [exp1, exp2] batch_ranges = get_batch_ranges(experiments, batch_offsets) assert batch_ranges == [(1, 200), (301, 500)]
def test_select_remove_on_experiment_identifiers(): table = flex.reflection_table() table["id"] = flex.int([0, 1, 2, 3]) experiments = ExperimentList() experiments.append(Experiment(identifier="abcd")) experiments.append(Experiment(identifier="efgh")) experiments.append(Experiment(identifier="ijkl")) experiments.append(Experiment(identifier="mnop")) table.experiment_identifiers()[0] = "abcd" table.experiment_identifiers()[1] = "efgh" table.experiment_identifiers()[2] = "ijkl" table.experiment_identifiers()[3] = "mnop" table.assert_experiment_identifiers_are_consistent(experiments) table = table.remove_on_experiment_identifiers(["efgh"]) del experiments[1] table.assert_experiment_identifiers_are_consistent(experiments) assert list(table.experiment_identifiers().keys()) == [0, 2, 3] assert list( table.experiment_identifiers().values()) == ["abcd", "ijkl", "mnop"] table = table.select_on_experiment_identifiers(["abcd", "mnop"]) del experiments[1] # now ijkl table.assert_experiment_identifiers_are_consistent(experiments) assert list(table.experiment_identifiers().keys()) == [0, 3] assert list(table.experiment_identifiers().values()) == ["abcd", "mnop"] # reset 'id' column such that they are numbered 0 .. n-1 table.reset_ids() table.assert_experiment_identifiers_are_consistent(experiments) assert list(table.experiment_identifiers().keys()) == [0, 1] assert list(table.experiment_identifiers().values()) == ["abcd", "mnop"] # test that the function doesn't fail if no identifiers set table1 = copy.deepcopy(table) for k in table1.experiment_identifiers().keys(): del table1.experiment_identifiers()[k] table1.reset_ids() assert list(table1.experiment_identifiers().keys()) == [] # Test exception is raised if bad choice with pytest.raises(KeyError): table.remove_on_experiment_identifiers(["efgh"]) with pytest.raises(KeyError): table.select_on_experiment_identifiers(["efgh"]) table = flex.reflection_table() table["id"] = flex.int([0, 1, 2, 3]) # Test exception is raised if identifiers map not set with pytest.raises(KeyError): table.remove_on_experiment_identifiers(["efgh"]) with pytest.raises(KeyError): table.select_on_experiment_identifiers(["abcd", "mnop"])
def test_experiment_consistent(dials_regression): # Create a sequence sequence_filenames = os.path.join(dials_regression, "centroid_test_data", "centroid*.cbf") sequence = ImageSetFactory.new(sorted(glob(sequence_filenames)))[0] # Create experiment with sequence and good scan e = Experiment(imageset=sequence, scan=sequence.get_scan()) assert e.is_consistent() # Create experiment with sequence and defective scan scan = sequence.get_scan() scan.set_image_range((1, 1)) e = Experiment(imageset=sequence, scan=scan)
def tst_equality(self): from dxtbx.model import Beam, Detector, Goniometer, Scan from dxtbx.model import Crystal # Create a load of models b1 = Beam() d1 = Detector() g1 = Goniometer() s1 = Scan() c1 = Crystal((1, 0, 0), (0, 1, 0), (0, 0, 1), space_group_symbol="P1") # Create a load of models that look the same but aren't b2 = Beam() d2 = Detector() g2 = Goniometer() s2 = Scan() c2 = Crystal((1, 0, 0), (0, 1, 0), (0, 0, 1), space_group_symbol="P1") # Create an experiment e1 = Experiment(beam=b1, detector=d1, goniometer=g1, scan=s1, crystal=c1, imageset=None) # Create an experiment e2 = Experiment(beam=b1, detector=d1, goniometer=g1, scan=s1, crystal=c1, imageset=None) # Create an experiment e3 = Experiment(beam=b2, detector=d2, goniometer=g2, scan=s2, crystal=c2, imageset=None) # Check e1 equals e2 but not e3 assert (e1 == e2) assert (e1 != e3) assert (e2 != e3) # Test passed print 'OK'
def test_experiment_consistent(dials_data): # Create a sequence sequence_filenames = dials_data("centroid_test_data").listdir( "centroid*.cbf") sequence = ImageSetFactory.new( sorted(f.strpath for f in sequence_filenames))[0] # Create experiment with sequence and good scan e = Experiment(imageset=sequence, scan=sequence.get_scan()) assert e.is_consistent() # Create experiment with sequence and defective scan scan = sequence.get_scan() scan.set_image_range((1, 1)) e = Experiment(imageset=sequence, scan=scan)
def from_sweep_and_crystal(imageset, crystal, load_models=True): """ Create an experiment list from sweep and crystal. """ if load_models: return ExperimentList([ Experiment( imageset=imageset, beam=imageset.get_beam(), detector=imageset.get_detector(), goniometer=imageset.get_goniometer(), scan=imageset.get_scan(), crystal=crystal, ) ]) else: return ExperimentList( [Experiment(imageset=imageset, crystal=crystal)])
def generated_single_exp(): """Generate an experiment object.""" experiments = ExperimentList() exp_dict = { "__id__": "crystal", "real_space_a": [1.0, 0.0, 0.0], "real_space_b": [0.0, 1.0, 0.0], "real_space_c": [0.0, 0.0, 2.0], "space_group_hall_symbol": " C 2y", } crystal = Crystal.from_dict(exp_dict) scan = Scan(image_range=[0, 60], oscillation=[0.0, 1.0]) beam = Beam(s0=(0.0, 0.0, 1.01)) goniometer = Goniometer((1.0, 0.0, 0.0)) detector = Detector() experiments.append( Experiment( beam=beam, scan=scan, goniometer=goniometer, detector=detector, crystal=crystal, )) experiments[0].identifier = "0" return experiments
def test_correct(space_group_symbol): sgi = sgtbx.space_group_info(space_group_symbol) cs = sgi.any_compatible_crystal_symmetry(volume=1000) ms = cs.build_miller_set(anomalous_flag=True, d_min=1).expand_to_p1() # the reciprocal matrix B = scitbx.matrix.sqr(cs.unit_cell().fractionalization_matrix()).transpose() crystal = Crystal(B, sgtbx.space_group()) expts = ExperimentList([Experiment(crystal=crystal)]) refl = flex.reflection_table() refl["miller_index"] = ms.indices() refl["rlp"] = B.elems * ms.indices().as_vec3_double() refl["imageset_id"] = flex.int(len(refl)) refl["xyzobs.mm.value"] = flex.vec3_double(len(refl)) non_primitive_basis.correct(expts, refl, assign_indices.AssignIndicesGlobal()) cs_corrected = expts.crystals()[0].get_crystal_symmetry() assert cs_corrected.change_of_basis_op_to_primitive_setting().is_identity_op() assert ( cs.change_of_basis_op_to_primitive_setting().apply(ms.indices()) == refl["miller_index"] )
def test_change_of_basis_ops_to_minimum_cell_mpro(): input_ucs = [ (46.023, 55.001, 64.452, 64.744, 78.659, 89.824), (44.747, 53.916, 62.554, 114.985, 99.610, 90.736), ] # Setup the input experiments and reflection tables expts = ExperimentList() for uc in input_ucs: uc = uctbx.unit_cell(uc) sg = sgtbx.space_group_info("P1").group() B = scitbx.matrix.sqr(uc.fractionalization_matrix()).transpose() expts.append( Experiment(crystal=Crystal(B, space_group=sg, reciprocal=True))) # Actually run the method we are testing cb_ops = change_of_basis_ops_to_minimum_cell( expts, max_delta=5, relative_length_tolerance=0.05, absolute_angle_tolerance=2) expts.change_basis(cb_ops, in_place=True) assert symmetry.unit_cells_are_similar_to( expts, median_unit_cell(expts), relative_length_tolerance=0.05, absolute_angle_tolerance=2, )
def test_experiment_consistent(dials_regression): from dxtbx.imageset import ImageSetFactory from dxtbx.model import Scan # Create a sweep sweep_filenames = os.path.join(dials_regression, 'centroid_test_data', 'centroid*.cbf') sweep = ImageSetFactory.new(sorted(glob(sweep_filenames)))[0] # Create experiment with sweep and good scan e = Experiment(imageset=sweep, scan=sweep.get_scan()) assert e.is_consistent() # Create experiment with sweep and defective scan scan = sweep.get_scan() scan.set_image_range((1, 1)) e = Experiment(imageset=sweep, scan=scan)
def test_as_miller_array(): table = flex.reflection_table() table["intensity.1.value"] = flex.double([1.0, 2.0, 3.0]) table["intensity.1.variance"] = flex.double([0.25, 1.0, 4.0]) table["miller_index"] = flex.miller_index([(1, 0, 0), (2, 0, 0), (3, 0, 0)]) crystal = Crystal( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group=sgtbx.space_group_info("P 222").group(), ) experiment = Experiment(crystal=crystal) iobs = table.as_miller_array(experiment, intensity="1") assert list(iobs.data()) == list(table["intensity.1.value"]) assert list(iobs.sigmas()) == list(flex.sqrt( table["intensity.1.variance"])) with pytest.raises(KeyError): _ = table.as_miller_array(experiment, intensity="2") table["intensity.2.value"] = flex.double([1.0, 2.0, 3.0]) with pytest.raises(KeyError): _ = table.as_miller_array(experiment, intensity="2")
def test_slice_experiments(): image_range = (0, 1000) oscillation = (0, 0.1) scan = Scan(image_range, oscillation) experiments = ExperimentList([Experiment(scan=scan)]) sliced_image_range = [(1, 5)] sliced_experiments = slice_experiments(experiments, sliced_image_range) assert sliced_experiments[0].scan.get_image_range() == sliced_image_range[0] copy.deepcopy(sliced_experiments)
def save_opt_det(phil_params, x, ref_params, SIM): opt_det = get_optimized_detector(x, ref_params, SIM) El = ExperimentList() E = Experiment() E.detector = opt_det El.append(E) El.as_file(phil_params.geometry.optimized_detector_name) print("Saved detector model to %s" % phil_params.geometry.optimized_detector_name)
def test_interpret_images_to_doses_options(): """Test handling of command line options for experiments input.""" experiments = ExperimentList() experiments.append( Experiment(scan=Scan(image_range=(1, 10), oscillation=(0, 1.0)))) experiments.append( Experiment(scan=Scan(image_range=(1, 20), oscillation=(0, 1.0)))) experiments.append( Experiment(scan=Scan(image_range=(1, 10), oscillation=(0, 1.0)))) # Default starting_doses, dpi = dials.pychef.interpret_images_to_doses_options( experiments, dose_per_image=[1]) assert starting_doses == [0, 0, 0] assert dpi == [1.0, 1.0, 1.0] # Multi-sweep measurements on same crystal starting_doses, dpi = dials.pychef.interpret_images_to_doses_options( experiments, dose_per_image=[1], shared_crystal=True) assert starting_doses == [0, 10, 30] assert dpi == [1.0, 1.0, 1.0] # Specify starting doses starting_doses, dpi = dials.pychef.interpret_images_to_doses_options( experiments, dose_per_image=[1], starting_doses=[0, 20, 0], shared_crystal=False) assert starting_doses == [0, 20, 0] assert dpi == [1.0, 1.0, 1.0] # Specify doses per image and shared crystal starting_doses, dpi = dials.pychef.interpret_images_to_doses_options( experiments, dose_per_image=[1.0, 2.0, 1.0], shared_crystal=True) assert starting_doses == [0, 10, 50] assert dpi == [1.0, 2.0, 1.0] # Test error is raised if bad input values for starting doses or dose per image. with pytest.raises(ValueError): _, __ = dials.pychef.interpret_images_to_doses_options( experiments, dose_per_image=[1.0], starting_doses=[0, 1]) with pytest.raises(ValueError): _, __ = dials.pychef.interpret_images_to_doses_options( experiments, dose_per_image=[1.0, 2.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"]
def test_experiment_equality(): # Create a load of models b1 = Beam() d1 = Detector() g1 = Goniometer() s1 = Scan() c1 = Crystal((1, 0, 0), (0, 1, 0), (0, 0, 1), space_group_symbol="P1") # Create a load of models that look the same but aren't b2 = Beam() d2 = Detector() g2 = Goniometer() s2 = Scan() c2 = Crystal((1, 0, 0), (0, 1, 0), (0, 0, 1), space_group_symbol="P1") # Create an experiment e1 = Experiment(beam=b1, detector=d1, goniometer=g1, scan=s1, crystal=c1, imageset=None) # Create an experiment e2 = Experiment(beam=b1, detector=d1, goniometer=g1, scan=s1, crystal=c1, imageset=None) # Create an experiment e3 = Experiment(beam=b2, detector=d2, goniometer=g2, scan=s2, crystal=c2, imageset=None) # Check e1 equals e2 but not e3 assert e1 == e2 assert e1 != e3 assert e2 != e3
def from_sweep_and_crystal(imageset, crystal): ''' Create an experiment list from sweep and crystal. ''' return ExperimentList([ Experiment(imageset=imageset, beam=imageset.get_beam(), detector=imageset.get_detector(), goniometer=imageset.get_goniometer(), scan=imageset.get_scan(), crystal=crystal) ])
def from_stills_and_crystal(imageset, crystal, load_models=True): """Create an experiment list from stills and crystal.""" experiments = ExperimentList() if load_models: for i in range(len(imageset)): experiments.append( Experiment( imageset=imageset[i:i + 1], beam=imageset.get_beam(i), detector=imageset.get_detector(i), goniometer=imageset.get_goniometer(i), scan=imageset.get_scan(i), crystal=crystal, )) else: for i in range(len(imageset)): experiments.append( Experiment(imageset=imageset[i:i + 1], crystal=crystal)) return experiments
def generated_exp(n=1, scan=True, image_range=[0, 10]): """Generate an experiment list with two experiments.""" experiments = ExperimentList() exp_dict = { "__id__": "crystal", "real_space_a": [1.0, 0.0, 0.0], "real_space_b": [0.0, 1.0, 0.0], "real_space_c": [0.0, 0.0, 2.0], "space_group_hall_symbol": " C 2y", } crystal = Crystal.from_dict(exp_dict) if scan: scan = Scan(image_range=image_range, oscillation=[0.0, 1.0]) else: scan = None beam = Beam(s0=(0.0, 0.0, 1.01)) goniometer = Goniometer((1.0, 0.0, 0.0)) goniometer_2 = Goniometer((1.0, 1.0, 0.0)) detector = Detector() experiments.append( Experiment( beam=beam, scan=scan, goniometer=goniometer, detector=detector, crystal=crystal, ) ) experiments[0].identifier = "0" if n > 1: for i in range(0, n - 1): experiments.append( Experiment( beam=beam, scan=scan, goniometer=goniometer_2, detector=detector, crystal=crystal, ) ) experiments[i + 1].identifier = str(i + 1) return experiments
def single_exp(): """Generate an experiment object.""" crystal = Crystal.from_dict({ "__id__": "crystal", "real_space_a": [1.0, 0.0, 0.0], "real_space_b": [0.0, 1.0, 0.0], "real_space_c": [0.0, 0.0, 2.0], "space_group_hall_symbol": " C 2y", }) scan = Scan(image_range=[0, 60], oscillation=[0.0, 1.0]) return Experiment(scan=scan, crystal=crystal)
def test_experiment_is_still(): experiment = Experiment() assert experiment.is_still() experiment.goniometer = Goniometer() assert experiment.is_still() experiment.scan = Scan() assert experiment.is_still() experiment.scan = Scan((1, 1000), (0, 0.05)) assert not experiment.is_still() # Specifically test the bug from dxtbx#4 triggered by ending on 0° experiment.scan = Scan((1, 1800), (-90, 0.05)) assert not experiment.is_still()