def run(self, iterable):

        with Capturing() as junk_output:
            try:
                ucs = Cluster.from_iterable(iterable=iterable)
                clusters, _ = ucs.ab_cluster(5000,
                                             log=False,
                                             write_file_lists=False,
                                             schnell=True,
                                             doplot=False)
            except Exception:
                clusters = []

        if len(clusters) > 0:
            info = []
            for cluster in clusters:
                uc_init = unit_cell(cluster.medians)
                symmetry = crystal.symmetry(unit_cell=uc_init,
                                            space_group_symbol='P1')
                groups = lattice_symmetry.metric_subgroups(
                    input_symmetry=symmetry, max_delta=3)
                top_group = groups.result_groups[0]
                best_uc = top_group['best_subsym'].unit_cell().parameters()
                best_sg = top_group['best_subsym'].space_group_info()

                uc_no_stdev = "{:<6.2f} {:<6.2f} {:<6.2f} " \
                              "{:<6.2f} {:<6.2f} {:<6.2f} " \
                              "".format(best_uc[0], best_uc[1], best_uc[2],
                                        best_uc[3], best_uc[4], best_uc[5])
                cluster_info = {
                    'number': len(cluster.members),
                    'pg': str(best_sg),
                    'uc': uc_no_stdev
                }

                info.append(cluster_info)

        else:
            info = None

        return info
    def make_prime_input(self, filename='prime.phil'):
        """ Imports default PRIME input parameters, modifies correct entries and
        prints out a starting PHIL file to be used with PRIME
    """

        if self.params.advanced.integrate_with == 'cctbx':
            img_pickle = self.final_objects[0].final['img']
            pixel_size = pickle.load(open(img_pickle, "rb"))['PIXEL_SIZE']
        elif self.params.advanced.integrate_with == 'dials':
            proc_pickle = self.final_objects[0].final['final']
            pixel_size = pickle.load(open(proc_pickle, 'rb'))['pixel_size']

        triclinic = ['P1']
        monoclinic = ['C2', 'P2']
        orthorhombic = ['P222', 'C222', 'I222', 'F222']
        tetragonal = ['I4', 'I422', 'P4', 'P422']
        hexagonal = ['P3', 'P312', 'P321', 'P6', 'P622']
        rhombohedral = ['R3', 'R32']
        cubic = ['F23', 'F432', 'I23', 'I432', 'P23', 'P432']

        sg = self.cons_pg.replace(" ", "")
        uc = ['{:4.2f}'.format(i) for i in self.cons_uc]

        if sg in triclinic:
            crystal_system = 'Triclinic'
        elif sg in monoclinic:
            crystal_system = 'Monoclinic'
        elif sg in orthorhombic:
            crystal_system = 'Orthorhombic'
        elif sg in tetragonal:
            crystal_system = 'Tetragonal'
        elif sg in hexagonal:
            crystal_system = 'Hexagonal'
        elif sg in rhombohedral:
            crystal_system = 'Rhombohedral'
        elif sg in cubic:
            crystal_system = 'Cubic'
        else:
            crystal_system = 'None'

        # Determine number of images for indexing ambiguity resolution
        # My default: 1/2 of images or 300, whichever is smaller
        if len(self.final_objects) >= 600:
            idx_ambiguity_sample = 300
            idx_ambiguity_selected = 100
        else:
            idx_ambiguity_sample = int(len(self.final_objects) / 2)
            idx_ambiguity_selected = int(idx_ambiguity_sample / 3)

        prime_params = mod_input.master_phil.extract()

        prime_params.data = [self.prime_data_path]
        prime_params.run_no = os.path.join(
            os.path.dirname(self.prime_data_path), '001')
        prime_params.title = 'Auto-generated by IOTA v{} on {}'.format(
            self.ver, self.now)
        prime_params.scale.d_min = np.mean(self.hres)
        prime_params.postref.scale.d_min = np.mean(self.hres)
        prime_params.postref.crystal_orientation.d_min = np.mean(self.hres)
        prime_params.postref.reflecting_range.d_min = np.mean(self.hres)
        prime_params.postref.unit_cell.d_min = np.mean(self.hres)
        prime_params.postref.allparams.d_min = np.mean(self.hres)
        prime_params.merge.d_min = np.mean(self.hres)
        prime_params.target_unit_cell = unit_cell(self.cons_uc)
        prime_params.target_space_group = sg
        prime_params.target_crystal_system = crystal_system
        prime_params.pixel_size_mm = pixel_size
        prime_params.n_residues = 500
        prime_params.indexing_ambiguity.n_sample_frames = idx_ambiguity_sample
        prime_params.indexing_ambiguity.n_selected_frames = idx_ambiguity_selected

        prime_phil = mod_input.master_phil.format(python_object=prime_params)

        with Capturing() as output:
            prime_phil.show()

        txt_out = ''
        for one_output in output:
            txt_out += one_output + '\n'

        prime_file = os.path.join(self.output_dir, filename)
        with open(prime_file, 'w') as pf:
            pf.write(txt_out)

        return prime_phil
  def process_image(self):
    if os.path.isfile(self.termfile):
      raise IOTATermination('IOTA_TRACKER: Termination signal received!')
    else:
      with Capturing() as junk_output:
        err = []
        start = time.time()
        fail = False
        sg = None
        uc = None
        obs = None
        status = None
        res = 99
        try:
          datablock = DataBlockFactory.from_filenames([self.img])[0]
          observed = self.processor.find_spots(datablock=datablock)
          status = 'spots found'
        except Exception, e:
          fail = True
          observed = []
          err.append(e)
          pass

        # TODO: Indexing / lattice determination very slow (how to speed up?)
        if self.run_indexing:
          if not fail:
            try:
              experiments, indexed = self.processor.index(
                datablock=datablock, reflections=observed)
            except Exception, e:
              fail = True
              err.append(e)
              pass

          if not fail:
            try:
              solution = self.processor.refine_bravais_settings(
                reflections=indexed, experiments=experiments)

              # Only reindex if higher-symmetry solution found
              if solution is not None:
                experiments, indexed = self.processor.reindex(
                  reflections=indexed,
                  experiments=experiments,
                  solution=solution)
              obs = experiments
              lat = experiments[0].crystal.get_space_group().info()
              sg = str(lat).replace(' ', '')
              status = 'indexed'

            except Exception:
              fail = True
              err.append(e)
              pass

          if not fail:
            unit_cell = experiments[0].crystal.get_unit_cell().parameters()
            uc = ' '.join(['{:.1f}'.format(i) for i in unit_cell])

          if self.run_integration:
            if not fail:
              try:
                # Run refinement
                experiments, indexed = self.processor.refine(
                  experiments=experiments,
                  centroids=indexed)

                integrated = self.processor.integrate(experiments=experiments,
                                                           indexed=indexed)
                status = 'integrated'
              except Exception:
                fail = True
                err.append(e)
                pass
    def run(self, idx, img):
        if self.meta_parent.terminated:
            raise IOTATermination(
                'IOTA_TRACKER: SPF Termination signal received!')
        else:
            with Capturing() as junk_output:
                fail = False
                sg = None
                uc = None
                try:
                    datablock = DataBlockFactory.from_filenames([img])[0]
                    observed = self.processor.find_spots(datablock=datablock)
                except Exception as e:
                    fail = True
                    observed = []
                    pass

                # TODO: Indexing / lattice determination very slow (how to speed up?)
                if self.run_indexing:
                    if not fail:
                        try:
                            experiments, indexed = self.processor.index(
                                datablock=datablock, reflections=observed)
                        except Exception as e:
                            fail = True
                            pass

                    if not fail:
                        try:
                            solution = self.processor.refine_bravais_settings(
                                reflections=indexed, experiments=experiments)

                            # Only reindex if higher-symmetry solution found
                            if solution is not None:
                                experiments, indexed = self.processor.reindex(
                                    reflections=indexed,
                                    experiments=experiments,
                                    solution=solution)
                            lat = experiments[0].crystal.get_space_group(
                            ).info()
                            sg = str(lat).replace(' ', '')
                        except Exception:
                            fail = True
                            pass

                    if not fail:
                        unit_cell = experiments[0].crystal.get_unit_cell(
                        ).parameters()
                        uc = ' '.join(['{:.4f}'.format(i) for i in unit_cell])

                    if self.run_integration:
                        if not fail:
                            try:
                                # Run refinement
                                experiments, indexed = self.processor.refine(
                                    experiments=experiments, centroids=indexed)
                            except Exception as e:
                                fail = True
                                pass

                        if not fail:
                            try:
                                print experiments
                                print indexed
                                integrated = self.processor.integrate(
                                    experiments=experiments, indexed=indexed)
                            except Exception as e:
                                pass

            return [idx, int(len(observed)), img, sg, uc]
    def process_image(self):
        if os.path.isfile(self.termfile):
            raise IOTATermination('IOTA_TRACKER: Termination signal received!')
        else:
            with Capturing() as junk_output:
                # if True:
                err = []
                start = time.time()
                fail = False
                sg = None
                uc = None
                status = None
                score = 0
                try:
                    datablock = DataBlockFactory.from_filenames([self.img])[0]
                    observed = self.processor.find_spots(datablock=datablock)
                    status = 'spots found'
                except Exception as e:
                    fail = True
                    observed = []
                    err.append('SPOTFINDING ERROR: {}'.format(e))
                    pass

                # TODO: Indexing / lattice determination very slow (how to speed up?)
                if self.run_indexing:
                    if not fail:
                        try:
                            experiments, indexed = self.processor.index(
                                datablock=datablock, reflections=observed)
                            score = len(indexed)
                        except Exception as e:
                            fail = True
                            err.append('INDEXING ERROR: {}'.format(e))
                            pass

                    if not fail:
                        try:
                            solution = self.processor.refine_bravais_settings(
                                reflections=indexed, experiments=experiments)

                            # Only reindex if higher-symmetry solution found
                            if solution is not None:
                                experiments, indexed = self.processor.reindex(
                                    reflections=indexed,
                                    experiments=experiments,
                                    solution=solution)
                            obs = experiments
                            lat = experiments[0].crystal.get_space_group(
                            ).info()
                            sg = str(lat).replace(' ', '')
                            status = 'indexed'
                        except Exception as e:
                            fail = True
                            err.append('LATTICE ERROR: {}'.format(e))
                            pass

                    if not fail:
                        unit_cell = experiments[0].crystal.get_unit_cell(
                        ).parameters()
                        uc = ' '.join(['{:.4f}'.format(i) for i in unit_cell])

                    if self.run_integration:
                        if not fail:
                            try:
                                # Run refinement
                                experiments, indexed = self.processor.refine(
                                    experiments=experiments, centroids=indexed)
                            except Exception as e:
                                fail = True
                                err.append('REFINEMENT ERROR: {}'.format(e))
                                pass

                        if not fail:
                            try:
                                print experiments
                                print indexed
                                integrated = self.processor.integrate(
                                    experiments=experiments, indexed=indexed)
                                frame = ConstructFrame(
                                    integrated, experiments[0]).make_frame()
                                status = 'integrated'
                            except Exception as e:
                                err.append('INTEGRATION ERROR: {}'.format(e))
                                pass

            if status == 'integrated':
                res = frame['observations'][0].d_max_min()
            else:
                detector = datablock.unique_detectors()[0]
                beam = datablock.unique_beams()[0]

                s1 = flex.vec3_double()
                for i in xrange(len(observed)):
                    s1.append(
                        detector[observed['panel'][i]].get_pixel_lab_coord(
                            observed['xyzobs.px.value'][i][0:2]))
                two_theta = s1.angle(beam.get_s0())
                d = beam.get_wavelength() / (2 * flex.asin(two_theta / 2))
                res = (np.max(d), np.min(d))

            if len(observed) < self.min_bragg:
                res = (99, 99)

        elapsed = time.time() - start
        info = [self.index, len(observed), self.img, sg, uc]
        return status, info, res, score, elapsed, err
