Esempio n. 1
0
def _cutoutForAlign2(fn, py, outFn=''):
    """
    if not outFn, default with 'cut' extention
    resulting array is imgSequence=0 (t,w,z,y,x)
    return outFn
    """
    h = imgManager.ImageManager(fn)
    slc, shiftZYX, ZYX = makeSlice(h, py)

    # input
    arr = N.empty((h.nt, h.nw, h.nz, h.ny, h.nx), h.dtype)
    for t in range(h.nt):
        for w in range(h.nw):
            arr[t, w] = h.get3DArr(w=w, t=t)

    canvas = N.squeeze(arr[slc].astype(arr.dtype.type))
    newNum = (canvas.shape[-1], canvas.shape[-2], N.prod(canvas.shape[:-3]))
    if not outFn:
        outFn = '_'.join((h.filePath, EXT_CUTOUT))  #arr.Mrc.path, EXT_CUTOUT))
    hdr = Mrc.makeHdrArray()
    Mrc.initHdrArrayFrom(hdr, h.hdr)  #arr.Mrc.hdr)
    hdr.ImgSequence = 2
    hdr.Num[:] = newNum
    mstart = [sl.start for sl in slc[::-1][:3] if isinstance(sl, slice)]
    hdr.mst[:len(mstart)] += mstart

    Mrc.save(canvas, outFn, ifExists='overwrite', hdr=hdr)
    return outFn
Esempio n. 2
0
    def __getitem__(self, idx):
        try:
            idx = int(idx)
            if idx < 0:
                idx = self.shape[0] + idx

            ws = range(self.nw)
            ts = range(self.nt)
            zs = range(self.nz)
            if self.maxaxs == 'w' and not self.ignor_color_axis:
                ws = [idx]
                self.sld_axs = 'w'
            elif self.maxaxs == 't' or (self.ignor_color_axis
                                        and self.ndim > 4):
                ts = [idx]
                self.sld_axs = 't'
            elif self.maxaxs == 'z' or (self.ignor_color_axis
                                        and self.ndim == 4):
                zs = [idx]
                self.sld_axs = 'z'
            else:
                raise ValueError, 'section axis not determined'
            if self.hdr.ImgSequence == 0:
                e = N.empty((len(ws), len(ts), len(zs), self.ny, self.nx),
                            self.dtype)
                for wo, wi in enumerate(ws):
                    for to, ti in enumerate(ts):
                        for zo, zi in enumerate(zs):
                            e[wo, to, zo] = self.img.getArr(w=wi, t=ti, z=zi)
            elif self.hdr.ImgSequence == 1:
                e = N.empty((len(ts), len(zs), len(ws), self.ny, self.nx),
                            self.dtype)
                for to, ti in enumerate(ts):
                    for zo, zi in enumerate(zs):
                        for wo, wi in enumerate(ws):
                            e[to, zo, wo] = self.img.getArr(w=wi, t=ti, z=zi)
            elif self.hdr.ImgSequence == 2:
                e = N.empty((len(ts), len(ws), len(zs), self.ny, self.nx),
                            self.dtype)
                for to, ti in enumerate(ts):
                    for wo, wi in enumerate(ws):
                        for zo, zi in enumerate(zs):
                            e[to, wo, zo] = self.img.getArr(w=wi, t=ti, z=zi)

        except TypeError:
            pass

        return N.squeeze(e)
Esempio n. 3
0
def Xcorr(a,
          b,
          phaseContrast=PHASE,
          nyquist=NYQUIST,
          removeEdge=0,
          gFit=True,
          win=11,
          ret=None,
          searchRad=None):
    """
    sigma uses F.gaussianArr in the Fourier domain
    if ret is None:
        return zyx, xcf
    elif ret is 2:
        return s, v, zyx, xcf
    elif ret:
        return v, zyx, xcf
    """
    #print 'phase contrast: %s' % str(phaseContrast)
    #global DATA
    # correct odd shape particularly Z axis
    a = N.squeeze(a)
    b = N.squeeze(b)
    a = imgFilters.evenShapeArr(a)
    b = imgFilters.evenShapeArr(b)
    shape = N.array(a.shape)

    # apodize
    a = apodize(a)
    b = apodize(b)

    # fourier transform
    af = F.rfft(a.astype(N.float32))
    bf = F.rfft(b.astype(N.float32))
    del a, b

    # phase contrast filter (removing any intensity information)
    if phaseContrast:
        afa = phaseContrastFilter(af, True, nyquist=nyquist)
        bfa = phaseContrastFilter(bf, True, nyquist=nyquist)
    else:
        afa = af
        bfa = bf
    del af, bf

    # removing edge if gaussian is not sufficient
    targetShape = shape - N.multiply(removeEdge, 2)
    if removeEdge:
        ap = imgFilters.cutOutCenter(F.irfft(afa), targetShape)
        bp = imgFilters.cutOutCenter(F.irfft(bfa), targetShape)
        afa = F.rfft(ap)
        bfa = F.rfft(bp)
        del ap, bp

    # shift array
    delta = targetShape / 2.
    shiftarr = F.fourierRealShiftArr(tuple(targetShape), delta)
    bfa *= shiftarr

    # cross correlation
    bfa = bfa.conjugate()
    c = cc = F.irfft(afa * bfa)

    center = N.divide(c.shape, 2)
    if searchRad:
        slc = imgGeo.nearbyRegion(c.shape, center, searchRad)
        cc = N.zeros_like(c)
        cc[slc] = c[slc]
    v, zyx, s = _findMaxXcor(cc, win, gFit=gFit)
    zyx -= center

    if ret == 2:
        return s, v, zyx, c
    elif ret:
        return v, zyx, c
    else:
        return zyx, c
