Ejemplo n.º 1
0
    def _to_dict(crystal):
        """Convert the crystal model to a dictionary

        Params:
            crystal The crystal model

        Returns:
            A dictionary of the parameters

        """
        from collections import OrderedDict
        from scitbx import matrix

        # Get the real space vectors
        A = matrix.sqr(crystal.get_A()).inverse()
        real_space_a = (A[0], A[1], A[2])
        real_space_b = (A[3], A[4], A[5])
        real_space_c = (A[6], A[7], A[8])

        # Get the space group Hall symbol
        hall = crystal.get_space_group().info().type().hall_symbol()

        # Isoforms used for stills
        try:
            identified_isoform = crystal.identified_isoform
        except AttributeError:
            identified_isoform = None

        # Collect the information as a python dictionary
        xl_dict = OrderedDict([
            ("__id__", "crystal"),
            ("real_space_a", real_space_a),
            ("real_space_b", real_space_b),
            ("real_space_c", real_space_c),
            ("space_group_hall_symbol", hall),
        ])

        if identified_isoform is not None:
            xl_dict["identified_isoform"] = identified_isoform

        # Add in scan points if present
        if crystal.num_scan_points > 0:
            A_at_scan_points = tuple([
                crystal.get_A_at_scan_point(i)
                for i in range(crystal.num_scan_points)
            ])
            xl_dict["A_at_scan_points"] = A_at_scan_points

        # Add in covariance of B if present
        cov_B = tuple(crystal.get_B_covariance())
        if len(cov_B) != 0:
            xl_dict["B_covariance"] = cov_B

        # Add in covariance of B at scan points if present
        if crystal.num_scan_points > 0:
            try:
                cov_B_at_scan_points = tuple([
                    tuple(crystal.get_B_covariance_at_scan_point(i))
                    for i in range(crystal.num_scan_points)
                ])
                xl_dict["B_covariance_at_scan_points"] = cov_B_at_scan_points
            except RuntimeError:
                pass

        return xl_dict
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
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