Beispiel #1
0
    def load_config(self, filename):
        self._cfg_file = filename
        self._cfgs = config.open(filename)

        # general
        self.analysisNameLineEdit.setText(self.cfg.analysis_name)
        self.workingDirLineEdit.setText(self.cfg.working_dir)
        self.multiprocessingSpinBox.setMaximum(multiprocessing.cpu_count())
        self.multiprocessingSpinBox.setValue(self.cfg.multiprocessing)
Beispiel #2
0
    def load_config(self, filename):
        self._cfg_file = filename
        self._cfgs = config.open(filename)

        # general
        self.analysisNameLineEdit.setText(self.cfg.analysis_name)
        self.workingDirLineEdit.setText(self.cfg.working_dir)
        self.multiprocessingSpinBox.setMaximum(multiprocessing.cpu_count())
        self.multiprocessingSpinBox.setValue(self.cfg.multiprocessing)
Beispiel #3
0
 def test_dump(self):
     self.assertEqual(self.cfgs[0].dirty, False)
     self.cfgs[0].analysis_name = 'baz'
     self.assertEqual(self.cfgs[0].dirty, True)
     with tempfile.NamedTemporaryFile(delete=False) as f:
         pass
     self.cfgs[0].dump(f.name)
     self.assertEqual(self.cfgs[0].dirty, False)
     cfg = config.open(f.name)[0]
     self.assertEqual(self.cfgs[0].analysis_name, 'baz')
Beispiel #4
0
 def test_dump(self):
     self.assertEqual(self.cfgs[0].dirty, False)
     self.cfgs[0].analysis_name = 'baz'
     self.assertEqual(self.cfgs[0].dirty, True)
     with tempfile.NamedTemporaryFile(delete=False) as f:
         pass
     self.cfgs[0].dump(f.name)
     self.assertEqual(self.cfgs[0].dirty, False)
     cfg = config.open(f.name)[0]
     self.assertEqual(self.cfgs[0].analysis_name, 'baz')
Beispiel #5
0
def execute(args, parser):
    import logging
    import os
    import sys

    import yaml

    from hexrd import config
    from hexrd.fitgrains import fit_grains


    # load the configuration settings
    cfgs = config.open(args.yml)

    # if find-orientations has not already been run, do so:
    quats_f = os.path.join(cfgs[0].working_dir, 'accepted_orientations.dat')
    if not os.path.exists(quats_f):
        from . import findorientations
        findorientations.execute(args, parser)

    # configure logging to the console:
    log_level = logging.DEBUG if args.debug else logging.INFO
    if args.quiet:
        log_level = logging.ERROR
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL if args.quiet else log_level)
    cf = logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
    ch.setFormatter(cf)
    logger.addHandler(ch)
    logger.info('=== begin fit-grains ===')

    for cfg in config.open(args.yml):
        # prepare the analysis directory
        if os.path.exists(cfg.analysis_dir) and not args.force:
            logger.error(
                'Analysis "%s" at %s already exists.'
                ' Change yml file or specify "force"',
                cfg.analysis_name, cfg.analysis_dir
                )
            sys.exit()
        if not os.path.exists(cfg.analysis_dir):
            os.makedirs(cfg.analysis_dir)

        logger.info('*** begin analysis "%s" ***', cfg.analysis_name)

        # configure logging to file for this particular analysis
        logfile = os.path.join(
            cfg.working_dir,
            cfg.analysis_name,
            'fit-grains.log'
            )
        fh = logging.FileHandler(logfile, mode='w')
        fh.setLevel(log_level)
        ff = logging.Formatter(
                '%(asctime)s - %(name)s - %(message)s',
                '%m-%d %H:%M:%S'
                )
        fh.setFormatter(ff)
        logger.info("logging to %s", logfile)
        logger.addHandler(fh)

        # process the data
        fit_grains(cfg, force=args.force, show_progress=not args.quiet)

        # stop logging for this particular analysis
        fh.flush()
        fh.close()
        logger.removeHandler(fh)

        logger.info('*** end analysis "%s" ***', cfg.analysis_name)

    logger.info('=== end fit-grains ===')
    # stop logging to the console
    ch.flush()
    ch.close()
    logger.removeHandler(ch)
Beispiel #6
0
def execute(args, parser):
    import logging
    import os
    import sys

    import yaml

    from hexrd import config
    from hexrd.fitgrains import fit_grains


    # load the configuration settings
    cfgs = config.open(args.yml)

    # configure logging to the console:
    log_level = logging.DEBUG if args.debug else logging.INFO
    if args.quiet:
        log_level = logging.ERROR
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL if args.quiet else log_level)
    cf = logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
    ch.setFormatter(cf)
    logger.addHandler(ch)

    # ...make this an attribute in cfg?
    analysis_id = '%s_%s' %(
        cfgs[0].analysis_name.strip().replace(' ', '-'),
        cfgs[0].material.active.strip().replace(' ', '-'),
        )
    
    # if find-orientations has not already been run, do so:
    quats_f = os.path.join(
        cfgs[0].working_dir, 
        'accepted_orientations_%s.dat' %analysis_id
        )
    if not os.path.exists(quats_f):
        logger.info("Missing %s, running find-orientations", quats_f)
        logger.removeHandler(ch)
        from . import findorientations
        findorientations.execute(args, parser)
        logger.addHandler(ch)

    logger.info('=== begin fit-grains ===')

    clobber = args.force or args.clean
    for cfg in cfgs:
        # prepare the analysis directory
        if os.path.exists(cfg.analysis_dir) and not clobber:
            logger.error(
                'Analysis "%s" at %s already exists.'
                ' Change yml file or specify "force"',
                cfg.analysis_name, cfg.analysis_dir
                )
            sys.exit()
        if not os.path.exists(cfg.analysis_dir):
            os.makedirs(cfg.analysis_dir)

        logger.info('*** begin analysis "%s" ***', cfg.analysis_name)

        # configure logging to file for this particular analysis
        logfile = os.path.join(
            cfg.working_dir,
            cfg.analysis_name,
            'fit-grains.log'
            )
        fh = logging.FileHandler(logfile, mode='w')
        fh.setLevel(log_level)
        ff = logging.Formatter(
                '%(asctime)s - %(name)s - %(message)s',
                '%m-%d %H:%M:%S'
                )
        fh.setFormatter(ff)
        logger.info("logging to %s", logfile)
        logger.addHandler(fh)

        if args.profile:
            import cProfile as profile, pstats, StringIO
            pr = profile.Profile()
            pr.enable()

        # process the data
        if args.grains is not None:
            args.grains = [int(i) for i in args.grains.split(',')]
        fit_grains(
            cfg,
            force=args.force,
            clean=args.clean,
            show_progress=not args.quiet,
            ids_to_refine=args.grains,
            )

        if args.profile:
            pr.disable()
            s = StringIO.StringIO()
            ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
            ps.print_stats(50)
            logger.info('%s', s.getvalue())

        # stop logging for this particular analysis
        fh.flush()
        fh.close()
        logger.removeHandler(fh)

        logger.info('*** end analysis "%s" ***', cfg.analysis_name)

    logger.info('=== end fit-grains ===')
    # stop logging to the console
    ch.flush()
    ch.close()
    logger.removeHandler(ch)
