def from_imageset_json_file(filename): ''' Load a datablock from a sweep file. ''' from dxtbx.serialize import load # Load the imageset and create a datablock from the filenames imageset = load.imageset(filename) return DataBlockFactory.from_imageset(imageset)
def tst_cspad_hierarchy(self): from dxtbx.serialize import dump, load from dxtbx.imageset import ImageSetFactory import os from glob import glob path = libtbx.env.dist_path('dials_regression') # Get the imageset filename = os.path.join(path, "spotfinding_test_data", "idx*.cbf") imageset = ImageSetFactory.new(glob(filename)) assert (len(imageset) == 1) imageset = imageset[0] # Dump and reload from uuid import uuid4 filename = '%s.json' % uuid4().hex dump.imageset(imageset, filename) imageset2 = load.imageset(filename) # Check they're are the same assert (imageset2.get_beam() == imageset.get_beam()) d1 = imageset.get_detector() d2 = imageset2.get_detector() assert (len(d1) == len(d2)) for i, (p1, p2) in enumerate(zip(d1, d2)): assert (p1 == p2) assert (imageset2.get_detector() == imageset.get_detector()) assert (imageset2 == imageset) # Test passed print 'OK'
def tst_no_image_data(self): from dxtbx.serialize import load import libtbx.load_env import os from glob import glob from dxtbx.imageset import ImageSweep, NullReader try: path = libtbx.env.dist_path('dials_regression') except Exception: print "No dials_regression directory found" return filename = os.path.join( path, 'centroid_test_data', 'sweep_no_image_data.json') imageset = load.imageset(filename) assert(isinstance(imageset, ImageSweep)) assert(isinstance(imageset.reader(), NullReader)) try: imageset[0] assert(False) except Exception: pass print 'OK'
def setup(dials_data): sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath ) fixture = {} # Get the models fixture["beam"] = sequence.get_beam() fixture["detector"] = sequence.get_detector() fixture["gonio"] = sequence.get_goniometer() fixture["scan"] = sequence.get_scan() # Set the delta_divergence/mosaicity n_sigma = 5 sigma_divergence = 0.060 * math.pi / 180 mosaicity = 0.154 * math.pi / 180 fixture["delta_divergence"] = n_sigma * sigma_divergence fixture["delta_mosaicity"] = n_sigma * mosaicity # Create the bounding box calculator fixture["calculate_bbox"] = BBoxCalculator3D( fixture["beam"], fixture["detector"], fixture["gonio"], fixture["scan"], fixture["delta_divergence"], fixture["delta_mosaicity"], ) return fixture
def tst_no_image_data(self): from dxtbx.serialize import load import libtbx.load_env import os from glob import glob from dxtbx.imageset import ImageSweep, NullReader try: path = libtbx.env.dist_path('dials_regression') except Exception: print "No dials_regression directory found" return filename = os.path.join(path, 'centroid_test_data', 'sweep_no_image_data.json') imageset = load.imageset(filename) assert (isinstance(imageset, ImageSweep)) assert (isinstance(imageset.reader(), NullReader)) try: imageset[0] assert (False) except Exception: pass print 'OK'
def tst_sweep(self): from dxtbx.serialize import load from dxtbx.serialize.helpers import tuple_almost_equal path = libtbx.env.dist_path('dials_regression') filename = os.path.join(path, "centroid_test_data", "test_sweep.json") sweep = load.imageset(filename) b = sweep.get_beam() d = sweep.get_detector() g = sweep.get_goniometer() s = sweep.get_scan() eps = 1e-7 assert (len(d) == 1) assert (tuple_almost_equal(b.get_direction(), (0.0, 0.0, 1.0))) assert (abs(b.get_wavelength() - 0.9795) < eps) assert (abs(b.get_divergence() - 0.0) < eps) assert (abs(b.get_sigma_divergence() - 0.058) < eps) assert (d[0].get_type() == "SENSOR_PAD") assert (d[0].get_name() == "Panel") assert (tuple_almost_equal(d[0].get_fast_axis(), (1.0, 0.0, 0.0))) assert (tuple_almost_equal(d[0].get_slow_axis(), (0.0, -1.0, 0.0))) assert (tuple_almost_equal(d[0].get_origin(), (-211.5, 219.5, -192.7))) assert (tuple_almost_equal(d[0].get_pixel_size(), (0.172, 0.172))) assert (tuple_almost_equal(d[0].get_image_size(), (2463, 2527))) assert (tuple_almost_equal(d[0].get_trusted_range(), (-1.0, 495976.0))) assert (tuple_almost_equal(g.get_rotation_axis(), (1.0, 0.0, 0.0))) assert (tuple_almost_equal( g.get_fixed_rotation(), (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0))) assert (tuple_almost_equal(s.get_image_range(), (1, 3))) assert (tuple_almost_equal(s.get_oscillation(), (0.0, 0.2))) assert (abs(s.get_exposure_times()[0] - 0.2) < eps) print 'OK'
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 sequence_and_model(dials_data): class Test: pass storage_class = Test() storage_class.sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models storage_class.beam = storage_class.sequence.get_beam() storage_class.detector = storage_class.sequence.get_detector() storage_class.gonio = storage_class.sequence.get_goniometer() storage_class.scan = storage_class.sequence.get_scan() return storage_class
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_generate_mask(dials_data): imageset = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath) params = dials.util.masking.phil_scope.extract() params.border = 2 params.d_min = 1.5 params.d_max = 40 params.untrusted[0].rectangle = (500, 600, 700, 800) params.untrusted[0].circle = (1000, 1200, 100) params.untrusted[0].polygon = (1500, 1500, 1600, 1600, 1700, 1500, 1500, 1500) params.untrusted[0].pixel = (1183, 1383) params.resolution_range = [(3.1, 3.2), (5.1, 5.2)] params.ice_rings.filter = True mask = dials.util.masking.generate_mask(imageset, params) assert len(mask) == len(imageset.get_detector()) for m, im in zip(mask, imageset.get_raw_data(0)): assert m.all() == im.all() assert mask[0].count(False) == 4060817
def tst_sweep(self): from dxtbx.serialize import load from dxtbx.serialize.helpers import tuple_almost_equal import libtbx.load_env import os try: path = libtbx.env.dist_path('dials_regression') except Exception: print "No dials_regression directory found" return filename = os.path.join(path, "centroid_test_data", "test_sweep.json") sweep = load.imageset(filename) b = sweep.get_beam() d = sweep.get_detector() g = sweep.get_goniometer() s = sweep.get_scan() eps = 1e-7 assert(len(d) == 1) assert(tuple_almost_equal(b.get_direction(), (0.0, 0.0, 1.0))) assert(abs(b.get_wavelength() - 0.9795) < eps) assert(abs(b.get_divergence() - 0.0) < eps) assert(abs(b.get_sigma_divergence() - 0.058) < eps) assert(d[0].get_type() == "SENSOR_PAD") assert(d[0].get_name() == "Panel") assert(tuple_almost_equal(d[0].get_fast_axis(), (1.0, 0.0, 0.0))) assert(tuple_almost_equal(d[0].get_slow_axis(), (0.0, -1.0, 0.0))) assert(tuple_almost_equal(d[0].get_origin(), (-211.5, 219.5, -192.7))) assert(tuple_almost_equal(d[0].get_pixel_size(), (0.172, 0.172))) assert(tuple_almost_equal(d[0].get_image_size(), (2463, 2527))) assert(tuple_almost_equal(d[0].get_trusted_range(), (-1.0, 495976.0))) assert(tuple_almost_equal(g.get_rotation_axis(), (1.0, 0.0, 0.0))) assert(tuple_almost_equal(g.get_fixed_rotation(), (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0))) assert(tuple_almost_equal(s.get_image_range(), (1, 3))) assert(tuple_almost_equal(s.get_oscillation(), (0.0, 0.2))) assert(abs(s.get_exposure_times()[0] - 0.2) < eps) print 'OK'
def tst_cspad_hierarchy(self): from dxtbx.serialize import dump, load from dxtbx.imageset import ImageSetFactory import libtbx.load_env import os from glob import glob try: path = libtbx.env.dist_path('dials_regression') except Exception: print "No dials_regression directory found" return # Get the imageset filename = os.path.join(path, "spotfinding_test_data", "idx*.cbf") imageset = ImageSetFactory.new(glob(filename)) assert(len(imageset) == 1) imageset = imageset[0] # Dump and reload from uuid import uuid4 filename = '%s.json' % uuid4().hex dump.imageset(imageset, filename) imageset2 = load.imageset(filename) # Check they're are the same assert(imageset2.get_beam() == imageset.get_beam()) d1 = imageset.get_detector() d2 = imageset2.get_detector() assert(len(d1) == len(d2)) for i, (p1, p2) in enumerate(zip(d1, d2)): assert(p1 == p2) assert(imageset2.get_detector() == imageset.get_detector()) assert(imageset2 == imageset) # Test passed print 'OK'
from __future__ import division import sys from dxtbx.serialize import load from dxtbx.model import ParallaxCorrectedPxMmStrategy, SimplePxMmStrategy from scitbx.array_family import flex from matplotlib import pylab convert = ParallaxCorrectedPxMmStrategy(0.252500934883) convert2 = SimplePxMmStrategy() sweep = load.imageset(sys.argv[1]) detector = sweep.get_detector() #print detector[0].pixel_to_millimeter((0, 1)) image_size = sweep[0].all() image = flex.double(flex.grid(image_size)) for j in range(image_size[0]): for i in range(image_size[1]): mm1 = convert2.to_millimeter(detector[0], (i, j)) px2 = convert.to_pixel(detector[0], mm1) image[j,i] = j - px2[1] print flex.max(image), flex.min(image) pylab.imshow(image.as_numpy_array()) pylab.show()
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
def test_run(dials_data): sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sequence.get_beam() detector = sequence.get_detector() gonio = sequence.get_goniometer() assert len(detector) == 1 # Get some stuff s0 = beam.get_s0() m2 = gonio.get_rotation_axis() image_size = detector[0].get_image_size() # Get a random s1/phi i = random.uniform(0, image_size[0]) j = random.uniform(1, image_size[1]) s1 = matrix.col(detector[0].get_pixel_lab_coord((i, j))) s1 = s1.normalize() * matrix.col(s0).length() phi = random.uniform(0, 5) x0 = int(math.floor(i - 10)) y0 = int(math.floor(j - 10)) # Set some parameters sigma_divergence = beam.get_sigma_divergence(deg=False) delta_divergence = 3 * sigma_divergence grid_half_size = 4 step_size = (delta_divergence / grid_half_size, delta_divergence / grid_half_size) # Create the coordinate system cs = CoordinateSystem(m2, s0, s1, phi) # Create the map of s1 coordinates s1_map = transform.beam_vector_map(detector[0], beam, True) # Create the grid index generator generate_indices = transform.GridIndexGenerator(cs, x0, y0, step_size, grid_half_size, s1_map) for j in range(0, 20): for i in range(0, 20): xx = x0 + i yy = y0 + j if xx < 0 or yy < 0 or xx >= image_size[0] or yy >= image_size[0]: continue # Get the grid indices gi_1, gj_1 = generate_indices(j, i) # Get the grid indices xyz = matrix.col(detector[0].get_pixel_lab_coord((x0 + i, y0 + j))) xyz = xyz.normalize() * matrix.col(s0).length() c1, c2 = matrix.col(cs.from_beam_vector(xyz)) gi_2 = grid_half_size + c1 / step_size[0] + 0.5 gj_2 = grid_half_size + c2 / step_size[1] + 0.5 # Check both are the same eps = 1e-7 assert abs(gj_1 - gj_2) <= eps, (gi_1, gi_2, gj_1, gj_2) assert abs(gi_1 - gi_2) <= eps, (gi_1, gi_2, gj_1, gj_2)
def test_map_frames_forward(dials_data): sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sequence.get_beam() detector = sequence.get_detector() gonio = sequence.get_goniometer() scan = sequence.get_scan() # Set the delta_divergence/mosaicity n_sigma = 3 sigma_divergence = 0.060 * math.pi / 180 mosaicity = 0.154 * math.pi / 180 delta_divergence = n_sigma * sigma_divergence delta_mosaicity = n_sigma * mosaicity # Set the grid size grid_size = (4, 4, 4) # Create the E3 fraction object transform = MapFramesForward( scan.get_array_range()[0], scan.get_oscillation(deg=False)[0], scan.get_oscillation(deg=False)[1], mosaicity, n_sigma, grid_size[2], ) # Create the bounding box calculator calculate_bbox = BBoxCalculator3D(beam, detector, gonio, scan, delta_divergence, delta_mosaicity) assert len(detector) == 1 s0 = beam.get_s0() m2 = gonio.get_rotation_axis() s0_length = matrix.col(beam.get_s0()).length() for i in range(100): # Get random x, y, z x = random.uniform(0, 2000) y = random.uniform(0, 2000) z = random.uniform(0, 9) # Get random s1, phi, panel s1 = matrix.col(detector[0].get_pixel_lab_coord( (x, y))).normalize() * s0_length phi = scan.get_angle_from_array_index(z, deg=False) panel = 0 # Calculate the bounding box bbox = calculate_bbox(s1, z, panel) # Create the XDS coordinate system xcs = CoordinateSystem(m2, s0, s1, phi) # Calculate the transform fraction fraction = transform(bbox[4:], phi, xcs.zeta()) # Ensure the minimum and maximum are 0 < 1 fmax = flex.max(fraction) fmin = flex.min(fraction) assert fmax <= ( 1.0 + 5e-15) and fmax > 0.0, f"{fmax:.16f} not between 0 and 1" assert fmin >= 0.0 and fmin <= 1.0 # Ensure the fraction for each image frame adds up to 1.0 for # all those frames completely within the grid for j in range(1, fraction.all()[0] - 1): tot = flex.sum(fraction[j:j + 1, :]) assert abs(tot - 1.0) < 1e-7 # Ensure the frames follow a progression through the grid. I.e, # check that values increase then decrease and don't jump around for j in range(fraction.all()[0]): f = fraction[j:j + 1, :] last = f[0] rev = False for i in range(1, len(f)): curr = f[1] if rev is False: if curr < last: rev = True else: assert curr <= last last = curr
def test_map_forward_reverse(dials_data): sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sequence.get_beam() detector = sequence.get_detector() gonio = sequence.get_goniometer() scan = sequence.get_scan() # Set the delta_divergence/mosaicity n_sigma = 3 sigma_divergence = 0.060 * math.pi / 180 mosaicity = 0.154 * math.pi / 180 delta_divergence = n_sigma * sigma_divergence delta_mosaicity = n_sigma * mosaicity # Set the grid size grid_size = (4, 4, 4) # Create the E3 fraction object transform_forward = MapFramesForward( scan.get_array_range()[0], scan.get_oscillation(deg=False)[0], scan.get_oscillation(deg=False)[1], mosaicity, n_sigma, grid_size[2], ) # Create the E3 fraction object transform_reverse = MapFramesReverse( scan.get_array_range()[0], scan.get_oscillation(deg=False)[0], scan.get_oscillation(deg=False)[1], mosaicity, n_sigma, grid_size[2], ) # Create the bounding box calculator calculate_bbox = BBoxCalculator3D(beam, detector, gonio, scan, delta_divergence, delta_mosaicity) s0 = beam.get_s0() m2 = gonio.get_rotation_axis() s0_length = matrix.col(beam.get_s0()).length() for i in range(100): # Get random x, y, z x = random.uniform(0, 2000) y = random.uniform(0, 2000) z = random.uniform(0, 9) # Get random s1, phi, panel s1 = matrix.col(detector[0].get_pixel_lab_coord( (x, y))).normalize() * s0_length phi = scan.get_angle_from_array_index(z, deg=False) panel = 0 # Calculate the bounding box bbox = calculate_bbox(s1, phi, panel) # Create the XDS coordinate system xcs = CoordinateSystem(m2, s0, s1, phi) # Calculate the transform fraction forward_fraction = transform_forward(bbox[4:], phi, xcs.zeta()) # Calculate the transform fraction reverse_fraction = transform_reverse(bbox[4:], phi, xcs.zeta()) # Check the same points are non-zero eps = 1e-7 for j in range(forward_fraction.all()[0]): for i in range(forward_fraction.all()[1]): if forward_fraction[j, i] > 0.0: assert reverse_fraction[i, j] > 0.0 else: assert reverse_fraction[i, j] < eps
from __future__ import division from __future__ import print_function import sys from dxtbx.serialize import load from dxtbx.model import ParallaxCorrectedPxMmStrategy, SimplePxMmStrategy from scitbx.array_family import flex from matplotlib import pylab convert = ParallaxCorrectedPxMmStrategy(0.252500934883) convert2 = SimplePxMmStrategy() sweep = load.imageset(sys.argv[1]) detector = sweep.get_detector() # print detector[0].pixel_to_millimeter((0, 1)) image_size = sweep[0].all() image = flex.double(flex.grid(image_size)) for j in range(image_size[0]): for i in range(image_size[1]): mm1 = convert2.to_millimeter(detector[0], (i, j)) px2 = convert.to_pixel(detector[0], mm1) image[j, i] = j - px2[1] print(flex.max(image), flex.min(image)) pylab.imshow(image.as_numpy_array()) pylab.show()
def test_forward(dials_data): sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sequence.get_beam() detector = sequence.get_detector() gonio = sequence.get_goniometer() scan = sequence.get_scan() # Set some parameters sigma_divergence = beam.get_sigma_divergence(deg=False) mosaicity = 0.157 * math.pi / 180 n_sigma = 3 grid_size = 7 delta_divergence = n_sigma * sigma_divergence step_size = delta_divergence / grid_size delta_divergence2 = delta_divergence + step_size * 0.5 delta_mosaicity = n_sigma * mosaicity # Create the bounding box calculator calculate_bbox = BBoxCalculator3D(beam, detector, gonio, scan, delta_divergence2, delta_mosaicity) # Initialise the transform spec = transform.TransformSpec(beam, detector, gonio, scan, sigma_divergence, mosaicity, n_sigma + 1, grid_size) # tst_conservation_of_counts(self): assert len(detector) == 1 s0 = beam.get_s0() m2 = gonio.get_rotation_axis() s0_length = matrix.col(beam.get_s0()).length() # Create an s1 map s1_map = transform.beam_vector_map(detector[0], beam, True) for i in range(100): # Get random x, y, z x = random.uniform(300, 1800) y = random.uniform(300, 1800) z = random.uniform(0, 9) # Get random s1, phi, panel s1 = matrix.col(detector[0].get_pixel_lab_coord( (x, y))).normalize() * s0_length phi = scan.get_angle_from_array_index(z, deg=False) panel = 0 # Calculate the bounding box bbox = calculate_bbox(s1, z, panel) x0, x1, y0, y1, z0, z1 = bbox # Create the coordinate system cs = CoordinateSystem(m2, s0, s1, phi) # The grid index generator step_size = delta_divergence / grid_size grid_index = transform.GridIndexGenerator(cs, x0, y0, (step_size, step_size), grid_size, s1_map) # Create the image # image = flex.double(flex.grid(z1 - z0, y1 - y0, x1 - x0), 1) image = gaussian((z1 - z0, y1 - y0, x1 - x0), 10.0, (z - z0, y - y0, x - x0), (2.0, 2.0, 2.0)) mask = flex.bool(flex.grid(image.all()), False) for j in range(y1 - y0): for i in range(x1 - x0): inside = False gx00, gy00 = grid_index(j, i) gx01, gy01 = grid_index(j, i + 1) gx10, gy10 = grid_index(j + 1, i) gx11, gy11 = grid_index(j + 1, i + 1) mingx = min([gx00, gx01, gx10, gx11]) maxgx = max([gx00, gx01, gx10, gx11]) mingy = min([gy00, gy01, gy10, gy11]) maxgy = max([gy00, gy01, gy10, gy11]) if (mingx >= 0 and maxgx < 2 * grid_size + 1 and mingy >= 0 and maxgy < 2 * grid_size + 1): inside = True for k in range(1, z1 - z0 - 1): mask[k, j, i] = inside # Transform the image to the grid transformed = transform.TransformForward(spec, cs, bbox, 0, image.as_double(), mask) grid = transformed.profile() # Get the sums and ensure they're the same eps = 1e-7 sum_grid = flex.sum(grid) sum_image = flex.sum(flex.double(flex.select(image, flags=mask))) assert abs(sum_grid - sum_image) <= eps # Test passed # tst_transform_with_background(self): assert len(detector) == 1 s0 = beam.get_s0() m2 = gonio.get_rotation_axis() s0_length = matrix.col(beam.get_s0()).length() # Create an s1 map s1_map = transform.beam_vector_map(detector[0], beam, True) for i in range(100): # Get random x, y, z x = random.uniform(300, 1800) y = random.uniform(300, 1800) z = random.uniform(0, 9) # Get random s1, phi, panel s1 = matrix.col(detector[0].get_pixel_lab_coord( (x, y))).normalize() * s0_length phi = scan.get_angle_from_array_index(z, deg=False) panel = 0 # Calculate the bounding box bbox = calculate_bbox(s1, z, panel) x0, x1, y0, y1, z0, z1 = bbox # Create the coordinate system cs = CoordinateSystem(m2, s0, s1, phi) # The grid index generator step_size = delta_divergence / grid_size grid_index = transform.GridIndexGenerator(cs, x0, y0, (step_size, step_size), grid_size, s1_map) # Create the image # image = flex.double(flex.grid(z1 - z0, y1 - y0, x1 - x0), 1) image = gaussian((z1 - z0, y1 - y0, x1 - x0), 10.0, (z - z0, y - y0, x - x0), (2.0, 2.0, 2.0)) background = flex.random_double(len(image)) background.resize(image.accessor()) mask = flex.bool(flex.grid(image.all()), False) for j in range(y1 - y0): for i in range(x1 - x0): inside = False gx00, gy00 = grid_index(j, i) gx01, gy01 = grid_index(j, i + 1) gx10, gy10 = grid_index(j + 1, i) gx11, gy11 = grid_index(j + 1, i + 1) mingx = min([gx00, gx01, gx10, gx11]) maxgx = max([gx00, gx01, gx10, gx11]) mingy = min([gy00, gy01, gy10, gy11]) maxgy = max([gy00, gy01, gy10, gy11]) if (mingx >= 0 and maxgx <= 2 * grid_size + 1 and mingy >= 0 and maxgy <= 2 * grid_size + 1): inside = True for k in range(1, z1 - z0 - 1): mask[k, j, i] = inside # Transform the image to the grid transformed = transform.TransformForward(spec, cs, bbox, 0, image.as_double(), background.as_double(), mask) igrid = transformed.profile() bgrid = transformed.background() # Get the sums and ensure they're the same eps = 1e-7 sum_igrid = flex.sum(igrid) sum_bgrid = flex.sum(bgrid) sum_image = flex.sum(flex.double(flex.select(image, flags=mask))) sum_bkgrd = flex.sum(flex.double(flex.select(background, flags=mask))) try: assert abs(sum_igrid - sum_image) <= eps assert abs(sum_bgrid - sum_bkgrd) <= eps except Exception: print("Failed for: ", (x, y, z)) raise
def test_forward_no_model(dials_data): sequence = load.imageset( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sequence.get_beam() detector = sequence.get_detector() gonio = sequence.get_goniometer() scan = sequence.get_scan() scan.set_image_range((0, 1000)) # Set some parameters sigma_divergence = beam.get_sigma_divergence(deg=False) mosaicity = 0.157 * math.pi / 180 n_sigma = 3 grid_size = 20 delta_divergence = n_sigma * sigma_divergence step_size = delta_divergence / grid_size delta_divergence2 = delta_divergence + step_size * 0.5 delta_mosaicity = n_sigma * mosaicity # Create the bounding box calculator calculate_bbox = BBoxCalculator3D(beam, detector, gonio, scan, delta_divergence2, delta_mosaicity) # Initialise the transform spec = transform.TransformSpec(beam, detector, gonio, scan, sigma_divergence, mosaicity, n_sigma + 1, grid_size) # tst_conservation_of_counts(self): random.seed(0) assert len(detector) == 1 s0 = beam.get_s0() m2 = gonio.get_rotation_axis() s0_length = matrix.col(beam.get_s0()).length() # Create an s1 map s1_map = transform.beam_vector_map(detector[0], beam, True) for i in range(100): # Get random x, y, z x = random.uniform(300, 1800) y = random.uniform(300, 1800) z = random.uniform(500, 600) # Get random s1, phi, panel s1 = matrix.col(detector[0].get_pixel_lab_coord( (x, y))).normalize() * s0_length phi = scan.get_angle_from_array_index(z, deg=False) panel = 0 # Calculate the bounding box bbox = calculate_bbox(s1, z, panel) x0, x1, y0, y1, z0, z1 = bbox # Create the coordinate system cs = CoordinateSystem(m2, s0, s1, phi) if abs(cs.zeta()) < 0.1: continue # The grid index generator step_size = delta_divergence / grid_size grid_index = transform.GridIndexGenerator(cs, x0, y0, (step_size, step_size), grid_size, s1_map) # Create the image # image = flex.double(flex.grid(z1 - z0, y1 - y0, x1 - x0), 1) image = gaussian((z1 - z0, y1 - y0, x1 - x0), 10.0, (z - z0, y - y0, x - x0), (2.0, 2.0, 2.0)) mask = flex.bool(flex.grid(image.all()), False) for j in range(y1 - y0): for i in range(x1 - x0): inside = False gx00, gy00 = grid_index(j, i) gx01, gy01 = grid_index(j, i + 1) gx10, gy10 = grid_index(j + 1, i) gx11, gy11 = grid_index(j + 1, i + 1) mingx = min([gx00, gx01, gx10, gx11]) maxgx = max([gx00, gx01, gx10, gx11]) mingy = min([gy00, gy01, gy10, gy11]) maxgy = max([gy00, gy01, gy10, gy11]) if (mingx >= 0 and maxgx < 2 * grid_size + 1 and mingy >= 0 and maxgy < 2 * grid_size + 1): inside = True for k in range(1, z1 - z0 - 1): mask[k, j, i] = inside # Transform the image to the grid transformed = transform.TransformForwardNoModel( spec, cs, bbox, 0, image.as_double(), mask) grid = transformed.profile() # Get the sums and ensure they're the same eps = 1e-7 sum_grid = flex.sum(grid) sum_image = flex.sum(flex.double(flex.select(image, flags=mask))) assert abs(sum_grid - sum_image) <= eps mask = flex.bool(flex.grid(image.all()), True) transformed = transform.TransformForwardNoModel( spec, cs, bbox, 0, image.as_double(), mask) grid = transformed.profile() # Boost the bbox to make sure all intensity is included x0, x1, y0, y1, z0, z1 = bbox bbox2 = (x0 - 10, x1 + 10, y0 - 10, y1 + 10, z0 - 10, z1 + 10) # Do the reverse transform transformed = transform.TransformReverseNoModel( spec, cs, bbox2, 0, grid) image2 = transformed.profile() # Check the sum of pixels are the same sum_grid = flex.sum(grid) sum_image = flex.sum(image2) assert abs(sum_grid - sum_image) <= eps # Do the reverse transform transformed = transform.TransformReverseNoModel( spec, cs, bbox, 0, grid) image2 = transformed.profile() from dials.algorithms.statistics import pearson_correlation_coefficient cc = pearson_correlation_coefficient(image.as_1d().as_double(), image2.as_1d()) assert cc >= 0.99
def from_imageset_json_file(filename): """Load a datablock from a sequence file.""" # Load the imageset and create a datablock from the filenames imageset = load.imageset(filename) return DataBlockFactory.from_imageset(imageset)