def __call__(self): """Run the script.""" from dials.model.serialize import load, dump import cPickle as pickle from scitbx import matrix # Load the reflection list print("Loading reflections from {0}".format(self.reflections_filename)) rlist = pickle.load(open(self.reflections_filename, "r")) # Try to load the models print("Loading models from {0}".format(self.sweep_filename)) sweep = load.sweep(open(self.sweep_filename, "r")) beam = sweep.get_beam() wavelength = beam.get_wavelength() detector = sweep.get_detector() # Loop through all the reflections for r in rlist: panel = detector[r.panel_number] lab_coord = panel.get_lab_coord(r.image_coord_mm) r.beam_vector = matrix.col(lab_coord).normalize() / wavelength # Write out reflections if self.output_filename is not None: print("Saving reflections to {0}".format(self.output_filename)) pickle.dump(rlist, open(self.output_filename, "wb"), pickle.HIGHEST_PROTOCOL)
class Test(object): def __init__(self): from dials.algorithms.profile_model.gaussian_rs import transform from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem import os import libtbx.load_env from math import floor from scitbx import matrix from dials.model.serialize import load from random import uniform try: dials_regression = libtbx.env.dist_path('dials_regression') except KeyError, e: print 'FAIL: dials_regression not configured' exit(0) # Set the sweep filename and load the sweep filename = os.path.join(dials_regression, 'centroid_test_data', 'sweep.json') self.sweep = load.sweep(filename) # Get the models self.beam = self.sweep.get_beam() self.detector = self.sweep.get_detector() self.gonio = self.sweep.get_goniometer() assert (len(self.detector) == 1) # Get some stuff self.s0 = self.beam.get_s0() self.m2 = self.gonio.get_rotation_axis() self.image_size = self.detector[0].get_image_size() # Get a random s1/phi i = uniform(0, self.image_size[0]) j = uniform(1, self.image_size[1]) self.s1 = matrix.col(self.detector[0].get_pixel_lab_coord((i, j))) self.s1 = self.s1.normalize() * matrix.col(self.s0).length() self.phi = uniform(0, 5) self.x0 = int(floor(i - 10)) self.y0 = int(floor(j - 10)) # Set some parameters self.sigma_divergence = self.beam.get_sigma_divergence(deg=False) self.delta_divergence = 3 * self.sigma_divergence self.grid_half_size = 4 self.step_size = (self.delta_divergence / self.grid_half_size, self.delta_divergence / self.grid_half_size) # Create the coordinate system self.cs = CoordinateSystem(self.m2, self.s0, self.s1, self.phi) # Create the map of s1 coordinates self.s1_map = transform.beam_vector_map(self.detector[0], self.beam, True) # Create the grid index generator self.generate_indices = transform.GridIndexGenerator( self.cs, self.x0, self.y0, self.step_size, self.grid_half_size, self.s1_map)
def __call__(self): """Run the script.""" from dials.model.serialize import load, dump import cPickle as pickle from scitbx import matrix # Load the reflection list print 'Loading reflections from {0}'.format(self.reflections_filename) rlist = pickle.load(open(self.reflections_filename, 'r')) # Try to load the models print 'Loading models from {0}'.format(self.sweep_filename) sweep = load.sweep(open(self.sweep_filename, 'r')) beam = sweep.get_beam() wavelength = beam.get_wavelength() detector = sweep.get_detector() # Loop through all the reflections for r in rlist: panel = detector[r.panel_number] lab_coord = panel.get_lab_coord(r.image_coord_mm) r.beam_vector = matrix.col(lab_coord).normalize() / wavelength # Write out reflections if self.output_filename is not None: print 'Saving reflections to {0}'.format(self.output_filename) pickle.dump(rlist, open(self.output_filename, 'wb'), pickle.HIGHEST_PROTOCOL)
def main(self, params, options, args): """Execute the script.""" from dials.algorithms.refinement import RefinerFactory from dials.model.serialize import load import cPickle as pickle from time import time import sys # Check the number of arguments is correct if len(args) != 3: self.config().print_help() return # Hack the phil parameters to control the number of reflections params.refinement.reflections.use_all_reflections = False params.refinement.reflections.random_seed = None # and also to make the target achieved criterion more stringent params.refinement.target.bin_size_fraction = 0.1 # print header for output table print "Nref RMSDx RMSDy RMSDphi Steps Target_acheived Time" #for n in range(1,170,2): for n in [e * 0.1 for e in range(1,100)]: # Set nref to use params.refinement.reflections.reflections_per_degree = n # Get the refiner from the input parameters refine = RefinerFactory.from_parameters(params, options.verbosity) # Try to load the models sweep = load.sweep(args[0]) crystal = load.crystal(args[1]) reflections = pickle.load(open(args[2], 'rb')) # Refine the geometry start_time = time() try: refined = refine(sweep, crystal, reflections) except Exception as e: print "ERROR occurred" continue time_taken = time() - start_time print refined.history.num_reflections[-1], print "%.6f %.6f %.8f" % refine.rmsds(), print "%d" % refined.get_num_steps(), print refined.test_for_termination(), print "%.3f" % time_taken # flush the buffer so we can see some output sys.stdout.flush() return
def main(self, params, options, args): """Execute the script.""" from dials.algorithms.refinement import RefinerFactory from dials.model.serialize import load import cPickle as pickle from time import time import sys # Check the number of arguments is correct if len(args) != 3: self.config().print_help() return # Hack the phil parameters to control the number of reflections params.refinement.reflections.use_all_reflections = False params.refinement.reflections.random_seed = None # and also to make the target achieved criterion more stringent params.refinement.target.bin_size_fraction = 0.1 # print header for output table print("Nref RMSDx RMSDy RMSDphi Steps Target_acheived Time") # for n in range(1,170,2): for n in [e * 0.1 for e in range(1, 100)]: # Set nref to use params.refinement.reflections.reflections_per_degree = n # Get the refiner from the input parameters refine = RefinerFactory.from_parameters(params, options.verbosity) # Try to load the models sweep = load.sweep(args[0]) crystal = load.crystal(args[1]) reflections = pickle.load(open(args[2], "rb")) # Refine the geometry start_time = time() try: refined = refine(sweep, crystal, reflections) except Exception as e: print("ERROR occurred") continue time_taken = time() - start_time print(refined.history.num_reflections[-1], end=" ") print("%.6f %.6f %.8f" % refine.rmsds(), end=" ") print("%d" % refined.get_num_steps(), end=" ") print(refined.test_for_termination(), end=" ") print("%.3f" % time_taken) # flush the buffer so we can see some output sys.stdout.flush() return
class Test(object): def __init__(self): import os import libtbx.load_env from dxtbx.serialize.load import crystal as load_crystal from dials.model.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.experiment_list import Experiment, ExperimentList try: dials_regression = libtbx.env.dist_path('dials_regression') except KeyError, e: 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.sweep(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) self.delta_m = 3 * self.crystal.get_mosaicity(deg=False) self.nsigma = 3 self.profile_model = Model(None, self.nsigma, self.beam.get_sigma_divergence(deg=False), self.crystal.get_mosaicity(deg=False)) 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 __init__(self, filename): from math import pi from dials.model.serialize import load from dials.algorithms.profile_model.gaussian_rs.transform import MapFramesReverse from dials.algorithms.profile_model.gaussian_rs.transform import MapFramesForward from dials.algorithms.profile_model.gaussian_rs import BBoxCalculator3D # Load the sweep self.sweep = load.sweep(filename) # Get the models self.beam = self.sweep.get_beam() self.detector = self.sweep.get_detector() self.gonio = self.sweep.get_goniometer() self.scan = self.sweep.get_scan() # Set the delta_divergence/mosaicity self.n_sigma = 3 self.sigma_divergence = 0.060 * pi / 180 self.mosaicity = 0.154 * pi / 180 self.delta_divergence = self.n_sigma * self.sigma_divergence self.delta_mosaicity = self.n_sigma * self.mosaicity # Set the grid size self.grid_size = (4, 4, 4) # Create the E3 fraction object self.transform_forward = MapFramesForward( self.scan.get_array_range()[0], self.scan.get_oscillation(deg=False)[0], self.scan.get_oscillation(deg=False)[1], self.mosaicity, self.n_sigma, self.grid_size[2]) # Create the E3 fraction object self.transform_reverse = MapFramesReverse( self.scan.get_array_range()[0], self.scan.get_oscillation(deg=False)[0], self.scan.get_oscillation(deg=False)[1], self.mosaicity, self.n_sigma, self.grid_size[2]) # Create the bounding box calculator self.calculate_bbox = BBoxCalculator3D( self.beam, self.detector, self.gonio, self.scan, self.delta_divergence, self.delta_mosaicity)
def __call__(self): """Run the script.""" from dials.model.serialize import load, dump from dials.model.data import ReflectionList import cPickle as pickle from dials.algorithms.spot_prediction import ray_intersection # Load the reflection list print("Loading reflections from {0}".format(self.reflections_filename)) rlist = pickle.load(open(self.reflections_filename, "r")) # Try to load the models print("Loading models from {0}".format(self.sweep_filename)) sweep = load.sweep(open(self.sweep_filename, "r")) beam = sweep.get_beam() wavelength = beam.get_wavelength() detector = sweep.get_detector() # get the intersections observations = ray_intersection(detector, rlist) if len(observations) != len(rlist): print("WARNING: not all reflections intersect the detector") # Why is this? Dump out the unique reflections to explore unique = ReflectionList() for r in rlist: try: obs = ray_intersection(detector, r) except RuntimeError: unique.append(r) unique_filename = "unique.pickle" print("Those reflections that do not intersect have been saved" " to {0}".format(unique_filename)) pickle.dump(observations, open(unique_filename, "wb"), pickle.HIGHEST_PROTOCOL) # update the centroid positions too for r in observations: r.centroid_position = r.image_coord_mm + (r.rotation_angle, ) # Write out reflections if self.output_filename is not None: print("Saving reflections to {0}".format(self.output_filename)) pickle.dump(observations, open(self.output_filename, "wb"), pickle.HIGHEST_PROTOCOL)
def __call__(self): """Run the script.""" from dials.model.serialize import load, dump from dials.model.data import ReflectionList import cPickle as pickle from dials.algorithms.spot_prediction import ray_intersection # Load the reflection list print 'Loading reflections from {0}'.format(self.reflections_filename) rlist = pickle.load(open(self.reflections_filename, 'r')) # Try to load the models print 'Loading models from {0}'.format(self.sweep_filename) sweep = load.sweep(open(self.sweep_filename, 'r')) beam = sweep.get_beam() wavelength = beam.get_wavelength() detector = sweep.get_detector() # get the intersections observations = ray_intersection(detector, rlist) if len(observations) != len(rlist): print "WARNING: not all reflections intersect the detector" # Why is this? Dump out the unique reflections to explore unique = ReflectionList() for r in rlist: try: obs = ray_intersection(detector, r) except RuntimeError: unique.append(r) unique_filename = "unique.pickle" print 'Those reflections that do not intersect have been saved' \ ' to {0}'.format(unique_filename) pickle.dump(observations, open(unique_filename, 'wb'), pickle.HIGHEST_PROTOCOL) # update the centroid positions too for r in observations: r.centroid_position = r.image_coord_mm + (r.rotation_angle, ) # Write out reflections if self.output_filename is not None: print 'Saving reflections to {0}'.format(self.output_filename) pickle.dump(observations, open(self.output_filename, 'wb'), pickle.HIGHEST_PROTOCOL)
def __init__(self, filename): from dials.model.serialize import load from dials.algorithms.profile_model.gaussian_rs import transform from dials.algorithms.profile_model.gaussian_rs import BBoxCalculator3D from math import pi # Load the sweep self.sweep = load.sweep(filename) # Get the models self.beam = self.sweep.get_beam() self.detector = self.sweep.get_detector() self.gonio = self.sweep.get_goniometer() self.scan = self.sweep.get_scan() self.scan.set_image_range((0, 1000)) # self.beam.set_direction((0.0, 0.0, 1.0)) # self.gonio.set_rotation_axis((1.0, 0.0, 0.0)) # self.detector.set_frame((1.0, 0.0, 0.0), # (0.0, 1.0, 0.0), # (-150, -150, -200)) # Set some parameters self.sigma_divergence = self.beam.get_sigma_divergence(deg=False) self.mosaicity = 0.157 * pi / 180 self.n_sigma = 3 self.grid_size = 20 self.delta_divergence = self.n_sigma * self.sigma_divergence step_size = self.delta_divergence / self.grid_size self.delta_divergence2 = self.delta_divergence + step_size * 0.5 self.delta_mosaicity = self.n_sigma * self.mosaicity # Create the bounding box calculator self.calculate_bbox = BBoxCalculator3D( self.beam, self.detector, self.gonio, self.scan, self.delta_divergence2, self.delta_mosaicity ) # Initialise the transform self.spec = transform.TransformSpec( self.beam, self.detector, self.gonio, self.scan, self.sigma_divergence, self.mosaicity, self.n_sigma + 1, self.grid_size, )
def run(self): from dials.model.serialize import load from dials.algorithms import shoebox from dxtbx.serialize.load import crystal as load_crystal from dials.array_family import flex # Load the sweep and crystal self.sweep = load.sweep(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 run(self): from dials.model.serialize import load from dials.algorithms import shoebox from dxtbx.serialize.load import crystal as load_crystal from dials.array_family import flex # Load the sweep and crystal self.sweep = load.sweep(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 __init__(self, filename): from dials.model.serialize import load from dials.algorithms.profile_model.gaussian_rs import transform from dials.algorithms.profile_model.gaussian_rs import BBoxCalculator3D from math import pi # Load the sweep self.sweep = load.sweep(filename) # Get the models self.beam = self.sweep.get_beam() self.detector = self.sweep.get_detector() self.gonio = self.sweep.get_goniometer() self.scan = self.sweep.get_scan() self.scan.set_image_range((0, 1000)) # self.beam.set_direction((0.0, 0.0, 1.0)) # self.gonio.set_rotation_axis((1.0, 0.0, 0.0)) # self.detector.set_frame((1.0, 0.0, 0.0), # (0.0, 1.0, 0.0), # (-150, -150, -200)) # Set some parameters self.sigma_divergence = self.beam.get_sigma_divergence(deg=False) self.mosaicity = 0.157 * pi / 180 self.n_sigma = 3 self.grid_size = 20 self.delta_divergence = self.n_sigma * self.sigma_divergence step_size = self.delta_divergence / self.grid_size self.delta_divergence2 = self.delta_divergence + step_size * 0.5 self.delta_mosaicity = self.n_sigma * self.mosaicity # Create the bounding box calculator self.calculate_bbox = BBoxCalculator3D(self.beam, self.detector, self.gonio, self.scan, self.delta_divergence2, self.delta_mosaicity) # Initialise the transform self.spec = transform.TransformSpec(self.beam, self.detector, self.gonio, self.scan, self.sigma_divergence, self.mosaicity, self.n_sigma + 1, self.grid_size)
def sweep_and_model(dials_data): class Test(object): pass storage_class = Test() from dials.model.serialize import load storage_class.sweep = load.sweep( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models storage_class.beam = storage_class.sweep.get_beam() storage_class.detector = storage_class.sweep.get_detector() storage_class.gonio = storage_class.sweep.get_goniometer() storage_class.scan = storage_class.sweep.get_scan() return storage_class
def sweep_and_model(dials_regression): from dials.model.serialize import load import os filename = os.path.join(dials_regression, 'centroid_test_data', 'sweep.json') class Test(object): pass storage_class = Test() # Load the sweep storage_class.sweep = load.sweep(filename) # Get the models storage_class.beam = storage_class.sweep.get_beam() storage_class.detector = storage_class.sweep.get_detector() storage_class.gonio = storage_class.sweep.get_goniometer() storage_class.scan = storage_class.sweep.get_scan() return storage_class
class Test(object): def __init__(self): from dials.algorithms.profile_model.gaussian_rs import BBoxCalculator3D from dials.model.serialize import load from math import pi import libtbx.load_env try: dials_regression = libtbx.env.dist_path('dials_regression') except KeyError, e: print 'FAIL: dials_regression not configured' exit(0) import os filename = os.path.join(dials_regression, 'centroid_test_data', 'sweep.json') sweep = load.sweep(filename) # Get the models self.beam = sweep.get_beam() self.detector = sweep.get_detector() self.gonio = sweep.get_goniometer() self.scan = sweep.get_scan() # Set the delta_divergence/mosaicity self.n_sigma = 5 self.sigma_divergence = 0.060 * pi / 180 self.mosaicity = 0.154 * pi / 180 self.delta_divergence = self.n_sigma * self.sigma_divergence self.delta_mosaicity = self.n_sigma * self.mosaicity # Create the bounding box calculator self.calculate_bbox = BBoxCalculator3D(self.beam, self.detector, self.gonio, self.scan, self.delta_divergence, self.delta_mosaicity)
def __init__(self): from dials.model.serialize import load import libtbx.load_env try: dials_regression = libtbx.env.dist_path('dials_regression') except KeyError: print 'FAIL: dials_regression not configured' exit(0) import os filename = os.path.join(dials_regression, 'centroid_test_data', 'sweep.json') # Load the sweep self.sweep = load.sweep(filename) # Get the models self.beam = self.sweep.get_beam() self.detector = self.sweep.get_detector() self.gonio = self.sweep.get_goniometer() self.scan = self.sweep.get_scan()
def test_map_forward_reverse(dials_data): from dials.model.serialize import load from dials.algorithms.profile_model.gaussian_rs.transform import MapFramesReverse from dials.algorithms.profile_model.gaussian_rs.transform import MapFramesForward from dials.algorithms.profile_model.gaussian_rs import BBoxCalculator3D sweep = load.sweep(dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sweep.get_beam() detector = sweep.get_detector() gonio = sweep.get_goniometer() scan = sweep.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 ) from scitbx import matrix from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem 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) x1, x2 = bbox[0], bbox[1] y1, y2 = bbox[2], bbox[3] z1, z2 = bbox[4], bbox[5] # 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 print_function from dials.model.serialize import load from dials.algorithms.reflection_basis import CoordinateSystem from dials.algorithms.reflection_basis import CoordinateSystem from dials.algorithms.reflection_basis import FromRotationAngleAccurate from dials.algorithms.reflection_basis import ToRotationAngleAccurate from dials.algorithms.reflection_basis import FromRotationAngleFast from scitbx.array_family import flex from scitbx import matrix from math import pi, sqrt, atan sweep = load.sweep( "/home/upc86896/Projects/cctbx/sources/dials_regression/centroid_test_data/sweep.json" ) beam = sweep.get_beam() gonio = sweep.get_goniometer() detector = sweep.get_detector() scan = sweep.get_scan() m2 = matrix.col(gonio.get_rotation_axis()) s0 = matrix.col(beam.get_s0()) xsize, ysize = detector.get_image_size() div = 4 mask1 = flex.bool(flex.grid(int(ysize / div), int(xsize / div))) mask2 = flex.bool(flex.grid(int(ysize / div), int(xsize / div))) n = 5 dm = n * 0.157 * pi / 180.0 # dm = n * 1.0 * pi / 180.0
def test_map_frames_forward(dials_data): from dials.model.serialize import load from dials.algorithms.profile_model.gaussian_rs.transform import MapFramesForward from dials.algorithms.profile_model.gaussian_rs import BBoxCalculator3D sweep = load.sweep(dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sweep.get_beam() detector = sweep.get_detector() gonio = sweep.get_goniometer() scan = sweep.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 ) from scitbx import matrix from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem from scitbx.array_family import flex 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) x1, x2 = bbox[0], bbox[1] y1, y2 = bbox[2], bbox[3] z1, z2 = bbox[4], bbox[5] # 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, "%.16f not between 0 and 1" % fmax 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 == False: if curr < last: rev = True else: assert curr <= last last = curr
def test_forward_no_model(dials_data): from dials.model.serialize import load from dials.algorithms.profile_model.gaussian_rs import transform from dials.algorithms.profile_model.gaussian_rs import BBoxCalculator3D sweep = load.sweep( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sweep.get_beam() detector = sweep.get_detector() gonio = sweep.get_goniometer() scan = sweep.get_scan() scan.set_image_range((0, 1000)) # beam.set_direction((0.0, 0.0, 1.0)) # gonio.set_rotation_axis((1.0, 0.0, 0.0)) # detector.set_frame((1.0, 0.0, 0.0), # (0.0, 1.0, 0.0), # (-150, -150, -200)) # 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): from scitbx import matrix from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem from dials.algorithms.profile_model.gaussian_rs import transform from scitbx.array_family import flex 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
# In [16]: refl[index[10000]].miller_index # Out[16]: (17, 23, 23) # In [17]: refl[index[10000]].image_coord_px # Out[17]: (788.1612634483189, 1969.9381446581392) # In [18]: refl[index[10000]].frame_number # Out[18]: 352.1425769902628 # In [19]: 2.688E+03 # Out[19]: 2688.0 from dials.model.serialize import load sweep = load.sweep("/home/upc86896/Data/TRP_M1S3_2_/sweep.json") crystal = load.crystal("/home/upc86896/Data/TRP_M1S3_2_/crystal.json") reference = load.reference("/home/upc86896/Data/TRP_M1S3_2_/reference.pickle") # from dials.algorithms.integration import ReflectionExtractor # extract = ReflectionExtractor(3) # refl = extract(sweep, crystal) # index = 104786 # print refl[index].miller_index import pickle # pickle.dump(refl[index], open('test_reflection', 'w')) refl = pickle.load(open("test_reflection", "r")) from dials.model.data import ReflectionList
def test_run(dials_data): from dials.algorithms.profile_model.gaussian_rs import transform from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem from scitbx import matrix from dials.model.serialize import load sweep = load.sweep(dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sweep.get_beam() detector = sweep.get_detector() gonio = sweep.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 compare_chunks(integrate_hkl, integrate_pkl, crystal_json, sweep_json, d_min=0.0): from cctbx.array_family import flex from annlib_ext import AnnAdaptor as ann_adaptor from dials.model.serialize import load sweep = load.sweep(sweep_json) rdx = derive_reindex_matrix(crystal_json, sweep_json, integrate_hkl) print "Reindex matrix:\n%d %d %d\n%d %d %d\n%d %d %d" % (rdx.elems) uc = integrate_hkl_to_unit_cell(integrate_hkl) xhkl, xi, xsigi, xxyz, xlp = pull_reference(integrate_hkl, d_min=d_min) dhkl, di, dsigi, dxyz, dlp = pull_calculated(integrate_pkl) reference = flex.double() query = flex.double() for xyz in xxyz: reference.append(xyz[0]) reference.append(xyz[1]) reference.append(xyz[2]) for xyz in dxyz: query.append(xyz[0]) query.append(xyz[1]) query.append(xyz[2]) # perform the match ann = ann_adaptor(data=reference, dim=3, k=1) ann.query(query) XDS = [] DIALS = [] HKL = [] XYZ = [] SIGMA_XDS = [] SIGMA_DIALS = [] XLP = [] DLP = [] # perform the analysis for j, hkl in enumerate(dhkl): c = ann.nn[j] if hkl == tuple(rdx * xhkl[c]): XDS.append(xi[c]) DIALS.append(di[j]) HKL.append(hkl) XYZ.append(dxyz[j]) SIGMA_XDS.append(xsigi[c]) SIGMA_DIALS.append(dsigi[j]) XLP.append(xlp[c]) DLP.append(dlp[j]) print "Found %d matches" % len(XDS) compare = CompareIntensity(sweep, uc, HKL, XYZ, XDS, DIALS, SIGMA_XDS, SIGMA_DIALS, XLP, DLP) # compare.plot_scale_factor_vs_resolution() # compare.plot_scale_factor_vs_frame_number() compare.plot_chunked_statistics_vs_resolution() compare.plot_chunked_statistics_vs_frame_number() compare.plot_chunked_statistics_vs_i_over_sigma() compare.plot_chunked_i_over_sigma_vs_frame_number() compare.plot_chunked_resolution_vs_frame_number() compare.plot_chunked_lp_vs_frame_number() compare.plot_scale_vs_x_y() compare.plot_scale_vs_i_over_sigma()
# Parse the arguments options, args = parser.parse_args() # Print help if not enough arguments specified if len(args) < 1: parser.print_help() sys.exit() from dials.model.serialize import load, dump # Try to load the models print("Loading detector from {0}".format(args[0])) with open(args[0], "r") as f: sweep = load.sweep(f) detector = sweep.get_detector() # calculate offsets from scitbx import matrix fast = matrix.col(detector[0].get_fast_axis()).normalize() slow = matrix.col(detector[0].get_slow_axis()).normalize() panel60_origin = matrix.col(detector[59].get_origin()) fast_offsets = [(matrix.col(p.get_origin()) - panel60_origin).dot(fast) for p in detector] slow_offsets = [(matrix.col(p.get_origin()) - panel60_origin).dot(slow) for p in detector] ref_origin = matrix.col(
from dials.algorithms.integration import ReflectionExtractor from dials.model.serialize import load import os path = "/home/upc86896/Projects/cctbx/sources/dials_regression/centroid_test_data" sweep = load.sweep(os.path.join(path, "sweep.json")) crystal = load.crystal(os.path.join(path, "crystal.json")) extract = ReflectionExtractor(3) refl = extract(sweep, crystal) import copy refl2 = copy.deepcopy(refl) from dials.algorithms.reflection_basis import transform from dials.util.command_line import Command Command.start("Init transform") trans = transform.Forward(sweep, crystal, 3, 4) Command.end("Init transform") Command.start("Transform") for i in range(1): trans(refl) Command.end("Transform") Command.start("Init Transform")
def get_dials_coordinate_frame(sweep_json): from dials.model.serialize import load sweep = load.sweep(sweep_json) return sweep.get_beam().get_direction(), sweep.get_goniometer().get_rotation_axis()
help='Destination filename for sweep with aligned detector') # Parse the arguments options, args = parser.parse_args() # Print help if not enough arguments specified if len(args) < 1: parser.print_help() sys.exit() from dials.model.serialize import load, dump # Try to load the models print 'Loading detector from {0}'.format(args[0]) with open(args[0], 'r') as f: sweep = load.sweep(f) detector = sweep.get_detector() # calculate offsets from scitbx import matrix fast = matrix.col(detector[0].get_fast_axis()).normalize() slow = matrix.col(detector[0].get_slow_axis()).normalize() panel60_origin = matrix.col(detector[59].get_origin()) fast_offsets = [(matrix.col(p.get_origin()) - panel60_origin).dot(fast) \ for p in detector] slow_offsets = [(matrix.col(p.get_origin()) - panel60_origin).dot(slow) \ for p in detector] ref_origin = matrix.col((-210.68270819092896,
def get_dials_coordinate_frame(sweep_json): from dials.model.serialize import load sweep = load.sweep(sweep_json) return sweep.get_beam().get_direction(), \ sweep.get_goniometer().get_rotation_axis()
from dials.algorithms.integration import ReflectionExtractor from dials.model.serialize import load import os path = '/home/upc86896/Projects/cctbx/sources/dials_regression/centroid_test_data' sweep = load.sweep(os.path.join(path, 'sweep.json')) crystal = load.crystal(os.path.join(path, 'crystal.json')) extract = ReflectionExtractor(3) refl = extract(sweep, crystal) import copy refl2 = copy.deepcopy(refl) from dials.algorithms.reflection_basis import transform from dials.util.command_line import Command Command.start('Init transform') trans = transform.Forward(sweep, crystal, 3, 4) Command.end('Init transform') Command.start('Transform') for i in range(1): trans(refl) Command.end('Transform')
from dials.model.serialize import load import pickle sweep = load.sweep('/home/upc86896/Projects/cctbx/sources/dials_regression/centroid_test_data/sweep.json') text = pickle.dumps(sweep) sweep2 = pickle.loads(text) sweep2.get_detector()
#Out[15]: 1910.0948427154015 #In [16]: refl[index[10000]].miller_index #Out[16]: (17, 23, 23) #In [17]: refl[index[10000]].image_coord_px #Out[17]: (788.1612634483189, 1969.9381446581392) #In [18]: refl[index[10000]].frame_number #Out[18]: 352.1425769902628 #In [19]: 2.688E+03 #Out[19]: 2688.0 from dials.model.serialize import load sweep = load.sweep('/home/upc86896/Data/TRP_M1S3_2_/sweep.json') crystal = load.crystal('/home/upc86896/Data/TRP_M1S3_2_/crystal.json') reference= load.reference('/home/upc86896/Data/TRP_M1S3_2_/reference.pickle') #from dials.algorithms.integration import ReflectionExtractor #extract = ReflectionExtractor(3) #refl = extract(sweep, crystal) #index = 104786 #print refl[index].miller_index import pickle #pickle.dump(refl[index], open('test_reflection', 'w')) refl = pickle.load(open('test_reflection', 'r')) from dials.model.data import ReflectionList ref_list = ReflectionList(1)
def compare_chunks(integrate_hkl, integrate_pkl, crystal_json, sweep_json, d_min=0.0): from cctbx.array_family import flex from annlib_ext import AnnAdaptor as ann_adaptor from dials.model.serialize import load sweep = load.sweep(sweep_json) rdx = derive_reindex_matrix(crystal_json, sweep_json, integrate_hkl) print 'Reindex matrix:\n%d %d %d\n%d %d %d\n%d %d %d' % (rdx.elems) uc = integrate_hkl_to_unit_cell(integrate_hkl) xhkl, xi, xsigi, xxyz, xlp = pull_reference(integrate_hkl, d_min=d_min) dhkl, di, dsigi, dxyz, dlp = pull_calculated(integrate_pkl) reference = flex.double() query = flex.double() for xyz in xxyz: reference.append(xyz[0]) reference.append(xyz[1]) reference.append(xyz[2]) for xyz in dxyz: query.append(xyz[0]) query.append(xyz[1]) query.append(xyz[2]) # perform the match ann = ann_adaptor(data=reference, dim=3, k=1) ann.query(query) XDS = [] DIALS = [] HKL = [] XYZ = [] SIGMA_XDS = [] SIGMA_DIALS = [] XLP = [] DLP = [] # perform the analysis for j, hkl in enumerate(dhkl): c = ann.nn[j] if hkl == tuple(rdx * xhkl[c]): XDS.append(xi[c]) DIALS.append(di[j]) HKL.append(hkl) XYZ.append(dxyz[j]) SIGMA_XDS.append(xsigi[c]) SIGMA_DIALS.append(dsigi[j]) XLP.append(xlp[c]) DLP.append(dlp[j]) print "Found %d matches" % len(XDS) compare = CompareIntensity(sweep, uc, HKL, XYZ, XDS, DIALS, SIGMA_XDS, SIGMA_DIALS, XLP, DLP) # compare.plot_scale_factor_vs_resolution() # compare.plot_scale_factor_vs_frame_number() compare.plot_chunked_statistics_vs_resolution() compare.plot_chunked_statistics_vs_frame_number() compare.plot_chunked_statistics_vs_i_over_sigma() compare.plot_chunked_i_over_sigma_vs_frame_number() compare.plot_chunked_resolution_vs_frame_number() compare.plot_chunked_lp_vs_frame_number() compare.plot_scale_vs_x_y() compare.plot_scale_vs_i_over_sigma()
def test_forward(dials_data): from dials.model.serialize import load from dials.algorithms.profile_model.gaussian_rs import transform from dials.algorithms.profile_model.gaussian_rs import BBoxCalculator3D sweep = load.sweep( dials_data("centroid_test_data").join("sweep.json").strpath) # Get the models beam = sweep.get_beam() detector = sweep.get_detector() gonio = sweep.get_goniometer() scan = sweep.get_scan() # beam.set_direction((0.0, 0.0, 1.0)) # gonio.set_rotation_axis((1.0, 0.0, 0.0)) # detector.set_frame((1.0, 0.0, 0.0), # (0.0, 1.0, 0.0), # (-150, -150, -200)) # 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): from scitbx import matrix from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem from dials.algorithms.profile_model.gaussian_rs import transform from scitbx.array_family import flex 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 # def tst_transformed_centroid(self): # from scitbx import matrix # from random import uniform # from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem # from dials.algorithms.profile_model.gaussian_rs import transform # from scitbx.array_family import flex # from time import time # 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, beam, True) # # Get random x, y, z # x = random.uniform(300, 1800) # y = random.uniform(300, 1800) # z = random.uniform(-10, 0) # # Get random s1, phi, panel # s1 = matrix.col(detector.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 = gaussian((z1 - z0, y1 - y0, x1 - x0), 10.0, # (z - z0, y - y0, x - x0), (2.0, 2.0, 2.0)) # print x, y, z, bbox # print (z1 - z0, y1 - y0, x1 - x0), (z - z0, y - y0, x - x0) # 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 # #image[k,j,i] *= inside # from matplotlib import pylab # pylab.imshow(image.as_numpy_array()[(z1 - z0) / 2,:,:], interpolation='none') # pylab.show() # # Transform the image to the grid # grid = transform(cs, bbox, image, mask) # from matplotlib import pylab # pylab.imshow(grid.as_numpy_array()[7,:,:], interpolation='none') # pylab.show() # # 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) # # Check the centroid # sz = grid_size * 2 + 1 # grid_x = flex.double(flex.grid(sz, sz, sz)) # grid_y = flex.double(flex.grid(sz, sz, sz)) # grid_z = flex.double(flex.grid(sz, sz, sz)) # for k in range(sz): # for j in range(sz): # for i in range(sz): # grid_x[k,j,i] = i + 0.5 # grid_y[k,j,i] = j + 0.5 # grid_z[k,j,i] = k + 0.5 # # sum_grid_x = flex.sum(grid * grid_x) # sum_grid_y = flex.sum(grid * grid_y) # sum_grid_z = flex.sum(grid * grid_z) # xc = sum_grid_x / sum_grid # yc = sum_grid_y / sum_grid # zc = sum_grid_z / sum_grid # print xc, yc, zc # assert(abs(xc - grid_size + 0.5) <= 0.5) # assert(abs(yc - grid_size + 0.5) <= 0.5) # assert(abs(zc - grid_size + 0.5) <= 0.5) # # Test passed # print 'OK' # tst_transform_with_background(self): from scitbx import matrix from dials.algorithms.profile_model.gaussian_rs import CoordinateSystem from dials.algorithms.profile_model.gaussian_rs import transform from scitbx.array_family import flex 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