Beispiel #7
0
    parser.add_argument('-s', '--pixel-size',
                        help="pixel size for rendering",
                        type=float,
                        default=0.5)

    args = parser.parse_args()

    experiment_cfg = args.experiment_cfg
    imageseries_file = args.imageseries_file
    slider_delta = args.slider_delta
    plane_distance = args.plane_distance
    tth_max = args.tth_max
    pixel_size = args.pixel_size

    # load instrument and imageseries
    cfg = config.open('example_config.yml')[0]
    instr = cfg.instrument.hedm
    ims = load_images(imageseries_file)
    materials_file = cfg.material.definitions
    materials_key = cfg.material.active

    # load plane data
    if np.isnan(tth_max):
        tth_max = None
    pdata = load_pdata(materials_file, materials_key, tth_max=tth_max)

    tvec = np.r_[0., 0., -plane_distance]

    iv = IView(instr, ims, pdata, tvec=tvec,
               slider_delta=slider_delta, pixel_size=pixel_size)
Beispiel #8
0
def execute(args, parser):
    import logging
    import os
    import sys

    import yaml

    from hexrd import config
    from hexrd.findorientations import find_orientations


    # make sure hkls are passed in as a list of ints
    try:
        if args.hkls is not None:
            args.hkls = [int(i) for i in args.hkls.split(',') if i]
    except AttributeError:
        # called from fit-grains, hkls not passed
        args.hkls = None

    # configure logging to the console
    log_level = logging.DEBUG if args.debug else logging.INFO
    if args.quiet:
        log_level = logging.ERROR
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL if args.quiet else log_level)
    ch.setFormatter(
        logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
        )
    logger.addHandler(ch)
    logger.info('=== begin find-orientations ===')

    # load the configuration settings
    cfg = config.open(args.yml)[0]

    # prepare the analysis directory
    quats_f = os.path.join(cfg.working_dir, 'accepted_orientations.dat')
    if os.path.exists(quats_f) and not (args.force or args.clean):
        logger.error(
            '%s already exists. Change yml file or specify "force"', quats_f
            )
        sys.exit()
    if not os.path.exists(cfg.working_dir):
        os.makedirs(cfg.working_dir)

    # configure logging to file
    logfile = os.path.join(
        cfg.working_dir,
        'find-orientations.log'
        )
    fh = logging.FileHandler(logfile, mode='w')
    fh.setLevel(log_level)
    fh.setFormatter(
        logging.Formatter(
            '%(asctime)s - %(name)s - %(message)s',
            '%m-%d %H:%M:%S'
            )
        )
    logger.info("logging to %s", logfile)
    logger.addHandler(fh)

    if args.profile:
        import cProfile as profile, pstats, StringIO
        pr = profile.Profile()
        pr.enable()

    # process the data
    find_orientations(cfg, hkls=args.hkls, clean=args.clean, profile=args.profile)

    if args.profile:
        pr.disable()
        s = StringIO.StringIO()
        ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
        ps.print_stats(50)
        logger.info('%s', s.getvalue())

    # clean up the logging
    fh.flush()
    fh.close()
    logger.removeHandler(fh)
    logger.info('=== end find-orientations ===')
    ch.flush()
    ch.close()
    logger.removeHandler(ch)
Beispiel #9
0
def make_dark_frame(cfg_name, nframes_use=100, nbytes_header=8192, pixel_type=np.uint16, quiet=False, output_name=None):
    """
    FOR BYTE STREAM IMAGES ONLY!

    nbytes_header and pixel_bytes default for raw GE
    """
    
    cfg = config.open(cfg_name)[0] # only first block...
    raw_cfg = [g for g in yaml.load_all(open(cfg_name, 'r'))] # take first block... maybe iterate?

    try:
        output_name = cfg.image_series.dark
    except IOError:
        if raw_cfg[0]['image_series'].has_key('dark') and output_name is None:
            output_name = raw_cfg[0]['image_series']['dark']

    if not os.path.exists(output_name):
        # create if necessary    
        open(output_name, 'w+').close()

    n_empty = cfg.image_series.images.start

    image_numbers = cfg.image_series.file.ids
    if isinstance(image_numbers[0], str): # then globbing
        filenames = glob.glob(cfg.image_series.file.stem %cfg.image_series.file.ids[0])
    else:
        filenames = [cfg.image_series.file.stem %i for i in cfg.image_series.file.ids]
    n_images = len(filenames)
    
    instr_cfg = get_instrument_parameters(cfg)

    nrows = instr_cfg['detector']['pixels']['rows']
    ncols = instr_cfg['detector']['pixels']['columns']

    pixel_bytes = pixel_type().itemsize
    nbytes_frame = pixel_bytes*nrows*ncols

    filename = filenames[0]
    file_bytes = os.stat(filename).st_size
    n_frames = getNFramesFromBytes(file_bytes, nbytes_header, nbytes_frame)
    n_frames -= n_empty
    n_frames_total = n_frames*n_images
    if nframes_use > n_frames_total:
        if not quiet:
            print "Requested %d frames, which is in image spec; defaulting to %d" %(nframes_use, n_frames_total)
        nframes_use = n_frames_total
    
    ii = 0
    jj = min(nframes_use, n_frames)

    n_frames_cum = 0
    n_frames_rem = nframes_use

    frames = np.empty((nframes_use, nrows, ncols), dtype=pixel_type)
    for i_frame in range(len(image_numbers)):
        filename = filenames[i_frame]

        if not quiet:
            print "Using %d frames from '%s'" %(min(n_frames, nframes_use, n_frames_rem), filename)

        nfr = jj - ii
        fid = open(filename, 'rb')
        fid.seek(nbytes_header+n_empty*nbytes_frame, 0)     # header plus junk frames
        tmp = np.frombuffer(fid.read(nfr*nbytes_frame), dtype=pixel_type).reshape(nfr, nrows, ncols)
        fid.close()
        
        # index into frames array
        frames[ii:jj] = tmp

        # increment...
        n_frames_rem -= nfr
        if n_frames_rem <= 0:
            break
        else:
            ii = jj
            jj += min(n_frames_rem, n_frames)
            if not quiet:
                print "\tframes left: %d" %n_frames_rem
        pass
    
    dark = np.median(frames, axis=0)

    if not quiet:
        print "Output file: %s" %output_name

    fid = open(cfg.image_series.dark, 'wb')
    fid.seek(nbytes_header)
    fid.write(dark.astype(pixel_type))
    fid.close()
    return dark.astype(np.uint16)
