예제 #1
0
def _smoothBorder(arr, start, stop, smooth, value):
    """
    start, stop: [z,y,x]
    """
    # prepare coordinates
    shape = N.array(arr.shape)
    start = N.ceil(start).astype(N.int16)
    stop = N.ceil(stop).astype(N.int16)
    smooth_start = start - smooth
    smooth_stop = stop + smooth
    smooth_start = N.where(smooth_start < 0, 0, smooth_start)
    smooth_stop = N.where(smooth_stop > shape, shape, smooth_stop)
    #print smooth_start, smooth_stop

    import copy
    sliceTemplate = [slice(None, None, None)] * arr.ndim
    shapeTemplate = list(shape)
    for d in range(arr.ndim):
        smooth_shape = shapeTemplate[:d] + shapeTemplate[d + 1:]

        # make an array containing the edge value
        edges = N.empty([2] + smooth_shape, N.float32)
        # start side
        slc = copy.copy(sliceTemplate)
        slc[d] = slice(start[d], start[d] + 1, None)
        edges[0] = arr[slc].reshape(smooth_shape)
        # stop side
        slc = copy.copy(sliceTemplate)
        slc[d] = slice(stop[d] - 1, stop[d], None)
        edges[1] = arr[slc].reshape(smooth_shape)

        edges = (edges - value) / float(
            smooth + 1)  # this value can be array??

        # both side
        for s, side in enumerate([start, stop]):
            if s == 0:
                rs = list(range(smooth_start[d], start[d]))
                rs.sort(reverse=True)
            elif s == 1:
                rs = list(range(stop[d], smooth_stop[d]))
            # smoothing
            for f, i in enumerate(rs):
                slc = copy.copy(sliceTemplate)
                slc[d] = slice(i, i + 1, None)
                edgeArr = edges[s].reshape(arr[slc].shape)
                #arr[slc] += edgeArr * (smooth - f)
                arr[slc] = arr[slc] + edgeArr * (smooth - f)  # casting rule

    arr = N.ascontiguousarray(arr)
    return arr
예제 #2
0
def _smoothBorder(arr, start, stop, smooth, value):
    """
    start, stop: [z,y,x]
    """
    # prepare coordinates
    shape = N.array(arr.shape)
    start = N.ceil(start).astype(N.int16)
    stop = N.ceil(stop).astype(N.int16)
    smooth_start = start - smooth
    smooth_stop = stop + smooth
    smooth_start = N.where(smooth_start < 0, 0, smooth_start)
    smooth_stop = N.where(smooth_stop > shape, shape, smooth_stop)
    #print smooth_start, smooth_stop

    import copy
    sliceTemplate = [slice(None,None,None)] * arr.ndim
    shapeTemplate = list(shape)
    for d in range(arr.ndim):
        smooth_shape = shapeTemplate[:d] + shapeTemplate[d+1:]

        # make an array containing the edge value
        edges = N.empty([2] + smooth_shape, N.float32)
        # start side
        slc = copy.copy(sliceTemplate)
        slc[d] = slice(start[d], start[d]+1, None)
        edges[0] = arr[slc].reshape(smooth_shape)
        # stop side
        slc = copy.copy(sliceTemplate)
        slc[d] = slice(stop[d]-1, stop[d], None)
        edges[1] = arr[slc].reshape(smooth_shape)

        edges = (edges - value) / float(smooth + 1) # this value can be array??

        # both side
        for s, side in enumerate([start, stop]):
            if s == 0:
                rs = list(range(smooth_start[d], start[d]))
                rs.sort(reverse=True)
            elif s == 1:
                rs = list(range(stop[d], smooth_stop[d]))
            # smoothing
            for f,i in enumerate(rs):
                slc = copy.copy(sliceTemplate)
                slc[d] = slice(i,i+1,None)
                edgeArr = edges[s].reshape(arr[slc].shape)
                #arr[slc] += edgeArr * (smooth - f) 
                arr[slc] = arr[slc] + edgeArr * (smooth - f) # casting rule

    arr = N.ascontiguousarray(arr)
    return arr
예제 #3
0
def img2polar2D(img,
                center,
                final_radius=None,
                initial_radius=None,
                phase_width=360,
                return_idx=False):
    """
    img: array
    center: coordinate y, x
    final_radius: ending radius
    initial_radius: starting radius
    phase_width: npixles / circle
    return_idx: return transformation coordinates (y,x)
    """
    if img.ndim > 2 or len(center) > 2:
        raise ValueError(
            'this function only support 2D, you entered %i-dim array and %i-dim center coordinate'
            % (img.ndim, len(center)))

    if initial_radius is None:
        initial_radius = 0

    if final_radius is None:
        rad0 = N.ceil(N.array(img.shape) - center)
        final_radius = min((int(min(rad0)), int(min(N.ceil(center)))))

    if phase_width is None:
        phase_width = N.sum(img.shape[-2:]) * 2

    theta, R = np.meshgrid(np.linspace(0, 2 * np.pi, phase_width),
                           np.arange(initial_radius, final_radius))

    Ycart, Xcart = polar2cart2D(R, theta, center)

    Ycart = N.where(Ycart >= img.shape[0], img.shape[0] - 1, Ycart)
    Xcart = N.where(Xcart >= img.shape[1], img.shape[1] - 1, Xcart)

    Ycart = Ycart.astype(int)
    Xcart = Xcart.astype(int)

    polar_img = img[Ycart, Xcart]
    polar_img = np.reshape(polar_img,
                           (final_radius - initial_radius, phase_width))

    if return_idx:
        return polar_img, Ycart, Xcart
    else:
        return polar_img
예제 #4
0
def apodize(img, napodize=10, doZ=True):
    """
    softens the edges of a singe xy section to reduce edge artifacts and improve the fits

    return copy of the img
    """
    img = img.copy()
    img = img.astype(N.float32) # casting rule

    # determine napodize
    shape = N.array(img.shape) // 2
    napodize = N.where(shape < napodize, shape, napodize)
    if doZ and img.ndim >= 3 and img.shape[0] > 3:
        rr = list(range(-3,0))
    else:
        rr = list(range(-2,0))
    for idx in rr:
        fact = N.arange(1./napodize[idx],napodize[idx],1./napodize[idx], dtype=N.float32)[:napodize[idx]]
        for napo in range(napodize[idx]):
            slc0 = [Ellipsis,slice(napo, napo+1)] + [slice(None)] * abs(idx+1)
            if not napo:
                slc1 = [Ellipsis,slice(-(napo+1),None)] + [slice(None)] * abs(idx+1)
            else:
                slc1 = [Ellipsis,slice(-(napo+1),-(napo))] + [slice(None)] * abs(idx+1)
            img[slc0] *= fact[napo]
            img[slc1] *= fact[napo]
                #img[slc0] = img[slc0] * fact[napo] # casting rule
                #img[slc1] = img[scl1] * fact[napo]
    return img
