예제 #1
0
파일: pf_kde.py 프로젝트: usnistgov/texture
def ex(fact=100, res=1.0,filename=None,ngrain=None):

    kde = stats.gaussian_kde

    mypf = upf.polefigure(ngrain=ngrain, filename=filename, csym='cubic')
    mypf.pf(pole=[[1,0,0]],mode='contourf')

    # PF[# of pole, mgrid(rot=azmthl), ngrid(tilt= declination)]
    PF = mypf.pfnodes[0]

    mn, nn = PF.shape
    dbeta  = 360./(mn -1) # Rotating
    dalpha =  90./(nn-1)  # tilting
    beta   = np.linspace(0.,360., mn) # 0.~360.
    alpha  = np.linspace(0., 90., nn) # 0.~90.

    #dum1 = np.arange(0.,360. + res/10., res)
    #dum2 = np.arange(0.,90.  + res/10., res)

    x = []
    y = []
    #len(dum2)))
    # |---------------|
    # |   |   |   |   |
    # |---------------|
    # |   |   |   |   |
    # |---------------|
    # |   |   |   |   |
    # |---------------|
    # |   |   |   |   |
    # |---------------|

    maxint = max(PF.flatten())
    for i in range(len(PF)):
        for j in range(len(PF[i])):
            nsample = int(PF[i][j]/maxint*fact)
            for k in range(nsample):
                x.append(beta[i])
                y.append(alpha[j])


    # For a new resolution
    beta  = np.arange(0.,360.+res/2.,res)
    alpha = np.arange(0.,90.+res/2.,res)
    A, B = np.meshgrid(alpha, beta)
    positions = np.vstack([B.ravel(), A.ravel()])
    values = np.vstack([x,y]) # beta, alpha
    kernel = stats.gaussian_kde(values)
    Z = np.reshape(kernel.evaluate(positions).T, A.shape)

    plt.figure(3)
    ax = plt.gca()
    ax.set_frame_on(False)
    ax.set_aspect('equal')
    ax.set_axis_off()
    rx, ry = upf.circle()
    ax.plot(rx,ry,'k')
    pi = np.pi

    nm = (360.0-0.)/res; nn = (180.-90.)/res
    theta = np.linspace(pi, pi/2., nn+1)
    phi = np.linspace(0.,2.*pi, nm+1)
    r = np.sin(theta)/(1-np.cos(theta))
    R, PHI = np.meshgrid(r,phi)
    PHI = PHI + pi/2.
    x = R*np.cos(PHI); y= R*np.sin(PHI)

    ax.contourf(x, y, Z)
예제 #2
0
def harm_pf(grains=None,
            filename=None,
            l=2,
            dm=7.5,
            dn=7.5,
            pole=[[1, 0, 0]],
            csym='cubic'):
    """
    Something is wrong and I couldn't figure out.
    Further development is defferred.

    Arguments
    =========
    grains   = None
    filename = None
    l        = 10
    dm       = 7.5
    dn       = 7.5
    pole     = [[1, 0, 0]]
    csym     = 'cubic'
    """
    import numpy as np
    import cmb
    from upf import polefigure, circle
    pi = np.pi

    if grains == None and filename == None:
        gr = cmb.random(filename='dum.tex',
                        ngrain=1000,
                        phi1=360.,
                        phi=90.,
                        phi2=180.)
        mypf = polefigure(grains=grains, csym=csym)
    else:
        mypf = polefigure(grains=grains, filename=filename, csym=csym)

    mypf.pf(pole=pole, dm=dm, dn=dn)
    hpf = []

    import matplotlib.pyplot as plt
    fact = 2.  #size factor
    figsize = (len(pole) * 2. * fact, 1. * 2. * fact)
    fig = plt.figure(33, figsize=figsize)
    ax = plt.gca()

    for ip in range(len(pole)):
        extended_PF = extPF(mypf.pfnodes[ip])
        print 'extended_PF.shape', extended_PF.shape
        #dum = harm_PF(PF=mypf.pfnodes[ip], l=l)
        dum = harm_PF(PF=extended_PF, l=l)
        reduced_PF = redPF(dum)
        hpf.append(reduced_PF)

#        print 'max:', np.real(max(hpf[ip].flatten()))
#        print 'min:', np.real(min(hpf[ip].flatten()))

    theta = np.linspace(pi, pi / 2., (90.) / dn + 1)
    phi = np.linspace(0., 2. * pi, (360.) / dm + 1)
    r = np.sin(theta) / (1 - np.cos(theta))
    R, PHI = np.meshgrid(r, phi)
    PHI = PHI + pi / 2.
    x = R * np.cos(PHI)
    y = R * np.sin(PHI)

    #    return x, y, mypf.pfnodes[0]
    cnt = ax.contourf(x, y, hpf[0])
    ax.set_frame_on(False)
    ax.set_axis_off()
    ax.set_aspect('equal')
    rx, ry = circle()
    ax.plot(rx, ry, 'k')
    ax.set_xlim(-1.2, 1.5)
    ax.set_ylim(-1.2, 1.5)

    tcolors = cnt.tcolors
    clev = cnt._levels
    for i in range(len(tcolors)):
        cc = tcolors[i][0][0:3]
        #if levels==None:
        if ip == len(pole) - 1:
            ## level line
            ax.plot([1.28, 1.35], [1. - i * 0.2, 1. - i * 0.2], color=cc)
            ## level text
            ax.text(x=1.40,
                    y=1. - i * 0.2 - 0.05,
                    s='%3.2f' % (clev[i]),
                    fontsize=4 * fact)

    return hpf, mypf.pfnodes