Beispiel #10
0
        sys.exit(1)
    elif sys.argv[1] == '-h' or sys.argv[1] == '--help':
        print 'USAGE: python spot_maxima_finder.py config.yml'
        sys.exit(1)
    else:
        cfg_file = sys.argv[1]

    # Setup logger
    log_level = logging.DEBUG
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(log_level)
    cf = logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
    ch.setFormatter(cf)
    logger.addHandler(ch)
    # load the configuration settings
    cfgs = config.open(cfg_file)

    # cfg is a list. We will loop over each cfg set.
    for cfg in cfgs:
        # Initialize the GE pre-processor
        gepp = GEPreProcessor(cfg=cfg, logger=logger)
        # Start analysis
        logger.info('=== begin image-smoothing ===')
        # Load the GE2 data
        gepp.load_data()
        # ID blobs and local maxima, and optionally print a text file with local maxima positions,
        # PNG files and GE2 file with cleaned-up data.
        gepp.find_blobs()
            idx = refit_idx[det_key]
            idx_0[det_key] = idx
            print("INFO: input reflection specify " +
                  "%d of %d total valid reflections" % (sum(idx), len(gtable)))

        hkls[det_key] = gtable[idx, 2:5]
        meas_omes = gtable[idx, 12].reshape(sum(idx), 1)
        xyo_det[det_key] = np.hstack([gtable[idx, -2:], meas_omes])
        pass
    return hkls, xyo_det, idx_0


# %% Initialization...

# read config
cfg = config.open(cfg_filename)[block_id]

# output for eta-ome maps as pickles
working_dir = cfg.working_dir
analysis_name = cfg.analysis_name
analysis_dir = cfg.analysis_dir
analysis_id = "%s_%s" % (analysis_name, cfg.material.active)

# instrument
parfilename = cfg.instrument.parameters
instr = load_instrument(parfilename)
num_panels = instr.num_panels
det_keys = instr.detectors.keys()

# plane data
plane_data = load_plane_data(cfg.material.definitions, cfg.material.active)
Beispiel #12
0
def execute(args, parser):
    import logging
    import os
    import sys

    import yaml

    from hexrd import config


    # load the configuration settings
    cfgs = config.open(args.yml)

    # configure logging to the console:
    log_level = logging.DEBUG if args.debug else logging.INFO
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(log_level)
    cf = logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
    ch.setFormatter(cf)
    logger.addHandler(ch)

    logger.info('=== begin cake-data ===')

    for cfg in cfgs:
        logger.info('*** begin caking for analysis "%s" ***', cfg.analysis_name)

        # configure logging to file for this particular analysis
        logfile = os.path.join(
            cfg.working_dir,
            'cake-data.log'
            )
        fh = logging.FileHandler(logfile, mode='w')
        fh.setLevel(log_level)
        ff = logging.Formatter(
                '%(asctime)s - %(name)s - %(message)s',
                '%m-%d %H:%M:%S'
                )
        fh.setFormatter(ff)
        logger.info("logging to %s", logfile)
        logger.addHandler(fh)

        # process the data
        pd, reader, detector = initialize_experiment(cfg)
        # Load frames, generate "max over all" frame and bin the data
        show_progress = True
        reader = process_cake(reader, detector, cfg, show_progress)

        # stop logging for this particular analysis
        fh.flush()
        fh.close()
        logger.removeHandler(fh)

        logger.info('*** end caking for analysis "%s" ***', cfg.analysis_name)

    logger.info('=== end cake-data ===')
    # stop logging to the console
    ch.flush()
    ch.close()
    logger.removeHandler(ch)