예제 #5
0
def apodize(img, napodize=10, doZ=True):
    """
    softens the edges of a singe xy section to reduce edge artifacts and improve the fits

    return copy of the img
    """
    img = img.copy()
    img = img.astype(N.float32)  # casting rule

    # determine napodize
    shape = N.array(img.shape) // 2
    napodize = N.where(shape < napodize, shape, napodize)
    if doZ and img.ndim >= 3 and img.shape[0] > 3:
        rr = range(-3, 0)
    else:
        rr = range(-2, 0)
    for idx in rr:
        fact = N.arange(1. / napodize[idx],
                        napodize[idx],
                        1. / napodize[idx],
                        dtype=N.float32)[:napodize[idx]]
        for napo in range(napodize[idx]):
            slc0 = [Ellipsis, slice(napo, napo + 1)
                    ] + [slice(None)] * abs(idx + 1)
            if not napo:
                slc1 = [Ellipsis, slice(-(napo + 1), None)
                        ] + [slice(None)] * abs(idx + 1)
            else:
                slc1 = [Ellipsis, slice(-(napo + 1), -(napo))
                        ] + [slice(None)] * abs(idx + 1)
            img[slc0] *= fact[napo]
            img[slc1] *= fact[napo]
            #img[slc0] = img[slc0] * fact[napo] # casting rule
            #img[slc1] = img[scl1] * fact[napo]
    return img
예제 #6
0
def mask_gaussianND(arr, zyx, v, sigma=2., ret=None, rot=0, clipZero=True):
    ''' 
    subtract elliptical gaussian at y,x with peakVal v
    if ret, return arr, else, arr itself is edited
    '''
    from . import imgGeo
    zyx = N.asarray(zyx)
    ndim = arr.ndim
    shape = N.array(arr.shape)
    try:
        if len(sigma) != ndim:
            raise ValueError('len(sigma) must be the same as len(shape)')
        else:
            sigma = N.asarray(sigma)
    except TypeError:#(TypeError, ValueError):
        sigma = N.asarray([sigma]*ndim)

    # prepare small window
    slc = imgGeo.nearbyRegion(shape, N.floor(zyx), sigma * 10)
    inds, LD = imgFit.rotateIndicesND(slc, dtype=N.float32, rot=rot)
    param = (0, v,) + tuple(zyx) + tuple(sigma)
    sidx = 2 + ndim
    g = imgFit.yGaussianND(N.asarray(param), inds, sidx).astype(arr.dtype.type)
    roi = arr[slc]
    if clipZero:
        g = N.where(g > roi, roi, g)

    if ret:
        e = N.zeros_like(arr)
        e[slc] = g  # this may be faster than copy()
        return arr - e
    else:
        arr[slc] -= g
예제 #7
0
def nanFilter(af, kernel=3):
    """
    3D phase contrast filter often creates 'nan'
    this filter removes nan by averaging surrounding pixels
    return af
    """
    af = af.copy()
    shape = N.array(af.shape)
    radius = N.subtract(kernel, 1) // 2
    box = kernel * af.ndim
    nan = N.isnan(af)
    nids = N.array(N.nonzero(nan)).T
    for nidx in nids:
        slc = [slice(idx,idx+1) for idx in nidx]
        slices = []
        for dim in range(af.ndim):
            slc2 = slice(slc[dim].start - radius, slc[dim].stop + radius)
            while slc2.start < 0:
                slc2 = slice(slc2.start + 1, slc2.stop)
            while slc2.stop > shape[dim]:
                slc2 = slice(slc2.start, slc2.stop -1)
            slices.append(slc2)
            
        val = af[slices]
        nanlocal = N.isnan(val)
        ss = N.sum(N.where(nanlocal, 0, val)) / float((box - N.sum(nanlocal)))
        af[slc] = ss
    return af
예제 #8
0
def nanFilter(af, kernel=3):
    """
    3D phase contrast filter often creates 'nan'
    this filter removes nan by averaging surrounding pixels
    return af
    """
    af = af.copy()
    shape = N.array(af.shape)
    radius = N.subtract(kernel, 1) // 2
    box = kernel * af.ndim
    nan = N.isnan(af)
    nids = N.array(N.nonzero(nan)).T
    for nidx in nids:
        slc = [slice(idx, idx + 1) for idx in nidx]
        slices = []
        for dim in range(af.ndim):
            slc2 = slice(slc[dim].start - radius, slc[dim].stop + radius)
            while slc2.start < 0:
                slc2 = slice(slc2.start + 1, slc2.stop)
            while slc2.stop > shape[dim]:
                slc2 = slice(slc2.start, slc2.stop - 1)
            slices.append(slc2)

        val = af[slices]
        nanlocal = N.isnan(val)
        ss = N.sum(N.where(nanlocal, 0, val)) / float((box - N.sum(nanlocal)))
        af[slc] = ss
    return af
예제 #9
0
def img2polar2D(img, center, final_radius=None, initial_radius = None, phase_width = 360, return_idx=False):
    """
    img: array
    center: coordinate y, x
    final_radius: ending radius
    initial_radius: starting radius
    phase_width: npixles / circle
    return_idx: return transformation coordinates (y,x)
    """
    if img.ndim > 2 or len(center) > 2:
        raise ValueError('this function only support 2D, you entered %i-dim array and %i-dim center coordinate' % (img.ndim, len(center)))
    
    if initial_radius is None:
        initial_radius = 0

    if final_radius is None:
        rad0 = N.ceil(N.array(img.shape) - center)
        final_radius = min((int(min(rad0)), int(min(N.ceil(center)))))

    if phase_width is None:
        phase_width = N.sum(img.shape[-2:]) * 2

    theta , R = np.meshgrid(np.linspace(0, 2*np.pi, phase_width), 
                            np.arange(initial_radius, final_radius))

    Ycart, Xcart = polar2cart2D(R, theta, center)

    Ycart = N.where(Ycart >= img.shape[0], img.shape[0]-1, Ycart)
    Xcart = N.where(Xcart >= img.shape[1], img.shape[1]-1, Xcart)
    
    Ycart = Ycart.astype(int)
    Xcart = Xcart.astype(int)


    polar_img = img[Ycart,Xcart]
    polar_img = np.reshape(polar_img,(final_radius-initial_radius,phase_width))

    if return_idx:
        return polar_img, Ycart, Xcart
    else:
        return polar_img
예제 #10
0
def keepShape(a, shape, difmod=None):
    canvas = N.zeros(shape, a.dtype.type)

    if difmod is None:
        dif = (shape - N.array(a.shape, N.float32)) / 2.
        mod = N.ceil(N.mod(dif, 1))
    else:
        dif, mod = difmod
    dif = N.where(dif > 0, N.ceil(dif), N.floor(dif))

    # smaller
    aoff = N.where(dif < 0, 0, dif)
    aslc = [slice(dp, shape[i]-dp+mod[i]) for i, dp in enumerate(aoff)]

    # larger
    coff = N.where(dif > 0, 0, -dif)
    cslc = [slice(dp, a.shape[i]-dp+mod[i]) for i, dp in enumerate(coff)]

    canvas[aslc] = a[cslc]

    if difmod is None:
        return canvas, mod
    else:
        return canvas
