def test(dials_regression, tmpdir): tmpdir.chdir() from dxtbx.serialize import load from dials.algorithms import shoebox from dials.array_family import flex # Load the sweep and crystal sweep_filename = os.path.join(dials_regression, 'centroid_test_data', 'sweep.json') crystal_filename = os.path.join(dials_regression, 'centroid_test_data', 'crystal.json') sweep = load.imageset(sweep_filename) crystal = load.crystal(crystal_filename) # Get models from the sweep beam = sweep.get_beam() detector = sweep.get_detector() gonio = sweep.get_goniometer() scan = sweep.get_scan() # Get the reflections and overlaps reflections, adjacency_list = predict_reflections(sweep, crystal) reflections['shoebox'] = flex.shoebox( reflections['panel'], reflections['bbox']) reflections['shoebox'].allocate_with_value(shoebox.MaskCode.Valid) # If the adjacency list is given, then create the reflection mask assert(len(detector) == 1) image_size = detector[0].get_image_size() shoeboxes = reflections['shoebox'] coords = reflections['xyzcal.px'] shoebox_masker = shoebox.MaskOverlapping() shoebox_masker(shoeboxes, coords, adjacency_list) # Loop through all edges overlapping = [] for e in adjacency_list.edges(): v1, v2 = adjacency_list.source(e), adjacency_list.target(e) overlapping.append(v1) overlapping.append(v2) # Ensure elements are unique overlapping = set(overlapping) # Ensure we have some overlaps assert len(overlapping) > 0 # Get all non-overlapping reflections all_r = set(range(len(reflections))) non_overlapping = all_r.difference(overlapping) # Run the tests tst_non_overlapping(reflections, non_overlapping, detector[0].get_image_size()) tst_overlapping(reflections, overlapping, adjacency_list, image_size)
def __init__(self): import os import libtbx.load_env from dxtbx.serialize import load from dials.algorithms.profile_model.gaussian_rs import Model from dials.algorithms.profile_model.gaussian_rs import MaskCalculator3D from dxtbx.model.experiment_list import Experiment, ExperimentList try: dials_regression = libtbx.env.dist_path('dials_regression') except KeyError: print 'FAIL: dials_regression not configured' exit(0) # Set the sweep filename and load the sweep sweep_filename = os.path.join(dials_regression, 'centroid_test_data', 'sweep.json') crystal_filename = os.path.join(dials_regression, 'centroid_test_data', 'crystal.json') # Load the sweep self.sweep = load.imageset(sweep_filename) self.crystal = load.crystal(crystal_filename) self.beam = self.sweep.get_beam() self.detector = self.sweep.get_detector() self.goniometer = self.sweep.get_goniometer() self.scan = self.sweep.get_scan() self.delta_d = 3 * self.beam.get_sigma_divergence(deg=False) try: mosaicity = self.crystal.get_mosaicity(deg=False) except AttributeError: mosaicity = 0 self.delta_m = 3 * mosaicity self.nsigma = 3 self.profile_model = Model(None, self.nsigma, self.beam.get_sigma_divergence(deg=False), mosaicity) self.experiment = ExperimentList() self.experiment.append( Experiment(imageset=self.sweep, beam=self.beam, detector=self.detector, goniometer=self.goniometer, scan=self.scan, crystal=self.crystal, profile=self.profile_model)) assert (len(self.detector) == 1) # Get the function object to mask the foreground self.mask_foreground = MaskCalculator3D(self.beam, self.detector, self.goniometer, self.scan, self.delta_d, self.delta_m)
def test(dials_data): from dxtbx.serialize import load from dials.algorithms import shoebox from dials.array_family import flex # Load the sequence and crystal sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath ) crystal = load.crystal( dials_data("centroid_test_data").join("crystal.json").strpath ) # Get models from the sequence detector = sequence.get_detector() # Get the reflections and overlaps reflections, adjacency_list = predict_reflections(sequence, crystal) reflections["shoebox"] = flex.shoebox(reflections["panel"], reflections["bbox"]) reflections["shoebox"].allocate_with_value(shoebox.MaskCode.Valid) # If the adjacency list is given, then create the reflection mask assert len(detector) == 1 image_size = detector[0].get_image_size() shoeboxes = reflections["shoebox"] coords = reflections["xyzcal.px"] shoebox_masker = shoebox.MaskOverlapping() shoebox_masker(shoeboxes, coords, adjacency_list) # Loop through all edges overlapping = [] for e in adjacency_list.edges(): v1, v2 = adjacency_list.source(e), adjacency_list.target(e) overlapping.append(v1) overlapping.append(v2) # Ensure elements are unique overlapping = set(overlapping) # Ensure we have some overlaps assert len(overlapping) > 0 # Get all non-overlapping reflections all_r = set(range(len(reflections))) non_overlapping = all_r.difference(overlapping) # Run the tests tst_non_overlapping(reflections, non_overlapping, detector[0].get_image_size()) tst_overlapping(reflections, overlapping, adjacency_list, image_size)
def run(self): from dxtbx.serialize import load from dials.algorithms import shoebox from dials.array_family import flex # Load the sweep and crystal self.sweep = load.imageset(self.sweep_filename) self.crystal = load.crystal(self.crystal_filename) # Get the reflections and overlaps reflections, adjacency_list = self.predict_reflections() reflections['shoebox'] = flex.shoebox(reflections['panel'], reflections['bbox']) reflections['shoebox'].allocate_with_value(shoebox.MaskCode.Valid) # If the adjacency list is given, then create the reflection mask assert (len(self.detector) == 1) image_size = self.detector[0].get_image_size() shoeboxes = reflections['shoebox'] coords = reflections['xyzcal.px'] shoebox_masker = shoebox.MaskOverlapping() shoebox_masker(shoeboxes, coords, adjacency_list) # Loop through all edges overlapping = [] for e in adjacency_list.edges(): v1, v2 = adjacency_list.source(e), adjacency_list.target(e) overlapping.append(v1) overlapping.append(v2) # Ensure elements are unique overlapping = set(overlapping) # Ensure we have some overlaps assert (len(overlapping) > 0) # Get all non-overlapping reflections all_r = set(range(len(reflections))) non_overlapping = all_r.difference(overlapping) # Run the tests self.tst_non_overlapping(reflections, non_overlapping, self.detector[0].get_image_size()) self.tst_overlapping(reflections, overlapping, adjacency_list, image_size)
def test(dials_data): from dxtbx.model.experiment_list import Experiment, ExperimentList from dxtbx.serialize import load from dials.algorithms.profile_model.gaussian_rs import MaskCalculator3D, Model sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath) crystal = load.crystal( dials_data("centroid_test_data").join("crystal.json").strpath) beam = sequence.get_beam() detector = sequence.get_detector() goniometer = sequence.get_goniometer() scan = sequence.get_scan() delta_d = 3 * beam.get_sigma_divergence(deg=False) try: mosaicity = crystal.get_mosaicity(deg=False) except AttributeError: mosaicity = 0 delta_m = 3 * mosaicity nsigma = 3 profile_model = Model(None, nsigma, beam.get_sigma_divergence(deg=False), mosaicity) experiment = ExperimentList() experiment.append( Experiment( imageset=sequence, beam=beam, detector=detector, goniometer=goniometer, scan=scan, crystal=crystal, profile=profile_model, )) assert len(detector) == 1 # Get the function object to mask the foreground mask_foreground = MaskCalculator3D(beam, detector, goniometer, scan, delta_d, delta_m) from scitbx.array_family import flex from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem from dials.algorithms.shoebox import MaskCode s0 = beam.get_s0() m2 = goniometer.get_rotation_axis() s0_length = matrix.col(beam.get_s0()).length() width, height = detector[0].get_image_size() zrange = scan.get_array_range() phi0, dphi = scan.get_oscillation(deg=False) # Generate some reflections reflections = generate_reflections(detector, beam, scan, experiment, 10) # Mask the foreground in each mask_foreground( reflections["shoebox"], reflections["s1"], reflections["xyzcal.px"].parts()[2], reflections["panel"], ) # Loop through all the reflections and check the mask values shoebox = reflections["shoebox"] beam_vector = reflections["s1"] rotation_angle = reflections["xyzcal.mm"].parts()[2] for l in range(len(reflections)): mask = shoebox[l].mask x0, x1, y0, y1, z0, z1 = shoebox[l].bbox s1 = beam_vector[l] phi = rotation_angle[l] cs = CoordinateSystem(m2, s0, s1, phi) def rs_coord(i, j, k): s1d = detector[0].get_pixel_lab_coord((i, j)) s1d = matrix.col(s1d).normalize() * s0_length e1, e2 = cs.from_beam_vector(s1d) e3 = cs.from_rotation_angle_fast(phi0 + (k - zrange[0]) * dphi) return e1, e2, e3 new_mask = flex.int(mask.accessor(), 0) for k in range(z1 - z0): for j in range(y1 - y0): for i in range(x1 - x0): # value1 = mask[k, j, i] e11, e12, e13 = rs_coord(x0 + i, y0 + j, z0 + k) e21, e22, e23 = rs_coord(x0 + i + 1, y0 + j, z0 + k) e31, e32, e33 = rs_coord(x0 + i, y0 + j + 1, z0 + k) e41, e42, e43 = rs_coord(x0 + i, y0 + j, z0 + k + 1) e51, e52, e53 = rs_coord(x0 + i + 1, y0 + j + 1, z0 + k) e61, e62, e63 = rs_coord(x0 + i + 1, y0 + j, z0 + k + 1) e71, e72, e73 = rs_coord(x0 + i, y0 + j + 1, z0 + k + 1) e81, e82, e83 = rs_coord(x0 + i + 1, y0 + j + 1, z0 + k + 1) de1 = (e11 / delta_d)**2 + ( e12 / delta_d)**2 # +(e13/delta_m)**2 de2 = (e21 / delta_d)**2 + ( e22 / delta_d)**2 # +(e23/delta_m)**2 de3 = (e31 / delta_d)**2 + ( e32 / delta_d)**2 # +(e33/delta_m)**2 de4 = (e41 / delta_d)**2 + ( e42 / delta_d)**2 # +(e43/delta_m)**2 de5 = (e51 / delta_d)**2 + ( e52 / delta_d)**2 # +(e53/delta_m)**2 de6 = (e61 / delta_d)**2 + ( e62 / delta_d)**2 # +(e63/delta_m)**2 de7 = (e71 / delta_d)**2 + ( e72 / delta_d)**2 # +(e73/delta_m)**2 de8 = (e81 / delta_d)**2 + ( e82 / delta_d)**2 # +(e83/delta_m)**2 de = math.sqrt( min([de1, de2, de3, de4, de5, de6, de7, de8])) if (x0 + i < 0 or y0 + j < 0 or x0 + i >= width or y0 + j >= height or z0 + k < zrange[0] or z0 + k >= zrange[1]): value2 = MaskCode.Valid else: if de <= 1.0: value2 = MaskCode.Valid | MaskCode.Foreground else: value2 = MaskCode.Valid | MaskCode.Background new_mask[k, j, i] = value2 if not all(m1 == m2 for m1, m2 in zip(mask, new_mask)): np.set_printoptions(threshold=10000) diff = (mask == new_mask).as_numpy_array() print(diff.astype(np.int)) # print mask.as_numpy_array() # print new_mask.as_numpy_array() # print (new_mask.as_numpy_array()[:,:,:] %2) * (new_mask.as_numpy_array() == 5) assert False