def get_miller_index_i_seqs(i_img, parallel=True): mt = flex.mersenne_twister(seed=work_params.noise.random_seed + i_img) crystal_rotation = mt.random_double_r3_rotation_matrix_arvo_1992() if (work_params.kirian_delta_vs_ewald_proximity): kirian_delta_vs_ewald_proximity( unit_cell=i_calc.p1_anom.unit_cell(), miller_indices=i_calc.p1_anom.indices(), crystal_rotation_matrix=crystal_rotation, ewald_radius=1 / work_params.wavelength, d_min=work_params.d_min, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels) img = image_simple( store_miller_index_i_seqs=True, store_signals=True).compute( unit_cell=i_calc.p1_anom.unit_cell(), miller_indices=i_calc.p1_anom.indices(), spot_intensity_factors=None, crystal_rotation_matrix=crystal_rotation, ewald_radius=1 / work_params.wavelength, ewald_proximity=work_params.ewald_proximity, signal_max=1, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels, point_spread=work_params.point_spread, gaussian_falloff_scale=work_params.gaussian_falloff_scale) result = img.miller_index_i_seqs if (work_params.usable_partiality_threshold is not None): result = result.select( img.signals > work_params.usable_partiality_threshold) if (parallel): return result.copy_to_byte_str() return result
def compute(work_params, use_wavelength_2=False, store_miller_index_i_seqs=False, store_spots=False, store_signals=False, set_pixels=False): i_calc = build_i_calc(work_params) from scitbx.math.euler_angles import xyz_matrix crystal_rotation_matrix = xyz_matrix(*work_params.euler_angles_xyz) work_params.crystal_rotation_matrix = crystal_rotation_matrix if (not use_wavelength_2): wavelength = work_params.wavelength else: wavelength = work_params.wavelength_2 from rstbx.simage import image_simple return i_calc, image_simple( store_miller_index_i_seqs=store_miller_index_i_seqs, store_spots=store_spots, store_signals=store_signals, set_pixels=set_pixels).compute( unit_cell=i_calc.p1_anom.unit_cell(), miller_indices=i_calc.p1_anom.indices(), spot_intensity_factors=i_calc.p1_anom.data(), crystal_rotation_matrix=crystal_rotation_matrix, ewald_radius=1 / wavelength, ewald_proximity=work_params.ewald_proximity, signal_max=work_params.signal_max, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels, point_spread=work_params.point_spread, gaussian_falloff_scale=work_params.gaussian_falloff_scale)
def get_miller_index_i_seqs(i_img, parallel=True): mt = flex.mersenne_twister(seed=work_params.noise.random_seed+i_img) crystal_rotation = mt.random_double_r3_rotation_matrix_arvo_1992() if (work_params.kirian_delta_vs_ewald_proximity): kirian_delta_vs_ewald_proximity( unit_cell=i_calc.p1_anom.unit_cell(), miller_indices=i_calc.p1_anom.indices(), crystal_rotation_matrix=crystal_rotation, ewald_radius=1/work_params.wavelength, d_min=work_params.d_min, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels) img = image_simple( store_miller_index_i_seqs=True, store_signals=True).compute( unit_cell=i_calc.p1_anom.unit_cell(), miller_indices=i_calc.p1_anom.indices(), spot_intensity_factors=None, crystal_rotation_matrix=crystal_rotation, ewald_radius=1/work_params.wavelength, ewald_proximity=work_params.ewald_proximity, signal_max=1, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels, point_spread=work_params.point_spread, gaussian_falloff_scale=work_params.gaussian_falloff_scale) result = img.miller_index_i_seqs if (work_params.usable_partiality_threshold is not None): result = result.select( img.signals > work_params.usable_partiality_threshold) if (parallel): return result.copy_to_byte_str() return result
def compute( work_params, use_wavelength_2=False, store_miller_index_i_seqs=False, store_spots=False, store_signals=False, set_pixels=False): i_calc = build_i_calc(work_params) from scitbx.math.euler_angles import xyz_matrix crystal_rotation_matrix = xyz_matrix(*work_params.euler_angles_xyz) work_params.crystal_rotation_matrix = crystal_rotation_matrix if (not use_wavelength_2): wavelength = work_params.wavelength else: wavelength = work_params.wavelength_2 from rstbx.simage import image_simple return i_calc, image_simple( store_miller_index_i_seqs=store_miller_index_i_seqs, store_spots=store_spots, store_signals=store_signals, set_pixels=set_pixels).compute( unit_cell=i_calc.p1_anom.unit_cell(), miller_indices=i_calc.p1_anom.indices(), spot_intensity_factors=i_calc.p1_anom.data(), crystal_rotation_matrix=crystal_rotation_matrix, ewald_radius=1/wavelength, ewald_proximity=work_params.ewald_proximity, signal_max=work_params.signal_max, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels, point_spread=work_params.point_spread, gaussian_falloff_scale=work_params.gaussian_falloff_scale)
def get_f(): vals = tuple(O.x[:6]) try: O.unit_cell = uctbx.unit_cell(vals) except RuntimeError as e: raise InfeasibleError(str(e)) vals = matrix.col(O.x[6:]) / O.uq_scale try: vals = vals.normalize() except ZeroDivisionError as e: raise InfeasibleError(str(e)) if (O.average_unit_cell is not None): O.unit_cell = O.average_unit_cell(O.unit_cell) O.crystal_rotation = vals.unit_quaternion_as_r3_rotation_matrix() from rstbx.simage import image_simple O.predicted_spots = image_simple( apply_detector_clipping=False, apply_proximity_filter=False, store_spots=True).compute( unit_cell=O.unit_cell, miller_indices=O.miller_indices, spot_intensity_factors=None, crystal_rotation_matrix=O.crystal_rotation, ewald_radius=1 / O.work_params.wavelength, ewald_proximity=O.work_params.ewald_proximity, signal_max=O.work_params.signal_max, detector_distance=O.work_params.detector.distance, detector_size=O.work_params.detector.size, detector_pixels=O.work_params.detector.pixels, point_spread=O.work_params.point_spread, gaussian_falloff_scale=O.work_params.gaussian_falloff_scale ).spots assert O.predicted_spots.size() == O.spots_xy0.size() return O.spots_xy0.rms_difference(O.predicted_spots)
def build_one_image(i_img): mt = flex.mersenne_twister(seed=work_params.noise.random_seed+i_img) scale = int(work_params.signal_max*(0.1+0.9*mt.random_double())) crystal_rotation = mt.random_double_r3_rotation_matrix_arvo_1992() i_perm = mt.random_size_t() % len(i_calc_data_perms) image = image_simple( store_miller_index_i_seqs=True, store_spots=True, store_signals=True, set_pixels=True).compute( unit_cell=i_calc.unit_cell(), miller_indices=i_calc.indices(), spot_intensity_factors=i_calc_data_perms[i_perm], crystal_rotation_matrix=crystal_rotation, ewald_radius=1/work_params.wavelength, ewald_proximity=work_params.ewald_proximity, signal_max=scale, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels, point_spread=work_params.point_spread, gaussian_falloff_scale=work_params.gaussian_falloff_scale) add_noise(work_params, pixels=image.pixels) if (not work_params.index_and_integrate): pixels = None else: pixels = image.pixels miller_index_i_seqs = image.miller_index_i_seqs if (use_mp): # to by-pass portable but slower pickling if (pixels is not None): assert pixels.is_0_based() assert not pixels.is_padded() assert pixels.all() == tuple(work_params.detector.pixels) pixels = pixels.copy_to_byte_str() miller_index_i_seqs = miller_index_i_seqs.copy_to_byte_str() return image_model( pixels=pixels, spot_positions=image.spots, spot_intensities=image.signals, unit_cell=i_calc.unit_cell(), crystal_rotation=crystal_rotation, miller_index_i_seqs=miller_index_i_seqs, scale=scale, i_perm=i_perm)
def predict_spot_positions(work_params, miller_indices, unit_cell, crystal_rotation): from rstbx.simage import image_simple image = image_simple( store_spots=True, store_miller_index_i_seqs=True).compute( unit_cell=unit_cell, miller_indices=miller_indices, spot_intensity_factors=None, crystal_rotation_matrix=crystal_rotation, ewald_radius=1 / work_params.wavelength, ewald_proximity=work_params.ewald_proximity, signal_max=work_params.signal_max, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels, point_spread=work_params.point_spread, gaussian_falloff_scale=work_params.gaussian_falloff_scale) return image.spots, image.miller_index_i_seqs
def reset_partialities(O, work_params, miller_indices): from rstbx.simage import image_simple O.partialities = image_simple( apply_detector_clipping=False, apply_proximity_filter=False, store_signals=True).compute( unit_cell=O.unit_cell, miller_indices=miller_indices.select(O.miller_index_i_seqs), spot_intensity_factors=None, crystal_rotation_matrix=O.crystal_rotation, ewald_radius=1/work_params.wavelength, ewald_proximity=work_params.ewald_proximity, signal_max=1, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels, point_spread=work_params.point_spread, gaussian_falloff_scale=work_params.gaussian_falloff_scale).signals assert O.partialities.size() == O.miller_index_i_seqs.size()
def predict_spot_positions( work_params, miller_indices, unit_cell, crystal_rotation): from rstbx.simage import image_simple image = image_simple( store_spots=True, store_miller_index_i_seqs=True).compute( unit_cell=unit_cell, miller_indices=miller_indices, spot_intensity_factors=None, crystal_rotation_matrix=crystal_rotation, ewald_radius=1/work_params.wavelength, ewald_proximity=work_params.ewald_proximity, signal_max=work_params.signal_max, detector_distance=work_params.detector.distance, detector_size=work_params.detector.size, detector_pixels=work_params.detector.pixels, point_spread=work_params.point_spread, gaussian_falloff_scale=work_params.gaussian_falloff_scale) return image.spots, image.miller_index_i_seqs
def run_labelit_index(O, use_original_uc_cr=False): if (O.spots is None): O.run_spotfinder() if (O.spots is None): return if (O.spots.size() < 10): print "Insufficient number of spotfinder spots." print return else: if (use_original_uc_cr): print print "Using original unit cell and crystal rotation" \ " for spot prediction." print uc = O.work_params.unit_cell cr = O.work_params.crystal_rotation_matrix else: from rstbx.simage.run_labelit_index import process try: ai = process(work_params=O.work_params, spots=O.spots) except Exception, e: print "Indexing exception:", e print return print "Spots indexed: %d of %d" % ( ai.hklobserved().size(), O.spots.size()) co = ai.getOrientation() uc = co.unit_cell() cr = co.crystal_rotation_matrix() print "labelit index unit cell:", uc from rstbx.simage import refine_uc_cr refined = refine_uc_cr.refine( work_params=O.work_params, spots=O.spots, good_i_seqs=ai.get_observed_spot_i_seqs_good_only(), miller_indices=ai.hklobserved(), unit_cell=uc, crystal_rotation=cr) uc = refined.unit_cell cr = refined.crystal_rotation print import cctbx.crystal crystal_symmetry = cctbx.crystal.symmetry( unit_cell=uc, space_group_symbol="P1") d_min = O.work_params.d_min if (d_min is None): d_min = O.work_params.wavelength import cctbx.miller miller_set = cctbx.miller.build_set( crystal_symmetry=crystal_symmetry, d_min=d_min, anomalous_flag=True) from rstbx.simage import image_simple O.predicted_spots = image_simple(store_spots=True).compute( unit_cell=miller_set.unit_cell(), miller_indices=miller_set.indices(), spot_intensity_factors=None, crystal_rotation_matrix=cr, ewald_radius=1/O.work_params.wavelength, ewald_proximity=O.work_params.ewald_proximity, signal_max=O.work_params.signal_max, detector_distance=O.work_params.detector.distance, detector_size=O.work_params.detector.size, detector_pixels=O.work_params.detector.pixels, point_spread=O.work_params.point_spread, gaussian_falloff_scale=O.work_params.gaussian_falloff_scale).spots print "Number of predicted spots:", O.predicted_spots.size() print
def run_labelit_index(O, use_original_uc_cr=False): if (O.spots is None): O.run_spotfinder() if (O.spots is None): return if (O.spots.size() < 10): print("Insufficient number of spotfinder spots.") print() return else: if (use_original_uc_cr): print() print("Using original unit cell and crystal rotation" \ " for spot prediction.") print() uc = O.work_params.unit_cell cr = O.work_params.crystal_rotation_matrix else: from rstbx.simage.run_labelit_index import process try: ai = process(work_params=O.work_params, spots=O.spots) except Exception as e: print("Indexing exception:", e) print() return print("Spots indexed: %d of %d" % (ai.hklobserved().size(), O.spots.size())) co = ai.getOrientation() uc = co.unit_cell() cr = co.crystal_rotation_matrix() print("labelit index unit cell:", uc) from rstbx.simage import refine_uc_cr refined = refine_uc_cr.refine( work_params=O.work_params, spots=O.spots, good_i_seqs=ai.get_observed_spot_i_seqs_good_only(), miller_indices=ai.hklobserved(), unit_cell=uc, crystal_rotation=cr) uc = refined.unit_cell cr = refined.crystal_rotation print() import cctbx.crystal crystal_symmetry = cctbx.crystal.symmetry(unit_cell=uc, space_group_symbol="P1") d_min = O.work_params.d_min if (d_min is None): d_min = O.work_params.wavelength import cctbx.miller miller_set = cctbx.miller.build_set( crystal_symmetry=crystal_symmetry, d_min=d_min, anomalous_flag=True) from rstbx.simage import image_simple O.predicted_spots = image_simple(store_spots=True).compute( unit_cell=miller_set.unit_cell(), miller_indices=miller_set.indices(), spot_intensity_factors=None, crystal_rotation_matrix=cr, ewald_radius=1 / O.work_params.wavelength, ewald_proximity=O.work_params.ewald_proximity, signal_max=O.work_params.signal_max, detector_distance=O.work_params.detector.distance, detector_size=O.work_params.detector.size, detector_pixels=O.work_params.detector.pixels, point_spread=O.work_params.point_spread, gaussian_falloff_scale=O.work_params.gaussian_falloff_scale ).spots print("Number of predicted spots:", O.predicted_spots.size()) print() O.Refresh()
class refinery(object): def __init__(O, work_params, spots_xy0, miller_indices, unit_cell, crystal_rotation_uq): assert spots_xy0.size() == miller_indices.size() O.work_params = work_params O.spots_xy0 = spots_xy0 O.miller_indices = miller_indices O.uq_scale = 100 # to balance gradients O.average_unit_cell = O.work_params.lattice_symmetry.group() \ .average_unit_cell unit_cell = O.average_unit_cell(unit_cell) from scitbx.array_family import flex O.x = flex.double( unit_cell.parameters() + (crystal_rotation_uq * O.uq_scale).elems) assert O.x.size() == 10 O.initial_functional = None import scitbx.lbfgs scitbx.lbfgs.run(target_evaluator=O) O.final_functional = O.compute_functional_and_gradients( functional_only=True) del O.average_unit_cell def compute_functional_and_gradients(O, functional_only=False): from cctbx import uctbx from scitbx import matrix def get_f(): vals = tuple(O.x[:6]) try: O.unit_cell = uctbx.unit_cell(vals) except RuntimeError, e: raise InfeasibleError(str(e)) vals = matrix.col(O.x[6:]) / O.uq_scale try: vals = vals.normalize() except ZeroDivisionError, e: raise InfeasibleError(str(e)) if (O.average_unit_cell is not None): O.unit_cell = O.average_unit_cell(O.unit_cell) O.crystal_rotation = vals.unit_quaternion_as_r3_rotation_matrix() from rstbx.simage import image_simple O.predicted_spots = image_simple( apply_detector_clipping=False, apply_proximity_filter=False, store_spots=True).compute( unit_cell=O.unit_cell, miller_indices=O.miller_indices, spot_intensity_factors=None, crystal_rotation_matrix=O.crystal_rotation, ewald_radius=1/O.work_params.wavelength, ewald_proximity=O.work_params.ewald_proximity, signal_max=O.work_params.signal_max, detector_distance=O.work_params.detector.distance, detector_size=O.work_params.detector.size, detector_pixels=O.work_params.detector.pixels, point_spread=O.work_params.point_spread, gaussian_falloff_scale=O.work_params.gaussian_falloff_scale).spots assert O.predicted_spots.size() == O.spots_xy0.size() return O.spots_xy0.rms_difference(O.predicted_spots)