コード例 #1
0
def wrap_func(
        sigma,
        dec_inv_frq,
        dec_interp,
        bounds,
        nbins,
        bragg,
        ird,
        nfrq):

    """
    Wrap ex_consistency and return x, y

    y = weight sigma - dsa sigma
    """
    from RS.rs_ex import ex_consistency as func

    myrs, flow_weight, flow_dsa = func(
        sigma=sigma,
        dec_inv_frq=dec_inv_frq,
        sin2psimx=bounds[1],
        psi_nbin=nbins,
        dec_interp=dec_interp,
        theta_b=bragg,
        ird=ird,
        nfrq=nfrq,

        iscatter=True,
        ig_sub=True,
        ilog=True,
        iplot=False, # iplot=True
    )

    ## My objects that quantify the propagated
    ## error to stress.

    if type(nfrq).__name__=='NoneType':
        epsilon_vm = flow_weight.epsilon_vm[::]
        sigma_vm   = flow_weight.sigma_vm[::]
    elif type(nfrq).__name__=='int':
        epsilon_vm = flow_weight.epsilon_vm[::nfrq]
        sigma_vm   = flow_weight.sigma_vm[::nfrq]

    if len(epsilon_vm) != len(sigma_vm):
        raise IOError, 'epsilon_vm and '+\
            'sigma_vm should have the same dimension'
    if len(epsilon_vm) != len(flow_dsa.sigma_vm):
        raise IOError, 'epsilon_vm and '+\
            'flow_dsa.sigma_vm should have the same dimension'

    x = epsilon_vm
    y = (sigma_vm - flow_dsa.sigma_vm)/\
        sigma_vm
    return x, y
コード例 #2
0
ファイル: mst_ex.py プロジェクト: brianlin-NIST/DiffStress
def wrap_func(
        sigma,
        dec_inv_frq,
        dec_interp,
        bounds,
        nbins,
        bragg,
        ird):
    """
    Wrap ex_consistency and return x, y

    y = weight sigma - dsa sigma
    """
    from RS.rs_ex import ex_consistency as func

    myrs, flow_weight, flow_dsa = func(
        sigma=sigma,
        dec_inv_frq=dec_inv_frq,
        sin2psimx=bounds[1],
        psi_nbin=nbins,
        dec_interp=dec_interp,
        theta_b=bragg,
        ird=ird,

        iscatter=True,
        ig_sub=True,
        iplot=False, # iplot=True
    )

    ## My objects that quantify the propagated
    ## error to stress.
    x = flow_weight.epsilon_vm
    y = (flow_weight.sigma_vm - flow_dsa.sigma_vm)/\
        flow_weight.sigma_vm

    return x, y
