def tst_nearest(self): from random import uniform from dials.algorithms.profile_model.modeller import GridSampler from math import floor width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) for i in range(1000): x = uniform(0, 1000) y = uniform(0, 1000) z = uniform(*scan_range) i = int(floor(x / (width / nx))) j = int(floor(y / (height / ny))) k = int(floor((z-scan_range[0]) / (depth / nz))) if i >= nx: i = nx - 1 if j >= ny: j = ny - 1 if k >= nz: k = nz - 1 index0 = i + j * nx + k * nx * ny index1 = sampler.nearest((x, y, z)) assert(index0 == index1) print 'OK'
def test_indexing(): from dials.algorithms.profile_model.modeller import GridSampler width = 1000 height = 1000 scan_range = (2, 12) nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) xstep, ystep, zstep = sampler.step_size() xind = [[i for i in range(nx)]] * ny * nz yind = [[j] * nx for j in range(ny)] * nz zind = [[k] * nx * ny for k in range(nz)] xind = [i for j in xind for i in j] yind = [i for j in yind for i in j] zind = [i for j in zind for i in j] xp = [(x + 0.5) * xstep for x in xind] yp = [(y + 0.5) * ystep for y in yind] zp = [(z + 0.5) * zstep + scan_range[0] for z in zind] eps = 1e-10 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 GridSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) xstep, ystep, zstep = sampler.step_size() xind = [[i for i in range(nx)]] * ny * nz yind = [[j] * nx for j in range(ny)] * nz zind = [[k] * nx * ny for k in range(nz)] xind = [i for j in xind for i in j] yind = [i for j in yind for i in j] zind = [i for j in zind for i in j] xp = [(x + 0.5) * xstep for x in xind] yp = [(y + 0.5) * ystep for y in yind] zp = [(z + 0.5) * zstep + scan_range[0] for z in zind] eps = 1e-10 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 tst_getters(self): from dials.algorithms.profile_model.modeller import GridSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) image_size = sampler.image_size() scan_range = sampler.scan_range() grid_size = sampler.grid_size() step_size = sampler.step_size() size = len(sampler) assert(width == image_size[0]) assert(height == image_size[1]) assert(scan_range[0] == scan_range[0]) assert(scan_range[1] == scan_range[1]) assert(nx == grid_size[0]) assert(ny == grid_size[1]) assert(nz == grid_size[2]) assert(step_size[0] == width / nx) assert(step_size[1] == height / ny) assert(step_size[2] == depth / nz) assert(nx * ny * nz == size) print 'OK'
def test_nearest(): from dials.algorithms.profile_model.modeller import GridSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) for i in range(1000): x = random.uniform(0, 1000) y = random.uniform(0, 1000) z = random.uniform(*scan_range) i = int(math.floor(x / (width / nx))) j = int(math.floor(y / (height / ny))) k = int(math.floor((z - scan_range[0]) / (depth / nz))) if i >= nx: i = nx - 1 if j >= ny: j = ny - 1 if k >= nz: k = nz - 1 index0 = i + j * nx + k * nx * ny index1 = sampler.nearest(0, (x, y, z)) assert index0 == index1
def compute_profile(experiments, reflection, reference, N): from dials.array_family import flex from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem from dials.algorithms.profile_model.modeller import GridSampler from dials_scratch.jmp.sim import compute_profile_internal from random import uniform sbox = reflection["shoebox"] bbox = sbox.bbox zs = sbox.zsize() ys = sbox.ysize() xs = sbox.xsize() profile = flex.double(flex.grid(zs, ys, xs)) m2 = experiments[0].goniometer.get_rotation_axis_datum() s0 = experiments[0].beam.get_s0() s1 = reflection["s1"] phi = reflection["xyzcal.mm"][2] detector = experiments[0].detector scan = experiments[0].scan cs = CoordinateSystem(m2, s0, s1, phi) scan_range = scan.get_array_range() image_size = detector[0].get_image_size() grid_size = (3, 3, 40) assert grid_size[0] * grid_size[1] * grid_size[2] == len(reference[0]) sampler = GridSampler(image_size, scan_range, grid_size) xyz = reflection["xyzcal.px"] index = sampler.nearest(0, xyz) for g in reference[0]: assert abs(flex.sum(g) - 1.0) < 1e-7 grid = reference[0][index] sigma_d = experiments[0].profile.sigma_b(deg=False) sigma_m = experiments[0].profile.sigma_m(deg=False) delta_d = 3.0 * sigma_d delta_m = 3.0 * sigma_m profile = compute_profile_internal(grid, bbox, zs, ys, xs, N, delta_d, delta_m, detector, scan, cs) # from dials_scratch.jmp.viewer import show_image_stack_multi_view # show_image_stack_multi_view(profile.as_numpy_array(), vmax=max(profile)) sum_p = flex.sum(profile) print("Partiality: %f" % sum_p) try: assert sum_p > 0, "sum_p == 0" except Exception as e: print(e) return None return profile
def test_self_consistent(): from dials.algorithms.profile_model.modeller import GridSampler width = 1000 height = 1000 scan_range = (2, 12) nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) for i in range(len(sampler)): coord = sampler.coord(i) index = sampler.nearest(0, coord) assert index == i
def test_pickle(): from dials.algorithms.profile_model.modeller import GridSampler width = 1000 height = 1000 scan_range = (2, 12) nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) sampler2 = pickle.loads(pickle.dumps(sampler)) assert sampler.image_size() == sampler2.image_size() assert sampler.grid_size() == sampler2.grid_size()
def tst_self_consistent(self): from dials.algorithms.profile_model.modeller import GridSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) for i in range(len(sampler)): coord = sampler.coord(i) index = sampler.nearest(coord) assert(index == i) print 'OK'
def tst_nearest_n(self): from random import uniform from dials.algorithms.profile_model.modeller import GridSampler from math import floor width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) for i in range(1000): x = uniform(0, 1000) y = uniform(0, 1000) z = uniform(*scan_range) i = int(floor(x * nx / width)) j = int(floor(y * ny / height)) k = int(floor((z-scan_range[0]) * nz / depth)) if i >= nx: i = nx - 1 if j >= ny: j = ny - 1 if k >= nz: k = nz - 1 index0 = i + j * nx + k * nx * ny index1 = sampler.nearest_n((x, y, z)) assert(len(set(index1)) == len(index1)) assert(index0 == index1[-1]) for ind in index1: ii = ind % nx jk = ind // nx jj = jk % ny kk = jk // ny assert(abs(ii - i) <= 1) assert(abs(jj - j) <= 1) assert(abs(kk - k) <= 1) print 'OK'
def tst_nearest_n(self): from random import uniform from dials.algorithms.profile_model.modeller import GridSampler from math import floor width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) for i in range(1000): x = uniform(0, 1000) y = uniform(0, 1000) z = uniform(*scan_range) i = int(floor(x * nx / width)) j = int(floor(y * ny / height)) k = int(floor((z - scan_range[0]) * nz / depth)) if i >= nx: i = nx - 1 if j >= ny: j = ny - 1 if k >= nz: k = nz - 1 index0 = i + j * nx + k * nx * ny index1 = sampler.nearest_n(0, (x, y, z)) assert (len(set(index1)) == len(index1)) assert (index0 == index1[-1]) for ind in index1: ii = ind % nx jk = ind // nx jj = jk % ny kk = jk // ny assert (abs(ii - i) <= 1) assert (abs(jj - j) <= 1) assert (abs(kk - k) <= 1) print 'OK'
def test_getters(): from dials.algorithms.profile_model.modeller import GridSampler width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, nz)) image_size = sampler.image_size() scan_range = sampler.scan_range() grid_size = sampler.grid_size() step_size = sampler.step_size() size = len(sampler) assert width == image_size[0] assert height == image_size[1] assert scan_range[0] == scan_range[0] assert scan_range[1] == scan_range[1] assert nx == grid_size[0] assert ny == grid_size[1] assert nz == grid_size[2] assert step_size[0] == width / nx assert step_size[1] == height / ny assert step_size[2] == depth / nz assert nx * ny * nz == size
def tst_pickle(self): from dials.algorithms.profile_model.modeller import GridSampler import tempfile import cPickle as pickle width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, 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.grid_size() == sampler2.grid_size()) print 'OK'
def tst_pickle(self): from dials.algorithms.profile_model.modeller import GridSampler import tempfile import cPickle as pickle width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, 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.grid_size() == sampler2.grid_size()) print 'OK'
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 GridSampler from scitbx import matrix from math import log, exp width = 1000 height = 1000 scan_range = (2, 12) depth = scan_range[1] - scan_range[0] nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, 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, coord) assert(abs(weight - 1.0) < eps) # Ensure we get the expected weight at the next grid point at half way # between grid points expected = exp(-4.0 * log(2.0)) for k in range(nz): for j in range(ny): for i in range(nx): l1 = (i + 0) + ((j + 0) + (k + 0) * ny) * nx l2 = (i + 1) + ((j + 0) + (k + 0) * ny) * nx l3 = (i - 1) + ((j + 0) + (k + 0) * ny) * nx l4 = (i + 0) + ((j + 1) + (k + 0) * ny) * nx l5 = (i + 0) + ((j - 1) + (k + 0) * ny) * nx l6 = (i + 0) + ((j + 0) + (k + 1) * ny) * nx l7 = (i + 0) + ((j + 0) + (k - 1) * ny) * nx coord1 = matrix.col(sampler.coord(l1)) if i < nx-1: coord = matrix.col(sampler.coord(l2)) weight = sampler.weight(l1, coord) assert(abs(weight - expected) < eps) weight = sampler.weight(l1, ( coord + coord1 )/2.0) assert(abs(weight - 0.5) < eps) if i > 0: coord = matrix.col(sampler.coord(l3)) weight = sampler.weight(l1, coord) assert(abs(weight - expected) < eps) weight = sampler.weight(l1, ( coord1 + coord )/2.0) assert(abs(weight - 0.5) < eps) if j < ny-1: coord = matrix.col(sampler.coord(l4)) weight = sampler.weight(l1, coord) assert(abs(weight - expected) < eps) weight = sampler.weight(l1, ( coord + coord1 )/2.0) assert(abs(weight - 0.5) < eps) if j > 0: coord = matrix.col(sampler.coord(l5)) weight = sampler.weight(l1, coord) assert(abs(weight - expected) < eps) weight = sampler.weight(l1, ( coord1 + coord )/2.0) assert(abs(weight - 0.5) < eps) if k < nz-1: coord = matrix.col(sampler.coord(l6)) weight = sampler.weight(l1, coord) assert(abs(weight - expected) < eps) weight = sampler.weight(l1, ( coord + coord1 )/2.0) assert(abs(weight - 0.5) < eps) if k > 0: coord = matrix.col(sampler.coord(l7)) weight = sampler.weight(l1, coord) assert(abs(weight - expected) < eps) weight = sampler.weight(l1, ( coord1 + coord )/2.0) assert(abs(weight - 0.5) < eps) print 'OK'
def test_weights(): from scitbx import matrix from dials.algorithms.profile_model.modeller import GridSampler width = 1000 height = 1000 scan_range = (2, 12) nx = 10 ny = 10 nz = 2 sampler = GridSampler((width, height), scan_range, (nx, ny, 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 # Ensure we get the expected weight at the next grid point at half way # between grid points expected = math.exp(-4.0 * math.log(2.0)) for k in range(nz): for j in range(ny): for i in range(nx): l1 = (i + 0) + ((j + 0) + (k + 0) * ny) * nx l2 = (i + 1) + ((j + 0) + (k + 0) * ny) * nx l3 = (i - 1) + ((j + 0) + (k + 0) * ny) * nx l4 = (i + 0) + ((j + 1) + (k + 0) * ny) * nx l5 = (i + 0) + ((j - 1) + (k + 0) * ny) * nx l6 = (i + 0) + ((j + 0) + (k + 1) * ny) * nx l7 = (i + 0) + ((j + 0) + (k - 1) * ny) * nx coord1 = matrix.col(sampler.coord(l1)) if i < nx - 1: coord = matrix.col(sampler.coord(l2)) weight = sampler.weight(l1, 0, coord) assert abs(weight - expected) < eps weight = sampler.weight(l1, 0, (coord + coord1) / 2.0) assert abs(weight - 0.5) < eps if i > 0: coord = matrix.col(sampler.coord(l3)) weight = sampler.weight(l1, 0, coord) assert abs(weight - expected) < eps weight = sampler.weight(l1, 0, (coord1 + coord) / 2.0) assert abs(weight - 0.5) < eps if j < ny - 1: coord = matrix.col(sampler.coord(l4)) weight = sampler.weight(l1, 0, coord) assert abs(weight - expected) < eps weight = sampler.weight(l1, 0, (coord + coord1) / 2.0) assert abs(weight - 0.5) < eps if j > 0: coord = matrix.col(sampler.coord(l5)) weight = sampler.weight(l1, 0, coord) assert abs(weight - expected) < eps weight = sampler.weight(l1, 0, (coord1 + coord) / 2.0) assert abs(weight - 0.5) < eps if k < nz - 1: coord = matrix.col(sampler.coord(l6)) weight = sampler.weight(l1, 0, coord) assert abs(weight - expected) < eps weight = sampler.weight(l1, 0, (coord + coord1) / 2.0) assert abs(weight - 0.5) < eps if k > 0: coord = matrix.col(sampler.coord(l7)) weight = sampler.weight(l1, 0, coord) assert abs(weight - expected) < eps weight = sampler.weight(l1, 0, (coord1 + coord) / 2.0) assert abs(weight - 0.5) < eps