def separable3dConv(im,fRow,fCol,ftime): temp = sd.convolve1d(im,fRow,axis = 1) temp = sd.convolve1d(temp,fCol,axis=0) temp = sd.convolve1d(temp,ftime,axis=2) return(temp)
def gaussian_weighted_average(series, sigma=3, width=39): """ Computes for rolling weighted average and variance using a gaussian signal filter. Parameters --------------- series: Array Series to be averaged sigma: Float Standard deviation of the gaussian filter Width: int Number of points to create the gaussian filter Returns --------------- average: Array Rolling weighted average of the series var: Array Rolling variance of the series """ #### Create the Gaussian Filter b = gaussian(width, sigma) #### Take the rolling average using convolution average = filters.convolve1d(series, b / b.sum()) #### Take the variance using convolution var = filters.convolve1d(np.power(series - average, 2), b / b.sum()) return average, var
def moving_average(series,sigma = 3,window_time = 39): #### Moving weighted gaussian average with window = 39 b = gaussian(window_time,sigma) average = filters.convolve1d(series,b/b.sum()) var = filters.convolve1d(np.power(series-average,2),b/b.sum()) return average,var
def smear(self, sigma: float = 0.0, func: str | Callable = "gaussian"): """ Apply Gaussian/Lorentzian smearing to spectrum y value. Args: sigma: Std dev for Gaussian smear function func: "gaussian" or "lorentzian" or a callable. If this is a callable, the sigma value is ignored. The callable should only take a single argument (a numpy array) and return a set of weights. """ points = np.linspace( np.min(self.x) - np.mean(self.x), np.max(self.x) - np.mean(self.x), len(self.x)) if callable(func): weights = func(points) elif func.lower() == "gaussian": weights = stats.norm.pdf(points, scale=sigma) elif func.lower() == "lorentzian": weights = lorentzian(points, sigma=sigma) else: raise ValueError(f"Invalid func {func}") weights /= np.sum(weights) if len(self.ydim) == 1: total = np.sum(self.y) self.y = convolve1d(self.y, weights) self.y *= total / np.sum( self.y ) # renormalize to maintain the same integrated sum as before. else: total = np.sum(self.y, axis=0) self.y = np.array([ convolve1d(self.y[:, k], weights) for k in range(self.ydim[1]) ]).T self.y *= total / np.sum( self.y, axis=0 ) # renormalize to maintain the same integrated sum as before.
def degrade_spec(spec, r): """Degrade the spectrum to the given spectral resolution by convolving with a Gaussian kernel. Args: spec (spectrum.HiresSpectrum): Target spectrum r (int): Desired spectral resolution """ spec = spec.copy() num_orders = spec.s.shape[0] for i in range(num_orders): # Perform convolution on every order mean_w = np.mean(spec.w[i]) fwhm = mean_w / r # Std dev of Gaussian = FWHM / 2.355 sigma_w = fwhm / 2.355 # Width of Gaussian in pixels dw = np.mean(spec.w[i, 1:] - spec.w[i, :-1]) sigma_pix = sigma_w / dw # number of pixels n = 201 # Create kernel kernel = gaussian(n, sigma_pix) kernel /= np.sum(kernel) # perform convolution spec.s[i] = convolve1d(spec.s[i], kernel) spec.serr[i] = convolve1d(spec.serr[i], kernel) return spec
def build(im, height, down_filt, up_filt=None): """ _img_ is a 2d numpy array. _height_ is the depth of the pyramid. _down_filt_ is the filter used before downsampling. _up_filt_ is the filter used after upsampling. """ pyr = [] if up_filt is None: up_filt = down_filt for h in xrange(height - 1): low = filters.convolve1d(im, down_filt, axis=0, mode='constant', cval=0) low = filters.convolve1d(low, down_filt, axis=1, mode='constant', cval=0) low = low[::2, ::2] dx, dy = low.shape high = np.zeros((2*dx, 2*dy)) high[::2, ::2] = low high = filters.convolve1d(high, up_filt, axis=1) high = filters.convolve1d(high, up_filt, axis=0) diff = im - high pyr.append(diff) im = low pyr.append(im) return pyr
def subdivide(self): val = util.try_default(lambda: int(Smooth.min_angle.get()), 0) if int(val) > 180: min_angle = math.pi else: min_angle = math.radians(int(val)) for obj, newObj in zip(self.objects, self.adjusted): points = obj['points'] splits = [] for i, (p1, p2, p3) in enumerate( zip(np.roll(points, -1, 0), points, np.roll(points, 1, 0))): a = math.atan2(*(p1 - p2)[::-1]) b = math.atan2(*(p3 - p2)[::-1]) angle = a - b angle = abs(angle) angle = [angle, 2 * math.pi - angle][angle > math.pi] #angle += [0, 2*math.pi][angle<0] if angle < min_angle: splits.append(i) splits = np.array(splits, int) new = convolve1d(points, (1, 1), axis=0, mode='wrap') / 2 splitShape = np.zeros((len(points) * 2, 2)) splitShape[1::2, :] = new splitShape[0::2, :] = points convolved = convolve1d(splitShape, self.rule, axis=0, mode='wrap') / sum(self.rule) #print(type(convolved), convolved, type(points), type(splits)) convolved[splits * 2] = np.array(points)[splits] newObj['points'] = convolved.tolist()
def inverse_transform(wave, fast=True, second_gen=False): """ Reconstructs an image fron its starlet decomposition coefficients :param wave: input coefficients, with shape (n_scales, np.sqrt(n_pixel), np.sqrt(n_pixel)) :param fast: if True, and only with second_gen is False, simply sums up all scales to reconstruct the image :param second_gen: if True, 'second generation' starlets are used """ if fast and not second_gen: # simply sum all scales, including the coarsest one return np.sum(wave, axis=0) mode = 'nearest' lvl, n1, n2 = np.shape(wave) h = np.array([1. / 16, 1. / 4, 3. / 8, 1. / 4, 1. / 16]) n = np.size(h) cJ = np.copy(wave[lvl - 1, :, :]) for i in range(1, lvl): newh = np.zeros((1, n + (n - 1) * (2**(lvl - 1 - i) - 1))) newh[0, np.linspace(0, np.size(newh) - 1, len(h), dtype=int)] = h H = np.dot(newh.T, newh) ###### Line convolution cnew = scf.convolve1d(cJ, newh[0, :], axis=0, mode=mode) ###### Column convolution cnew = scf.convolve1d(cnew, newh[0, :], axis=1, mode=mode) cJ = cnew + wave[lvl - 1 - i, :, :] return np.reshape(cJ, (n1, n2))
def ivarsmooth(flux, ivar, width, weight=None, profile=None): """Generates an inverse variance weighted 1d spectrum. flux- science spectrum to be smoothed ivar- inverse variance spectrum of the science spectrum width- the number of pixels to be smoothed weight- weight to be applied per pixel (default=1) profile- profile to be applied (default=1) NOTE: This is expected to give similar results as ivarsmooth.pro from ucolick: http://tinyurl.com/nm5udej @themiyan 16/06/2015 """ import scipy.ndimage.filters as fil if weight == None: weight=1 if profile == None: profile=1 width = np.ones((1,width), dtype=float)[0] numerator = flux * ivar * weight * profile convolved_numerator = fil.convolve1d(numerator , width, axis=-1, mode='reflect', cval=0.0, origin=0 ) denominator = ivar * weight * profile convolved_denominator = fil.convolve1d( denominator, width, axis=-1, mode='reflect', cval=0.0, origin=0 ) smoothed_flux = convolved_numerator / convolved_denominator smoothed_ivar = convolved_denominator return smoothed_flux, smoothed_ivar
def iuwt_original(wave, convol2d =0, newwave=1, fast=True): """private function : Inverse wavelet transform through original MuSCADeT algorithm""" if newwave == 0 and fast: # simply sum all scales, including the coarsest one return np.sum(wave, axis=0) mode = 'nearest' lvl,n1,n2 = np.shape(wave) h = np.array([1./16, 1./4, 3./8, 1./4, 1./16]) n = np.size(h) cJ = np.copy(wave[lvl-1,:,:]) for i in range(1, lvl): newh = np.zeros( ( 1, int(n+(n-1)*(2**(lvl-1-i)-1)) ) ) newh[0,np.int_(np.linspace(0,np.size(newh)-1,len(h)))] = h H = np.dot(newh.T,newh) ###### Line convolution if convol2d == 1: cnew = scs.convolve2d(cJ, H, mode='same', boundary='symm') else: cnew = scf.convolve1d(cJ,newh[0,:],axis = 0, mode = mode) ###### Column convolution cnew = scf.convolve1d(cnew,newh[0,:],axis = 1, mode = mode) cJ = cnew+wave[lvl-1-i,:,:] out = np.reshape(cJ,(n1,n2)) return out
def moving_average(series, sigma=3, window_time=39): #### Moving weighted gaussian average with window = 39 b = gaussian(window_time, sigma) average = filters.convolve1d(series, b / b.sum()) var = filters.convolve1d(np.power(series - average, 2), b / b.sum()) return average, var
def iuwt(wave, convol2d=0): mode = 'nearest' lvl, n1, n2 = np.shape(wave) h = np.array([1. / 16, 1. / 4, 3. / 8, 1. / 4, 1. / 16]) n = np.size(h) cJ = np.copy(wave[lvl - 1, :, :]) for i in np.linspace(1, lvl - 1, lvl - 1): newh = np.zeros((1, n + (n - 1) * (2**(lvl - 1 - i) - 1))) newh[0, np.int_(np.linspace(0, np.size(newh) - 1, len(h)))] = h H = np.dot(newh.T, newh) ###### Line convolution if convol2d == 1: cnew = cp.convolve2d(cJ, H, mode='same', boundary='symm') else: cnew = sc.convolve1d(cJ, newh[0, :], axis=0, mode=mode) ###### Column convolution cnew = sc.convolve1d(cnew, newh[0, :], axis=1, mode=mode) cJ = cnew + wave[lvl - 1 - i, :, :] return np.reshape(cJ, (n1, n2))
def animate(i): sigma = 1.5 * 6.0 * float(nsamp) / 200. # x extent of filter xsamp = np.arange(-sigma*3,sigma*3) num = convolve1d(y*weight, gaussian(xsamp,sigma),mode='wrap') den = convolve1d(weight, gaussian(xsamp,sigma),mode='wrap') den[den==0] = np.nan ynew = num/den line1.set_data(x[:i],ynew[:i]) ax[0].set_title(f'$\sigma$ {sigma:5.1f} $x_c = {i}$') xsamp = np.arange(-i,nsamp-i).astype(float) filt = gaussian(xsamp,sigma) line0.set_data(x,5*filt/filt.max()) point0.set_data([i],[5]) point1.set_data([i],[ynew[i]]) # ones to show some = filt > 0.01 noise0.set_data(x[some],y[some]) noise1.set_data(x[some],y[some]) return(line0,line1,point0,point1,\ noise0,noise1)
def iuwt(wave, convol2d =0): mode = 'nearest' lvl,n1,n2 = np.shape(wave) h = np.array([1./16, 1./4, 3./8, 1./4, 1./16]) n = np.size(h) cJ = np.copy(wave[lvl-1,:,:]) for i in np.linspace(1,lvl-1,lvl-1): newh = np.zeros((1,n+(n-1)*(2**(lvl-1-i)-1))) newh[0,np.int_(np.linspace(0,np.size(newh)-1,len(h)))] = h H = np.dot(newh.T,newh) ###### Line convolution if convol2d == 1: cnew = cp.convolve2d(cJ, H, mode='same', boundary='symm') else: cnew = sc.convolve1d(cJ,newh[0,:],axis = 0, mode = mode) ###### Column convolution cnew = sc.convolve1d(cnew,newh[0,:],axis = 1, mode = mode) cJ = cnew+wave[lvl-1-i,:,:] return np.reshape(cJ,(n1,n2))
def get_boundingbox(self): fstd = numpy.std(self.frame_set,axis=0) framesstd = numpy.mean(fstd) th = framesstd ones = numpy.ones(10) big_var = (fstd>th) if (framesstd==0): # no bb, take full frame frameROIRes = numpy.zeros([20,50,50]) for i in range(20): frameROIRes[i,:,:] = scipy.misc.imresize(self.frame_set[i,:,:], size=(50,50),interp='bilinear') frameROIRes = numpy.reshape(frameROIRes, (1,frameROIRes.shape[0]*frameROIRes.shape[1]*frameROIRes.shape[2])) frameROIRes = frameROIRes.astype(numpy.float32) return (frameROIRes) big_var = big_var.astype(numpy.float32) big_var = filters.convolve1d(big_var, ones, axis=0) big_var = filters.convolve1d(big_var, ones, axis=1) th2 = 80 i,j = numpy.nonzero(big_var>th2) if (i.size > 0): si = numpy.sort(i) sj = numpy.sort(j) ll = si.shape[0] th1 = round(ll*0.02) th2 = numpy.floor(ll*0.98) y1 = si[th1] y2 = si[th2] x1 = sj[th1] x2 = sj[th2] # cut image ROI if (((x2-x1)>0) and ((y2-y1)>0)): framesRoi = self.frame_set[:,y1:y2,x1:x2] else: framesRoi = self.frame_set[:,:,:] else: framesRoi = self.frame_set[:,:,:] # resize to 50x50 frameROIRes = numpy.zeros([20,50,50]) for i in range(20): frameROIRes[i,:,:] = scipy.misc.imresize(framesRoi[i,:,:], size=(50,50),interp='bilinear') riofstd = numpy.std(frameROIRes,axis=0) self.cur_std = numpy.mean(riofstd) frameROIRes = numpy.reshape(frameROIRes, (1,frameROIRes.shape[0]*frameROIRes.shape[1]*frameROIRes.shape[2])) frameROIRes = frameROIRes.astype(numpy.float32) return (frameROIRes)
def convolve(sequence, rule, **kwds): """Wrapper around scipy.ndimage.convolve1d that allows complex input.""" dtype = np.result_type(float, np.ravel(sequence)[0]) seq = np.asarray(sequence, dtype=dtype) if np.iscomplexobj(seq): return (convolve1d(seq.real, rule, **kwds) + 1j * convolve1d(seq.imag, rule, **kwds)) return convolve1d(seq, rule, **kwds)
def smooth(y, yhat, sw): #opt = fmin(func = pred_smoother, x0 = np.asarray([15, 2]), args = ((y, yhat, sw), )) #gauss_weights = gaussian(M = opt[0].round(), std = opt[1].round()) gauss_weights = gaussian(M=20, std=5) gauss_weights = gauss_weights / gauss_weights.sum() y = filters.convolve1d(input=y, weights=gauss_weights) yhat = filters.convolve1d(input=yhat, weights=gauss_weights) return y, yhat
def calc_comp_grad(im): # Obliczanie gradientow z maska [-1 0 1] dx = convolve1d(np.int32(im), np.array([-1, 0, 1]), 1) dy = convolve1d(np.int32(im), np.array([-1, 0, 1]), 0) grad = np.sqrt(dx**2 + dy**2) orient = np.arctan2(dy, dx) return grad, orient
def gradient(image): dx = filters.convolve1d(np.int32(image), np.array([-1, 0, 1]), 1) dy = filters.convolve1d(np.int32(image), np.array([-1, 0, 1]), 0) grad = np.sqrt(dx**2 + dy**2) grad = grad / np.amax(grad) orient = np.arctan2(dy, dx) return grad, orient
def testGauss(X, fs): b = gaussian(300, 25) gaX = filters.convolve1d(X[:, 0][0:], b / b.sum()) gaY = filters.convolve1d(X[:, 1][0:], b / b.sum()) gaZ = filters.convolve1d(X[:, 2][0:], b / b.sum()) #print(ga) #plt.plot(t, ga) return gaX, gaY, gaZ
def __mul__(self, img): ''' do the actual convolution. If the kernel is 1D, a separable convolution is done. ''' if self.is2D: return filters.convolve(img, self.kernel, self.mode) else: res = filters.convolve1d(img, self.kernel, axis=-1, mode=self.mode) return filters.convolve1d(res, self.kernel, axis=0, mode=self.mode)
def kernel(self, series, sigma=3): # fix the weight of data # http://www.nehalemlabs.net/prototype/blog/2014/04/12/ # how-to-fix-scipys-interpolating-spline-default-behavior/ series = np.asarray(series) b = gaussian(25, sigma) averages = filters.convolve1d(series, b/b.sum()) variances = filters.convolve1d(np.power(series-averages, 2), b/b.sum()) variances[variances == 0] = 1 return averages, variances
def kernel(self, series, sigma=3): '''Apply a gaussian kernel to our data, and get the variances''' # http://www.nehalemlabs.net/prototype/blog/2014/04/12/ # how-to-fix-scipys-interpolating-spline-default-behavior/ series = np.asarray(series) b = gaussian(25, sigma) averages = filters.convolve1d(series, b/b.sum()) variances = filters.convolve1d(np.power(series-averages, 2), b/b.sum()) variances[variances == 0] = 1 return averages, variances
def reduce(im, filter): """ reduce the image size by 2. :param im: image to reduce :param filter: size of blur filter to use :return: reduced image """ im = convolve1d(im, filter, mode='constant') im = convolve1d(im.T, filter, mode='constant') return im.T[::2, ::2]
def __mul__(self, img): ''' do the actual convolution. If the kernel is 1D, a separable convolution is done. ''' if self.is2D: return filters.convolve(img, self.kernel, self.mode) else: res = filters.convolve1d(img, self.kernel, axis= -1, mode=self.mode) return filters.convolve1d(res, self.kernel, axis=0, mode=self.mode)
def spikeProps(times,spike,errs): dt = times[1]-times[0] wind = int(round(2e-3/dt)) c = gaussian(wind,wind/2) smoothed = filters.convolve1d(spike,c/c.sum()) errsS = filters.convolve1d(errs,c/c.sum()) ind = argmax(smoothed) errsL = [] deriv = smoothed[1:]-smoothed[0:-1] thresh = 0.03*max(deriv) ### the prefactor is the proportion of the maximum derivative at which the threshold sits ind2 = argmax(deriv) segment = deriv[0:ind2] flip = segment[::-1]-thresh flip = flip/abs(flip) thpoint = ind2-argmin(flip) errsL.append(errsS[thpoint]) amplitude = max(spike)-smoothed[thpoint] errsL.append(sqrt(errsS[thpoint]**2+errsS[argmax(spike)]**2)) halfmax = amplitude/2+spike[thpoint] comp = abs(smoothed-halfmax) hfpoint1 = argmin(comp[0:ind]) hfpoint2 = ind+argmin(comp[ind:]) width = dt*(hfpoint2-hfpoint1) err1 = (times[hfpoint1+wind/10+1]-times[hfpoint1-wind/10-1])*errsS[hfpoint1]/(smoothed[hfpoint1+wind/10+1]-smoothed[hfpoint1-wind/10-1]) err2 = (times[hfpoint2+wind/10+1]-times[hfpoint2-wind/10-1])*errsS[hfpoint2]/(smoothed[hfpoint2+wind/10+1]-smoothed[hfpoint2-wind/10-1]) errsL.append(sqrt(err1**2+err2**2)) ahppoint = argmin(smoothed[ind:])+ind ahpsize = smoothed[thpoint]-smoothed[ahppoint] errsL.append(sqrt(errsS[thpoint]**2+errsS[ahppoint]**2)) fig = figure() ax = fig.add_subplot(111) ax.plot(times,spike,'b-') ax.plot(times,smoothed,'g-',linewidth=2) ax.plot([times[thpoint]],smoothed[thpoint],'ro') ax.plot(times,smoothed[thpoint]*ones([len(times,)]),'y-') ax.plot(times[hfpoint1:hfpoint2],halfmax*ones([hfpoint2-hfpoint1,]),'k-') ax.plot(times[argmax(spike)]*ones([500,]),linspace(smoothed[thpoint],max(spike),500),'m-') ax.plot(times[ahppoint]*ones([100,]),linspace(smoothed[ahppoint],smoothed[thpoint],100),'c-') return smoothed[thpoint],amplitude,width,ahpsize,errsL
def reduce(im, filter_vec): """ reduces a given image by 2 :param im: the image to reduce :param filter: the filter_vec to blur the image with :return: the image after reduction """ img = convolve1d(convolve1d(im, filter_vec[0], mode='constant').T, filter_vec[0], mode='constant').T return img[::2, ::2]
def flag_outliers(signal, thresh_stdv=4, buffer=10, visualize=False): """ Flag outliers based on median abs deviation. Returns two arrays of indices. The first gives the indices to be deleted. The second gives the indices of locations in the new signal which will potentially have discontinuities due to fluroescence reset. """ # z-score to locate outliers keep_idx = abs(signal - np.median(signal)) < thresh_stdv * np.std(signal) # minimum filter removes pixels within buffer distance of outliers keep_idx = filt.minimum_filter(keep_idx, size=2 * buffer + 1) # Plot flagged outliers -- hacky so may break if params unreasonable if visualize: fig = plt.figure(figsize=(16, 4)) trans_idx = np.argwhere(filt.convolve1d(keep_idx, np.array([1, -1]))) for idx in range(len(trans_idx)): if idx == 0: plt_idx = np.arange(0, trans_idx[idx]) else: plt_idx = np.arange(trans_idx[idx - 1], trans_idx[idx]) color = 'b' if keep_idx[trans_idx[idx] - 1] else 'r' plt.plot(plt_idx, signal[plt_idx], color) if trans_idx[-1] < len(signal): plt_idx = np.arange(trans_idx[idx], len(signal)) color = 'b' if keep_idx[len(signal) - 1] else 'r' plt.plot(plt_idx, signal[plt_idx], color) plt.plot(np.arange(len(signal)), (np.ones(len(signal)) * np.median(signal)) - (thresh_stdv * np.std(signal)), 'g') plt.plot(np.arange(len(signal)), (np.ones(len(signal)) * np.median(signal)) + (thresh_stdv * np.std(signal)), 'g') plt.title('Outliers Flagged For Removal & Threshold') plt.show() # List of indices to be deleted del_idx = np.argwhere(~keep_idx) # list of indices where samples were cutout (possible discontinuities) disc_idx = np.argwhere(filt.convolve1d( keep_idx, np.array([1, -1]))[keep_idx]) return del_idx, disc_idx
def gaussian_filter( im, sigma, wsize ): #im = hstack( (im[:,extendSize-1::-1], im, im[:,-1:-1-extendSize:-1] ) ) kernel = createGaussianKernel( sigma, wsize ) result = zeros( im.shape ) if len(im.shape) == 3: for i in range(3): imX = filters.convolve1d( im[:,:,i], kernel, axis = 1, mode = 'reflect' ) result[:,:,i] = filters.convolve1d( imX, kernel, axis = 0, mode = 'reflect' ) else: imX = filters.convolve1d( im, kernel, axis = 1,mode = 'reflect' ) result = filters.convolve1d( imX, kernel, axis = 0,mode = 'reflect' ) return result
def gaussian(self, image, radius): data = numpy.array(image) kernel = range(-radius, radius + 1) kernel = [(d ** 2) / (2 * (radius * .5) ** 2) for d in kernel] kernel = [e ** -d for d in kernel] kernel = array(kernel, dtype=float) / sum(kernel) convolve1d(data, kernel, output=data, axis=0) convolve1d(data, kernel, output=data, axis=1) return Image.fromarray(data)
def reconstruct(pyr, up_filt): """Reconstruct original from pyramid. Use code """ im = pyr[-1] for nxt in reversed(pyr[:-1]): dx, dy = im.shape tmp = np.zeros((2 * dx, 2 * dy)) tmp[::2, ::2] = im tmp = filters.convolve1d(tmp, up_filt, axis=1) tmp = filters.convolve1d(tmp, up_filt, axis=0) im = tmp + nxt return im
def deconv_lucy_richardson(img, psf, max_iter, axis=-1, init_img=None): '''1D Lucy Richardson Deconvolution''' assert (psf.ndim == 1) # make sure PSF is 1D if init_img is None: u = img else: u = init_img psf_hat = psf[::-1] for i in xrange(max_iter): temp = convolve1d(u, psf, axis=axis) temp[temp == 0.0] = EPS u = u * convolve1d(img / temp, psf_hat, axis=axis) return u
def expand(im, filter): """ expand the image size by 2. :param im: image to expand :param filter: size of blur filter to use :return: expanded image """ x, y = im.shape im = np.insert(im, np.arange(1, y + 1, 1), 0, axis=1) im = np.insert(im, np.arange(1, x + 1, 1), 0, axis=0) im = convolve1d(im, 2 * filter, mode='constant') im = convolve1d(im.T, 2 * filter, mode='constant') return im.T
def despike(y, thresh, winsize, ax = 0): """ despiking for masked arrays, removes spikes by masking it. removes values above thresh * std of moving window with size winsize """ y = np.asanyarray(y) N = winsize win = np.ones((N,))/N mbar = filters.convolve1d(y, win, axis = ax) devs = np.abs(y - mbar) mstd = np.sqrt( filters.convolve1d(devs**2, win, axis = ax) ) yn = np.ma.masked_where(np.abs(y)>=thresh*mstd, y) return yn
def notSoRandomWalk(shape, std=1, trendFilterLength=32, lpfLength=16): """bandpass filter a random walk so that the low-frequency trend / drift is eliminated and the high-frequency noise is attenuated""" walk = randwalk(shape, std=std) filt = np.hamming(trendFilterLength) filt /= np.sum(filt) whichAxis = len(walk.shape) > 1 # 0 iff 1d, else 1 # subtract baseline drift, roughly trend = filters.convolve1d(walk, weights=filt, axis=whichAxis, mode='reflect') walk -= trend # subtract noisey spikes walk = filters.convolve1d(walk, weights=np.hamming(lpfLength), axis=whichAxis, mode='reflect') return walk
def reconstruct(pyr, up_filt): """Reconstruct original from pyramid. Use code """ im = pyr[-1] for nxt in reversed(pyr[:-1]): dx, dy = im.shape tmp = np.zeros((2*dx, 2*dy)) tmp[::2, ::2] = im tmp = filters.convolve1d(tmp, up_filt, axis=1) tmp = filters.convolve1d(tmp, up_filt, axis=0) im = tmp + nxt return im
def smooth_price(df, sd=20., N=10000, double=False): """ Applies a gaussian filter to the closing price in ohlc data frame. """ N = max(N, 4 * sd) f_ga = gaussian(N, std=sd) f_ga = f_ga / f_ga.sum() if double: df = df.assign(Smoothed=filters.convolve1d( filters.convolve1d(df.Close, f_ga), f_ga)) else: df = df.assign(Smoothed=filters.convolve1d(df.Close, f_ga)) return df
def gaussFilt(X, wdim=(1, )): ''' Gaussian Filtering in 1 or 2d. Made to fit matlab ''' from scipy.signal import gaussian if len(wdim) == 1: from scipy.ndimage.filters import convolve1d l1 = len(X) N1 = wdim[0] * 10 S1 = (N1 - 1) / float(2 * 5) gw = gaussian(N1, S1) gw = gw / gw.sum() #convolution if len(X.shape) == 2: filtered_X = convolve1d(X, gw, axis=1) elif len(X.shape) == 1: filtered_X = convolve1d(X, gw) return filtered_X elif len(wdim) == 2: from scipy.signal import convolve2d def conv2(x, y, mode='same'): return np.rot90( convolve2d(np.rot90(x, 2), np.rot90(y, 2), mode=mode), 2) l1, l2 = X.shape N1, N2 = wdim # create bordered matrix Xf = np.flipud(X) bordered_X = np.vstack([ np.hstack([np.fliplr(Xf), Xf, np.fliplr(Xf)]), np.hstack([np.fliplr(X), X, np.fliplr(X)]), np.hstack([np.fliplr(Xf), Xf, np.fliplr(Xf)]), ]) # gaussian windows N1 = N1 * 10 N2 = N2 * 10 S1 = (N1 - 1) / float(2 * 5) S2 = (N2 - 1) / float(2 * 5) gw = np.vstack(gaussian(N1, S1)) * gaussian(N2, S2) gw = gw / gw.sum() # convolution filtered_X = conv2(bordered_X, gw, mode='same') return filtered_X[l1:l1 + l1, l2:l2 + l2] else: print("Error, dimensions larger than 2") return
def expand(im, filter_vec): """ expands a given image by 2 :param im: the image to reduce :param filter_vec: the filter_vec to blur the image with :return: the image after expanding """ length, width = im.shape filter_vec = filter_vec.copy() expanded_im = np.insert(im, np.arange(1, length + 1), 0, axis=0) expanded_im = np.insert(expanded_im, np.arange(1, width + 1), 0, axis=1) return convolve1d(convolve1d(expanded_im, filter_vec[0], mode='constant').T, filter_vec[0], mode='constant').T
def deltas(frames, window=9, output_frames=None): """Use a window-point window to calculate deltas. From Dan P. Ellis rastamat: http://labrosa.ee.columbia.edu/matlab/rastamat/ window: int, should be odd, rounded up """ half_length = window/2 window_indices = numpy.arange(-half_length, half_length+1) if output_frames is not None: filters.convolve1d(frames, weights=window_indices, axis=0, output=output_frames, mode="constant") else: output_frames = filters.convolve1d(frames, weights=window_indices, axis=0, mode="constant") return output_frames
def moving_average(series, window=100, sigma=50): ''' Calculates the moving average of a series by using a Gaussian kernel to smooth it. This function is used by continuumFlattenSpec. Borrowed from http://www.nehalemlabs.net/prototype/blog/2014/04/12/how-to-fix-scipys-interpolating-spline-default-behavior/ Input: series or array, window size, and Gaussian width (sigma). Returns: the smoothed data and the smoothed variance. ''' from scipy.signal import gaussian from scipy.ndimage import filters b = gaussian(window, sigma) average = filters.convolve1d(series, b/b.sum()) var = filters.convolve1d(np.power(series-average,2), b/b.sum()) return average, var
def testGauss(x, y, s, npts): b = gaussian(39, 10) #ga = filtfilt(b/b.sum(), [1.0], y) ga = filters.convolve1d(y, b/b.sum()) plt.plot(x, ga) print "gaerr", ssqe(ga, s, npts) return ga
def testGauss(x, y, s, npts): #b = gaussian(39, 10) b = gaussian(75, 15) ga = filters.convolve1d(y, b/b.sum()) plt.plot(x, ga) print "gaerr", ssqe(ga, s, npts) return ga
def iuwt_1D(wave): """ Inverse Starlet transform. INPUTS: wave: wavelet decomposition of an image. OUTPUTS: out: image reconstructed from wavelet coefficients OPTIONS: convol2d: if set, a 2D version of the filter is used (slower, default is 0) """ mode = 'nearest' lvl,n1= np.shape(wave) h = np.array([1./16, 1./4, 3./8, 1./4, 1./16]) n = np.size(h) cJ = np.copy(wave[lvl-1,:]) for i in np.linspace(1,lvl-1,lvl-1): newh = np.zeros((1,n+(n-1)*(2**(lvl-1-i)-1))) newh[0,np.int_(np.linspace(0,np.size(newh)-1,len(h)))] = h H = np.dot(newh.T,newh) ###### Line convolution cnew = sc.convolve1d(cJ,newh[0,:],axis = 0, mode = mode) cJ = cnew+wave[lvl-1-i,:] out = cJ return out
def losvd_convolve(spec, losvd, velscale): """ Apply LOSVD to a given spectra given that both wavelength and spec arrays are log-binned. """ # Convert to pixel scale pars = np.copy(losvd) pars[:2] /= velscale dx = int(np.ceil(np.max(abs(pars[0]) + 5*pars[1]))) nl = 2*dx + 1 x = np.linspace(-dx, dx, nl) # Evaluate the Gaussian using steps of 1/factor pixel vel = pars[0] w = (x - vel)/pars[1] w2 = w**2 gauss = np.exp(-0.5*w2) profile = gauss/gauss.sum() # Hermite polynomials normalized as in Appendix A of van der Marel & Franx (1993). # Coefficients for h5, h6 are given e.g. in Appendix C of Cappellari et al. (2002) if losvd.size > 2: # h_3 h_4 poly = 1 + pars[2]/np.sqrt(3)*(w*(2*w2-3)) \ + pars[3]/np.sqrt(24)*(w2*(4*w2-12)+3) if len(losvd) == 6: # h_5 h_6 poly += pars[4]/np.sqrt(60)*(w*(w2*(4*w2-20)+15)) \ + pars[5]/np.sqrt(720)*(w2*(w2*(8*w2-60)+90)-15) profile *= poly profile = profile / profile.sum() return convolve1d(spec, profile)
def gaussSmooth(vals, num): b = gaussian(num, 1) smoothVals = filters.convolve1d(vals, b/b.sum()) return smoothVals # if __name__ == '__main__': # singleRunPlot("writefile.txt", 70, 80, 75)
def runave(data, N, axis=0, step=None): """ Compute a smooth box-car running average and masked edge values using scipy.ndimage.filters.convolve1d Args: data (numpy array) N (int) : width of the box car axis (int): axis along which the average is computed (See scipy.ndimage.filters.convolve1d) step (optional, int): skips between the box car samples Returns: numpy masked array (same shape as data) """ if isinstance(data, numpy.ma.core.MaskedArray): data = data.filled(numpy.nan) if step is None: weights = numpy.ones((N,)) else: if type(step) is not int: raise Exception("step should be an integer") weights = numpy.array(([1.]+[0.]*(step-1))*(N-1)+[1.]) weights /= weights.sum() return numpy.ma.masked_invalid(filters.convolve1d( data, weights, mode='constant', cval=numpy.nan, axis=axis))
def imageHistogram (img): # Convert to grayscale if img.ndim == 3: img = mean (img, axis=2) # Prepare gradient image gradient_kernel = array([-1,0,1]) gradx = convolve1d (img, gradient_kernel, axis=1) grady = convolve1d (img, gradient_kernel, axis=0) # Quantize gradient orientations (yeah, that's ugly) grado = floor ((arctan2 (grady, gradx) + pi)/(2*pi)*9) gradw = pow (gradx,2)+pow (grady,2) # Compute Harris features fc = get_harris_points (compute_harris_response(img), 8, 0.10) # Compute histograms (erk, more ugliness) h,w = img.shape hbr = (histogram_block_size-1)/2 hcr = (histogram_cell_size-1)/2 histograms = array ( \ [ [ [histogram( \ grad_quant_nb, \ grado[x+i*histogram_cell_size-hcr:x+(i+1)*histogram_cell_size-hcr,y+j*histogram_cell_size-hcr:y+(j+1)*histogram_cell_size-hcr], \ gradw[x+i*histogram_cell_size-hcr:x+(i+1)*histogram_cell_size-hcr,y+j*histogram_cell_size-hcr:y+(j+1)*histogram_cell_size-hcr] \ ) \ for j in range(-hbr,hbr+1)] \ for i in range(-hbr,hbr+1)] for (x,y) in fc] \ ) # Normalize them if histograms.ndim != 4: return None,None cnt,_,_,_ = histograms.shape histograms = array ( \ [normalizeBlock ( \ histograms[i,:,:,:].squeeze() ) \ for i in range (0,cnt)] \ ) return histograms,fc
def h__foragingData(self, nose_bend_angle_d, min_win_size): """ Compute the foraging amplitude and angular speed. Parameters --------------------------------------- nose_bend_angle_d : [n_frames x 1] min_win_size : (scalar) Returns --------------------------------------- amplitudes : [1 x n_frames] speeds : [1 x n_frames] Notes --------------------------------------- Formerly [amps,speeds] = h__foragingData(nose_bend_angle_d, min_win_size, fps) """ if min_win_size > 0: # Clean up the signal with a gaussian filter. gauss_filter = utils.gausswin(2 * min_win_size + 1) \ / min_win_size nose_bend_angle_d = filters.convolve1d(nose_bend_angle_d, gauss_filter, cval=0, mode='constant') #gaussFilter = np.gausswin(2 * min_win_size + 1) / min_win_size #nose_bend_angle_d = np.conv(nose_bend_angle_d, gaussFilter, 'same') # Remove partial data frames ... nose_bend_angle_d[:min_win_size] = np.NaN nose_bend_angle_d[-min_win_size:] = np.NaN # Calculate amplitudes amplitudes = self.h__getAmplitudes(nose_bend_angle_d) assert(np.shape(nose_bend_angle_d) == np.shape(amplitudes)) # Calculate angular speed # Compute the speed centered between the back and front foraging movements. # # TODO: fix the below comments to conform to 0-based indexing # I believe I've fixed the code already. - @MichaelCurrie # 1 2 3 # d1 d2 d1 = 2 - 1, d2 = 3 - 2 # x assign to x, avg of d1 and d2 #???? - why multiply and not divide by fps???? d_data = np.diff(nose_bend_angle_d) * config.FPS speeds = np.empty(amplitudes.size) * np.NaN # This will leave the first and last frame's speed as NaN: speeds[1:-1] = (d_data[:-1] + d_data[1:]) / 2 # Propagate NaN for speeds to amplitudes amplitudes[np.isnan(speeds)] = np.NaN return amplitudes, speeds
def Gauss_filt(y, M, std): from scipy.signal import gaussian from scipy.ndimage import filters b = gaussian(M, std) ga = filters.convolve1d(y, b/b.sum()) return ga
def movingAvg(data, window_size): """ Apply a moving average to data using convolution""" window = np.ones(int(window_size))/float(window_size-1) window[window_size/2] = 0 #window = np.hamming(window_size) print window movAvg = convolve1d(data, window, axis=0) return movAvg
def __call__(self, sequence, steps): ne = sequence.shape[0] rule = self._get_richardson_rule(ne) nr = rule.size - 1 m = ne - nr new_sequence = convolve1d(sequence, rule[::-1], axis=0, origin=(nr//2)) abserr = self._estimate_error(new_sequence, sequence, steps, rule) return new_sequence[:m], abserr[:m], steps[:m]
def separable_convolution(input, weights, output=None, mode="reflect", cval=0.0, origin=0): r""" Calculate a n-dimensional convolution of a separable kernel to a n-dimensional input. Achieved by calling convolution1d along the first axis, obtaining an intermediate image, on which the next convolution1d along the second axis is called and so on. Parameters ---------- input : array_like Array of which to estimate the noise. weights : ndarray One-dimensional sequence of numbers. output : array, optional The `output` parameter passes an array in which to store the filter output. mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional The `mode` parameter determines how the array borders are handled, where `cval` is the value when mode is equal to 'constant'. Default is 'reflect' cval : scalar, optional Value to fill past edges of input if `mode` is 'constant'. Default is 0.0 origin : scalar, optional The `origin` parameter controls the placement of the filter. Default 0.0. Returns ------- output : ndarray Input image convolved with the supplied kernel. """ input = numpy.asarray(input) output, return_value = _ni_support._get_output(output, input) axes = list(range(input.ndim)) if len(axes) > 0: convolve1d(input, weights, axes[0], output, mode, cval, origin) for ii in range(1, len(axes)): convolve1d(output, weights, axes[ii], output, mode, cval, origin) else: output[...] = input[...] return return_value
def broaden(self, vsini, spec): """ Applies a broadening kernel to the given spectrum (or error) Args: vsini (float): vsini to determine width of broadening spec (Spectrum): spectrum to broaden Returns: broadened (Spectrum): Broadened spectrum """ SPEED_OF_LIGHT = 2.99792e5 dv = (self.w[1]-self.w[0])/self.w[0]*SPEED_OF_LIGHT n = 151 # fixed number of points in the kernel varr, kernel = specmatchemp.kernels.rot(n, dv, vsini) # broadened = signal.fftconvolve(spec, kernel, mode='same') spec.s = convolve1d(spec.s, kernel) spec.serr = convolve1d(spec.serr, kernel) return spec
def movingAvg(data, window_size): """ Apply a moving average to data using convolution""" #window = np.ones(int(window_size))/float(window_size-1) #window[window_size/2] = 0 # don't use target spectrum window = np.hanning(window_size) window = window / window.sum() window = np.insert(window, len(window)/2, 0) movAvg = convolve1d(data, window, axis=0) return movAvg