예제 #11
0
def keepShape(a, shape, difmod=None):
    canvas = N.zeros(shape, a.dtype.type)

    if difmod is None:
        dif = (shape - N.array(a.shape, N.float32)) / 2.
        mod = N.ceil(N.mod(dif, 1))
    else:
        dif, mod = difmod
    dif = N.where(dif > 0, N.ceil(dif), N.floor(dif))

    # smaller
    aoff = N.where(dif < 0, 0, dif)
    aslc = [slice(dp, shape[i] - dp + mod[i]) for i, dp in enumerate(aoff)]

    # larger
    coff = N.where(dif > 0, 0, -dif)
    cslc = [slice(dp, a.shape[i] - dp + mod[i]) for i, dp in enumerate(coff)]

    canvas[aslc] = a[cslc]

    if difmod is None:
        return canvas, mod
    else:
        return canvas
예제 #12
0
def mask_gaussianND(arr, zyx, v, sigma=2., ret=None, rot=0, clipZero=True):
    ''' 
    subtract elliptical gaussian at y,x with peakVal v
    if ret, return arr, else, arr itself is edited
    '''
    from . import imgGeo
    zyx = N.asarray(zyx)
    ndim = arr.ndim
    shape = N.array(arr.shape)
    try:
        if len(sigma) != ndim:
            raise ValueError('len(sigma) must be the same as len(shape)')
        else:
            sigma = N.asarray(sigma)
    except TypeError:  #(TypeError, ValueError):
        sigma = N.asarray([sigma] * ndim)

    # prepare small window
    slc = imgGeo.nearbyRegion(shape, N.floor(zyx), sigma * 10)
    inds, LD = imgFit.rotateIndicesND(slc, dtype=N.float32, rot=rot)
    param = (
        0,
        v,
    ) + tuple(zyx) + tuple(sigma)
    sidx = 2 + ndim
    g = imgFit.yGaussianND(N.asarray(param), inds, sidx).astype(arr.dtype.type)
    roi = arr[slc]
    if clipZero:
        g = N.where(g > roi, roi, g)

    if ret:
        e = N.zeros_like(arr)
        e[slc] = g  # this may be faster than copy()
        return arr - e
    else:
        arr[slc] -= g
