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
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
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
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