Beispiel #13
0
def check_indexing_plots(cfg_filename,
                         plot_trials=False,
                         plot_from_grains=False):
    cfg = config.open(cfg_filename)[0]  # use first block, like indexing

    working_dir = cfg.working_dir
    analysis_dir = os.path.join(working_dir, cfg.analysis_name)

    #instrument parameters
    icfg = get_instrument_parameters(cfg)
    chi = icfg['oscillation_stage']['chi']

    # load maps that were used
    oem = cPickle.load(open(cfg.find_orientations.orientation_maps.file, 'r'))
    nmaps = len(oem.dataStore)
    omeEdges = np.degrees(oem.omeEdges)
    nome = len(omeEdges) - 1
    etaEdges = np.degrees(oem.etaEdges)
    neta = len(etaEdges) - 1
    delta_ome = abs(omeEdges[1] - omeEdges[0])

    full_ome_range = xf.angularDifference(omeEdges[0], omeEdges[-1]) == 0
    full_eta_range = xf.angularDifference(etaEdges[0], etaEdges[-1]) == 0

    # grab plane data and figure out IDs of map HKLS
    pd = oem.planeData
    gvids = [
        pd.hklDataList[i]['hklID']
        for i in np.where(pd.exclusions == False)[0].tolist()
    ]

    # load orientations
    quats = np.atleast_2d(
        np.loadtxt(os.path.join(working_dir, 'accepted_orientations.dat')))
    if plot_trials:
        scored_trials = np.load(
            os.path.join(working_dir, 'scored_orientations.dat'))
        quats = scored_orientations[:4, scored_orientations[
            -1, :] >= cfg.find_orientations.clustering.completeness]
        pass
    expMaps = np.tile(2. * np.arccos(quats[:, 0]),
                      (3, 1)) * unitVector(quats[:, 1:].T)

    ##########################################
    #      SPECIAL CASE FOR FIT GRAINS       #
    ##########################################
    if plot_from_grains:
        distortion = (GE_41RT, icfg['detector']['distortion']['parameters'])
        #
        grain_table = np.atleast_2d(
            np.loadtxt(os.path.join(analysis_dir, 'grains.out')))
        ngrains = len(grain_table)
        #
        expMaps = grain_table[:, 3:6]
        tVec_c = grain_table[:, 6:9]
        vInv = grain_table[:, 6:12]
        #
        rMat_d = xf.makeDetectorRotMat(
            icfg['detector']['transform']['tilt_angles'])
        tVec_d = np.vstack(icfg['detector']['transform']['t_vec_d'])
        #
        chi = icfg['oscillation_stage']['chi']
        tVec_s = np.vstack(icfg['oscillation_stage']['t_vec_s'])
        #
        oes = np.zeros(oem.dataStore.shape)
        for i_grn in range(ngrains):
            spots_table = np.loadtxt(
                os.path.join(analysis_dir, 'spots_%05d.out' % i_grn))
            idx_m = spots_table[:, 0] >= 0
            for i_map in range(nmaps):
                idx_g = spots_table[:, 1] == gvids[i_map]
                idx = np.logical_and(idx_m, idx_g)
                nrefl = sum(idx)

                omes_fit = xf.mapAngle(spots_table[idx, 9],
                                       np.radians(
                                           cfg.find_orientations.omega.period),
                                       units='radians')
                xy_det = spots_table[idx, -3:]
                xy_det[:, 2] = np.zeros(nrefl)

                rMat_s_array = xfcapi.makeOscillRotMatArray(chi, omes_fit)

                # form in-plane vectors for detector points list in DETECTOR FRAME
                P2_d = xy_det.T

                # in LAB FRAME
                P2_l = np.dot(rMat_d, P2_d) + tVec_d  # point on detector
                P0_l = np.hstack([
                    tVec_s +
                    np.dot(rMat_s_array[j], tVec_c[i_grn, :].reshape(3, 1))
                    for j in range(nrefl)
                ])  # origin of CRYSTAL FRAME

                # diffraction unit vector components in LAB FRAME
                dHat_l = unitVector(P2_l - P0_l)
                P2_l = np.dot(rMat_d, xy_det.T) + tVec_d

                # angles for reference frame
                dHat_ref_l = unitVector(P2_l)

                # append etas and omes
                etas_fit = np.arctan2(dHat_ref_l[1, :],
                                      dHat_ref_l[0, :]).flatten()

                # find indices, then truncate or wrap
                i_ome = cellIndices(oem.omeEdges, omes_fit)
                if full_ome_range:
                    i_ome[i_ome < 0] = np.mod(i_ome, nome) + 1
                    i_ome[i_ome >= nome] = np.mod(i_ome, nome)
                else:
                    incl = np.logical_or(i_ome >= 0, i_ome < nome)
                    i_ome = i_ome[incl]
                j_eta = cellIndices(oem.etaEdges, etas_fit)
                if full_eta_range:
                    j_eta[j_eta < 0] = np.mod(j_eta, neta) + 1
                    j_eta[j_eta >= neta] = np.mod(j_eta, neta)
                else:
                    incl = np.logical_or(j_eta >= 0, j_eta < neta)
                    j_eta = j_eta[incl]

                #if np.max(i_ome) >= nome or np.min(i_ome) < 0 or np.max(j_eta) >= neta or np.min(j_eta) < 0:
                #    import pdb; pdb.set_trace()
                # add to map
                oes[i_map][i_ome, j_eta] = 1
            pass
        pass

    # simulate quaternion points
    if not plot_from_grains:
        oes = simulateOmeEtaMaps(omeEdges,
                                 etaEdges,
                                 pd,
                                 expMaps,
                                 chi=chi,
                                 etaTol=0.01,
                                 omeTol=0.01,
                                 etaRanges=None,
                                 omeRanges=None,
                                 bVec=xf.bVec_ref,
                                 eVec=xf.eta_ref,
                                 vInv=xf.vInv_ref)

    # tick labling
    omes = np.degrees(oem.omeEdges)
    etas = np.degrees(oem.etaEdges)
    num_ticks = 7
    xmin = np.amin(etas)
    xmax = np.amax(etas)
    dx = (xmax - xmin) / (num_ticks - 1.)
    dx1 = (len(etas) - 1) / (num_ticks - 1.)
    xtlab = ["%.0f" % (xmin + i * dx) for i in range(num_ticks)]
    xtloc = np.array([i * dx1 for i in range(num_ticks)]) - 0.5
    ymin = np.amin(omes)
    ymax = np.amax(omes)
    dy = (ymax - ymin) / (num_ticks - 1.)
    dy1 = (len(omes) - 1) / (num_ticks - 1.)
    ytlab = ["%.0f" % (ymin + i * dy) for i in range(num_ticks)]
    ytloc = np.array([i * dy1 for i in range(num_ticks)]) - 0.5

    # Plot the three kernel density estimates
    n_maps = len(oem.iHKLList)

    fig_list = [plt.figure(num=i + 1) for i in range(n_maps)]
    ax_list = [fig_list[i].gca() for i in range(n_maps)]
    for i_map in range(n_maps):
        y, x = np.where(oes[i_map] > 0)
        ax_list[i_map].hold(True)
        ax_list[i_map].imshow(oem.dataStore[i_map] > 0.1, cmap=cm.bone)
        ax_list[i_map].set_title(r'Map for $\{%d %d %d\}$' %
                                 tuple(pd.hkls[:, i_map]))
        ax_list[i_map].set_xlabel(
            r'Azimuth channel, $\eta$; $\Delta\eta=%.3f$' % delta_ome)
        ax_list[i_map].set_ylabel(
            r'Rotation channel, $\omega$; $\Delta\omega=%.3f$' % delta_ome)
        ax_list[i_map].plot(x, y, 'c+')
        ax_list[i_map].xaxis.set_ticks(xtloc)
        ax_list[i_map].xaxis.set_ticklabels(xtlab)
        ax_list[i_map].yaxis.set_ticks(ytloc)
        ax_list[i_map].yaxis.set_ticklabels(ytlab)
        ax_list[i_map].axis('tight')
    plt.show()
    return fig_list, oes
