def refined_settings_factory_from_refined_triclinic(params, experiments, reflections, i_setting=None, lepage_max_delta=5.0, nproc=1, refiner_verbosity=0): assert len(experiments.crystals()) == 1 crystal = experiments.crystals()[0] used_reflections = copy.deepcopy(reflections) UC = crystal.get_unit_cell() from rstbx.dps_core.lepage import iotbx_converter Lfat = refined_settings_list() for item in iotbx_converter(UC, lepage_max_delta): Lfat.append(bravais_setting(item)) supergroup = Lfat.supergroup() triclinic = Lfat.triclinic() triclinic_miller = used_reflections['miller_index'] # assert no transformation between indexing and bravais list assert str(triclinic['cb_op_inp_best']) == "a,b,c" Nset = len(Lfat) for j in xrange(Nset): Lfat[j].setting_number = Nset - j from cctbx.crystal_orientation import crystal_orientation from cctbx import sgtbx from scitbx import matrix for j in xrange(Nset): cb_op = Lfat[j]['cb_op_inp_best'].c().as_double_array()[0:9] orient = crystal_orientation(crystal.get_A(), True) orient_best = orient.change_basis(matrix.sqr(cb_op).transpose()) constrain_orient = orient_best.constrain(Lfat[j]['system']) bravais = Lfat[j]["bravais"] cb_op_best_ref = Lfat[j][ 'best_subsym'].change_of_basis_op_to_reference_setting() space_group = sgtbx.space_group_info( number=bravais_lattice_to_lowest_symmetry_spacegroup_number[ bravais]).group() space_group = space_group.change_basis(cb_op_best_ref.inverse()) bravais = str(bravais_types.bravais_lattice(group=space_group)) Lfat[j]["bravais"] = bravais Lfat[j].unrefined_crystal = dials_crystal_from_orientation( constrain_orient, space_group) args = [] for subgroup in Lfat: args.append((params, subgroup, used_reflections, experiments, refiner_verbosity)) results = easy_mp.parallel_map(func=refine_subgroup, iterable=args, processes=nproc, method="multiprocessing", preserve_order=True, asynchronous=True, preserve_exception_message=True) for i, result in enumerate(results): Lfat[i] = result identify_likely_solutions(Lfat) return Lfat
def refined_settings_from_refined_triclinic(experiments, reflections, params): """Generate a RefinedSettingsList from a triclinic model. Args: experiments: The experiments refined with a triclinic model reflections: A reflection table containing observed centroids params: The working PHIL parameters. Returns: RefinedSettingsList: A list of the refined settings. The highest symmetry setting will be first item in the list, and the triclinic setting will be last. """ if params.nproc is libtbx.Auto: params.nproc = number_of_processors() if params.refinement.reflections.outlier.algorithm in ("auto", libtbx.Auto): if experiments[0].goniometer is None: params.refinement.reflections.outlier.algorithm = "sauter_poon" else: # different default to dials.refine # tukey is faster and more appropriate at the indexing step params.refinement.reflections.outlier.algorithm = "tukey" assert len(experiments.crystals()) == 1 crystal = experiments.crystals()[0] used_reflections = copy.deepcopy(reflections) UC = crystal.get_unit_cell() refined_settings = RefinedSettingsList() for item in iotbx_converter( UC, params.lepage_max_delta, best_monoclinic_beta=params.best_monoclinic_beta): refined_settings.append(BravaisSetting(item)) triclinic = refined_settings.triclinic() # assert no transformation between indexing and bravais list assert str(triclinic["cb_op_inp_best"]) == "a,b,c" Nset = len(refined_settings) for j in range(Nset): refined_settings[j].setting_number = Nset - j for subgroup in refined_settings: bravais_lattice = str( bravais_types.bravais_lattice( group=subgroup["best_subsym"].space_group())) space_group = lowest_symmetry_space_group_for_bravais_lattice( bravais_lattice) orient = crystal_orientation(crystal.get_A(), True).change_basis( scitbx.matrix.sqr(subgroup["cb_op_inp_best"].c().as_double_array() [0:9]).transpose()) constrain_orient = orient.constrain(subgroup["system"]) subgroup["bravais"] = bravais_lattice subgroup.unrefined_crystal = dxtbx_crystal_from_orientation( constrain_orient, space_group) with concurrent.futures.ProcessPoolExecutor( max_workers=params.nproc) as pool: for i, result in enumerate( pool.map( refine_subgroup, ((params, subgroup, used_reflections, experiments) for subgroup in refined_settings), )): refined_settings[i] = result identify_likely_solutions(refined_settings) return refined_settings
def run(self): if self.verbose: print('Starting strategy using {} and {}'.format( self.imagefiles[0], self.imagefiles[1])) # make experiments experiments = self.processor.make_experiments(self.imagefiles) # find spots if self.verbose: print("Spotfinding...") observed = self.processor.find_spots(experiments=experiments, params=self.spf_params) if self.verbose: print("Spotfinding DONE: found {} spots total".format( observed.size())) # Populate spotfinding results spotfinding_dict = {} for i, experiment in enumerate(experiments): refl = observed.select(observed["id"] == i) overloads = refl.select(refl.is_overloaded(experiments) == True) imageset = experiment.imageset beam = imageset.get_beam() s0 = beam.get_s0() detector = imageset.get_detector()[0] spf_dict = { 'imagefile': imageset.paths()[0], 'spotTotal': len(refl), 'inResOverlSpots': len(overloads), 'beamXY': detector.get_beam_centre(s0), 'distance': detector.get_distance(), 'wavelength': beam.get_wavelength(), 'pixelsize': detector.get_pixel_size(), 'imagesize': detector.get_image_size(), } spotfinding_dict.update({i + 1: spf_dict}) # index if self.verbose: print("Indexing...") indexed, experiments = self.processor.index(experiments=experiments, reflections=observed, params=self.idx_params) if self.verbose: crystals = experiments.crystals() print("Initial indexing solution: ", crystals[0].get_space_group().info(), crystals[0].get_unit_cell()) print("{} indexed reflections".format(len(indexed))) # refine Bravais settings if self.verbose: print("Determining the likeliest space group...") self.brv_phil.show() P1_solution, highest_sym_solution = self.processor.refine_bravais_settings( experiments=experiments, indexed=indexed, params=self.brv_params) # the below can be expanded to all acceptable solutions solutions = [P1_solution, highest_sym_solution] # Reindex and integrate for all Bravais solutions solutions_dict = {} for i, solution in enumerate(solutions): experiments, reindexed = self.processor.reindex( solution=solution, reflections=indexed, experiments=experiments) crystal = experiments.crystals()[0] if self.verbose: print(crystal, "\n") print("{} reindexed reflections".format(len(reindexed))) # populate index / reindex results uc = crystal.get_unit_cell() idx_dict = { "id": solution.setting_number, "metricfit": solution['max_angular_difference'], "rmsd": solution.rmsd, "spots": solution.Nmatches, "crystalsystem": solution['system'], "lattice": solution['bravais'], "unitcell": uc.parameters(), "volume": uc.volume(), } # integrate if self.verbose: print("Integrating...") integrated, experiments = self.processor.integrate( experiments=experiments, indexed=reindexed, params=self.int_params) if self.verbose: print("{} integrated reflections".format(len(integrated))) crystal = experiments.crystals()[0] # export BEST parameters if self.verbose: print('exporting results...') self.exp_params.output.prefix = "best{}".format(i + 1) exporter = CustomBestExporter(params=self.exp_params, reflections=[integrated], experiments=experiments) datfile, parfile, hkl1, hkl2 = exporter.export() s_dict = { "matrix": crystal.get_A(), # UB matrix "dataForBest": os.path.abspath(os.path.join(os.curdir, datfile)), "paramFileForBest": os.path.abspath(os.path.join(os.curdir, parfile)), "hkl1": os.path.abspath(os.path.join(os.curdir, hkl1)), "hkl2": os.path.abspath(os.path.join(os.curdir, hkl2)), "indexResult": idx_dict, } solutions_dict.update({solution.setting_number: s_dict}) # collate other information crystal = experiments.crystals()[0] info_dict = { "imagefiles": self.imagefiles, "spotfinding": spotfinding_dict, "solutions": solutions_dict, } with open('info.json', "w") as jf: json.dump(info_dict, jf) if self.verbose: print("\n*****") info_print = json.dumps(info_dict, indent=1) print(info_print)
def refined_settings_factory_from_refined_triclinic( params, experiments, reflections, i_setting=None, lepage_max_delta=5.0, nproc=1, refiner_verbosity=0): assert len(experiments.crystals()) == 1 crystal = experiments.crystals()[0] used_reflections = copy.deepcopy(reflections) UC = crystal.get_unit_cell() from rstbx.dps_core.lepage import iotbx_converter Lfat = refined_settings_list() for item in iotbx_converter(UC, lepage_max_delta): Lfat.append(bravais_setting(item)) supergroup = Lfat.supergroup() triclinic = Lfat.triclinic() triclinic_miller = used_reflections['miller_index'] # assert no transformation between indexing and bravais list assert str(triclinic['cb_op_inp_best'])=="a,b,c" Nset = len(Lfat) for j in xrange(Nset): Lfat[j].setting_number = Nset-j from cctbx.crystal_orientation import crystal_orientation from cctbx import sgtbx from scitbx import matrix for j in xrange(Nset): cb_op = Lfat[j]['cb_op_inp_best'].c().as_double_array()[0:9] orient = crystal_orientation(crystal.get_A(),True) orient_best = orient.change_basis(matrix.sqr(cb_op).transpose()) constrain_orient = orient_best.constrain(Lfat[j]['system']) bravais = Lfat[j]["bravais"] cb_op_best_ref = Lfat[j]['best_subsym'].change_of_basis_op_to_reference_setting() space_group = sgtbx.space_group_info( number=bravais_lattice_to_lowest_symmetry_spacegroup_number[bravais]).group() space_group = space_group.change_basis(cb_op_best_ref.inverse()) bravais = str(bravais_types.bravais_lattice(group=space_group)) Lfat[j]["bravais"] = bravais Lfat[j].unrefined_crystal = dials_crystal_from_orientation( constrain_orient, space_group) args = [] for subgroup in Lfat: args.append(( params, subgroup, used_reflections, experiments, refiner_verbosity)) results = easy_mp.parallel_map( func=refine_subgroup, iterable=args, processes=nproc, method="multiprocessing", preserve_order=True, asynchronous=True, preserve_exception_message=True) for i, result in enumerate(results): Lfat[i] = result return Lfat