Ejemplo n.º 1
0
    def test_orientation_FF_reduced(quat):
        """
        input parameters are [
        plane_data, instrument, imgser_dict,
        tth_tol, eta_tol, ome_tol, npdiv, threshold
        ]
        """
        plane_data = paramMP['plane_data']
        instrument = paramMP['instrument']
        imgser_dict = paramMP['imgser_dict']
        tth_tol = paramMP['tth_tol']
        eta_tol = paramMP['eta_tol']
        ome_tol = paramMP['ome_tol']
        npdiv = paramMP['npdiv']
        threshold = paramMP['threshold']

        phi = 2*np.arccos(quat[0])
        n = xfcapi.unitRowVector(quat[1:])
        grain_params = np.hstack([
            phi*n, cnst.zeros_3, cnst.identity_6x1,
        ])

        compl, scrap = instrument.pull_spots(
            plane_data, grain_params, imgser_dict,
            tth_tol=tth_tol, eta_tol=eta_tol, ome_tol=ome_tol,
            npdiv=npdiv, threshold=threshold,
            eta_ranges=None, ome_period=(-np.pi, np.pi),
            check_only=True)

        return sum(compl)/float(len(compl))
Ejemplo n.º 2
0
def write_GOE(GOE_quats, compl, i, dump_dir):
    phi = 2 * np.arccos(GOE_quats[0, :])
    n = xfcapi.unitRowVector(GOE_quats[1:, :])

    expmaps = phi * n

    ori_data = np.hstack([np.expand_dims(compl, axis=1), expmaps.T])

    delim = '   '
    header_items = ('# completeness', 'exp_map_c[0]', 'exp_map_c[1]',
                    'exp_map_c[2]')

    header = delim.join(np.tile('{:<12}', 4)).format(*header_items)

    # print(output_str, file=

    fname = os.path.join(dump_dir, 'grain_%04d.out' % i)

    f = open(fname, 'w+')
    print(header, file=f)
    for i in range(len(phi)):

        output_str = delim.join(['{:<12f}', '{:<12e}', '{:<12e}',
                                 '{:<12e}']).format(*ori_data[i, :])
        print(output_str, file=f)

    f.close()
Ejemplo n.º 3
0
def calc_angles_from_beam_vec(bvec):
    """
    Return the azimuth and polar angle from a beam
    vector
    """
    bvec = np.atleast_1d(bvec).flatten()
    nvec = unitRowVector(-bvec)
    azim = float(np.degrees(np.arctan2(nvec[2], nvec[0])))
    pola = float(np.degrees(np.arccos(nvec[1])))
    return azim, pola
Ejemplo n.º 4
0
def calc_angles_from_beam_vec(bvec):
    """
    Return the azimuth and polar angle from a beam
    vector
    """
    bvec = np.atleast_1d(bvec).flatten()
    nvec = unitRowVector(-bvec)
    azim = float(
        np.degrees(np.arctan2(nvec[2], nvec[0]))
    )
    pola = float(np.degrees(np.arccos(nvec[1])))
    return azim, pola
Ejemplo n.º 5
0
# =============================================================================
# %% FITTING
# =============================================================================

# make sure grains.out is there...
if not os.path.exists(grains_filename) or clobber_grains:
    try:
        qbar = np.loadtxt('accepted_orientations_' + analysis_id + '.dat',
                          ndmin=2).T

        gw = instrument.GrainDataWriter(grains_filename)
        grain_params_list = []
        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)
            grain_params_list.append(grain_params)
        gw.close()
    except (IOError):
        if os.path.exists(cfg.fit_grains.estimate):
            grains_filename = cfg.fit_grains.estimate
        else:
            raise (
                RuntimeError,
                "neither estimate nor %s exist!" % 'accepted_orientations_' +
                analysis_id + '.dat')
    pass
Ejemplo n.º 6
0
import sys, os, time, random
import numpy as np

from hexrd.xrd import transforms as xf
from hexrd.xrd import transforms_CAPI as xfcapi

epsf = 2.2e-16

vec = np.array([[random.uniform(-np.pi,np.pi),random.uniform(-np.pi,np.pi),random.uniform(-np.pi,np.pi)]])
vHat1 = xf.unitVector(vec.T)
vHat2 = xfcapi.unitRowVector(vec)
print "unitVector results match:             ",np.linalg.norm(vHat1.T-vHat2)/np.linalg.norm(vHat1) < epsf

tAng = np.array([0.0011546340766314521,-0.0040527538387122993,-0.0026221336905160211])
rMat1 = xf.makeDetectorRotMat(tAng)
rMat2 = xfcapi.makeDetectorRotMat(tAng)
print "makeDetectorRotMat results match:     ",np.linalg.norm(rMat1-rMat2)/np.linalg.norm(rMat1) < epsf

