def test_indexing(): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) zstep = depth / nz eps = 1e-10 dt = 2 * math.pi / 8 r2 = sampler.r2() xp = [width / 2.0 ] + [width / 2.0 + r2 * math.cos(i * dt) for i in range(8)] yp = [height / 2.0 ] + [height / 2.0 + r2 * math.sin(i * dt) for i in range(8)] zind = [[k] * 9 for k in range(nz)] zind = [i for j in zind for i in j] zp = [(z + 0.5) * zstep + scan_range[0] for z in zind] for x0, y0, z0, i in zip(xp, yp, zp, range(len(sampler))): x1, y1, z1 = sampler.coord(i) assert (abs(x0 - x1) <= eps) assert (abs(y0 - y1) <= eps) assert (abs(z0 - z1) <= eps)
def tst_indexing(self): from dials.algorithms.profile_model.modeller import CircleSampler from math import sin, cos, pi width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) zstep = depth / nz eps = 1e-10 dt = 2 * pi / 8 r2 = sampler.r2() xp = [width / 2.0] + [width / 2.0 + r2 * cos(i * dt) for i in range(8)] yp = [height / 2.0] + [height / 2.0 + r2 * sin(i * dt) for i in range(8)] zind = [[k] * 9 for k in range(nz)] zind = [i for j in zind for i in j] zp = [(z + 0.5) * zstep + scan_range[0] for z in zind] for x0, y0, z0, i in zip(xp, yp, zp, range(len(sampler))): x1, y1, z1 = sampler.coord(i) assert(abs(x0 - x1) <= eps) assert(abs(y0 - y1) <= eps) assert(abs(z0 - z1) <= eps) print 'OK'
def test_nearest(): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) xc, yc = sampler.image_centre() r1 = sampler.r1() for i in range(1000): x = random.uniform(0, 1000) y = random.uniform(0, 1000) z = random.uniform(scan_range[0], scan_range[1]) r = math.sqrt((x - xc)**2 + (y - yc)**2) if r < r1: index00 = 0 else: t = math.atan2(y - yc, x - xc) ai = int(math.floor(t * 8 / (2 * math.pi) + 0.5)) % 8 index00 = ai + 1 index01 = int((z - scan_range[0]) * nz / depth) if index01 < 0: index01 = 0 if index01 >= 2: index01 = 1 index0 = index00 + index01 * 9 index1 = sampler.nearest(0, (x, y, z)) assert index0 == index1
def tst_nearest_n(self): from math import sqrt, atan2, pi, floor from random import uniform from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) xc, yc = sampler.image_centre() r1 = sampler.r1() for i in range(1000): x = uniform(0, 1000) y = uniform(0, 1000) z = uniform(scan_range[0], scan_range[1]) r = sqrt((x - xc)**2 + (y - yc)**2) if r < r1: index00 = 0 else: t = atan2(y - yc, x - xc) ai = int(floor(t * 8 / (2 * pi) + 0.5)) % 8 index00 = ai + 1 index01 = int((z-scan_range[0]) * nz / depth) if index01 < 0: index01 = 0 if index01 >= 2: index01 = 1 index0 = index00 + index01 * 9 index1 = sampler.nearest_n(0, (x, y, z)) assert(index0 == index1[0]) if index0 % 9 == 0: assert(len(index1) == 9) assert(all(idx == index0 + i for i, idx in enumerate(index1))) else: assert(len(index1) == 4) assert(index1[1] == (index0 // 9) * 9) if (index0 % 9) == 1: assert(index1[2] == index0 + 1) assert(index1[3] == index0 + 7) elif (index0 % 9) == 8: assert(index1[2] == index0 - 7) assert(index1[3] == index0 - 1) else: assert(index1[2] == index0 + 1) assert(index1[3] == index0 - 1) print 'OK'
def tst_nearest_n(self): from math import sqrt, atan2, pi, floor from random import uniform from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) xc, yc = sampler.image_centre() r1 = sampler.r1() for i in range(1000): x = uniform(0, 1000) y = uniform(0, 1000) z = uniform(scan_range[0], scan_range[1]) r = sqrt((x - xc)**2 + (y - yc)**2) if r < r1: index00 = 0 else: t = atan2(y - yc, x - xc) ai = int(floor(t * 8 / (2 * pi) + 0.5)) % 8 index00 = ai + 1 index01 = int((z - scan_range[0]) * nz / depth) if index01 < 0: index01 = 0 if index01 >= 2: index01 = 1 index0 = index00 + index01 * 9 index1 = sampler.nearest_n((x, y, z)) assert (index0 == index1[0]) if index0 % 9 == 0: assert (len(index1) == 9) assert (all(idx == index0 + i for i, idx in enumerate(index1))) else: assert (len(index1) == 4) assert (index1[1] == (index0 // 9) * 9) if (index0 % 9) == 1: assert (index1[2] == index0 + 1) assert (index1[3] == index0 + 7) elif (index0 % 9) == 8: assert (index1[2] == index0 - 7) assert (index1[3] == index0 - 1) else: assert (index1[2] == index0 + 1) assert (index1[3] == index0 - 1) print 'OK'
def test_pickle(): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) nz = 2 sampler = CircleSampler((width, height), scan_range, nz) sampler2 = pickle.loads(pickle.dumps(sampler)) assert sampler.image_size() == sampler2.image_size() assert sampler.num_z() == sampler2.num_z()
def test_self_consistent(): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) for i in range(len(sampler)): coord = sampler.coord(i) index = sampler.nearest(0, coord) assert index == i
def tst_getters(self): from math import sqrt from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) image_size = sampler.image_size() scan_range = sampler.scan_range() image_centre = sampler.image_centre() r0 = sampler.r0() r1 = sampler.r1() r2 = sampler.r2() size = len(sampler) assert (width == image_size[0]) assert (height == image_size[1]) assert (width // 2 == image_centre[0]) assert (height // 2 == image_centre[1]) assert (r0 == min([width // 2, height // 2])) assert (r1 == r0 / 3.0) assert (r2 == r1 * sqrt(5.0)) assert (9 * nz == size) print 'OK'
def test_nearest_n(): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) xc, yc = sampler.image_centre() r1 = sampler.r1() for i in range(1000): x = random.uniform(0, 1000) y = random.uniform(0, 1000) z = random.uniform(scan_range[0], scan_range[1]) r = math.sqrt((x - xc)**2 + (y - yc)**2) if r < r1: index00 = 0 else: t = math.atan2(y - yc, x - xc) ai = int(math.floor(t * 8 / (2 * math.pi) + 0.5)) % 8 index00 = ai + 1 index01 = int((z - scan_range[0]) * nz / depth) if index01 < 0: index01 = 0 if index01 >= 2: index01 = 1 index0 = index00 + index01 * 9 index1 = sampler.nearest_n(0, (x, y, z)) assert index0 == index1[0] if index0 % 9 == 0: assert len(index1) == 9 assert all(idx == index0 + i for i, idx in enumerate(index1)) else: assert len(index1) == 4 assert index1[1] == (index0 // 9) * 9 if (index0 % 9) == 1: assert index1[2] == index0 + 1 assert index1[3] == index0 + 7 elif (index0 % 9) == 8: assert index1[2] == index0 - 7 assert index1[3] == index0 - 1 else: assert index1[2] == index0 + 1 assert index1[3] == index0 - 1
def tst_self_consistent(self): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) for i in range(len(sampler)): coord = sampler.coord(i) index = sampler.nearest(0, coord) assert(index == i) print 'OK'
def tst_getters(self): from math import sqrt from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) image_size = sampler.image_size() scan_range = sampler.scan_range() image_centre = sampler.image_centre() r0 = sampler.r0() r1 = sampler.r1() r2 = sampler.r2() size = len(sampler) assert(width == image_size[0]) assert(height == image_size[1]) assert(width // 2 == image_centre[0]) assert(height // 2 == image_centre[1]) assert(r0 == min([width // 2, height // 2])) assert(r1 == r0 / 3.0) assert(r2 == r1 * sqrt(5.0)) assert(9 * nz == size) print 'OK'
def create(cls, data, detector_space=False, deconvolution=False): from dials.algorithms.profile_model.gaussian_rs.algorithm import ( GaussianRSIntensityCalculatorFactory, ) from dials.algorithms.integration.parallel_integrator import ( GaussianRSReferenceProfileData, ) from dials.algorithms.integration.parallel_integrator import ( GaussianRSMultiCrystalReferenceProfileData, ) from dials.algorithms.integration.parallel_integrator import ( ReferenceProfileData, ) from dials.algorithms.profile_model.modeller import CircleSampler from dials.algorithms.profile_model.gaussian_rs.transform import TransformSpec reference = data.reference[0] experiments = data.experiments assert len(reference) % 9 == 0 num_scan_points = len(reference) // 9 data_spec = GaussianRSMultiCrystalReferenceProfileData() for e in experiments: sampler = CircleSampler( e.detector[0].get_image_size(), e.scan.get_array_range(), num_scan_points, ) spec = TransformSpec( e.beam, e.detector, e.goniometer, e.scan, e.profile.sigma_b(deg=False), e.profile.sigma_m(deg=False), e.profile.n_sigma() * 1.5, 5, ) temp = reference reference = ReferenceProfileData() for d, m in temp: reference.append(d, m) spec = GaussianRSReferenceProfileData(reference, sampler, spec) data_spec.append(spec) return GaussianRSIntensityCalculatorFactory.create( data_spec, detector_space, deconvolution )
def tst_pickle(self): from dials.algorithms.profile_model.modeller import CircleSampler import tempfile import cPickle as pickle width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) tf = tempfile.TemporaryFile() pickle.dump(sampler, tf) tf.flush() tf.seek(0) sampler2 = pickle.load(tf) assert(sampler.image_size() == sampler2.image_size()) assert(sampler.num_z() == sampler2.num_z()) print 'OK'
def tst_pickle(self): from dials.algorithms.profile_model.modeller import CircleSampler import tempfile import cPickle as pickle width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) tf = tempfile.TemporaryFile() pickle.dump(sampler, tf) tf.flush() tf.seek(0) sampler2 = pickle.load(tf) assert (sampler.image_size() == sampler2.image_size()) assert (sampler.num_z() == sampler2.num_z()) print 'OK'
def load_sampler(experiments, reference): from dials.algorithms.profile_model.modeller import CircleSampler assert len(reference[0]) % 9 == 0 num_scan_points = len(reference[0]) // 9 sampler = CircleSampler( experiments[0].detector[0].get_image_size(), experiments[0].scan.get_array_range(), num_scan_points, ) return sampler
def test_detector_area(): from dials.algorithms.profile_model.modeller import CircleSampler from scitbx.array_family import flex width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) im = flex.int(flex.grid(height, width)) for j in range(height): for i in range(width): im[j, i] = sampler.nearest(0, (i, j, 0)) assert (im[height // 2, width // 2] == 0) assert (im[height // 2, width - 1] == 1) assert (im[height - 1, width - 1] == 2) assert (im[height - 1, width // 2] == 3) assert (im[height - 1, 0] == 4) assert (im[height // 2, 0] == 5) assert (im[0, 0] == 6) assert (im[0, width // 2] == 7) assert (im[0, width - 1] == 8)
def tst_nearest(self): from math import sqrt, atan2, pi, floor from random import uniform from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) xc, yc = sampler.image_centre() r1 = sampler.r1() for i in range(1000): x = uniform(0, 1000) y = uniform(0, 1000) z = uniform(scan_range[0], scan_range[1]) r = sqrt((x - xc)**2 + (y - yc)**2) if r < r1: index00 = 0 else: t = atan2(y - yc, x - xc) ai = int(floor(t * 8 / (2 * pi) + 0.5)) % 8 index00 = ai + 1 index01 = int((z-scan_range[0]) * nz / depth) if index01 < 0: index01 = 0 if index01 >= 2: index01 = 1 index0 = index00 + index01 * 9 index1 = sampler.nearest(0, (x, y, z)) assert(index0 == index1) print 'OK'
def tst_detector_area(self): from dials.algorithms.profile_model.modeller import CircleSampler from scitbx.array_family import flex width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) im = flex.int(flex.grid(height, width)) for j in range(height): for i in range(width): im[j,i] = sampler.nearest(0, (i, j, 0)) assert(im[height//2, width//2] == 0) assert(im[height//2, width-1] == 1) assert(im[height-1, width-1] == 2) assert(im[height-1, width//2] == 3) assert(im[height-1, 0] == 4) assert(im[height//2, 0] == 5) assert(im[0, 0] == 6) assert(im[0, width//2] == 7) assert(im[0, width-1] == 8) print 'OK'
def test_gaussianrs_profile_data_pickling(data): from dials.algorithms.integration.parallel_integrator import ( GaussianRSReferenceProfileData, ) from dials.algorithms.integration.parallel_integrator import ( GaussianRSMultiCrystalReferenceProfileData, ) from dials.algorithms.integration.parallel_integrator import ReferenceProfileData from dials.algorithms.profile_model.modeller import CircleSampler from dials.algorithms.profile_model.gaussian_rs.transform import TransformSpec reference = data.reference[0] experiments = data.experiments assert len(reference) % 9 == 0 num_scan_points = len(reference) // 9 data_spec = GaussianRSMultiCrystalReferenceProfileData() for e in experiments: sampler = CircleSampler( e.detector[0].get_image_size(), e.scan.get_array_range(), num_scan_points ) spec = TransformSpec( e.beam, e.detector, e.goniometer, e.scan, e.profile.sigma_b(deg=False), e.profile.sigma_m(deg=False), e.profile.n_sigma() * 1.5, 5, ) temp = reference reference = ReferenceProfileData() for d, m in temp: reference.append(d, m) spec = GaussianRSReferenceProfileData(reference, sampler, spec) data_spec.append(spec) s = pickle.dumps(data_spec) pickle.loads(s)
def test_z_index(): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) assert (sampler.nearest(0, (500, 500, 2.0)) / 9) == 0 assert (sampler.nearest(0, (500, 500, 3.0)) / 9) == 0 assert (sampler.nearest(0, (500, 500, 4.0)) / 9) == 0 assert (sampler.nearest(0, (500, 500, 5.0)) / 9) == 0 assert (sampler.nearest(0, (500, 500, 6.0)) / 9) == 0 assert (sampler.nearest(0, (500, 500, 6.5)) / 9) == 0 assert (sampler.nearest(0, (500, 500, 7.0)) / 9) == 1 assert (sampler.nearest(0, (500, 500, 7.5)) / 9) == 1 assert (sampler.nearest(0, (500, 500, 8.0)) / 9) == 1 assert (sampler.nearest(0, (500, 500, 9.0)) / 9) == 1 assert (sampler.nearest(0, (500, 500, 10.0)) / 9) == 1 assert (sampler.nearest(0, (500, 500, 11.0)) / 9) == 1 assert (sampler.nearest(0, (500, 500, 12.0)) / 9) == 1
def tst_z_index(self): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 2 sampler = CircleSampler((width, height), scan_range, nz) assert((sampler.nearest(0, (500, 500, 2.0)) / 9) == 0) assert((sampler.nearest(0, (500, 500, 3.0)) / 9) == 0) assert((sampler.nearest(0, (500, 500, 4.0)) / 9) == 0) assert((sampler.nearest(0, (500, 500, 5.0)) / 9) == 0) assert((sampler.nearest(0, (500, 500, 6.0)) / 9) == 0) assert((sampler.nearest(0, (500, 500, 6.5)) / 9) == 0) assert((sampler.nearest(0, (500, 500, 7.0)) / 9) == 1) assert((sampler.nearest(0, (500, 500, 7.5)) / 9) == 1) assert((sampler.nearest(0, (500, 500, 8.0)) / 9) == 1) assert((sampler.nearest(0, (500, 500, 9.0)) / 9) == 1) assert((sampler.nearest(0, (500, 500, 10.0)) / 9) == 1) assert((sampler.nearest(0, (500, 500, 11.0)) / 9) == 1) assert((sampler.nearest(0, (500, 500, 12.0)) / 9) == 1) print 'OK'
def construct_reference(experiments, reference): from dials.algorithms.integration.parallel_integrator import ( GaussianRSReferenceProfileData, ) from dials.algorithms.integration.parallel_integrator import ( GaussianRSMultiCrystalReferenceProfileData, ) from dials.algorithms.integration.parallel_integrator import ReferenceProfileData from dials.algorithms.profile_model.modeller import CircleSampler from dials.array_family import flex from dials.algorithms.profile_model.gaussian_rs.transform import TransformSpec assert len(reference) % 9 == 0 num_scan_points = len(reference) // 9 data_spec = GaussianRSMultiCrystalReferenceProfileData() for e in experiments: sampler = CircleSampler(e.detector[0].get_image_size(), e.scan.get_array_range(), num_scan_points) spec = TransformSpec( e.beam, e.detector, e.goniometer, e.scan, e.profile.sigma_b(deg=False), e.profile.sigma_m(deg=False), e.profile.n_sigma() * 1.5, grid_size, ) temp = reference reference = ReferenceProfileData() for d, m in temp: reference.append(d, m) spec = GaussianRSReferenceProfileData(reference, sampler, spec) data_spec.append(spec) return data_spec
def test_getters(): from dials.algorithms.profile_model.modeller import CircleSampler width = 1000 height = 1000 scan_range = (2, 12) nz = 2 sampler = CircleSampler((width, height), scan_range, nz) image_size = sampler.image_size() image_centre = sampler.image_centre() r0 = sampler.r0() r1 = sampler.r1() r2 = sampler.r2() size = len(sampler) assert width == image_size[0] assert height == image_size[1] assert width // 2 == image_centre[0] assert height // 2 == image_centre[1] assert r0 == min([width // 2, height // 2]) assert r1 == r0 / 3.0 assert r2 == r1 * math.sqrt(5.0) assert 9 * nz == size
def create(cls, experiments, grid_size=5, scan_step=5, grid_method="circular_grid"): """ Create the intensity calculator """ from dials.algorithms.integration.parallel_integrator import ( GaussianRSReferenceCalculator, ) from dials.algorithms.profile_model.modeller import SingleSampler from dials.algorithms.profile_model.modeller import CircleSampler from dials.algorithms.profile_model.modeller import GridSampler from dials.algorithms.profile_model.modeller import EwaldSphereSampler from dials.algorithms.profile_model.gaussian_rs.transform import TransformSpec from math import ceil # Assume the detector and scan are the same in each case detector = experiments[0].detector scan = experiments[0].scan # Get the number of scan points scan_range = scan.get_oscillation_range(deg=True) scan_range = abs(scan_range[1] - scan_range[0]) num_scan_points = int(ceil(scan_range / scan_step)) # If multi panel then set to single if grid_method in ["regular_grid", "circular_grid" ] and len(detector) > 1: grid_method = "single" # Create the sampler if grid_method == "single": sampler = SingleSampler(scan.get_array_range(), num_scan_points) elif grid_method == "regular_grid": sampler = GridSampler( detector[0].get_image_size(), scan.get_array_range(), (3, 3, num_scan_points), ) elif grid_method == "circular_grid": sampler = CircleSampler(detector[0].get_image_size(), scan.get_array_range(), num_scan_points) elif grid_method == "spherical_grid": sampler = EwaldSphereGridSampler( experiments[0].beam, experiments[0].detector, experiments[0].goniometer, experiments[0].scan, num_scan_points, ) else: raise RuntimeError("Unknown grid type") # Create the spec list spec_list = [] for experiment in experiments: spec = TransformSpec( experiment.beam, experiment.detector, experiment.goniometer, experiment.scan, experiment.profile.sigma_b(deg=False), experiment.profile.sigma_m(deg=False), experiment.profile.n_sigma() * 1.5, grid_size, ) spec_list.append(spec) # Return the intensity algorithm return GaussianRSReferenceCalculator(sampler, spec_list)
def tst_weights(self): from dials.algorithms.profile_model.modeller import CircleSampler from math import exp, log from scitbx import matrix width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 1 sampler = CircleSampler((width, height), scan_range, nz) # Check the weight at the coord in 1.0 eps = 1e-7 for i in range(len(sampler)): coord = sampler.coord(i) weight = sampler.weight(i, 0, coord) assert(abs(weight - 1.0) < eps) r0 = sampler.r0() r1 = sampler.r1() r2 = sampler.r2() r = r2 / (2.0*r1) expected = exp(-4.0*r*r*log(2.0)) for i in range(1, 9): coord = sampler.coord(i) weight = sampler.weight(0, 0, coord) assert(abs(weight - expected) < eps) r = r2 / (2.0*(r2 - r1)) expected = exp(-4.0*r*r*log(2.0)) for i in range(1, 9): coord = sampler.coord(0) weight = sampler.weight(i, 0, coord) assert(abs(weight - expected) < eps) for i in range(1, 9): coord1 = matrix.col(sampler.coord(0)) coord2 = matrix.col(sampler.coord(i)) coord = coord1 + r1 * (coord2 - coord1) / r2 weight = sampler.weight(0, 0, coord) assert(abs(weight - 0.5) < eps) weight = sampler.weight(i, 0, coord) assert(abs(weight - 0.5) < eps) print 'OK'
def integrate(experiments, reflections, reference): from dials.algorithms.profile_model.modeller import CircleSampler from dials.array_family import flex from dials.algorithms.profile_model.gaussian_rs.transform import TransformReverse from dials.algorithms.profile_model.gaussian_rs.transform import TransformForward from dials.algorithms.profile_model.gaussian_rs.transform import ( TransformReverseNoModel, ) from dials.algorithms.profile_model.gaussian_rs.transform import TransformSpec from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem selection = reflections.get_flags(reflections.flags.integrated_sum) reflections = reflections.select(selection) print("Selected %d reflections to integrate" % len(reflections)) sampler = CircleSampler( experiments[0].detector[0].get_image_size(), experiments[0].scan.get_array_range(), 1, ) n_sigma = 4.0 grid_size = 25 spec = TransformSpec( experiments[0].beam, experiments[0].detector, experiments[0].goniometer, experiments[0].scan, experiments[0].profile.sigma_b(deg=False), experiments[0].profile.sigma_m(deg=False), n_sigma, grid_size, ) m2 = experiments[0].goniometer.get_rotation_axis() s0 = experiments[0].beam.get_s0() Iprf = flex.double(len(reflections)) Vprf = flex.double(len(reflections)) Cprf = flex.double(len(reflections)) Fprf = flex.bool(len(reflections)) for i, r in enumerate(reflections): s1 = r["s1"] phi = r["xyzcal.mm"][2] xyz = r["xyzcal.px"] bbox = r["bbox"] panel = r["panel"] image = r["shoebox"].data.as_double() background = r["shoebox"].background.as_double() mask = r["shoebox"].mask.as_1d( ) == 5 # | (r['shoebox'].mask.as_1d() == 3) mask.reshape(image.accessor()) cs = CoordinateSystem(m2, s0, s1, phi) index = sampler.nearest(0, xyz) profile = reference[index] # print flex.sum(profile) # print r['partiality'] if False: from dials.algorithms.integration.maximum_likelihood import ( ProfileFittingDouble as ProfileFitting, ) transform = TransformReverseNoModel(spec, cs, bbox, panel, profile) p = transform.profile() d = image m = mask b = background # print flex.sum(p) ysize, xsize = p.all()[1:3] p1 = flex.double(flex.grid(1, ysize, xsize)) d1 = flex.double(flex.grid(1, ysize, xsize)) b1 = flex.double(flex.grid(1, ysize, xsize)) m1 = flex.double(flex.grid(1, ysize, xsize)) for k in range(p.all()[0]): p1 += p[k:k + 1, :, :] d1 += d[k:k + 1, :, :] b1 += b[k:k + 1, :, :] m1 = m[k:k + 1, :, :] try: fit = ProfileFitting(p1, m1, d1, b1, 1e-3, 1000) assert fit.niter() < 1000 Iprf[i] = fit.intensity() Vprf[i] = fit.variance() Cprf[i] = fit.correlation() Fprf[i] = True print(i, fit.intensity(), flex.sum(p1)) # from matplotlib import pylab # pylab.imshow(p1.as_numpy_array()[0,:,:], interpolation='none') # pylab.show() except Exception: pass else: from dials.algorithms.integration.fit import ( ProfileFittingDouble as ProfileFitting, ) try: transform = TransformForward(spec, cs, bbox, panel, image, background, mask) index = sampler.nearest(0, xyz) p = reference[index] d = transform.profile() b = transform.background() m = p > 0 fit = ProfileFitting(p, m, d, b, 1e-3, 1000) assert fit.niter() < 1000 Iprf[i] = fit.intensity() Vprf[i] = fit.variance() Cprf[i] = fit.correlation() Fprf[i] = True print(i, fit.intensity(), flex.sum(p)) # from matplotlib import pylab # pylab.imshow(p1.as_numpy_array()[0,:,:], interpolation='none') # pylab.show() except Exception: pass reflections["intensity.prf.value"] = Iprf reflections["intensity.prf.variance"] = Vprf reflections["intensity.prf.correlation"] = Cprf reflections.set_flags(Fprf, reflections.flags.integrated_prf) return reflections
def integrate_job(block, experiments, reflections, reference, grid_size=5, detector_space=False): from dials.algorithms.profile_model.modeller import CircleSampler from dials.array_family import flex from dials.algorithms.profile_model.gaussian_rs.transform import TransformReverse from dials.algorithms.profile_model.gaussian_rs.transform import TransformForward from dials.algorithms.profile_model.gaussian_rs.transform import ( TransformReverseNoModel, ) from dials.algorithms.profile_model.gaussian_rs.transform import TransformSpec from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem from dials.algorithms.integration.fit import ProfileFitter from dials.array_family import flex from dials.model.data import make_image reflections["shoebox"] = flex.shoebox(reflections["panel"], reflections["bbox"], allocate=True) frame0, frame1 = experiments[0].scan.get_array_range() frame0 = frame0 + block[0] frame1 = frame0 + block[1] reflections["shoebox"] = flex.shoebox(reflections["panel"], reflections["bbox"], allocate=True) extractor = flex.ShoeboxExtractor(reflections, 1, frame0, frame1) iset = experiments[0].imageset[block[0]:block[1]] for i in range(len(iset)): print("Reading image %d" % i) data = iset.get_raw_data(i) mask = iset.get_mask(i) extractor.next(make_image(data, mask)) print("Computing mask") reflections.compute_mask(experiments) print("Computing background") reflections.compute_background(experiments) print("Computing centroid") reflections.compute_centroid(experiments) print("Computing summed intensity") reflections.compute_summed_intensity() assert len(reference) % 9 == 0 num_scan_points = len(reference) // 9 sampler = CircleSampler( experiments[0].detector[0].get_image_size(), experiments[0].scan.get_array_range(), num_scan_points, ) spec = TransformSpec( experiments[0].beam, experiments[0].detector, experiments[0].goniometer, experiments[0].scan, experiments[0].profile.sigma_b(deg=False), experiments[0].profile.sigma_m(deg=False), experiments[0].profile.n_sigma() * 1.5, grid_size, ) m2 = experiments[0].goniometer.get_rotation_axis() s0 = experiments[0].beam.get_s0() Iprf = flex.double(len(reflections)) Vprf = flex.double(len(reflections)) Cprf = flex.double(len(reflections)) Fprf = flex.bool(len(reflections)) Part = reflections["partiality"] reflections["intensity.prf_old.value"] = reflections["intensity.prf.value"] reflections["intensity.prf_old.variance"] = reflections[ "intensity.prf.variance"] selection = reflections.get_flags(reflections.flags.integrated_prf) reflections.unset_flags(~Fprf, reflections.flags.integrated_prf) for i, r in enumerate(reflections): if selection[i] == False: continue s1 = r["s1"] phi = r["xyzcal.mm"][2] xyz = r["xyzcal.px"] bbox = r["bbox"] panel = r["panel"] image = r["shoebox"].data.as_double() background = r["shoebox"].background.as_double() mask = r["shoebox"].mask.as_1d( ) == 5 # | (r['shoebox'].mask.as_1d() == 3) mask.reshape(image.accessor()) cs = CoordinateSystem(m2, s0, s1, phi) index = sampler.nearest(0, xyz) profile, profile_mask = reference[index] # print flex.sum(profile) # print r['partiality'] if detector_space: transform = TransformReverseNoModel(spec, cs, bbox, panel, profile) p = transform.profile() d = image m = mask b = background # print flex.sum(p) Part[i] = flex.sum(p) # ysize, xsize = p.all()[1:3] # p1 = flex.double(flex.grid(1, ysize , xsize)) # d1 = flex.double(flex.grid(1, ysize , xsize)) # b1 = flex.double(flex.grid(1, ysize , xsize)) # m1 = flex.double(flex.grid(1, ysize , xsize)) # for k in range(p.all()[0]): # p1 += p[k:k+1,:,:] # d1 += d[k:k+1,:,:] # b1 += b[k:k+1,:,:] # m1 = m[k:k+1,:,:] try: fit = ProfileFitter(d, b, m, p, 1e-3, 100) assert fit.niter() < 100 Iprf[i] = fit.intensity() Vprf[i] = fit.variance() Cprf[i] = fit.correlation() Fprf[i] = True # if r['intensity.sum.value'] > 10 and abs(fit.intensity()) < 1e-3: print( r["miller_index"], i, fit.intensity(), r["intensity.sum.value"], r["intensity.prf_old.value"], Part[i], fit.niter(), ) # from matplotlib import pylab # pylab.imshow(p1.as_numpy_array()[0,:,:], interpolation='none') # pylab.show() except Exception as e: print(e) pass else: try: transform = TransformForward(spec, cs, bbox, panel, image, background, mask) p = profile d = transform.profile() b = transform.background() m = transform.mask() & profile_mask # if r['miller_index'] == (9, -25, 74): # print list(p) # print list(m) # print list(b) # print list(d) # exit(0) fit = ProfileFitter(d, b, m, p, 1e-3, 100) assert fit.niter() < 100 Iprf[i] = fit.intensity()[0] Vprf[i] = fit.variance()[0] Cprf[i] = fit.correlation() Fprf[i] = True print(r["miller_index"], i, fit.intensity(), r["intensity.prf_old.value"]) # from matplotlib import pylab # pylab.imshow(p1.as_numpy_array()[0,:,:], interpolation='none') # pylab.show() except Exception as e: pass reflections["intensity.prf.value"] = Iprf reflections["intensity.prf.variance"] = Vprf reflections["intensity.prf.correlation"] = Cprf reflections.set_flags(Fprf, reflections.flags.integrated_prf) del reflections["shoebox"] return reflections
def test_weights(): from dials.algorithms.profile_model.modeller import CircleSampler from scitbx import matrix width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nz = 1 sampler = CircleSampler((width, height), scan_range, nz) # Check the weight at the coord in 1.0 eps = 1e-7 for i in range(len(sampler)): coord = sampler.coord(i) weight = sampler.weight(i, 0, coord) assert abs(weight - 1.0) < eps r0 = sampler.r0() r1 = sampler.r1() r2 = sampler.r2() r = r2 / (2.0 * r1) expected = math.exp(-4.0 * r * r * math.log(2.0)) for i in range(1, 9): coord = sampler.coord(i) weight = sampler.weight(0, 0, coord) assert abs(weight - expected) < eps r = r2 / (2.0 * (r2 - r1)) expected = math.exp(-4.0 * r * r * math.log(2.0)) for i in range(1, 9): coord = sampler.coord(0) weight = sampler.weight(i, 0, coord) assert abs(weight - expected) < eps for i in range(1, 9): coord1 = matrix.col(sampler.coord(0)) coord2 = matrix.col(sampler.coord(i)) coord = coord1 + r1 * (coord2 - coord1) / r2 weight = sampler.weight(0, 0, coord) assert abs(weight - 0.5) < eps weight = sampler.weight(i, 0, coord) assert abs(weight - 0.5) < eps
def compute_reference(experiments, reflections): from dials.algorithms.profile_model.modeller import CircleSampler from dials.array_family import flex from dials.algorithms.profile_model.gaussian_rs.transform import TransformForward from dials.algorithms.profile_model.gaussian_rs.transform import TransformSpec from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem reflections = select_strong(reflections) print("Selected %d strong spots" % len(reflections)) sampler = CircleSampler( experiments[0].detector[0].get_image_size(), experiments[0].scan.get_array_range(), 1, ) n_sigma = 4.0 grid_size = 25 spec = TransformSpec( experiments[0].beam, experiments[0].detector, experiments[0].goniometer, experiments[0].scan, experiments[0].profile.sigma_b(deg=False), experiments[0].profile.sigma_m(deg=False), n_sigma, grid_size, ) m2 = experiments[0].goniometer.get_rotation_axis() s0 = experiments[0].beam.get_s0() reference = [ flex.double( flex.grid(1 + 2 * grid_size, 1 + 2 * grid_size, 1 + 2 * grid_size)) for i in range(len(sampler)) ] count = [0] * len(sampler) for r in reflections: s1 = r["s1"] phi = r["xyzcal.mm"][2] xyz = r["xyzcal.px"] bbox = r["bbox"] panel = r["panel"] image = r["shoebox"].data.as_double() mask = r["shoebox"].mask.as_1d() == 5 mask.reshape(image.accessor()) cs = CoordinateSystem(m2, s0, s1, phi) try: transform = TransformForward(spec, cs, bbox, panel, image, mask) d = transform.profile() d /= flex.sum(d) index = sampler.nearest(0, xyz) indices = sampler.nearest_n(0, xyz) for i in indices: w = sampler.weight(i, 0, xyz) reference[i] += w * d count[i] += 1 except Exception: pass for i in range(len(reference)): r = reference[i] if flex.sum(r) > 0: print(flex.max(r)) g = r.accessor() r = r.as_1d() s = r > 0.02 * flex.max(r) r.set_selected(~s, flex.double(len(r), 0)) r = r / flex.sum(r) r.reshape(g) reference[i] = r for i in range(len(reference)): from matplotlib import pylab print(count[i]) r = reference[i] d = r.as_numpy_array()[11, :, :] pylab.imshow(d, interpolation="None") pylab.show() return reference