Beispiel #14
0
def execute(args, parser):
    # load the configuration settings
    cfgs = config.open(args.yml)

    # configure logging to the console:
    log_level = logging.DEBUG if args.debug else logging.INFO
    if args.quiet:
        log_level = logging.ERROR
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL if args.quiet else log_level)
    cf = logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
    ch.setFormatter(cf)
    logger.addHandler(ch)

    # if find-orientations has not already been run, do so:
    quats_f = os.path.join(
        cfgs[0].working_dir,
        'accepted_orientations_%s.dat' % cfgs[0].analysis_id)
    if os.path.exists(quats_f):
        try:
            qbar = np.loadtxt(quats_f).T
        except (IOError):
            raise (RuntimeError,
                   "error loading indexing results '%s'" % quats_f)
    else:
        logger.info("Missing %s, running find-orientations", quats_f)
        logger.removeHandler(ch)
        from hexrd.findorientations import find_orientations
        results = find_orientations(cfgs[0])
        qbar = results['qbar']
        logger.addHandler(ch)

    logger.info('=== begin fit-grains ===')

    clobber = args.force or args.clean
    for cfg in cfgs:
        # prepare the analysis directory
        if os.path.exists(cfg.analysis_dir) and not clobber:
            logger.error(
                'Analysis "%s" at %s already exists.'
                ' Change yml file or specify "force"', cfg.analysis_name,
                cfg.analysis_dir)
            sys.exit()

        # make output directories
        instr = cfg.instrument.hedm
        if not os.path.exists(cfg.analysis_dir):
            os.makedirs(cfg.analysis_dir)
            for det_key in instr.detectors:
                os.mkdir(os.path.join(cfg.analysis_dir, det_key))
        else:
            # make sure panel dirs exist under analysis dir
            for det_key in instr.detectors:
                if not os.path.exists(os.path.join(cfg.analysis_dir, det_key)):
                    os.mkdir(os.path.join(cfg.analysis_dir, det_key))

        logger.info('*** begin analysis "%s" ***', cfg.analysis_name)

        # configure logging to file for this particular analysis
        logfile = os.path.join(cfg.working_dir, cfg.analysis_name,
                               'fit-grains.log')
        fh = logging.FileHandler(logfile, mode='w')
        fh.setLevel(log_level)
        ff = logging.Formatter('%(asctime)s - %(name)s - %(message)s',
                               '%m-%d %H:%M:%S')
        fh.setFormatter(ff)
        logger.info("logging to %s", logfile)
        logger.addHandler(fh)

        if args.profile:
            import cProfile as profile
            import pstats
            from io import StringIO

            pr = profile.Profile()
            pr.enable()

        grains_filename = os.path.join(cfg.analysis_dir, 'grains.out')

        # some conditions for arg handling
        existing_analysis = os.path.exists(grains_filename)
        new_with_estimate = not existing_analysis \
            and cfg.fit_grains.estimate is not None
        new_without_estimate = not existing_analysis \
            and cfg.fit_grains.estimate is None
        force_with_estimate = args.force \
            and cfg.fit_grains.estimate is not None
        force_without_estimate = args.force and cfg.fit_grains.estimate is None

        # handle args
        if args.clean or force_without_estimate or new_without_estimate:
            # need accepted orientations from indexing in this case
            if args.clean:
                logger.info(
                    "'clean' specified; ignoring estimate and using default")
            elif force_without_estimate:
                logger.info(
                    "'force' option specified, but no initial estimate; " +
                    "using default")
            try:
                gw = instrument.GrainDataWriter(grains_filename)
                for i_g, q in enumerate(qbar.T):
                    phi = 2 * np.arccos(q[0])
                    n = xfcapi.unitRowVector(q[1:])
                    grain_params = np.hstack(
                        [phi * n, cnst.zeros_3, cnst.identity_6x1])
                    gw.dump_grain(int(i_g), 1., 0., grain_params)
                gw.close()
            except (IOError):
                raise (RuntimeError, "indexing results '%s' not found!" %
                       'accepted_orientations_' + cfg.analysis_id + '.dat')
        elif force_with_estimate or new_with_estimate:
            grains_filename = cfg.fit_grains.estimate
        elif existing_analysis and not (clean or force):
            raise (RuntimeError, "fit results '%s' exist, " % grains_filename +
                   "but --clean or --force options not specified")

        grains_table = np.loadtxt(grains_filename, ndmin=2)

        # process the data
        gid_list = None
        if args.grains is not None:
            gid_list = [int(i) for i in args.grains.split(',')]
            pass

        cfg.fit_grains.qbar = qbar
        fit_results = fit_grains(
            cfg,
            grains_table,
            show_progress=not args.quiet,
            ids_to_refine=gid_list,
        )

        if args.profile:
            pr.disable()
            s = StringIO.StringIO()
            ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
            ps.print_stats(50)
            logger.info('%s', s.getvalue())

        # stop logging for this particular analysis
        fh.flush()
        fh.close()
        logger.removeHandler(fh)

        logger.info('*** end analysis "%s" ***', cfg.analysis_name)

        write_results(fit_results, cfg, grains_filename)

    logger.info('=== end fit-grains ===')
    # stop logging to the console
    ch.flush()
    ch.close()
    logger.removeHandler(ch)