コード例 #3
0
ファイル: mst_ex.py プロジェクト: brianlin-NIST/DiffStress
def influence_of_cnts_stats(
        ## characteristics of an ensemble for stress data
        sigmas=[1e-5, 2e-5, 5e-5, 1e-4],
        bounds=[0.,0.5],
        ss=3,
        nbins=10,
        iwgt=False,
        nsample=4,
        intp_opt=0,
        iplot=False,

        ## diffraction condition
        bragg=78.2,    ## Fe {211} using Cr K-alpha
        ird=0.182,     ## Intensity of random distribution
                       ## for {211} using window of 10 degree.
        DEC_freq_sym=True,
        NCPU=0):
    """
    Influence of counting statistics uncertainty

    With fixing other variables, investigates the
    propagation of counting stat error on to the final
    diffraction stress by examining a number of
    statistical ensembles (nsample) characterized
    by given arguments

    Arguments
    =========
    sigmas      : Pure CSE
    bounds      : Bounds of tilting range
    ss          : Sampling frequency in DECs
    nbins       : How many tilting angles in diffraction
    iwgt        : Do we weight (E-e) by peak intensity?
    nsample     : The number of ensembles.
    intp_opt    : DEC interpolation method
    iplot       : Flag for plotting
    bragg       : Bragg's angle
    ird         : Intensity of random distribution
    DEC_freq_sym: Plotting option for where DECs are sampled.
    NCPU        : The number of CPU cores allowed to run
    """
    from RS.rs_ex import ex_consistency as func

    if iplot:
        import matplotlib.pyplot as plt
        from MP.lib import mpl_lib,axes_label

        fancy_legend = mpl_lib.fancy_legend
        wide_fig     = mpl_lib.wide_fig
        deco         = axes_label.__deco__
        fig          = wide_fig(nw=2,nh=1)
        ax1,ax2      = fig.axes[0], fig.axes[1]

    print '\n\n****************'
    print 'test run started'
    print '****************\n\n'
    myrs,flow1,flow2 =func(
        sin2psimx=bounds[1],
        iscatter=True,
        sigma=sigmas[0],
        psi_nbin=nbins,
        ig_sub=True,
        iplot=False, # iplot=True
        dec_inv_frq=ss,
        dec_interp=intp_opt,
    )

    print '\n\n**************'
    print 'test completed'
    print '**************\n\n'
    # return ## debugging

    Y_all = np.zeros((len(sigmas), nsample, flow1.nstp))
    M = []
    ls=['-+','-s','-o','-d','-t']
    import multiprocessing
    from multiprocessing import Pool
    if NCPU==0: NCPU = multiprocessing.cpu_count()
    print 'NCPU: %i'%NCPU
    pool = Pool(processes = NCPU)

    ## function is now wrap_func
    results = []
    for i in range(len(sigmas)):
        results.append([])
        for j in range(nsample):
            results[i].append(
                pool.apply_async(
                    wrap_func,
                    args=(
                        sigmas[i],
                        ss,
                        intp_opt,
                        bounds,
                        nbins
                        )
                    ,))
    ## close/join
    pool.close()
    pool.join()
    ## terminate
    pool.terminate()

    ## below is to post-process the results
    for i in range(len(sigmas)):
        for j in range(nsample):
            x,y = results[i][j].get()
            Y_all[i][j][:] = y[::]

    M = np.zeros((len(sigmas),len(x)))
    S = np.zeros((len(sigmas),len(x)))
    for i in range(len(sigmas)):
        y = Y_all[i][:][:]
        y = y.T[::] ## len(x), nsample
        for k in range(len(x)):
            M[i][k] = y[k].mean()
            S[i][k] = y[k].std()

    nbins = 10
    H  = np.zeros((len(sigmas), len(x), nbins))
    BE = np.zeros((len(sigmas), len(x), nbins+1))
    for i in range(len(sigmas)):
        y = Y_all[i][:][:]
        y = y.T[::] ## len(x), nsample
        for k in range(len(x)):
            hist,bin_edges = np.histogram(y[k],bins=10)
            H[i,k,:] = hist[::]
            BE[i,k,:] = bin_edges[::]

    if iplot:
        for i in range(len(sigmas)):
            ax1.plot(x,M[i],ls[i],mfc='None',color='k',label='%6.0e'%sigmas[i])
            ax2.errorbar(x,M[i],yerr=S[i],color='k',ls=ls[i])
        if type(ss).__name__=='int' and DEC_freq_sym:
            ax1.plot(x[::ss],np.zeros((len(x[::ss]),)),'o',mec='r',mfc='None',ms=8)
            ax2.plot(x[::ss],np.zeros((len(x[::ss]),)),'o',mec='r',mfc='None',ms=8)
        elif type(ss).__name__=='list' and DEC_freq_sym:
            ax1.plot(x[ss],np.zeros((len(x[ss]),)),'o',mec='r',mfc='None',ms=8)
            ax2.plot(x[ss],np.zeros((len(x[ss]),)),'o',mec='r',mfc='None',ms=8)
        ax1.set_ylim(0.,); ax1.set_xlim(0.,ax1.get_xlim()[1]*1.05)
        deco(iopt=9,ft=15,ax=ax1)
        fancy_legend(ax=ax1, size=7,ncol=2,nscat=1)
        ax2.set_ylim(0.,); ax2.set_xlim(0.,ax2.get_xlim()[1]*1.05)
        deco(iopt=9,ft=15,ax=ax2)
        fig.savefig('ee.pdf')
    return x, M, S, H, BE