oAng = np.array([-0.0011591608938627839,0.0011546340766314521])
rMat1 = xf.makeOscillRotMat(oAng)
rMat2 = xfcapi.makeOscillRotMat(oAng)
print "makeOscillRotMat results match:       ",np.linalg.norm(rMat1-rMat2)/np.linalg.norm(rMat1) < epsf

eMap = np.array([ 0.66931818,-0.98578066,0.73593251])
rMat1 = xf.makeRotMatOfExpMap(eMap)
rMat2 = xfcapi.makeRotMatOfExpMap(eMap)
print "makeRotMatOfExpMap results match:     ",np.linalg.norm(rMat1-rMat2)/np.linalg.norm(rMat1) < epsf

axis = np.array([ 0.66931818,-0.98578066,0.73593251])
rMat1 = xf.makeBinaryRotMat(axis)
rMat2 = xfcapi.makeBinaryRotMat(axis)
Ejemplo n.º 7
0
 def vector(self, x):
     assert len(x) == 3
     self._vector = unitRowVector(np.atleast_1d(x).flatten())
Ejemplo n.º 8
0
def find_orientations(cfg, hkls=None, clean=False, profile=False, nsim=100):
    print('ready to run find_orientations')
    # %%
    # =============================================================================
    # SEARCH SPACE GENERATION
    # =============================================================================

    hedm = cfg.instrument.hedm
    plane_data = cfg.material.plane_data

    ncpus = cfg.multiprocessing

    # for indexing
    active_hkls = cfg.find_orientations.orientation_maps.active_hkls
    fiber_ndiv = cfg.find_orientations.seed_search.fiber_ndiv
    fiber_seeds = cfg.find_orientations.seed_search.hkl_seeds
    on_map_threshold = cfg.find_orientations.threshold

    # for clustering
    cl_radius = cfg.find_orientations.clustering.radius
    min_compl = cfg.find_orientations.clustering.completeness
    compl_thresh = cfg.find_orientations.clustering.completeness

    eta_ome = get_eta_ome(cfg, clean=clean)

    print("INFO:\tgenerating search quaternion list using %d processes" %
          ncpus)
    start = timeit.default_timer()
    qfib = generate_orientation_fibers(eta_ome,
                                       hedm.chi,
                                       on_map_threshold,
                                       fiber_seeds,
                                       fiber_ndiv,
                                       ncpus=ncpus)
    print("INFO:\t\t...took %f seconds" % (timeit.default_timer() - start))
    print("INFO: will test %d quaternions using %d processes" %
          (qfib.shape[1], ncpus))

    # %%
    # =============================================================================
    # ORIENTATION SCORING
    # =============================================================================

    scoredq_filename = 'scored_orientations_' + analysis_id(cfg) + '.npz'

    print("INFO:\tusing map search with paintGrid on %d processes" % ncpus)
    start = timeit.default_timer()

    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)
    print("INFO:\t\t...took %f seconds" % (timeit.default_timer() - start))
    completeness = np.array(completeness)

    # export scored orientations
    np.savez_compressed(scoredq_filename,
                        quaternions=qfib,
                        completeness=completeness)
    print("INFO:\tsaved scored orientations to file: '%s'" %
          (scoredq_filename))

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

    if not os.path.exists(cfg.analysis_dir):
        os.makedirs(cfg.analysis_dir)
    qbar_filename = 'accepted_orientations_' + analysis_id(cfg) + '.dat'

    print("INFO:\trunning clustering using '%s'" %
          cfg.find_orientations.clustering.algorithm)
    start = timeit.default_timer()

    # Simulate N random grains to get neighborhood size
    print("INFO:\trunning %d simulations to determine neighborhood size" %
          nsim)
    seed_hkl_ids = [
        plane_data.hklDataList[active_hkls[i]]['hklID'] for i in fiber_seeds
    ]

    # need ome_ranges from imageseries
    # CAVEAT: assumes that all imageseries have same omega ranges!!!
    oims = OmegaImageSeries(cfg.image_series.itervalues().next())
    ome_ranges = [(np.radians([i['ostart'], i['ostop']]))
                  for i in oims.omegawedges.wedges]

    if seed_hkl_ids is not None:
        rand_q = mutil.unitVector(np.random.randn(4, nsim))
        rand_e = np.tile(2.*np.arccos(rand_q[0, :]), (3, 1)) \
          * mutil.unitVector(rand_q[1:, :])
        refl_per_grain = np.zeros(nsim)
        num_seed_refls = np.zeros(nsim)
        grain_param_list = np.vstack([
            rand_e,
            np.zeros((3, nsim)),
            np.tile(cnst.identity_6x1, (nsim, 1)).T
        ]).T
        sim_results = hedm.simulate_rotation_series(
            plane_data,
            grain_param_list,
            eta_ranges=np.radians(cfg.find_orientations.eta.range),
            ome_ranges=ome_ranges,
            ome_period=np.radians(cfg.find_orientations.omega.period))

        refl_per_grain = np.zeros(nsim)
        seed_refl_per_grain = np.zeros(nsim)
        for sim_result in sim_results.itervalues():
            for i, refl_ids in enumerate(sim_result[0]):
                refl_per_grain[i] += len(refl_ids)
                seed_refl_per_grain[i] += np.sum(
                    [sum(refl_ids == hkl_id) for hkl_id in seed_hkl_ids])

        min_samples = max(
            int(
                np.floor(0.5 * cfg.find_orientations.clustering.completeness *
                         min(seed_refl_per_grain))), 2)
        mean_rpg = int(np.round(np.average(refl_per_grain)))
    else:
        min_samples = 1
        mean_rpg = 1

    print("INFO:\tmean reflections per grain: %d" % mean_rpg)
    print("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)

    print("INFO:\t\t...took %f seconds" % (timeit.default_timer() - start))
    print("INFO:\tfound %d grains; saved to file: '%s'" %
          (qbar.shape[1], qbar_filename))

    np.savetxt(qbar_filename, qbar.T, fmt='%.18e', delimiter='\t')

    gw = instrument.GrainDataWriter(
        os.path.join(cfg.analysis_dir, 'grains.out'))
    grain_params_list = []
    for gid, 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(gid, 1., 0., grain_params)
        grain_params_list.append(grain_params)
    gw.close()
Ejemplo n.º 9
0
import sys, os, time, random
import numpy as np

from hexrd.xrd import transforms as xf
from hexrd.xrd import transforms_CAPI as xfcapi

epsf = 2.2e-16

vec = np.array([[
    random.uniform(-np.pi, np.pi),
    random.uniform(-np.pi, np.pi),
    random.uniform(-np.pi, np.pi)
]])
vHat1 = xf.unitVector(vec.T)
vHat2 = xfcapi.unitRowVector(vec)
print "unitVector results match:             ", np.linalg.norm(
    vHat1.T - vHat2) / np.linalg.norm(vHat1) < epsf

tAng = np.array(
    [0.0011546340766314521, -0.0040527538387122993, -0.0026221336905160211])
rMat1 = xf.makeDetectorRotMat(tAng)
rMat2 = xfcapi.makeDetectorRotMat(tAng)
print "makeDetectorRotMat results match:     ", np.linalg.norm(
    rMat1 - rMat2) / np.linalg.norm(rMat1) < epsf

oAng = np.array([-0.0011591608938627839, 0.0011546340766314521])
rMat1 = xf.makeOscillRotMat(oAng)
rMat2 = xfcapi.makeOscillRotMat(oAng)
print "makeOscillRotMat results match:       ", np.linalg.norm(
    rMat1 - rMat2) / np.linalg.norm(rMat1) < epsf
Ejemplo n.º 10
0
 def vector(self, x):
     assert len(x) == 3
     self._vector = unitRowVector(np.atleast_1d(x).flatten())
Ejemplo n.º 11
0
image_stack=gen_nf_cleaned_image_stack(data_folder,img_nums,dark,ome_dilation_iter,threshold,experiment.nrows,experiment.ncols,num_digits=6,grey_bnds=(5,5),gaussian=4.5)

#%%

test_crds_load = np.load(output_dir + missing_grain_coordinates)
test_crds = test_crds_load[:,:]
n_crds = test_crds.shape[0]

random_quaternions = np.load(output_dir + quaternion_test_list)

n_grains = random_quaternions.shape[1]
rMat_c = rot.rotMatOfQuat(random_quaternions)
exp_maps = np.zeros([random_quaternions.shape[1],3])
for i in range(0,random_quaternions.shape[1]):
    phi = 2*np.arccos(random_quaternions[0,i])
    n = xfcapi.unitRowVector(random_quaternions[1:,i])
    exp_maps[i,:] = phi*n

#%%

experiment.n_grains = n_grains
experiment.rMat_c = rMat_c
experiment.exp_maps = exp_maps

#==============================================================================
# %% INSTANTIATE CONTROLLER - RUN BLOCK NO EDITING
#==============================================================================

progress_handler = nfutil.progressbar_progress_observer()
save_handler=nfutil.forgetful_result_handler()
Ejemplo n.º 12
0
def find_orientations(cfg, hkls=None, clean=False, profile=False, nsim=100):
    print('ready to run find_orientations')
    # %%
    # =============================================================================
    # SEARCH SPACE GENERATION
    # =============================================================================

    hedm = cfg.instrument.hedm
    plane_data = cfg.material.plane_data

    ncpus = cfg.multiprocessing

    # for indexing
    active_hkls = cfg.find_orientations.orientation_maps.active_hkls 
    fiber_ndiv = cfg.find_orientations.seed_search.fiber_ndiv
    fiber_seeds = cfg.find_orientations.seed_search.hkl_seeds
    on_map_threshold = cfg.find_orientations.threshold

    # for clustering
    cl_radius = cfg.find_orientations.clustering.radius
    min_compl = cfg.find_orientations.clustering.completeness
    compl_thresh = cfg.find_orientations.clustering.completeness

    eta_ome = get_eta_ome(cfg, clean=clean)

    print("INFO:\tgenerating search quaternion list using %d processes" % ncpus)
    start = timeit.default_timer()
    qfib = generate_orientation_fibers(
        eta_ome, hedm.chi, on_map_threshold,
        fiber_seeds, fiber_ndiv,
        ncpus=ncpus
    )
    print("INFO:\t\t...took %f seconds" % (timeit.default_timer() - start))
    print("INFO: will test %d quaternions using %d processes"
          % (qfib.shape[1], ncpus))

    # %%
    # =============================================================================
    # ORIENTATION SCORING
    # =============================================================================

    scoredq_filename = 'scored_orientations_' + analysis_id(cfg) + '.npz'

    print("INFO:\tusing map search with paintGrid on %d processes"
          % ncpus)
    start = timeit.default_timer()

    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
        )
    print("INFO:\t\t...took %f seconds" % (timeit.default_timer() - start))
    completeness = np.array(completeness)

    # export scored orientations
    np.savez_compressed(scoredq_filename,
                        quaternions=qfib,
                        completeness=completeness)
    print("INFO:\tsaved scored orientations to file: '%s'"
          % (scoredq_filename))
    
    # %%
    # =============================================================================
    # CLUSTERING AND GRAINS OUTPUT
    # =============================================================================

    if not os.path.exists(cfg.analysis_dir):
        os.makedirs(cfg.analysis_dir)
    qbar_filename = 'accepted_orientations_' + analysis_id(cfg) + '.dat'

    print("INFO:\trunning clustering using '%s'"
          % cfg.find_orientations.clustering.algorithm
    )
    start = timeit.default_timer()

    # Simulate N random grains to get neighborhood size
    print("INFO:\trunning %d simulations to determine neighborhood size"
          % nsim
    )
    seed_hkl_ids = [
        plane_data.hklDataList[active_hkls[i]]['hklID'] for i in fiber_seeds
    ]
    
    # need ome_ranges from imageseries
    # CAVEAT: assumes that all imageseries have same omega ranges!!!
    oims = OmegaImageSeries(cfg.image_series.itervalues().next())
    ome_ranges = [
        (np.radians([i['ostart'], i['ostop']])) for i in oims.omegawedges.wedges
    ]
    
    if seed_hkl_ids is not None:
        rand_q = mutil.unitVector(np.random.randn(4, nsim))
        rand_e = np.tile(2.*np.arccos(rand_q[0, :]), (3, 1)) \
          * mutil.unitVector(rand_q[1:, :])
        refl_per_grain = np.zeros(nsim)
        num_seed_refls = np.zeros(nsim)
        grain_param_list = np.vstack([rand_e, 
                                      np.zeros((3, nsim)),
                                      np.tile(cnst.identity_6x1, (nsim, 1)).T]).T
        sim_results = hedm.simulate_rotation_series(
                plane_data, grain_param_list, 
                eta_ranges=np.radians(cfg.find_orientations.eta.range),
                ome_ranges=ome_ranges,
                ome_period=np.radians(cfg.find_orientations.omega.period)
        )
        
        refl_per_grain = np.zeros(nsim)
        seed_refl_per_grain = np.zeros(nsim)
        for sim_result in sim_results.itervalues():
            for i, refl_ids in enumerate(sim_result[0]):
                refl_per_grain[i] += len(refl_ids)
                seed_refl_per_grain[i] += np.sum([sum(refl_ids == hkl_id) for hkl_id in seed_hkl_ids])
    
        min_samples = max(
            int(np.floor(0.5*cfg.find_orientations.clustering.completeness*min(seed_refl_per_grain))),
            2
            )
        mean_rpg = int(np.round(np.average(refl_per_grain)))
    else:
        min_samples = 1
        mean_rpg = 1
    
    print("INFO:\tmean reflections per grain: %d" % mean_rpg)
    print("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
    )

    print("INFO:\t\t...took %f seconds" % (timeit.default_timer() - start))
    print("INFO:\tfound %d grains; saved to file: '%s'"
          % (qbar.shape[1], qbar_filename))

    np.savetxt(qbar_filename, qbar.T,
               fmt='%.18e', delimiter='\t')

    gw = instrument.GrainDataWriter(os.path.join(cfg.analysis_dir, 'grains.out'))
    grain_params_list = []
    for gid, 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(gid, 1., 0., grain_params)
        grain_params_list.append(grain_params)
    gw.close()