Beispiel #15
0
def check_indexing_plots(cfg_filename, plot_trials=False, plot_from_grains=False):
    cfg = config.open(cfg_filename)[0]    # use first block, like indexing

    working_dir = cfg.working_dir
    analysis_dir = os.path.join(working_dir, cfg.analysis_name)
    
    #instrument parameters
    icfg = get_instrument_parameters(cfg)
    chi = icfg['oscillation_stage']['chi']

    # load maps that were used
    oem = cPickle.load(
        open(cfg.find_orientations.orientation_maps.file, 'r')
        )
    nmaps = len(oem.dataStore)
    omeEdges = np.degrees(oem.omeEdges); nome = len(omeEdges) - 1
    etaEdges = np.degrees(oem.etaEdges); neta = len(etaEdges) - 1
    delta_ome = abs(omeEdges[1]-omeEdges[0])

    full_ome_range = xf.angularDifference(omeEdges[0], omeEdges[-1]) == 0
    full_eta_range = xf.angularDifference(etaEdges[0], etaEdges[-1]) == 0
    
    # grab plane data and figure out IDs of map HKLS
    pd = oem.planeData
    gvids = [pd.hklDataList[i]['hklID'] for i in np.where(pd.exclusions == False)[0].tolist()]

    # load orientations
    quats = np.atleast_2d(np.loadtxt(os.path.join(working_dir, 'accepted_orientations.dat')))
    if plot_trials:
        scored_trials = np.load(os.path.join(working_dir, 'scored_orientations.dat'))
        quats = scored_orientations[:4, scored_orientations[-1, :] >= cfg.find_orientations.clustering.completeness]
        pass
    expMaps = np.tile(2. * np.arccos(quats[:, 0]), (3, 1))*unitVector(quats[:, 1:].T)

    ##########################################
    #      SPECIAL CASE FOR FIT GRAINS       #
    ##########################################
    if plot_from_grains:
        distortion = (GE_41RT, icfg['detector']['distortion']['parameters'])
        #
        grain_table = np.atleast_2d(np.loadtxt(os.path.join(analysis_dir, 'grains.out')))
        ngrains = len(grain_table)
        #
        expMaps = grain_table[:, 3:6]
        tVec_c = grain_table[:, 6:9]
        vInv = grain_table[:, 6:12]
        #
        rMat_d = xf.makeDetectorRotMat(icfg['detector']['transform']['tilt_angles'])
        tVec_d = np.vstack(icfg['detector']['transform']['t_vec_d'])
        #
        chi = icfg['oscillation_stage']['chi']
        tVec_s = np.vstack(icfg['oscillation_stage']['t_vec_s'])
        #
        oes = np.zeros(oem.dataStore.shape)
        for i_grn in range(ngrains):
            spots_table = np.loadtxt(os.path.join(analysis_dir, 'spots_%05d.out' %i_grn))
            idx_m = spots_table[:, 0] >= 0
            for i_map in range(nmaps):
                idx_g = spots_table[:, 1] == gvids[i_map]
                idx = np.logical_and(idx_m, idx_g)
                nrefl = sum(idx)
                
                omes_fit = xf.mapAngle(spots_table[idx, 9], np.radians(cfg.find_orientations.omega.period), units='radians')
                xy_det = spots_table[idx, -3:]
                xy_det[:, 2] = np.zeros(nrefl)
                
                rMat_s_array = xfcapi.makeOscillRotMatArray(chi, omes_fit)
                
                # form in-plane vectors for detector points list in DETECTOR FRAME
                P2_d = xy_det.T
                
                # in LAB FRAME
                P2_l = np.dot(rMat_d, P2_d) + tVec_d # point on detector
                P0_l = np.hstack(
                    [tVec_s + np.dot(rMat_s_array[j], tVec_c[i_grn, :].reshape(3, 1)) for j in range(nrefl)]
                ) # origin of CRYSTAL FRAME

                # diffraction unit vector components in LAB FRAME
                dHat_l = unitVector(P2_l - P0_l)
                P2_l = np.dot(rMat_d, xy_det.T) + tVec_d
                
                # angles for reference frame
                dHat_ref_l = unitVector(P2_l)
    
                # append etas and omes
                etas_fit = np.arctan2(dHat_ref_l[1, :], dHat_ref_l[0, :]).flatten()
           
                # find indices, then truncate or wrap
                i_ome = cellIndices(oem.omeEdges, omes_fit)
                if full_ome_range:
                    i_ome[i_ome < 0] = np.mod(i_ome, nome) + 1
                    i_ome[i_ome >= nome] = np.mod(i_ome, nome)
                else:
                    incl = np.logical_or(i_ome >= 0, i_ome < nome)
                    i_ome = i_ome[incl]
                j_eta = cellIndices(oem.etaEdges, etas_fit)
                if full_eta_range:
                    j_eta[j_eta < 0] = np.mod(j_eta, neta) + 1
                    j_eta[j_eta >= neta] = np.mod(j_eta, neta)
                else:
                    incl = np.logical_or(j_eta >= 0, j_eta < neta)
                    j_eta = j_eta[incl]

                #if np.max(i_ome) >= nome or np.min(i_ome) < 0 or np.max(j_eta) >= neta or np.min(j_eta) < 0:
                #    import pdb; pdb.set_trace()
                # add to map
                oes[i_map][i_ome, j_eta] = 1
            pass
        pass
    
    # simulate quaternion points
    if not plot_from_grains:
        oes = simulateOmeEtaMaps(omeEdges, etaEdges, pd,
                                 expMaps,
                                 chi=chi,
                                 etaTol=0.01, omeTol=0.01,
                                 etaRanges=None, omeRanges=None,
                                 bVec=xf.bVec_ref, eVec=xf.eta_ref, vInv=xf.vInv_ref)
    
    # tick labling
    omes = np.degrees(oem.omeEdges)
    etas = np.degrees(oem.etaEdges)
    num_ticks = 7
    xmin = np.amin(etas); xmax = np.amax(etas)
    dx = (xmax - xmin) / (num_ticks - 1.); dx1 = (len(etas) - 1) / (num_ticks - 1.)
    xtlab = ["%.0f" % (xmin + i*dx) for i in range(num_ticks)]
    xtloc = np.array([i*dx1 for i in range(num_ticks)]) - 0.5
    ymin = np.amin(omes); ymax = np.amax(omes)
    dy = (ymax - ymin) / (num_ticks - 1.); dy1 = (len(omes) - 1) / (num_ticks - 1.)
    ytlab = ["%.0f" % (ymin + i*dy) for i in range(num_ticks)]
    ytloc = np.array([i*dy1 for i in range(num_ticks)]) - 0.5
    
    # Plot the three kernel density estimates
    n_maps = len(oem.iHKLList)
    
    fig_list =[plt.figure(num=i+1) for i in range(n_maps)]
    ax_list = [fig_list[i].gca() for i in range(n_maps)]
    for i_map in range(n_maps):
        y, x = np.where(oes[i_map] > 0)
        ax_list[i_map].hold(True)
        ax_list[i_map].imshow(oem.dataStore[i_map] > 0.1, cmap=cm.bone)
        ax_list[i_map].set_title(r'Map for $\{%d %d %d\}$' %tuple(pd.hkls[:, i_map]))
        ax_list[i_map].set_xlabel(r'Azimuth channel, $\eta$; $\Delta\eta=%.3f$' %delta_ome)
        ax_list[i_map].set_ylabel(r'Rotation channel, $\omega$; $\Delta\omega=%.3f$' %delta_ome)
        ax_list[i_map].plot(x, y, 'c+')
        ax_list[i_map].xaxis.set_ticks(xtloc)
        ax_list[i_map].xaxis.set_ticklabels(xtlab)
        ax_list[i_map].yaxis.set_ticks(ytloc)
        ax_list[i_map].yaxis.set_ticklabels(ytlab)
        ax_list[i_map].axis('tight')
    plt.show()
    return fig_list, oes
