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
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
def yGaussian2DR(param, shape, LD, rot=0): """ param: (mean, max - mean, y, x, sigma[y], [sigmax], [rot]) shape: (y,x) rot: scaler (counter clockwise) if len(param) < 7 LD: left down coordinate (offset) """ if len(param) == 7: rot = parm[-1] sy = 2. * (param[4] * param[4]) sx = 2. * (param[5] * param[5]) # import imgFilters yx = N.subtract(param[2:4], LD) # ll.append(yx) yyi, xxi = rotateIndices2D(shape, rot, yx) # return float64 dist = -(yyi) * (yyi) / sy - (xxi) * (xxi) / sx return param[0] + param[1] * N.exp(dist)
def yGaussian2DR(param, shape, LD, rot=0): """ param: (mean, max - mean, y, x, sigma[y], [sigmax], [rot]) shape: (y,x) rot: scaler (counter clockwise) if len(param) < 7 LD: left down coordinate (offset) """ if len(param) == 7: rot = parm[-1] sy = 2. * (param[4]*param[4]) sx = 2. * (param[5]*param[5]) # import imgFilters yx = N.subtract(param[2:4], LD) # ll.append(yx) yyi, xxi = rotateIndices2D(shape, rot, yx) # return float64 dist = -(yyi)*(yyi)/sy -(xxi)*(xxi)/sx return param[0] + param[1] * N.exp(dist)
def gaussianArr2D(shape=(256,256), sigma=[2.,2.], peakVal=None, orig=None, rot=0): """ >1.5x faster implemetation than gaussianArrND shape: (y,x) sigma: scaler or [sigmay, sigmax] orig: (y,x) rot: scaler anti-clockwise return N.float32 """ shape = N.asarray(shape, N.uint) try: if len(sigma) == len(shape): sy = 2*(sigma[0]*sigma[0]) sx = 2*(sigma[1]*sigma[1]) elif len(sigma) == 1: sx = sy = 2*(sigma[0]*sigma[0]) else: raise ValueError('sigma must be scaler or [sigmay, sigmax]') except TypeError: # sigma scaler sx = sy = 2*(sigma*sigma) # print y, x if rot: yyi, xxi = imgFit.rotateIndices2D(shape, rot, orig, N.float32) else: if orig is None: y, x = shape / 2. - 0.5 # pixel center remove else: y, x = N.subtract(orig, 0.5) # pixel center remove yi, xi = N.indices(shape, dtype=N.float32) yyi = y-yi xxi = x-xi k1 = -(yyi)*(yyi)/(sy) - (xxi)*(xxi)/(sx) if peakVal: k0 = peakVal else: k0 = 1. / ((sx+sy)/2. * ((2*N.pi)**0.5)) return k0 * N.exp(k1)
def gaussianArr2D( shape=(256, 256), sigma=[2., 2.], peakVal=None, orig=None, rot=0): """ >1.5x faster implemetation than gaussianArrND shape: (y,x) sigma: scaler or [sigmay, sigmax] orig: (y,x) rot: scaler anti-clockwise return N.float32 """ shape = N.asarray(shape, N.uint) try: if len(sigma) == len(shape): sy = 2 * (sigma[0] * sigma[0]) sx = 2 * (sigma[1] * sigma[1]) elif len(sigma) == 1: sx = sy = 2 * (sigma[0] * sigma[0]) else: raise ValueError('sigma must be scaler or [sigmay, sigmax]') except TypeError: # sigma scaler sx = sy = 2 * (sigma * sigma) # print y, x if rot: yyi, xxi = imgFit.rotateIndices2D(shape, rot, orig, N.float32) else: if orig is None: y, x = shape / 2. - 0.5 # pixel center remove else: y, x = N.subtract(orig, 0.5) # pixel center remove yi, xi = N.indices(shape, dtype=N.float32) yyi = y - yi xxi = x - xi k1 = -(yyi) * (yyi) / (sy) - (xxi) * (xxi) / (sx) if peakVal: k0 = peakVal else: k0 = 1. / ((sx + sy) / 2. * ((2 * N.pi)**0.5)) return k0 * N.exp(k1)