예제 #13
0
def rotateIndicesND(slicelist, dtype=N.float64, rot=0, mode=2):
    """
    slicelist: even shape works much better than odd shape
    rot:       counter-clockwise, xy-plane
    mode:      testing different ways of doing, (1 or 2 and the same result)

    return inds, LD
    """
    global INDS_DIC
    shape = []
    LD = []

    for sl in slicelist:
        if isinstance(sl, slice):
            shape.append(sl.stop - sl.start)
            LD.append(sl.start)

    shapeTuple = tuple(shape + [rot])
    if shapeTuple in INDS_DIC:
        inds = INDS_DIC[shapeTuple]
    else:
        shape = N.array(shape)
        ndim = len(shape)
        odd_even = shape % 2

        s2 = N.ceil(shape * (2**0.5))

        if mode == 1:  # everything is even
            s2 = N.where(s2 % 2, s2 + 1, s2)
        elif mode == 2:  # even & even or odd & odd
            for d, s in enumerate(shape):
                if (s % 2 and not s2[d] % 2) or (not s % 2 and s2[d] % 2):
                    s2[d] += 1
        cent = s2 / 2.
        dif = (s2 - shape) / 2.
        dm = dif % 1
        # print s2, cent, dif, dm
        slc = [Ellipsis] + [
            slice(int(d),
                  int(d) + shape[i]) for i, d in enumerate(dif)
        ]
        # This slice is float which shift array when cutting out!!

        s2 = tuple([int(ss)
                    for ss in s2])  # numpy array cannot use used for slice
        inds = N.indices(s2, N.float32)
        ind_shape = inds.shape
        nz = N.product(ind_shape[:-2])
        nsec = nz / float(ndim)
        if ndim > 2:
            inds = N.reshape(inds, (nz, ) + ind_shape[-2:])
        irs = N.empty_like(inds)
        for d, ind in enumerate(inds):
            idx = int(d // nsec)
            c = cent[idx]
            if rot and inds.ndim > 2:
                U.trans2d(ind - c, irs[d], (0, 0, rot, 1, 0, 1))
                irs[d] += c - dif[idx]
            else:
                irs[d] = ind - dif[idx]

        if len(ind_shape) > 2:
            irs = N.reshape(irs, ind_shape)

        irs = irs[slc]
        if mode == 1 and N.sometrue(dm):
            inds = N.empty_like(irs)
            # print 'translate', dm
            for d, ind in enumerate(irs):
                U.trans2d(ind, inds[d], (-dm[1], -dm[0], 0, 1, 0, 1))
        else:
            inds = irs
        INDS_DIC[shapeTuple] = inds

    r_inds = N.empty_like(inds)
    for d, ld in enumerate(LD):
        r_inds[d] = inds[d] + ld

    return r_inds, LD
예제 #14
0
def Xcorr(a, b, highpassSigma=2.5, wiener=0.2, cutoffFreq=3,
forceSecondPeak=None, acceptOrigin=True, maskSigmaFact=1., removeY=None, removeX=None, ret=None, normalize=True, gFit=True, lap=None, win=11):
    """
    returns (y,x), image
    if ret is True, returns [v, yx, image]

    to get yx cordinate of the image,
    yx += N.divide(picture.shape, 2)

    a, b:            2D array
    highpassSigma:   sigma value used for highpass pre-filter
    wiener:          wiener value used for highpass pre-filter
    cutoffFreq:      kill lowest frequency component from 0 to this level
    forceSecondPeak: If input is n>0 (True is 1), pick up n-th peak
    acceptOrigin:    If None, result at origin is rejected, look for the next peak
    maskSigmaFact:   Modifier to remove previous peak to look for another peak
    removeYX:        Rremove given number of pixel high intensity lines of the Xcorr
                     Y: Vertical, X: Horizontal
    normalize:       intensity normalized
    gFit:            peak is fitted to 2D gaussian array, if None use center of mass
    win:             window for gFit

    if b is a + (y,x) then, answer is (-y,-x)
    """
    shapeA = N.asarray(a.shape)
    shapeB = N.asarray(b.shape)
    shapeM = N.max([shapeA, shapeB], axis=0)
    shapeM = N.where(shapeM % 2, shapeM+1, shapeM)
    center = shapeM / 2.

    arrs = [a,b]
    arrsS = ['a','b']
    arrsF = []
    for i, arr in enumerate(arrs):
        if arr.dtype not in [N.float32, N.float64]:
            arr = N.asarray(arr, N.float32)
        # this convolution has to be done beforehand to remove 2 pixels at the edge
        if lap == 'nothing':
            pass
        elif lap:
            arr = arr_Laplace(arr, mask=2)
        else:
            arr = arr_sorbel(arr, mask=1)
    
        if N.sometrue(shapeA < shapeM):
            arr = paddingMed(arr, shapeM)

        if normalize:
            mi, ma, me, sd = U.mmms(arr)
            arr = (arr - me) / sd
    
        if i ==1:
            arr = F.shift(arr)
        af = F.rfft(arr)

        af = highPassF(af, highpassSigma, wiener, cutoffFreq)
        arrsF.append(af)

    # start cross correlation
    af, bf = arrsF
    bf = bf.conjugate()
    cf = af * bf

    # go back to space domain
    c = F.irfft(cf)
  #  c = _changeOrigin(cr)

    # removing lines
    if removeX:
        yi, xi = N.indices((removeX, shapeM[-1]))#sx))
        yi += center[-2] - removeX/2.#sy/2 - removeX/2
        c[yi, xi] = 0
    if removeY:
        yi, xi = N.indices((shapeM[-2], removeY))#sy, removeY))
        xi += center[-1] - removeY/2.#sx/2 - removeY/2
        c[yi, xi] = 0

    # find the first peak
    if gFit:
        v, yx, s = findMaxWithGFit(c, win=win)#, window=win, gFit=gFit)
        if v == 0:
            v, yx, s = findMaxWithGFit(c, win=win+2)#, window=win+2, gFit=gFit)
            if v == 0:
                v = U.findMax(c)[0]
        yx = N.add(yx, 0.5)
        #yx += 0.5
    else:
        vzyx = U.findMax(c)
        v = vzyx[0]
        yx = vzyx[-2:]
        s = 2.5

    yx -= center

    if N.alltrue(N.abs(yx) < 1.0) and not acceptOrigin:
        forceSecondPeak = True

    # forceSecondPeak:
    if not forceSecondPeak:
        forceSecondPeak = 0
    for i in range(int(forceSecondPeak)):
        print('%i peak was removed' % (i+1)) #, sigma: %.2f' % (i+1, s)
        yx += center
        g = gaussianArr2D(c.shape, sigma=s/maskSigmaFact, peakVal=v, orig=yx)
        c = c - g
        #c = mask_gaussian(c, yx[0], yx[1], v, s)
        if gFit:
            v, yx, s = findMaxWithGFit(c, win=win)#, window=win, gFit=gFit)
            if v == 0:
                v, yx, s = findMaxWithGFit(c, win=win+2)#, window=win+2, gFit=gFit)
                if v == 0:
                    v = U.findMax(c)[0]
            yx -= (center - 0.5)
        else:
            vzyx = U.findMax(c)
            v = vzyx[0]

    if not gFit:
        yx = centerOfMass(c, vzyx[-2:]) - center
    if lap is not 'nothing':
        c = paddingValue(c, shapeM+2)

    if ret == 2:
        return yx, af, bf.conjugate()
    elif ret:
        return v, yx, c
    else:
        return yx, c
예제 #15
0
def pointsCutOut3D(arr, posList, windowSize=100, d2=None, interpolate=True, removeWrongShape=True):
    """
    array:       nd array
    posList:     ([(z,)y,x]...)
    windowSize:  scalar (in pixel or as percent < 1.) or ((z,)y,x)
                 if arr.ndim > 2, and len(windowSize) == 2, then
                 cut out section-wise (higher dimensions stay the same)
    d2:          conern only XY of windowSize (higher dimensions stay the same)
    interpolate: shift array by subpixel interpolation to adjust center

    return:      list of array centered at each pos in posList
    """

    shape = N.array(arr.shape)
    center = shape / 2.

    if arr.ndim <= 2:
        ndim_arr = arr.ndim
    else:
        ndim_arr = 3

    # prepare N-dimensional window size
    try:
        len(windowSize) # seq
        if d2:
            windowSize = windowSize[-2:]
        if len(windowSize) != arr.ndim:
            dim = len(windowSize)
            windowSize = tuple(shape[:-dim]) + tuple(windowSize)
    except TypeError: # scaler
        if windowSize < 1 and windowSize > 0: # percentage
            w = shape * windowSize
            if d2:
                w[:-2] = shape[:-2]
            elif ndim_arr > 3:
                w[:-3] = shape[:-3]
            windowSize = w.astype(N.uint)
        else:
            windowSize = N.where(shape >= windowSize, windowSize, shape)
            if d2:
                windowSize[:-2] = shape[:-2]
    windowSize = N.asarray(windowSize)
    halfWin = windowSize / 2
    #ndim_win = len(windowSize)

    margin = int(interpolate)
    # cutout individual position
    arrList = []
    for pos in posList:
        ndim_pos = len(pos)

        # calculate idx
        dif = pos +0.5 - halfWin[-ndim_pos:]

        dif = N.round_(dif)
        dif = dif.astype(N.int)
        starts = dif - margin
        starts = N.where(starts < 0, 0, starts)
        stops = dif + windowSize[-ndim_pos:] + margin
        stops = N.where(stops > shape[-ndim_pos:], shape[-ndim_pos:], stops)

        #if removeWrongShape and (N.any((starts) < 0) or N.any((stops) > shape[-ndim_pos:])):
        #    continue
        
        slc = [Ellipsis]
        for dim, s0 in enumerate(starts):
            s1 = stops[dim]
            slc.append(slice(s0, s1))
        cpa = arr[slc]
        
        if interpolate:
            dif = pos + 0.5 - halfWin[-ndim_pos:]
            sub = (arr.ndim - len(dif)) * [0] + list(dif)
            sub = N.array(sub)
            sar = U.nd.shift(cpa, sub % 1)

            slc = [Ellipsis]
            for d in range(ndim_arr):
                slc.append(slice(margin, -margin))
            cpa = sar[slc]

        arrList.append(cpa)


    if removeWrongShape:
        if d2 and arr.ndim == 2:
            arrList = N.array([a for a in arrList if N.all(a.shape[-2:] == windowSize[-2:])])
        else:
            arrList = N.array([a for a in arrList if N.all(a.shape[-3:] == windowSize[-3:])])
    return arrList
예제 #16
0
def pointsCutOutND(arr, posList, windowSize=100, sectWise=None, interpolate=True):
    """
    array:       nd array
    posList:     ([(z,)y,x]...)
    windowSize:  scalar (in pixel or as percent < 1.) or ((z,)y,x)
                 if arr.ndim > 2, and len(windowSize) == 2, then
                 cut out section-wise (higher dimensions stay the same)
    sectWise:    conern only XY of windowSize (higher dimensions stay the same)
    interpolate: shift array by subpixel interpolation to adjust center

    return:      list of array centered at each pos in posList
    """
    shape = N.array(arr.shape)
    center = shape / 2.
    # prepare N-dimensional window size
    try:
        len(windowSize) # seq
        if sectWise:
            windowSize = windowSize[-2:]
        if len(windowSize) != arr.ndim:
            dim = len(windowSize)
            windowSize = tuple(shape[:-dim]) + tuple(windowSize)
    except TypeError: # scaler
        if windowSize < 1 and windowSize > 0: # percentage
            w = shape * windowSize
            if sectWise:
                w[:-2] = shape[:-2]
            windowSize = w.astype(N.uint16)
        else:
            windowSize = N.where(shape >= windowSize, windowSize, shape)
            if sectWise:
                windowSize = arr.shape[:-2] + tuple(windowSize[-2:])
    windowSize = N.asarray(windowSize)

    # cutout individual position
    arrList=[]
    for pos in posList:
        # prepare N-dimensional coordinate
        n = len(pos)
        if n != len(windowSize):
            temp = center.copy()
            center[-n:] = pos
            pos = center
        
        # calculate idx
        ori = pos - (windowSize / 2.) # float value
        oidx = N.ceil(ori) # idx
        subpxl = oidx - ori # subpixel mod
        if interpolate and N.sometrue(subpxl): # comit to make shift
            SHIFT = 1
        else:
            SHIFT = 0

        # prepare slice
        # when comitted to make shift, first cut out window+1,
        # then make subpixle shift, and then cutout 1 edge
        slc = [Ellipsis] # Ellipsis is unnecessary, just in case...
        slc_edge = [slice(1,-1,None)] * arr.ndim
        for d in range(arr.ndim):
            start = oidx[d] - SHIFT
            if start < 0: 
                start = 0
                slc_edge[d] = slice(0, slc_edge[d].stop, None)
            stop = oidx[d] + windowSize[d] + SHIFT
            if stop > shape[d]: 
                stop = shape[d]
                slc_edge[d] = slice(slc_edge[d].start, shape[d], None)
            slc += [slice(int(start), int(stop), None)]

        # cutout, shift and cutout
        #print(slc, slc_edge)
        try:
            canvas = arr[slc]
            if SHIFT:
                # 20180214 subpixel shift +0.5 was fixed
                #raise RuntimeError('check')
                if sectWise:
                    subpxl[-2:] = N.where(windowSize[-2:] > 1, subpxl[-2:]-0.5, subpxl[-2:])
                else:
                    subpxl[-n:] = N.where(windowSize[-n:] > 1, subpxl[-n:]-0.5, subpxl[-n:])
                canvas = U.nd.shift(canvas, subpxl)
                canvas = canvas[slc_edge]
            check = 1
        except IndexError:
            print('position ', pos, ' was skipped')
            check = 0
            raise
        if check:
            arrList += [N.ascontiguousarray(canvas)]

    return arrList
예제 #17
0
def pointsCutOutND(arr,
                   posList,
                   windowSize=100,
                   sectWise=None,
                   interpolate=True):
    """
    array:       nd array
    posList:     ([(z,)y,x]...)
    windowSize:  scalar (in pixel or as percent < 1.) or ((z,)y,x)
                 if arr.ndim > 2, and len(windowSize) == 2, then
                 cut out section-wise (higher dimensions stay the same)
    sectWise:    conern only XY of windowSize (higher dimensions stay the same)
    interpolate: shift array by subpixel interpolation to adjust center

    return:      list of array centered at each pos in posList
    """
    shape = N.array(arr.shape)
    center = shape / 2.
    # prepare N-dimensional window size
    try:
        len(windowSize)  # seq
        if sectWise:
            windowSize = windowSize[-2:]
        if len(windowSize) != arr.ndim:
            dim = len(windowSize)
            windowSize = tuple(shape[:-dim]) + tuple(windowSize)
    except TypeError:  # scaler
        if windowSize < 1 and windowSize > 0:  # percentage
            w = shape * windowSize
            if sectWise:
                w[:-2] = shape[:-2]
            windowSize = w.astype(N.uint16)
        else:
            windowSize = N.where(shape >= windowSize, windowSize, shape)
            if sectWise:
                windowSize = arr.shape[:-2] + windowSize[-2:]
    windowSize = N.asarray(windowSize)

    # cutout individual position
    arrList = []
    for pos in posList:
        # prepare N-dimensional coordinate
        n = len(pos)
        if n != len(windowSize):
            temp = center.copy()
            center[-n:] = pos
            pos = center

        # calculate idx
        ori = pos - (windowSize / 2.)  # float value
        oidx = N.ceil(ori)  # idx
        subpxl = oidx - ori  # subpixel mod
        if interpolate and N.sometrue(subpxl):  # comit to make shift
            SHIFT = 1
        else:
            SHIFT = 0

        # prepare slice
        # when comitted to make shift, first cut out window+1,
        # then make subpixle shift, and then cutout 1 edge
        slc = [Ellipsis]  # Ellipsis is unnecessary, just in case...
        slc_edge = [slice(1, -1, None)] * arr.ndim
        for d in range(arr.ndim):
            start = oidx[d] - SHIFT
            if start < 0:
                start = 0
                slc_edge[d] = slice(0, slc_edge[d].stop, None)
            stop = oidx[d] + windowSize[d] + SHIFT
            if stop > shape[d]:
                stop = shape[d]
                slc_edge[d] = slice(slc_edge[d].start, shape[d], None)
            slc += [slice(int(start), int(stop), None)]

        # cutout, shift and cutout
        try:
            canvas = arr[slc]
            if SHIFT:
                canvas = U.nd.shift(canvas, subpxl)
                canvas = canvas[slc_edge]
            check = 1
        except IndexError:
            print('position ', pos, ' was skipped')
            check = 0
            raise
        if check:
            arrList += [N.ascontiguousarray(canvas)]

    return arrList
예제 #18
0
def getShift(shift, ZYX, erosionZYX=0):
    """
    shift: zyxrmm
    return [zmin,zmax,ymin,ymax,xmin,xmax]
    """
    # erosion
    try:
        if len(erosionZYX) == 3:
            erosionZ = erosionZYX[0]
            erosionYX = erosionZYX[1:]
        elif len(erosionZYX) == 2:
            erosionZ = 0
            erosionYX = erosionZYX
        elif len(erosionZYX) == 1:
            erosionZ = 0
            erosionYX = erosionZYX[0]
    except TypeError:  # scalar
        erosionZ = erosionZYX
        erosionYX = erosionZYX

    # magnification
    magZYX = N.ones((3, ), N.float32)
    magZYX[3 - len(shift[4:]):] = shift[4:]
    if len(shift[4:]) == 1:
        magZYX[1] = shift[4]

    # rotation
    r = shift[3]

    # target shape
    ZYX = N.asarray(ZYX, N.float32)
    ZYXm = ZYX * magZYX

    # Z
    z = N.where(shift[0] < 0, N.floor(shift[0]), N.ceil(shift[0]))
    ztop = ZYXm[0] + z
    nz = N.ceil(N.where(ztop > ZYX[0], ZYX[0], ztop))
    z += erosionZ
    nz -= erosionZ
    if z < 0:
        z = 0
    if nz < 0:
        nz = z + 1
    zyx0 = N.ceil((ZYX - ZYXm) / 2.)
    #print zyx0
    #if zyx0[0] > 0:
    #    z -= zyx0[0]
    #    nz += zyx0[0]

    zs = N.array([z, nz])

    # YX
    #try:
    #    if len(erosionYX) != 2:
    #        raise ValueError, 'erosion is only applied to lateral dimension'
    #except TypeError:
    #    erosionYX = (erosionYX, erosionYX)

    yxShift = N.where(shift[1:3] < 0, N.floor(shift[1:3]), N.ceil(shift[1:3]))

    # rotate the magnified center
    xyzm = N.ceil(ZYXm[::-1]) / 2.
    xyr = imgGeo.RotateXY(xyzm[:-1], r)
    xyr -= xyzm[:-1]
    yx = xyr[::-1]
    leftYX = N.ceil(N.abs(yx))
    rightYX = -N.ceil(N.abs(yx))

    # then translate
    leftYXShift = (leftYX + yxShift) + zyx0[1:]

    leftYXShift = N.where(leftYXShift < 0, 0, leftYXShift)

    rightYXShift = (rightYX + yxShift) - zyx0[1:]
    YXmax = N.where(ZYXm[1:] > ZYX[1:], ZYXm[1:], ZYX[1:])
    rightYXShift = N.where(rightYXShift > 0, YXmax,
                           rightYXShift + YXmax)  # deal with - idx

    rightYXShift = N.where(rightYXShift > ZYX[1:], ZYX[1:], rightYXShift)

    leftYXShift += erosionYX
    rightYXShift -= erosionYX

    # (z0,z1,y0,y1,x0,x1)
    tempZYX = N.array(
        (zs[0], zs[1], int(N.ceil(leftYXShift[0])), int(rightYXShift[0]),
         int(N.ceil(leftYXShift[1])), int(rightYXShift[1])))
    return tempZYX
예제 #19
0
def Xcorr(a,
          b,
          highpassSigma=2.5,
          wiener=0.2,
          cutoffFreq=3,
          forceSecondPeak=None,
          acceptOrigin=True,
          maskSigmaFact=1.,
          removeY=None,
          removeX=None,
          ret=None,
          normalize=True,
          gFit=True,
          lap=None,
          win=11):
    """
    returns (y,x), image
    if ret is True, returns [v, yx, image]

    to get yx cordinate of the image,
    yx += N.divide(picture.shape, 2)

    a, b:            2D array
    highpassSigma:   sigma value used for highpass pre-filter
    wiener:          wiener value used for highpass pre-filter
    cutoffFreq:      kill lowest frequency component from 0 to this level
    forceSecondPeak: If input is n>0 (True is 1), pick up n-th peak
    acceptOrigin:    If None, result at origin is rejected, look for the next peak
    maskSigmaFact:   Modifier to remove previous peak to look for another peak
    removeYX:        Rremove given number of pixel high intensity lines of the Xcorr
                     Y: Vertical, X: Horizontal
    normalize:       intensity normalized
    gFit:            peak is fitted to 2D gaussian array, if None use center of mass
    win:             window for gFit

    if b is a + (y,x) then, answer is (-y,-x)
    """
    shapeA = N.asarray(a.shape)
    shapeB = N.asarray(b.shape)
    shapeM = N.max([shapeA, shapeB], axis=0)
    shapeM = N.where(shapeM % 2, shapeM + 1, shapeM)
    center = shapeM / 2.

    arrs = [a, b]
    arrsS = ['a', 'b']
    arrsF = []
    for i, arr in enumerate(arrs):
        if arr.dtype not in [N.float32, N.float64]:
            arr = N.asarray(arr, N.float32)
        # this convolution has to be done beforehand to remove 2 pixels at the edge
        if lap == 'nothing':
            pass
        elif lap:
            arr = arr_Laplace(arr, mask=2)
        else:
            arr = arr_sorbel(arr, mask=1)

        if N.sometrue(shapeA < shapeM):
            arr = paddingMed(arr, shapeM)

        if normalize:
            mi, ma, me, sd = U.mmms(arr)
            arr = (arr - me) / sd

        if i == 1:
            arr = F.shift(arr)
        af = F.rfft(arr)

        af = highPassF(af, highpassSigma, wiener, cutoffFreq)
        arrsF.append(af)

    # start cross correlation
    af, bf = arrsF
    bf = bf.conjugate()
    cf = af * bf

    # go back to space domain
    c = F.irfft(cf)
    #  c = _changeOrigin(cr)

    # removing lines
    if removeX:
        yi, xi = N.indices((removeX, shapeM[-1]))  #sx))
        yi += center[-2] - removeX / 2.  #sy/2 - removeX/2
        c[yi, xi] = 0
    if removeY:
        yi, xi = N.indices((shapeM[-2], removeY))  #sy, removeY))
        xi += center[-1] - removeY / 2.  #sx/2 - removeY/2
        c[yi, xi] = 0

    # find the first peak
    if gFit:
        v, yx, s = findMaxWithGFit(c, win=win)  #, window=win, gFit=gFit)
        if v == 0:
            v, yx, s = findMaxWithGFit(c, win=win +
                                       2)  #, window=win+2, gFit=gFit)
            if v == 0:
                v = U.findMax(c)[0]
        yx = N.add(yx, 0.5)
        #yx += 0.5
    else:
        vzyx = U.findMax(c)
        v = vzyx[0]
        yx = vzyx[-2:]
        s = 2.5

    yx -= center

    if N.alltrue(N.abs(yx) < 1.0) and not acceptOrigin:
        forceSecondPeak = True

    # forceSecondPeak:
    if not forceSecondPeak:
        forceSecondPeak = 0
    for i in range(int(forceSecondPeak)):
        print('%i peak was removed' % (i + 1))  #, sigma: %.2f' % (i+1, s)
        yx += center
        g = gaussianArr2D(c.shape, sigma=s / maskSigmaFact, peakVal=v, orig=yx)
        c = c - g
        #c = mask_gaussian(c, yx[0], yx[1], v, s)
        if gFit:
            v, yx, s = findMaxWithGFit(c, win=win)  #, window=win, gFit=gFit)
            if v == 0:
                v, yx, s = findMaxWithGFit(c, win=win +
                                           2)  #, window=win+2, gFit=gFit)
                if v == 0:
                    v = U.findMax(c)[0]
            yx -= (center - 0.5)
        else:
            vzyx = U.findMax(c)
            v = vzyx[0]

    if not gFit:
        yx = centerOfMass(c, vzyx[-2:]) - center
    if lap is not 'nothing':
        c = paddingValue(c, shapeM + 2)

    if ret == 2:
        return yx, af, bf.conjugate()
    elif ret:
        return v, yx, c
    else:
        return yx, c
예제 #20
0
def trans3D_affine(arr, tzyx=(0,0,0), r=0, mag=1, dzyx=(0,0,0), rzy=0, ncpu=NCPU, order=ORDER):#**kwds):
    """
    return array 
    """    
    dtype = arr.dtype.type
    arr = arr.astype(N.float32)
    
    ndim = arr.ndim
    if ndim == 2:
        arr = arr.reshape((1,)+arr.shape)
    elif ndim == 3:
        if len(tzyx) < ndim:
            tzyx = (0,)*(ndim-len(tzyx)) + tuple(tzyx)
        if len(dzyx) < ndim:
            dzyx = (0,)*(ndim-len(dzyx)) + tuple(dzyx)

    dzyx = N.asarray(dzyx)

    magz = 1
    try:
        if len(mag) == 3:
            magz = mag[0]
            mag = mag[1:]
    except TypeError:
        pass

    if ndim == 3 and (magz != 1 or tzyx[-3] or rzy):
        #print magz, arr.shape
        # because, mergins introduced after 2D transformation may interfere the result of this vertical transform, vertical axis was processed first, since rzy is 0 usually.
        arrT = arr.T # zyx -> xyz
        magzz = (1,magz)
        canvas = N.zeros_like(arrT)
        tzy = (0,tzyx[-3])
        dzy = (dzyx[-2], dzyx[-3])

        #if ncpu > 1 and mp:
        ret = ppro.pmap(_dothat, arrT, ncpu, tzy, rzy, magzz, dzy, order)
        for x, a in enumerate(ret):
            canvas[x] = a
        #else:
        #    for x, a in enumerate(arrT):
        #        canvas[x] = _dothat(a, tzy, rzy, magzz, dzy, order)

        arr = canvas.T
        #del arrT

    if N.any(tzyx[-2:]) or r or N.any(mag):
        #print ndim, arr.shape
        canvas = N.zeros_like(arr)
        if ndim == 3:# and ncpu > 1 and mp:
            # dividing XY into pieces did not work for rotation and magnification
            # here parallel processing is done section-wise since affine works only for 2D
            ret = ppro.pmap(_dothat, arr, ncpu, tzyx[-2:], r, mag, dzyx[-2:], order)
            for z, a in enumerate(ret):
                canvas[z] = a
        else:
            for z, a in enumerate(arr):
                canvas[z] = _dothat(a, tzyx[-2:], r, mag, dzyx[-2:], order)

        if ndim == 2:
            canvas = canvas[0]

        arr = canvas

    if dtype in (N.int, N.uint8, N.uint16, N.uint32):
        arr = N.where(arr < 0, 0, arr)
        
    return arr.astype(dtype)
예제 #21
0
def trans3D_spline(a, tzyx=(0,0,0), r=0, mag=1, dzyx=(0,0), rzy=0, mr=0, reshape=False, ncpu=1, **splinekwds):
    """
    mag: scalar_for_yx or [y,x] or [z,y,x]
    mr: rotational direction of yx-zoom in degrees
    ncpu: no usage
    """
    splinekwds['prefilter'] = splinekwds.get('prefilter', True)
    splinekwds['order'] = splinekwds.get('order', 3)
    
    ndim = a.ndim
    shape = N.array(a.shape, N.float32)
    tzyx = N.asarray(tzyx, N.float32)
    
    # rotation axis
    if ndim == 3:
        axes = (1,2)
    else:
        axes = (1,0)

    # magnification
    try:
        if len(mag) == 1: # same as scalar
            mag = [1] * (ndim-2) + list(mag) * 2
        else:
            mag = [1] * (ndim-2) * (3-len(mag)) + list(mag)
    except: # scalar -> convert to yx mag only
        mag = [1] * (ndim-2) + ([mag] * ndim)[:2]
    mag = N.asarray(mag)

    try:
        dzyx = N.array([0] * (ndim-2) * (3-len(dzyx)) + list(dzyx))
    except: # scalar
        pass

    if mr:
        a = U.nd.rotate(a, mr, axes=axes, reshape=reshape, **splinekwds)
        splinekwds['prefilter'] = False
        
    if N.any(dzyx):
        a = U.nd.shift(a, -dzyx, **splinekwds)
        splinekwds['prefilter'] = False

    if r:
        a = U.nd.rotate(a, -r, axes=axes, reshape=reshape, **splinekwds)
        splinekwds['prefilter'] = False

    if N.any(mag != 1):
        a = U.nd.zoom(a, zoom=mag, **splinekwds)
        splinekwds['prefilter'] = False

        if not reshape:
            dif = (shape - N.array(a.shape, N.float32)) / 2.
            mod = N.ceil(N.mod(dif, 1))
            tzyx[-ndim:] -= (mod / 2.)
        
    if rzy and ndim >= 3: # is this correct?? havn't tried yet
        a = U.nd.rotate(a, -rzy, axes=(0,1), reshape=reshape, **splinekwds)

    if N.any(dzyx):
        a = U.nd.shift(a, dzyx, **splinekwds)

    if mr:
        a = U.nd.rotate(a, -mr, axes=axes, reshape=reshape, **splinekwds)

    if reshape:
        a = U.nd.shift(a, tzyx[-ndim:], **splinekwds)
    else:
        tzyx0 = N.where(mag >= 1, tzyx[-ndim:], 0)
        if N.any(tzyx0[-ndim:]):
            a = U.nd.shift(a, tzyx0[-ndim:], **splinekwds)

    if N.any(mag != 1) and not reshape:
        a = keepShape(a, shape, (dif, mod))
        old="""
        canvas = N.zeros(shape, a.dtype.type)

        #dif = (shape - N.array(a.shape, N.float32)) / 2
        #mod = N.ceil(N.mod(dif, 1))
        dif = N.where(dif > 0, N.ceil(dif), N.floor(dif))

        # smaller
        aoff = N.where(dif < 0, 0, dif)
        aslc = [slice(dp, shape[i]-dp+mod[i]) for i, dp in enumerate(aoff)]

        # larger
        coff = N.where(dif > 0, 0, -dif)
        cslc = [slice(dp, a.shape[i]-dp+mod[i]) for i, dp in enumerate(coff)]

        canvas[aslc] = a[cslc]
        a = canvas"""

    if not reshape:
        tzyx0 = N.where(mag < 1, tzyx[-ndim:], 0)
        if N.any(mag != 1):
            tzyx0[-ndim:] -= (mod / 2.)
        if N.any(tzyx0[-ndim:]):
            a = U.nd.shift(a, tzyx0[-ndim:], **splinekwds)
        
    return a
예제 #22
0
def arr_log(arr):
    logArr = N.log(arr)
    return N.where(logArr < 0, 0, logArr)
예제 #23
0
def trans3D_spline(a,
                   tzyx=(0, 0, 0),
                   r=0,
                   mag=1,
                   dzyx=(0, 0),
                   rzy=0,
                   mr=0,
                   reshape=False,
                   ncpu=1,
                   **splinekwds):
    """
    mag: scalar_for_yx or [y,x] or [z,y,x]
    mr: rotational direction of yx-zoom in degrees
    ncpu: no usage
    """
    splinekwds['prefilter'] = splinekwds.get('prefilter', True)
    splinekwds['order'] = splinekwds.get('order', 3)

    ndim = a.ndim
    shape = N.array(a.shape, N.float32)
    tzyx = N.asarray(tzyx, N.float32)

    # rotation axis
    if ndim == 3:
        axes = (1, 2)
    else:
        axes = (1, 0)

    # magnification
    try:
        if len(mag) == 1:  # same as scalar
            mag = [1] * (ndim - 2) + list(mag) * 2
        else:
            mag = [1] * (ndim - 2) * (3 - len(mag)) + list(mag)
    except:  # scalar -> convert to yx mag only
        mag = [1] * (ndim - 2) + ([mag] * ndim)[:2]
    mag = N.asarray(mag)

    try:
        dzyx = N.array([0] * (ndim - 2) * (3 - len(dzyx)) + list(dzyx))
    except:  # scalar
        pass

    if mr:
        a = U.nd.rotate(a, mr, axes=axes, reshape=reshape, **splinekwds)
        splinekwds['prefilter'] = False

    if N.any(dzyx):
        a = U.nd.shift(a, -dzyx, **splinekwds)
        splinekwds['prefilter'] = False

    if r:
        a = U.nd.rotate(a, -r, axes=axes, reshape=reshape, **splinekwds)
        splinekwds['prefilter'] = False

    if N.any(mag != 1):
        a = U.nd.zoom(a, zoom=mag, **splinekwds)
        splinekwds['prefilter'] = False

        if not reshape:
            dif = (shape - N.array(a.shape, N.float32)) / 2.
            mod = N.ceil(N.mod(dif, 1))
            tzyx[-ndim:] -= (mod / 2.)

    if rzy and ndim >= 3:  # is this correct?? havn't tried yet
        a = U.nd.rotate(a, -rzy, axes=(0, 1), reshape=reshape, **splinekwds)

    if N.any(dzyx):
        a = U.nd.shift(a, dzyx, **splinekwds)

    if mr:
        a = U.nd.rotate(a, -mr, axes=axes, reshape=reshape, **splinekwds)

    if reshape:
        a = U.nd.shift(a, tzyx[-ndim:], **splinekwds)
    else:
        tzyx0 = N.where(mag >= 1, tzyx[-ndim:], 0)
        if N.any(tzyx0[-ndim:]):
            a = U.nd.shift(a, tzyx0[-ndim:], **splinekwds)

    if N.any(mag != 1) and not reshape:
        a = keepShape(a, shape, (dif, mod))
        old = """
        canvas = N.zeros(shape, a.dtype.type)

        #dif = (shape - N.array(a.shape, N.float32)) / 2
        #mod = N.ceil(N.mod(dif, 1))
        dif = N.where(dif > 0, N.ceil(dif), N.floor(dif))

        # smaller
        aoff = N.where(dif < 0, 0, dif)
        aslc = [slice(dp, shape[i]-dp+mod[i]) for i, dp in enumerate(aoff)]

        # larger
        coff = N.where(dif > 0, 0, -dif)
        cslc = [slice(dp, a.shape[i]-dp+mod[i]) for i, dp in enumerate(coff)]

        canvas[aslc] = a[cslc]
        a = canvas"""

    if not reshape:
        tzyx0 = N.where(mag < 1, tzyx[-ndim:], 0)
        if N.any(mag != 1):
            tzyx0[-ndim:] -= (mod / 2.)
        if N.any(tzyx0[-ndim:]):
            a = U.nd.shift(a, tzyx0[-ndim:], **splinekwds)

    return a
예제 #24
0
def arr_log(arr):
    logArr = N.log(arr)
    return N.where(logArr < 0, 0, logArr)
예제 #25
0
def trans3D_affine(arr,
                   tzyx=(0, 0, 0),
                   r=0,
                   mag=1,
                   dzyx=(0, 0, 0),
                   rzy=0,
                   ncpu=NCPU,
                   order=ORDER):  #**kwds):
    """
    return array 
    """
    dtype = arr.dtype.type
    arr = arr.astype(N.float32)

    ndim = arr.ndim
    if ndim == 2:
        arr = arr.reshape((1, ) + arr.shape)
    elif ndim == 3:
        if len(tzyx) < ndim:
            tzyx = (0, ) * (ndim - len(tzyx)) + tuple(tzyx)
        if len(dzyx) < ndim:
            dzyx = (0, ) * (ndim - len(dzyx)) + tuple(dzyx)

    dzyx = N.asarray(dzyx)

    magz = 1
    try:
        if len(mag) == 3:
            magz = mag[0]
            mag = mag[1:]
    except TypeError:
        pass

    if ndim == 3 and (magz != 1 or tzyx[-3] or rzy):
        #print magz, arr.shape
        # because, mergins introduced after 2D transformation may interfere the result of this vertical transform, vertical axis was processed first, since rzy is 0 usually.
        arrT = arr.T  # zyx -> xyz
        magzz = (1, magz)
        canvas = N.zeros_like(arrT)
        tzy = (0, tzyx[-3])
        dzy = (dzyx[-2], dzyx[-3])

        #if ncpu > 1 and mp:
        ret = ppro.pmap(_dothat, arrT, ncpu, tzy, rzy, magzz, dzy, order)
        for x, a in enumerate(ret):
            canvas[x] = a
        #else:
        #    for x, a in enumerate(arrT):
        #        canvas[x] = _dothat(a, tzy, rzy, magzz, dzy, order)

        arr = canvas.T
        #del arrT

    if N.any(tzyx[-2:]) or r or N.any(mag):
        #print ndim, arr.shape
        canvas = N.zeros_like(arr)
        if ndim == 3:  # and ncpu > 1 and mp:
            # dividing XY into pieces did not work for rotation and magnification
            # here parallel processing is done section-wise since affine works only for 2D
            ret = ppro.pmap(_dothat, arr, ncpu, tzyx[-2:], r, mag, dzyx[-2:],
                            order)
            for z, a in enumerate(ret):
                canvas[z] = a
        else:
            for z, a in enumerate(arr):
                canvas[z] = _dothat(a, tzyx[-2:], r, mag, dzyx[-2:], order)

        if ndim == 2:
            canvas = canvas[0]

        arr = canvas

    if dtype in (N.int, N.uint8, N.uint16, N.uint32):
        arr = N.where(arr < 0, 0, arr)

    return arr.astype(dtype)
예제 #26
0
def rotateIndicesND(slicelist, dtype=N.float64, rot=0, mode=2):
    """
    slicelist: even shape works much better than odd shape
    rot:       counter-clockwise, xy-plane
    mode:      testing different ways of doing, (1 or 2 and the same result)

    return inds, LD
    """
    global INDS_DIC
    shape = []
    LD = []
    
    for sl in slicelist:
        if isinstance(sl, slice):
            shape.append(sl.stop - sl.start)
            LD.append(sl.start)

    shapeTuple = tuple(shape+[rot])
    if shapeTuple in INDS_DIC:
        inds = INDS_DIC[shapeTuple]
    else:
        shape = N.array(shape)
        ndim = len(shape)
        odd_even = shape % 2

        s2 = N.ceil(shape * (2**0.5))

        if mode == 1: # everything is even
            s2 = N.where(s2 % 2, s2 + 1, s2)
        elif mode == 2: # even & even or odd & odd
            for d, s in enumerate(shape):
                if (s % 2 and not s2[d] % 2) or (not s % 2 and s2[d] % 2):
                    s2[d] += 1
        cent = s2 / 2.
        dif = (s2 - shape) / 2.
        dm = dif % 1
       # print s2, cent, dif, dm
        slc = [Ellipsis] + [slice(int(d), int(d)+shape[i]) for i, d in enumerate(dif)]
        # This slice is float which shift array when cutting out!!

        s2 = tuple([int(ss) for ss in s2]) # numpy array cannot use used for slice
        inds = N.indices(s2, N.float32)
        ind_shape = inds.shape
        nz = N.product(ind_shape[:-2])
        nsec = nz / float(ndim)
        if ndim > 2:
            inds = N.reshape(inds, (nz,)+ind_shape[-2:])
        irs = N.empty_like(inds)
        for d, ind in enumerate(inds):
            idx = int(d//nsec)
            c = cent[idx]
            if rot and inds.ndim > 2:
                U.trans2d(ind - c, irs[d], (0,0,rot,1,0,1))
                irs[d] += c - dif[idx]
            else:
                irs[d] = ind - dif[idx]

        if len(ind_shape) > 2:
            irs = N.reshape(irs, ind_shape)

        irs = irs[slc]
        if mode == 1 and N.sometrue(dm):
            inds = N.empty_like(irs)
           # print 'translate', dm
            for d, ind in enumerate(irs):
                U.trans2d(ind, inds[d], (-dm[1], -dm[0], 0, 1, 0, 1))
        else:
            inds = irs
        INDS_DIC[shapeTuple] = inds

    r_inds = N.empty_like(inds)
    for d, ld in enumerate(LD):
        r_inds[d] = inds[d] + ld

    return r_inds, LD