示例#1
0
def xyregrid(img, mask, X, Y, nx=None, ny=None):
    ''' regrid with (nx,ny) points. 
        This can also be used on QPHI coordinates
        any 2D rectangular grid.
    '''

    pixellist = np.where(mask.ravel() == 1)
    data = img.ravel()[pixellist]

    x = X.ravel()[pixellist]
    y = Y.ravel()[pixellist]
    xlist = linplist(np.min(X), np.max(X), nx)
    ylist = linplist(np.min(Y), np.max(Y), ny)
    xid, yid, pselect = partition2D(xlist, ylist, x, y)
    bid = (yid * nx + xid)

    xy = partition_avg(data[pselect], bid)
    x_new = partition_avg(x[pselect], bid)
    y_new = partition_avg(y[pselect], bid)
    mask_new = partition_avg(pselect * 0 + 1, bid)

    xytot = np.zeros((ny, nx))
    X_new = np.zeros((ny, nx))
    Y_new = np.zeros((ny, nx))
    MASK_new = np.zeros((ny, nx))

    maxid = np.max(bid)
    xytot.ravel()[:maxid + 1] = xy
    X_new.ravel()[:maxid + 1] = x_new
    Y_new.ravel()[:maxid + 1] = y_new
    MASK_new.ravel()[:maxid + 1] = mask_new

    return xytot, MASK_new, X_new, Y_new
示例#2
0
def xy2qphi(img, mask, X, Y, noqs=None, nophis=None):
    ''' Transform image from coordinates x,y to q,phi.
        Uses a binning scheme.
    '''
    if (mask is None):
        pxlst = np.arange(img.shape[0] * img.shape[1])
    else:
        pxlst = np.where(mask.ravel() == 1)[0]

    # make q coordinate system
    QS, PHIS = mkpolar(X=X, Y=Y)
    qs, phis = QS.ravel()[pxlst], PHIS.ravel()[pxlst]

    data = img.ravel()[pxlst]

    if (noqs is None):
        noqs = (np.max(qs) - np.min(qs))

    if (nophis is None):
        nophis = 12

    # make the lists for selection
    nophis = int(nophis)
    noqs = int(noqs)
    qlist_m1 = linplist(np.min(qs), np.max(qs), noqs)
    philist_m1 = linplist(np.min(phis), np.max(phis), nophis)

    # partition the grid (digitize)
    qid, pid, pselect = partition2D(qlist_m1, philist_m1, qs, phis)
    bid = (qid * nophis + pid).astype(int)

    # average the partitions
    sq = partition_avg(data[pselect], bid)
    q_new = partition_avg(qs[pselect], bid)
    phi_new = partition_avg(phis[pselect], bid)
    mask_new = partition_avg(pselect * 0 + 1, bid)

    sqtot = np.zeros((noqs, nophis))
    Q_new = np.zeros((noqs, nophis))
    PHI_new = np.zeros((noqs, nophis))
    MASK_new = np.zeros((noqs, nophis))

    maxid = np.max(bid)
    sqtot.ravel()[:maxid + 1] = sq
    Q_new.ravel()[:maxid + 1] = q_new
    PHI_new.ravel()[:maxid + 1] = phi_new
    MASK_new.ravel()[:maxid + 1] = mask_new

    return sqtot, MASK_new, Q_new, PHI_new
示例#3
0
def qphi2xy(img, mask, Q, PHI, noxs=None, noys=None):
    ''' Transform image from coordinates x,y to q,phi.
        Uses a binning scheme.
    '''
    if (mask is None):
        pxlst = np.arange(img.shape[0] * img.shape[1])
    else:
        pxlst = np.where(mask.ravel() == 1)[0]

    X, Y = mkcartesian(Q=Q, PHI=PHI)
    x, y = X.ravel()[pxlst], Y.ravel()[pxlst]

    data = img.ravel()[pxlst]

    if (noxs is None):
        noxs = 400

    if (noys is None):
        noys = 400

    noxs = int(noxs)
    noys = int(noys)
    xlist_m1 = linplist(np.min(x), np.max(x), noxs)
    ylist_m1 = linplist(np.min(y), np.max(y), noys)

    xid, yid, pselect = partition2D(xlist_m1, ylist_m1, x, y)
    bid = (yid * noxs + xid).astype(int)

    xy = partition_avg(data[pselect], bid)
    x_new = partition_avg(x[pselect], bid)
    y_new = partition_avg(y[pselect], bid)
    mask_new = partition_avg(pselect * 0 + 1, bid)

    xytot = np.zeros((noys, noxs))
    X_new = np.zeros((noys, noxs))
    Y_new = np.zeros((noys, noxs))
    MASK_new = np.zeros((noys, noxs))

    maxid = np.max(bid)
    xytot.ravel()[:maxid + 1] = xy
    X_new.ravel()[:maxid + 1] = x_new
    Y_new.ravel()[:maxid + 1] = y_new
    MASK_new.ravel()[:maxid + 1] = mask_new

    return xytot, MASK_new, X_new, Y_new