Beispiel #16
0
                        '--pixel-size',
                        help="pixel size for rendering",
                        type=float,
                        default=0.5)

    args = parser.parse_args()

    experiment_cfg = args.experiment_cfg
    imageseries_file = args.imageseries_file
    slider_delta = args.slider_delta
    plane_distance = args.plane_distance
    tth_max = args.tth_max
    pixel_size = args.pixel_size

    # load instrument and imageseries
    cfg = config.open('example_config.yml')[0]
    instr = cfg.instrument.hedm
    ims = load_images(imageseries_file)
    materials_file = cfg.material.definitions
    materials_key = cfg.material.active

    # load plane data
    if np.isnan(tth_max):
        tth_max = None
    pdata = load_pdata(materials_file, materials_key, tth_max=tth_max)

    tvec = np.r_[0., 0., -plane_distance]

    iv = IView(instr,
               ims,
               pdata,
import yaml

from hexrd import config

from hexrd.xrd import distortion as dFuncs
from hexrd.xrd import transforms_CAPI as xfcapi


from hexrd.gridutil import cellIndices


from hexrd.xrd.xrdutil import simulateGVecs

#%% File Loading
cfg = config.open(cfg_file)[0] # NOTE: always a list of cfg objects

instr_cfg = yaml.load(open(instr_file, 'r'))

mat_list = cpl.load(open(matl_file, 'r'))

grain_params_list=np.loadtxt(grains_file)

#%% Extract Quantities from Loaded data

#Instrument Info
# stack into array for later
detector_params = np.hstack([
    instr_cfg['detector']['transform']['tilt_angles'],
    instr_cfg['detector']['transform']['t_vec_d'],
    instr_cfg['oscillation_stage']['chi'],
Beispiel #18
0
def execute(args, parser):
    import logging
    import os
    import sys

    import yaml

    from hexrd import config
    from hexrd.fitgrains import fit_grains

    # load the configuration settings
    cfgs = config.open(args.yml)

    # configure logging to the console:
    log_level = logging.DEBUG if args.debug else logging.INFO
    if args.quiet:
        log_level = logging.ERROR
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL if args.quiet else log_level)
    cf = logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
    ch.setFormatter(cf)
    logger.addHandler(ch)

    # ...make this an attribute in cfg?
    analysis_id = '%s_%s' % (
        cfgs[0].analysis_name.strip().replace(' ', '-'),
        cfgs[0].material.active.strip().replace(' ', '-'),
    )

    # if find-orientations has not already been run, do so:
    quats_f = os.path.join(cfgs[0].working_dir,
                           'accepted_orientations_%s.dat' % analysis_id)
    if not os.path.exists(quats_f):
        logger.info("Missing %s, running find-orientations", quats_f)
        logger.removeHandler(ch)
        from . import findorientations
        findorientations.execute(args, parser)
        logger.addHandler(ch)

    logger.info('=== begin fit-grains ===')

    clobber = args.force or args.clean
    for cfg in cfgs:
        # prepare the analysis directory
        if os.path.exists(cfg.analysis_dir) and not clobber:
            logger.error(
                'Analysis "%s" at %s already exists.'
                ' Change yml file or specify "force"', cfg.analysis_name,
                cfg.analysis_dir)
            sys.exit()
        if not os.path.exists(cfg.analysis_dir):
            os.makedirs(cfg.analysis_dir)

        logger.info('*** begin analysis "%s" ***', cfg.analysis_name)

        # configure logging to file for this particular analysis
        logfile = os.path.join(cfg.working_dir, cfg.analysis_name,
                               'fit-grains.log')
        fh = logging.FileHandler(logfile, mode='w')
        fh.setLevel(log_level)
        ff = logging.Formatter('%(asctime)s - %(name)s - %(message)s',
                               '%m-%d %H:%M:%S')
        fh.setFormatter(ff)
        logger.info("logging to %s", logfile)
        logger.addHandler(fh)

        if args.profile:
            import cProfile as profile, pstats, StringIO
            pr = profile.Profile()
            pr.enable()

        # process the data
        gid_list = None
        if args.grains is not None:
            gid_list = [int(i) for i in args.grains.split(',')]

        fit_grains(
            cfg,
            force=args.force,
            clean=args.clean,
            show_progress=not args.quiet,
            ids_to_refine=gid_list,
        )

        if args.profile:
            pr.disable()
            s = StringIO.StringIO()
            ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
            ps.print_stats(50)
            logger.info('%s', s.getvalue())

        # stop logging for this particular analysis
        fh.flush()
        fh.close()
        logger.removeHandler(fh)

        logger.info('*** end analysis "%s" ***', cfg.analysis_name)

    logger.info('=== end fit-grains ===')
    # stop logging to the console
    ch.flush()
    ch.close()
    logger.removeHandler(ch)
Beispiel #19
0
def execute(args, parser):
    import logging
    import os
    import sys

    import yaml

    from hexrd import config
    from hexrd.actions.find_orientations import find_orientations


    # make sure hkls are passed in as a list of ints
    try:
        if args.hkls is not None:
            args.hkls = [int(i) for i in args.hkls.split(',') if i]
    except AttributeError:
        # called from fit-grains, hkls not passed
        args.hkls = None

    # configure logging to the console
    log_level = logging.DEBUG if args.debug else logging.INFO
    if args.quiet:
        log_level = logging.ERROR
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL if args.quiet else log_level)
    ch.setFormatter(
        logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
        )
    logger.addHandler(ch)
    logger.info('=== begin find-orientations ===')

    # load the configuration settings
    cfg = config.open(args.yml)[0]

    # ...make this an attribute in cfg?
    analysis_id = '%s_%s' %(
        cfg.analysis_name.strip().replace(' ', '-'),
        cfg.material.active.strip().replace(' ', '-'),
        )

    # prepare the analysis directory
    quats_f = os.path.join(
        cfg.working_dir,
        'accepted_orientations_%s.dat' %analysis_id
        )
    if os.path.exists(quats_f) and not (args.force or args.clean):
        logger.error(
            '%s already exists. Change yml file or specify "force" or "clean"', quats_f
            )
        sys.exit()
    if not os.path.exists(cfg.working_dir):
        os.makedirs(cfg.working_dir)

    # configure logging to file
    logfile = os.path.join(
        cfg.working_dir,
        'find-orientations_%s.log' %analysis_id
        )
    fh = logging.FileHandler(logfile, mode='w')
    fh.setLevel(log_level)
    fh.setFormatter(
        logging.Formatter(
            '%(asctime)s - %(name)s - %(message)s',
            '%m-%d %H:%M:%S'
            )
        )
    logger.info("logging to %s", logfile)
    logger.addHandler(fh)

    if args.profile:
        import cProfile as profile, pstats, StringIO
        pr = profile.Profile()
        pr.enable()

    # process the data
    find_orientations(cfg, hkls=args.hkls, clean=args.clean, profile=args.profile)

    if args.profile:
        pr.disable()
        s = StringIO.StringIO()
        ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
        ps.print_stats(50)
        logger.info('%s', s.getvalue())

    # clean up the logging
    fh.flush()
    fh.close()
    logger.removeHandler(fh)
    logger.info('=== end find-orientations ===')
    ch.flush()
    ch.close()
    logger.removeHandler(ch)
        print "USAGE: python smooth_ge_data.py config.yml"
        sys.exit(1)
    elif sys.argv[1] == "-h" or sys.argv[1] == "--help":
        print "USAGE: python smooth_ge_data.py config.yml"
        sys.exit(1)
    else:
        cfg_file = sys.argv[1]

    # Setup logger
    log_level = logging.DEBUG
    logger = logging.getLogger("hexrd")
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(log_level)
    cf = logging.Formatter("%(asctime)s - %(message)s", "%y-%m-%d %H:%M:%S")
    ch.setFormatter(cf)
    logger.addHandler(ch)
    # load the configuration settings
    cfgs = config.open(cfg_file)
    # cfg is a list. We will loop over each cfg set.

    for cfg in cfgs:
        # Initialize the GE pre-processor
        gepp = GEPreProcessor(cfg=cfg, logger=logger)
        # Start analysis
        logger.info("=== begin image-smoothing ===")
        # Load the GE2 data
        gepp.load_data()
        # ID blobs and the local maxima
        gepp.find_blobs()
Beispiel #21
0
        'cfg', metavar='cfg_filename',
        type=str, help='a YAML config filename')
    parser.add_argument(
        '-m', '--multiplier',
        help='multiplier on angular tolerance',
        type=float, default=0.5)
    parser.add_argument(
        '-b', '--block-id',
        help='block-id in config',
        type=int, default=0)

    args = vars(parser.parse_args(sys.argv[1:]))

    print "loaded config file %s" %args['cfg']
    print "will use block %d" % args['block_id']
    cfg = config.open(args['cfg'])[args['block_id']]
    overlap_table = build_overlap_table(cfg, tol_mult=args['multiplier'])
    np.savez(os.path.join(cfg.analysis_dir, 'overlap_table.npz'),
             *overlap_table)
