Exemplo n.º 1
0
    def run_indexer(self):
        config = create_indexing_config()

        # Find orientations
        self.update_progress_text('Running indexer (paintGrid)')
        ncpus = config.multiprocessing
        self.completeness = indexer.paintGrid(
            self.qfib,
            self.ome_maps,
            etaRange=np.radians(config.find_orientations.eta.range),
            omeTol=np.radians(config.find_orientations.omega.tolerance),
            etaTol=np.radians(config.find_orientations.eta.tolerance),
            omePeriod=np.radians(config.find_orientations.omega.period),
            threshold=config.find_orientations.threshold,
            doMultiProc=ncpus > 1,
            nCPUs=ncpus)
        print('paintGrid complete')

        orientations_cfg = HexrdConfig().indexing_config['find_orientations']
        if orientations_cfg.get('_write_scored_orientations'):
            # Write out the scored orientations
            results = {}
            results['scored_orientations'] = {
                'test_quaternions': self.qfib,
                'score': self.completeness
            }
            print(f'Writing scored orientations in {config.working_dir} ...')
            write_scored_orientations(results, config)
Exemplo n.º 2
0
    def run_indexer(self, config):
        # Generate the orientation fibers
        self.update_progress_text('Generating orientation fibers')
        self.qfib = generate_orientation_fibers(config, self.ome_maps)

        # Find orientations
        self.update_progress_text('Running indexer (paintGrid)')
        ncpus = config.multiprocessing
        self.completeness = indexer.paintGrid(
            self.qfib,
            self.ome_maps,
            etaRange=np.radians(config.find_orientations.eta.range),
            omeTol=np.radians(config.find_orientations.omega.tolerance),
            etaTol=np.radians(config.find_orientations.eta.tolerance),
            omePeriod=np.radians(config.find_orientations.omega.period),
            threshold=config.find_orientations.threshold,
            doMultiProc=ncpus > 1,
            nCPUs=ncpus)
        print('Indexing complete')