示例#4
0
def circavg(img,x0=None,y0=None,mask=None,SIMG=None,noqs=None,xrdata=None):
    ''' Compute a circular average of the data. 
        x0 : x center
        y0 : y center
        mask : the mask
        If SIMG is not null, put the data into this image.
        noqs : the number of qs to partition into. Default is 
            the number of pixels approximately.
    '''
    if xrdata is not None:
        x0, y0 = xrdata.x0, xrdata.y0
        if hasattr(xrdata,"mask_name"):
            mask = openmask(SDIR + "/" + xrdata.mask_name)
    dimy, dimx = img.shape
    if(mask is None):
        pxlst = np.arange(dimx*dimy)
    else:
        pxlst = np.where(mask.ravel() == 1)[0]

    if(x0 is None):
        x0 = dimx/2
    if(y0 is None):
        y0 = dimy/2
    
    QS,PHIS = mkpolar(img,x0=x0,y0=y0)
    qs,phis = QS.ravel()[pxlst],PHIS.ravel()[pxlst]

    data = img.ravel()[pxlst]

    
    if(noqs is None):
        noqs = (np.max(qs)-np.min(qs)).astype(int)
    qlist_m1 = linplist(np.min(qs),np.max(qs),noqs)
    #philist_m1 = linplist(np.min(phis),np.max(phis),1)

    qid,pselect = partition1D(qlist_m1,qs)

    sqy = partition_avg(data[pselect],qid)
    #sqx = partition_avg(qs[pselect],qid)
    sqx = (qlist_m1[0::2] + qlist_m1[1::2])/2.
    if(len(sqy) < len(sqx)):
        sqy = np.concatenate((sqy,np.zeros(len(sqx)-len(sqy))))

    if(SIMG is not None):
        SIMGtmp =  0*SIMG
        SIMGtmp = np.interp(QS,sqx,sqy)
        np.copyto(SIMG,SIMGtmp)
    return sqx, sqy
示例#5
0
def qphiavg_getlists(img,
                     x0=None,
                     y0=None,
                     mask=None,
                     SIMG=None,
                     qlist=None,
                     philist=None,
                     noqs=None,
                     nophis=None,
                     interp=None):
    ''' get lists from data.'''
    dimy, dimx = img.shape
    if (mask is None):
        pxlst = np.arange(dimx * dimy)
    else:
        pxlst = np.where(mask.ravel() == 1)[0]

    if (x0 is None):
        x0 = dimx / 2
    if (y0 is None):
        y0 = dimy / 2

    if (qlist is None):
        if (noqs is None):
            noqs = 800
        qlist_m1 = None
    elif (len(qlist) == 1):
        noqs = qlist
        qlist_m1 = None
    else:
        qlist_m1 = np.array(qlist)
        noqs = qlist_m1.shape[0] // 2

    if (philist is None):
        if (nophis is None):
            nophis = 360
        philist_m1 = None
    elif (len(philist) == 1):
        noqs = philist
        philist_m1 = None
    else:
        philist_m1 = np.array(philist)
        nophis = philist_m1.shape[0] // 2

    QS, PHIS = mkpolar(img, x0=x0, y0=y0)
    qs, phis = QS.ravel()[pxlst], PHIS.ravel()[pxlst]

    data = img.ravel()[pxlst]

    if (noqs is None):
        noqs = (np.max(qs) - np.min(qs))
    if (nophis is None):
        nophis = 12

    nophis = int(nophis)
    noqs = int(noqs)
    if (qlist_m1 is None):
        qlist_m1 = linplist(np.min(qs), np.max(qs), noqs)
    if (philist_m1 is None):
        philist_m1 = linplist(np.min(phis), np.max(phis), nophis)
    return qlist_m1, philist_m1