Example #6
0
  def make_prime_input(self, filename='prime.phil'):
    """ Imports default PRIME input parameters, modifies correct entries and
        prints out a starting PHIL file to be used with PRIME
    """

    img_pickle = self.final_objects[0].final['img']
    pixel_size = pickle.load(open(img_pickle, "rb"))['PIXEL_SIZE']

    triclinic = ['P1']
    monoclinic = ['C2', 'P2']
    orthorhombic = ['P222', 'C222', 'I222', 'F222']
    tetragonal = ['I4', 'I422', 'P4', 'P422']
    hexagonal = ['P3', 'P312', 'P321', 'P6', 'P622']
    rhombohedral = ['R3', 'R32']
    cubic = ['F23', 'F432', 'I23', 'I432', 'P23', 'P432']

    sg = self.cons_pg.replace(" ", "")
    uc = ['{:4.2f}'.format(i) for i in self.cons_uc]

    if sg in triclinic:
      crystal_system = 'Triclinic'
    elif sg in monoclinic:
      crystal_system = 'Monoclinic'
    elif sg in orthorhombic:
      crystal_system = 'Orthorhombic'
    elif sg in tetragonal:
      crystal_system = 'Tetragonal'
    elif sg in hexagonal:
      crystal_system = 'Hexagonal'
    elif sg in rhombohedral:
      crystal_system = 'Rhombohedral'
    elif sg in cubic:
      crystal_system = 'Cubic'
    else:
      crystal_system = 'None'

    prime_params = mod_input.master_phil.extract()

    prime_params.data = [self.prime_data_path]
    prime_params.run_no = os.path.join(os.path.dirname(self.prime_data_path), '001')
    prime_params.title = 'Auto-generated by IOTA v{} on {}'.format(self.ver, self.now)
    prime_params.scale.d_min = np.mean(self.hres)
    prime_params.postref.scale.d_min = np.mean(self.hres)
    prime_params.postref.crystal_orientation.d_min = np.mean(self.hres)
    prime_params.postref.reflecting_range.d_min = np.mean(self.hres)
    prime_params.postref.unit_cell.d_min = np.mean(self.hres)
    prime_params.postref.allparams.d_min = np.mean(self.hres)
    prime_params.merge.d_min = np.mean(self.hres)
    prime_params.target_unit_cell = unit_cell(self.cons_uc)
    prime_params.target_space_group = sg
    prime_params.target_crystal_system = crystal_system
    prime_params.pixel_size_mm = pixel_size

    prime_phil = mod_input.master_phil.format(python_object=prime_params)

    with Capturing() as output:
      prime_phil.show()

    txt_out = ''
    for one_output in output:
      txt_out += one_output + '\n'

    prime_file = os.path.join(self.output_dir, filename)
    with open(prime_file, 'w') as pf:
      pf.write(txt_out)

    return prime_phil