Exemplo n.º 3
0
def find_orientations(cfg,
                      hkls=None,
                      clean=False,
                      profile=False,
                      use_direct_testing=False):
    """


    Parameters
    ----------
    cfg : TYPE
        DESCRIPTION.
    hkls : TYPE, optional
        DESCRIPTION. The default is None.
    clean : TYPE, optional
        DESCRIPTION. The default is False.
    profile : TYPE, optional
        DESCRIPTION. The default is False.
    use_direct_search : TYPE, optional
        DESCRIPTION. The default is False.

    Returns
    -------
    None.

    """
    # grab objects from config
    plane_data = cfg.material.plane_data
    imsd = cfg.image_series
    instr = cfg.instrument.hedm
    eta_ranges = cfg.find_orientations.eta.range

    # tolerances
    tth_tol = plane_data.tThWidth
    eta_tol = np.radians(cfg.find_orientations.eta.tolerance)
    ome_tol = np.radians(cfg.find_orientations.omega.tolerance)

    # handle omega period
    ome_period, ome_ranges = _process_omegas(imsd)

    # for multiprocessing
    ncpus = cfg.multiprocessing

    # thresholds
    image_threshold = cfg.find_orientations.orientation_maps.threshold
    on_map_threshold = cfg.find_orientations.threshold
    compl_thresh = cfg.find_orientations.clustering.completeness

    # clustering
    cl_algorithm = cfg.find_orientations.clustering.algorithm
    cl_radius = cfg.find_orientations.clustering.radius

    # =========================================================================
    # ORIENTATION SCORING
    # =========================================================================
    do_grid_search = cfg.find_orientations.use_quaternion_grid is not None

    if use_direct_testing:
        npdiv_DFLT = 2
        params = dict(plane_data=plane_data,
                      instrument=instr,
                      imgser_dict=imsd,
                      tth_tol=tth_tol,
                      eta_tol=eta_tol,
                      ome_tol=ome_tol,
                      eta_ranges=np.radians(eta_ranges),
                      ome_period=np.radians(ome_period),
                      npdiv=npdiv_DFLT,
                      threshold=image_threshold)

        logger.info("\tusing direct search on %d processes", ncpus)

        # handle search space
        if cfg.find_orientations.use_quaternion_grid is None:
            # doing seeded search
            logger.info("Will perform seeded search")
            logger.info(
                "\tgenerating search quaternion list using %d processes",
                ncpus)
            start = timeit.default_timer()

            # need maps
            eta_ome = load_eta_ome_maps(cfg,
                                        plane_data,
                                        imsd,
                                        hkls=hkls,
                                        clean=clean)

            # generate trial orientations
            qfib = generate_orientation_fibers(cfg, eta_ome)

            logger.info("\t\t...took %f seconds",
                        timeit.default_timer() - start)
        else:
            # doing grid search
            try:
                qfib = np.load(cfg.find_orientations.use_quaternion_grid)
            except (IOError):
                raise RuntimeError(
                    "specified quaternion grid file '%s' not found!" %
                    cfg.find_orientations.use_quaternion_grid)

        # execute direct search
        pool = mp.Pool(ncpus, indexer.test_orientation_FF_init, (params, ))
        completeness = pool.map(indexer.test_orientation_FF_reduced, qfib.T)
        pool.close()
    else:
        logger.info("\tusing map search with paintGrid on %d processes", ncpus)

        start = timeit.default_timer()

        # handle eta-ome maps
        eta_ome = load_eta_ome_maps(cfg,
                                    plane_data,
                                    imsd,
                                    hkls=hkls,
                                    clean=clean)

        # handle search space
        if cfg.find_orientations.use_quaternion_grid is None:
            # doing seeded search
            logger.info(
                "\tgenerating search quaternion list using %d processes",
                ncpus)
            start = timeit.default_timer()

            qfib = generate_orientation_fibers(cfg, eta_ome)
            logger.info("\t\t...took %f seconds",
                        timeit.default_timer() - start)
        else:
            # doing grid search
            try:
                qfib = np.load(cfg.find_orientations.use_quaternion_grid)
            except (IOError):
                raise RuntimeError(
                    "specified quaternion grid file '%s' not found!" %
                    cfg.find_orientations.use_quaternion_grid)
        # do map-based indexing
        start = timeit.default_timer()

        logger.info("will test %d quaternions using %d processes",
                    qfib.shape[1], ncpus)

        completeness = indexer.paintGrid(
            qfib,
            eta_ome,
            etaRange=np.radians(cfg.find_orientations.eta.range),
            omeTol=np.radians(cfg.find_orientations.omega.tolerance),
            etaTol=np.radians(cfg.find_orientations.eta.tolerance),
            omePeriod=np.radians(cfg.find_orientations.omega.period),
            threshold=on_map_threshold,
            doMultiProc=ncpus > 1,
            nCPUs=ncpus)
        logger.info("\t\t...took %f seconds", timeit.default_timer() - start)
    completeness = np.array(completeness)

    logger.info("\tSaving %d scored orientations with max completeness %f%%",
                qfib.shape[1], 100 * np.max(completeness))

    results = {}
    results['scored_orientations'] = {
        'test_quaternions': qfib,
        'score': completeness
    }

    # =========================================================================
    # CLUSTERING AND GRAINS OUTPUT
    # =========================================================================

    logger.info("\trunning clustering using '%s'", cl_algorithm)

    start = timeit.default_timer()

    if do_grid_search:
        min_samples = 1
        mean_rpg = 1
    else:
        min_samples, mean_rpg = create_clustering_parameters(cfg, eta_ome)

    logger.info("\tmean reflections per grain: %d", mean_rpg)
    logger.info("\tneighborhood size: %d", min_samples)

    qbar, cl = run_cluster(completeness,
                           qfib,
                           plane_data.getQSym(),
                           cfg,
                           min_samples=min_samples,
                           compl_thresh=compl_thresh,
                           radius=cl_radius)

    logger.info("\t\t...took %f seconds", (timeit.default_timer() - start))
    logger.info("\tfound %d grains", qbar.shape[1])

    results['qbar'] = qbar

    return results