#%%
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
#
#etas = np.radians(np.linspace(0, 359, num=360))
#cx = np.cos(etas)
#cy = np.sin(etas)
#cz = np.zeros_like(etas)
#
#ax.plot(cx, cy, cz, c='b')
#ax.plot(cx, cz, cy, c='g')
#ax.plot(cz, cx, cy, c='r')
    for o, val in opts:
        if o == "-p":
            prof_dict["profile"] = True
        elif o == "-n":
            prof_dict["use_nvtx"] = True
        elif o=="-c":
            try:
                max_grains = int(val)
            except ValueError:
                print "invalid grain count"
                usage()
                sys.exit(2)
        else:
            assert False, "unhandled option '{0}'".format(o)

    if len(args) < 1:
        usage()
        sys.exit(2)
    cfg_filename = args[0]

    with profiling(**prof_dict):
        start = time.time()
        print "Using cfg file '%s'" % (cfg_filename)
        config = config.open(cfg_filename)[0]
        config._cfg['multiproc'] = 1 # force sequential run

        target.fit_grains(config, force=True, grains=range(max_grains))

        elapsed = time.time() - start
        print "\nTotal processing time %.2f seconds" % elapsed
Beispiel #23
0
def execute(args, parser):
    import logging
    import os
    import sys

    import yaml

    from hexrd import config
    from hexrd.cacheframes import cache_frames


    # load the configuration settings
    cfgs = config.open(args.yml)

    # configure logging to the console:
    log_level = logging.DEBUG if args.debug else logging.INFO
    if args.quiet:
        log_level = logging.ERROR
    logger = logging.getLogger('hexrd')
    logger.setLevel(log_level)
    ch = logging.StreamHandler()
    ch.setLevel(logging.CRITICAL if args.quiet else log_level)
    cf = logging.Formatter('%(asctime)s - %(message)s', '%y-%m-%d %H:%M:%S')
    ch.setFormatter(cf)
    logger.addHandler(ch)

    logger.info('=== begin cache-frames ===')

    for cfg in cfgs:
        logger.info('*** begin caching for analysis "%s" ***', cfg.analysis_name)

        # configure logging to file for this particular analysis
        logfile = os.path.join(
            cfg.working_dir,
            cfg.analysis_name,
            'cache-frames.log'
            )
        fh = logging.FileHandler(logfile, mode='w')
        fh.setLevel(log_level)
        ff = logging.Formatter(
                '%(asctime)s - %(name)s - %(message)s',
                '%m-%d %H:%M:%S'
                )
        fh.setFormatter(ff)
        logger.info("logging to %s", logfile)
        logger.addHandler(fh)

        # process the data
        pd, reader, detector = initialize_experiment(cfg)
        cache_frames(reader, cfg, show_progress=not args.quiet)

        # stop logging for this particular analysis
        fh.flush()
        fh.close()
        logger.removeHandler(fh)

        logger.info('*** end caching for analysis "%s" ***', cfg.analysis_name)

    logger.info('=== end cache-frames ===')
    # stop logging to the console
    ch.flush()
    ch.close()
    logger.removeHandler(ch)
Beispiel #24
0
    return cmap, norm
    
#%%
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Make overlap table from cfg file')
    parser.add_argument(
        'cfg', metavar='cfg_filename', 
        type=str, help='a YAML config filename')
    parser.add_argument(
        '-m', '--multiplier', 
        help='multiplier on angular tolerance', 
        type=float, default=0.5)

    args = vars(parser.parse_args(sys.argv[1:]))
    
    cfg = config.open(args['cfg'])[0]
    print "loaded config file %s" %args['cfg']
    overlap_table = build_overlap_table(cfg)
    np.savez(os.path.join(cfg.analysis_dir, 'overlap_table.npz'), *overlap_table)
#%%
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
# 
#etas = np.radians(np.linspace(0, 359, num=360))
#cx = np.cos(etas)
#cy = np.sin(etas)
#cz = np.zeros_like(etas)
#
#ax.plot(cx, cy, cz, c='b')
#ax.plot(cx, cz, cy, c='g')
#ax.plot(cz, cx, cy, c='r')
Beispiel #25
0
    parser.add_argument('-m',
                        '--multiplier',
                        help='multiplier on angular tolerance',
                        type=float,
                        default=0.5)
    parser.add_argument('-b',
                        '--block-id',
                        help='block-id in config',
                        type=int,
                        default=0)

    args = vars(parser.parse_args(sys.argv[1:]))

    print "loaded config file %s" % args['cfg']
    print "will use block %d" % args['block_id']
    cfg = config.open(args['cfg'])[args['block_id']]
    overlap_table = build_overlap_table(cfg, tol_mult=args['multiplier'])
    np.savez(os.path.join(cfg.analysis_dir, 'overlap_table.npz'),
             *overlap_table)
#%%
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
#
#etas = np.radians(np.linspace(0, 359, num=360))
#cx = np.cos(etas)
#cy = np.sin(etas)
#cz = np.zeros_like(etas)
#
#ax.plot(cx, cy, cz, c='b')
#ax.plot(cx, cz, cy, c='g')
#ax.plot(cz, cx, cy, c='r')
Beispiel #26
0
data_dir = os.getcwd()

fc_stem = "%s_%06d-fc_%%s.npz" % (samp_name, scan_number)

block_number = 0  # ONLY FOR TIMESERIES CFG

clobber_grains = False

tol_loop_idx = -1

threshold = None

# =============================================================================
# %% INITIALIZATION
# =============================================================================
cfg = config.open(cfg_filename)[block_number]

analysis_id = '%s_%s' % (
    cfg.analysis_name.strip().replace(' ', '-'),
    cfg.material.active.strip().replace(' ', '-'),
)

grains_filename = os.path.join(cfg.analysis_dir, 'grains.out')

# !!! handle max_tth config option
max_tth = cfg.fit_grains.tth_max
if max_tth:
    if type(cfg.fit_grains.tth_max) != bool:
        max_tth = np.radians(float(max_tth))
else:
    max_tth = None
Beispiel #27
0
 def setUp(self):
     self.cfgs = config.open(self.file_name)
Beispiel #28
0
 def setUp(self):
     self.cfgs = config.open(self.file_name)
Beispiel #29
0
def test_config(example_repo_config_path, example_repo_include_path):
    conf = config.open(example_repo_config_path)[0]
    conf.working_dir = str(example_repo_include_path)

    return conf
Beispiel #30
0
# for clustering neighborhood
# FIXME
min_samples = 2

# maps options
clobber_maps = False
show_maps = False

#%% one grain only
grain_out = '/grains.out'
load_data_zero = np.loadtxt(load_step_zero_dir + grain_out)
grain_id = load_data_zero[145:,0]

#%% LOAD YML FILE
cfg = config.open(cfg_filename)[0]

#analysis_id = '%s_%s' % (
#    cfg.analysis_name.strip().replace(' ', '-'),
#    cfg.material.active.strip().replace(' ', '-'),
#    )

active_hkls = cfg.find_orientations.orientation_maps.active_hkls
if active_hkls == 'all':
    active_hkls = None

max_tth = cfg.fit_grains.tth_max
if max_tth:
    if type(cfg.fit_grains.tth_max) != bool:
        max_tth = np.degrees(float(max_tth))
else:
Beispiel #31
0
def test_config(single_ge_config_path, single_ge_include_path):
    conf = config.open(single_ge_config_path)[0]
    conf.working_dir = single_ge_include_path

    return conf