Esempio n. 4
0
def Xcorr(a, b, phaseContrast=PHASE, nyquist=NYQUIST, gFit=True, win=11, ret=None, searchRad=None, npad=4):
    """
    sigma uses F.gaussianArr in the Fourier domain
    if ret is None:
        return zyx, xcf
    elif ret is 2:
        return s, v, zyx, xcf
    elif ret is 3:
        return zyx, xcf, a_phase_cotrast, b_phase_contrast
    elif ret:
        return v, zyx, xcf
    """
    #print 'phase contrast: %s' % str(phaseContrast)
    #global DATA
    # correct odd shape particularly Z axis
    a = N.squeeze(a)
    b = N.squeeze(b)
    a = imgFilters.evenShapeArr(a)
    b = imgFilters.evenShapeArr(b)
    shape = N.array(a.shape)

    # padding strange shape
    #nyx = max(shape[-2:])
    #pshape = N.array(a.shape[:-2] + (nyx,nyx))

    # apodize
    a = paddAndApo(a, npad)#, pshape) #apodize(a)
    b = paddAndApo(b, npad)#, pshape) #apodize(b)

    # fourier transform
    af = F.rfft(a.astype(N.float32))
    bf = F.rfft(b.astype(N.float32))
    del a, b

    # phase contrast filter (removing any intensity information)
    if phaseContrast:
        afa = phaseContrastFilter(af, True, nyquist=nyquist)
        bfa = phaseContrastFilter(bf, True, nyquist=nyquist)
    else:
        afa = af
        bfa = bf
    del af, bf

    #targetShape = shape + (npad * 2)
    targetShape = shape + (npad * 2)

    # shift array
    delta = targetShape / 2.
    shiftarr = F.fourierRealShiftArr(tuple(targetShape), delta)
    bfa *= shiftarr

    # cross correlation
    bfa = bfa.conjugate()
    #c = cc = F.irfft(afa * bfa)
    c = F.irfft(afa * bfa)

    # 20180214 the padded region was cutout before finding the peak.
    c = cc = imgFilters.cutOutCenter(c, N.array(c.shape) - (npad * 2), interpolate=False)
    #cc = c
    center = N.divide(c.shape, 2)
    if searchRad:
        slc = imgGeo.nearbyRegion(c.shape, center, searchRad)
        cc = N.zeros_like(c)
        cc[slc] = c[slc]
    v, zyx, s = _findMaxXcor(cc, win, gFit=gFit)
    #return cc
    #print(zyx, center)
    zyx -= center

    #c = imgFilters.cutOutCenter(c, N.array(c.shape) - (npad * 2), interpolate=False)
    #c = imgFilters.cutOutCenter(c, shape, interpolate=False)

    if ret == 3:
        return zyx, c, F.irfft(afa), F.irfft(bfa)
    elif ret == 2:
        return s, v, zyx, c
    elif ret:
        return v, zyx, c
    else:
        return zyx, c
Esempio n. 5
0
def Xcorr(a,
          b,
          phaseContrast=PHASE,
          nyquist=NYQUIST,
          gFit=True,
          win=11,
          ret=None,
          searchRad=None,
          npad=4):
    """
    sigma uses F.gaussianArr in the Fourier domain
    if ret is None:
        return zyx, xcf
    elif ret is 2:
        return s, v, zyx, xcf
    elif ret is 3:
        return zyx, xcf, a_phase_cotrast, b_phase_contrast
    elif ret:
        return v, zyx, xcf
    """
    #print 'phase contrast: %s' % str(phaseContrast)
    #global DATA
    # correct odd shape particularly Z axis
    a = N.squeeze(a)
    b = N.squeeze(b)
    a = imgFilters.evenShapeArr(a)
    b = imgFilters.evenShapeArr(b)
    shape = N.array(a.shape)

    # padding strange shape
    #nyx = max(shape[-2:])
    #pshape = N.array(a.shape[:-2] + (nyx,nyx))

    # apodize
    a = paddAndApo(a, npad)  #, pshape) #apodize(a)
    b = paddAndApo(b, npad)  #, pshape) #apodize(b)

    # fourier transform
    af = F.rfft(a.astype(N.float32))
    bf = F.rfft(b.astype(N.float32))
    del a, b

    # phase contrast filter (removing any intensity information)
    if phaseContrast:
        afa = phaseContrastFilter(af, True, nyquist=nyquist)
        bfa = phaseContrastFilter(bf, True, nyquist=nyquist)
    else:
        afa = af
        bfa = bf
    del af, bf

    #targetShape = shape + (npad * 2)
    targetShape = shape + (npad * 2)

    # shift array
    delta = targetShape / 2.
    shiftarr = F.fourierRealShiftArr(tuple(targetShape), delta)
    bfa *= shiftarr

    # cross correlation
    bfa = bfa.conjugate()
    c = cc = F.irfft(afa * bfa)

    center = N.divide(c.shape, 2)
    if searchRad:
        slc = imgGeo.nearbyRegion(c.shape, center, searchRad)
        cc = N.zeros_like(c)
        cc[slc] = c[slc]
    v, zyx, s = _findMaxXcor(cc, win, gFit=gFit)
    zyx -= center

    c = imgFilters.cutOutCenter(c,
                                N.array(c.shape) - (npad * 2),
                                interpolate=False)
    #c = imgFilters.cutOutCenter(c, shape, interpolate=False)

    if ret == 3:
        return zyx, c, F.irfft(afa), F.irfft(bfa)
    elif ret == 2:
        return s, v, zyx, c
    elif ret:
        return v, zyx, c
    else:
        return zyx, c