示例#6
0
def qphisum(img,
            x0=None,
            y0=None,
            mask=None,
            SIMG=None,
            qlist=None,
            philist=None,
            noqs=None,
            nophis=None,
            interp=None):
    ''' Compute a qphi average of the data. 
        x0 : x center
        y0 : y center
        mask : the mask
        If SIMG is not null, put the data into this image.
        noqs : the number of qs to partition into. Default is 
            the number of pixels approximately.
        interp : interpolation methods:
            None (default) : no interpolation
            1 : interpolate in phi only (loop over q's)
            ... so far no other methods
    '''
    dimy, dimx = img.shape
    if (mask is None):
        pxlst = np.arange(dimx * dimy)
    else:
        pxlst = np.where(mask.ravel() == 1)[0]

    if (x0 is None):
        x0 = dimx / 2
    if (y0 is None):
        y0 = dimy / 2
    if (qlist is None):
        if (noqs is None):
            noqs = 800
        qlist_m1 = None
    elif (len(qlist) == 1):
        noqs = qlist
        qlist_m1 = None
    else:
        qlist_m1 = np.array(qlist)
        noqs = qlist_m1.shape[0] // 2

    if (philist is None):
        if (nophis is None):
            nophis = 360
        philist_m1 = None
    elif (len(philist) == 1):
        noqs = philist
        philist_m1 = None
    else:
        philist_m1 = np.array(philist)
        nophis = philist_m1.shape[0] // 2

    QS, PHIS = mkpolar(img, x0=x0, y0=y0)
    qs, phis = QS.ravel()[pxlst], PHIS.ravel()[pxlst]

    data = img.ravel()[pxlst]

    if (noqs is None):
        noqs = (np.max(qs) - np.min(qs))
    if (nophis is None):
        nophis = 12

    nophis = int(nophis)
    noqs = int(noqs)
    if (qlist_m1 is None):
        qlist_m1 = linplist(np.min(qs), np.max(qs), noqs)
    if (philist_m1 is None):
        philist_m1 = linplist(np.min(phis), np.max(phis), nophis)

    qid, pid, pselect = partition2D(qlist_m1, philist_m1, qs, phis)
    bid = (qid * nophis + pid).astype(int)

    sqphi = partition_sum(data[pselect], bid)

    sqphitot = np.zeros((noqs, nophis))
    maxid = np.max(bid)
    sqphitot.ravel()[:maxid + 1] = sqphi

    phis_m1 = (philist_m1[0::2] + philist_m1[1::2]) / 2.
    qs_m1 = (qlist_m1[0::2] + qlist_m1[1::2]) / 2.

    if (interp is not None):
        sqmask = np.zeros((noqs, nophis))
        sqmask.ravel()[:maxid + 1] = partition_avg(pselect * 0 + 1, bid)
        if ((interp == 1) or (interp == 2)):
            for i in range(sqphitot.shape[0]):
                # won't work if you have numpy version < 1.10
                sqphitot[i] = fillin1d(phis_m1,
                                       sqphitot[i],
                                       sqmask[i],
                                       period=2 * np.pi)
                # best suited for phi range -pi/2 to pi/2, might need to change
                # for diff versions
        if interp == 2:
            # second interp method also uses point symmetry
            # reverse philist, and rebin
            # assume the point of reflection is about zero, if not, maybe this
            # commented code could be tried (not tested)
            # first, find the point of reflection (is it about zero, -pi or pi?)
            #avgphi = np.average(phis_m1)
            #if(avgphi > -np.pi/2. and avgphi < np.pi/2.):
            #const = 0. #do nothing
            #elif(avgphi > np.pi/2. and avgphi < 3*np.pi/2.):
            #const = np.pi
            #elif(avgphi > -3*np.pi/2. and avgphi < -np.pi/2.):
            #const = np.pi
            const = 0.
            # now reflect philist
            philist_rev = const - philist_m1[::-1]
            qidr, pidr, pselectr = partition2D(qlist_m1, philist_rev, qs, phis)
            bidr = (qidr * nophis + pidr).astype(int)
            maxidr = np.max(bidr)
            sqphitotr = np.zeros((noqs, nophis))
            sqphitotr.ravel()[:maxidr + 1] = partition_avg(
                data[pselectr], bidr)
            sqphitotr = sqphitotr[:, ::-1]
            sqmaskr = np.zeros((noqs, nophis))
            sqmaskr.ravel()[:maxidr + 1] = partition_avg(
                pselectr * 0 + 1, bidr)
            sqmaskr = sqmaskr[:, ::-1]
            # now fill in values
            # just fill in the points, don't interp
            w = np.where((sqmask == 0) * (sqmaskr == 1))
            sqphitot[w] = sqphitotr[w]

    if (SIMG is not None):
        SIMG.ravel()[pxlst[pselect]] = sqphitot.ravel()[bid]

    return sqphitot