コード例 #4
0
def influence_of_cnts_stats(
        ## characteristics of an ensemble for
        ## stress data
        sigmas=[1e-5, 2e-5, 5e-5, 1e-4],
        bounds=[0.,0.5],
        ss=3,
        nbins=10,
        iwgt=False,
        nsample=4,
        intp_opt=0,
        iplot=False,

        ## diffraction condition
        bragg=78.2*np.pi/180.,     ## Fe {211} using Cr K-alpha
        #bragg=162.2*np.pi/180.,   ## Fe {310} using Cobalt Radiation.
        ird=0.182,     ## Intensity of random distribution
                       ## for {211} using window of 10 degree.
        ## sample a portion of data.
        nfrq=None,
        DEC_freq_sym=True,
        NCPU=0):
    """
    Propagate errors due to
     1) counting statistics error (CSE)
     2) incomplete DECs (by utilizing only
          a portion of model DECs)
     3) Infinite number of tilting angles in use
     4) Incomplete range of psi angle ranges in use
     5) Geometric distortion (Bragg angle / psi)
     6) Peak intensity: I(phi,psi) / Ird

    With fixing other variables, investigates the
    propagation of counting stat error to final
    diffraction stress by examining a number of
    statistical ensembles <nsample> characterized
    by given arguments

    ## diffraction condition
    #bragg=78.2,  ## Fe {211} using Cr K-alpha
    #bragg=78.2,  ## Fe {310} using Cobalt
    #ird=0.182,     ## Intensity of random distribution

    Arguments
    =========
    sigmas      : Counting Stat Error
    bounds      : Bounds of tilting range
                  (low psi to high psi)
    ss          : Sampling frequency in DECs
    nbins       : How many tilting angles in
                  diffraction
    iwgt        : Do we weight (E-e) by peak intensity?
    nsample     : The number of ensembles
    intp_opt    : DEC interpolation method
    iplot       : Flag for plotting
    bragg       : Bragg's angle
    ird         : Intensity of random distribution
    nfrq        : if not (None) but an integer,
              use it as denominator to 'continue' to skip

              --------------------------
              if istp % nfrq != 0 :
                  continue ! skip
              else:
                  run stress analysis
              --------------------------

    DEC_freq_sym: Plotting option for where DECs are sampled.
    NCPU        : The number of CPU cores allowed to run
    """
    from RS.rs_ex import ex_consistency as func

    if iplot:
        import matplotlib.pyplot as plt
        from MP.lib import mpl_lib,axes_label

        fancy_legend = mpl_lib.fancy_legend
        wide_fig     = mpl_lib.wide_fig
        deco         = axes_label.__deco__
        fig          = wide_fig(nw=2,nh=1)
        ax1,ax2      = fig.axes[0], fig.axes[1]

    print '\n\n****************'
    print 'test run started'
    print '****************\n\n'
    myrs,flow_raw,flow2 =func(
        sin2psimx=bounds[1],
        iscatter=False,
        sigma=sigmas[0],
        psi_nbin=nbins,
        ig_sub=True,
        iplot=False, # iplot=True
        dec_inv_frq=ss,
        dec_interp=intp_opt,
        nfrq=nfrq)
    print '\n\n**************'
    print 'test completed'
    print '**************\n\n'

    Y_all = np.zeros((len(sigmas), nsample, flow2.nstp))
    M = []
    ls=['-+','-s','-o','-d','-t']
    import multiprocessing
    from multiprocessing import Pool
    if NCPU==0: NCPU = multiprocessing.cpu_count()
    print 'NCPU: %i'%NCPU
    pool = Pool(processes = NCPU)

    ## function is now wrap_func
    results = []
    for i in xrange(len(sigmas)):
        results.append([])
        for j in xrange(nsample):
            results[i].append(
                pool.apply_async(
                    wrap_func,
                    args=(
                        sigmas[i],
                        ss,
                        intp_opt,
                        bounds,
                        nbins,
                        bragg,
                        ird,nfrq),))

    ## close/join
    pool.close(); pool.join()
    ## terminate
    pool.terminate()

    ## below is to post-process the results
    for i in xrange(len(sigmas)):
        for j in xrange(nsample):
            x,y = results[i][j].get()
            Y_all[i][j][:] = y[::]

    M = np.zeros((len(sigmas),len(x)))
    S = np.zeros((len(sigmas),len(x)))
    for i in xrange(len(sigmas)):
        y = Y_all[i][:][:]
        y = y.T[::] ## len(x), nsample
        for k in xrange(len(x)):
            M[i][k] = y[k].mean()
            S[i][k] = y[k].std()

    nbins = 10
    H  = np.zeros((len(sigmas), len(x), nbins))
    BE = np.zeros((len(sigmas), len(x), nbins+1))
    for i in xrange(len(sigmas)):
        y = Y_all[i][:][:]
        y = y.T[::] ## len(x), nsample
        for k in xrange(len(x)):
            hist,bin_edges = np.histogram(y[k],bins=10)
            H[i,k,:] = hist[::]
            BE[i,k,:] = bin_edges[::]

    if iplot:
        for i in xrange(len(sigmas)):
            ax1.plot(x,M[i],ls[i],mfc='None',color='k',label='%6.0e'%sigmas[i])
            ax2.errorbar(x,M[i],yerr=S[i],color='k',ls=ls[i])
        if type(ss).__name__=='int' and DEC_freq_sym:
            ax1.plot(x[::ss],np.zeros((len(x[::ss]),)),'o',mec='r',mfc='None',ms=8)
            ax2.plot(x[::ss],np.zeros((len(x[::ss]),)),'o',mec='r',mfc='None',ms=8)
        elif type(ss).__name__=='list' and DEC_freq_sym:
            ax1.plot(x[ss],np.zeros((len(x[ss]),)),'o',mec='r',mfc='None',ms=8)
            ax2.plot(x[ss],np.zeros((len(x[ss]),)),'o',mec='r',mfc='None',ms=8)
        # ax1.set_ylim(0.,); ax1.set_xlim(0.,ax1.get_xlim()[1]*1.05)
        deco(iopt=9,ft=15,ax=ax1)
        fancy_legend(ax=ax1, size=7,ncol=2,nscat=1)
        ax2.set_ylim(0.,); ax2.set_xlim(0.,ax2.get_xlim()[1]*1.05)
        deco(iopt=9,ft=15,ax=ax2)
        fig.savefig('ee.pdf')
    return x, M, S, H, BE, flow_raw