예제 #3
0
def harm_pf(grains=None, filename=None, l=2, dm=7.5, dn=7.5,
            pole=[[1,0,0]], csym='cubic'):
    """
    Something is wrong and I couldn't figure out.
    Further development is defferred.

    Arguments
    =========
    grains   = None
    filename = None
    l        = 10
    dm       = 7.5
    dn       = 7.5
    pole     = [[1, 0, 0]]
    csym     = 'cubic'
    """
    import numpy as np
    import cmb
    from upf import polefigure, circle
    pi = np.pi

    if grains==None and filename==None:
        gr = cmb.random(filename='dum.tex',ngrain=1000,
                        phi1=360., phi=90., phi2=180.)
        mypf = polefigure(grains=grains, csym=csym)
    else: mypf = polefigure(grains=grains, filename=filename, csym=csym)

    mypf.pf(pole=pole, dm=dm, dn=dn)
    hpf = []

    import matplotlib.pyplot as plt
    fact = 2. #size factor
    figsize = (len(pole)*2.*fact, 1.*2.*fact)
    fig = plt.figure(33, figsize=figsize)
    ax = plt.gca()

    for ip in range(len(pole)):
        extended_PF = extPF(mypf.pfnodes[ip])
        print 'extended_PF.shape', extended_PF.shape
        #dum = harm_PF(PF=mypf.pfnodes[ip], l=l)
        dum = harm_PF(PF=extended_PF, l=l)
        reduced_PF = redPF(dum)
        hpf.append(reduced_PF)

#        print 'max:', np.real(max(hpf[ip].flatten()))
#        print 'min:', np.real(min(hpf[ip].flatten()))

    theta = np.linspace(pi, pi/2., (90.)/dn + 1)
    phi = np.linspace(0.,2.*pi,  (360.)/dm+1)
    r = np.sin(theta)/(1-np.cos(theta))
    R, PHI = np.meshgrid(r,phi)
    PHI = PHI + pi/2.
    x = R*np.cos(PHI); y = R*np.sin(PHI)

    #    return x, y, mypf.pfnodes[0]
    cnt = ax.contourf(x,y, hpf[0])
    ax.set_frame_on(False)
    ax.set_axis_off()
    ax.set_aspect('equal')
    rx, ry = circle()
    ax.plot(rx, ry, 'k')
    ax.set_xlim(-1.2,1.5)
    ax.set_ylim(-1.2,1.5)

    tcolors = cnt.tcolors
    clev = cnt._levels
    for i in range(len(tcolors)):
        cc = tcolors[i][0][0:3]
                    #if levels==None:
        if ip==len(pole)-1:
                        ## level line
            ax.plot([1.28, 1.35],
                    [1. - i * 0.2, 1. - i * 0.2],
                    color=cc)
                        ## level text
            ax.text(x=1.40, y= 1. - i*0.2 - 0.05,
                    s='%3.2f'%(clev[i]),
                    fontsize=4*fact)

    return hpf, mypf.pfnodes
예제 #4
0
def ex(fact=100, res=1.0, filename=None, ngrain=None):

    kde = stats.gaussian_kde

    mypf = upf.polefigure(ngrain=ngrain, filename=filename, csym='cubic')
    mypf.pf(pole=[[1, 0, 0]], mode='contourf')

    # PF[# of pole, mgrid(rot=azmthl), ngrid(tilt= declination)]
    PF = mypf.pfnodes[0]

    mn, nn = PF.shape
    dbeta = 360. / (mn - 1)  # Rotating
    dalpha = 90. / (nn - 1)  # tilting
    beta = np.linspace(0., 360., mn)  # 0.~360.
    alpha = np.linspace(0., 90., nn)  # 0.~90.

    #dum1 = np.arange(0.,360. + res/10., res)
    #dum2 = np.arange(0.,90.  + res/10., res)

    x = []
    y = []
    #len(dum2)))
    # |---------------|
    # |   |   |   |   |
    # |---------------|
    # |   |   |   |   |
    # |---------------|
    # |   |   |   |   |
    # |---------------|
    # |   |   |   |   |
    # |---------------|

    maxint = max(PF.flatten())
    for i in range(len(PF)):
        for j in range(len(PF[i])):
            nsample = int(PF[i][j] / maxint * fact)
            for k in range(nsample):
                x.append(beta[i])
                y.append(alpha[j])

    # For a new resolution
    beta = np.arange(0., 360. + res / 2., res)
    alpha = np.arange(0., 90. + res / 2., res)
    A, B = np.meshgrid(alpha, beta)
    positions = np.vstack([B.ravel(), A.ravel()])
    values = np.vstack([x, y])  # beta, alpha
    kernel = stats.gaussian_kde(values)
    Z = np.reshape(kernel.evaluate(positions).T, A.shape)

    plt.figure(3)
    ax = plt.gca()
    ax.set_frame_on(False)
    ax.set_aspect('equal')
    ax.set_axis_off()
    rx, ry = upf.circle()
    ax.plot(rx, ry, 'k')
    pi = np.pi

    nm = (360.0 - 0.) / res
    nn = (180. - 90.) / res
    theta = np.linspace(pi, pi / 2., nn + 1)
    phi = np.linspace(0., 2. * pi, nm + 1)
    r = np.sin(theta) / (1 - np.cos(theta))
    R, PHI = np.meshgrid(r, phi)
    PHI = PHI + pi / 2.
    x = R * np.cos(PHI)
    y = R * np.sin(PHI)

    ax.contourf(x, y, Z)