def xdog(img: str, std_deviation: int, k: int, p: int): global image, first_mask_size image = cv2.imread(img, cv2.IMREAD_GRAYSCALE) ## im_g = cv2.im2double(image) first_mask_size = round((2 * 3 * std_deviation) + 1) first_kernel = g.generate_Gaussian_kernel(shape=(first_mask_size, first_mask_size), sigma=std_deviation) blur_strength = k * std_deviation second_mask_size = round((2 * 3 * blur_strength) + 1) second_kernel = g.generate_Gaussian_kernel(shape=(second_mask_size, second_mask_size), sigma=blur_strength) first_blurred_img = image.copy() second_blurred_img = image.copy() out_img = image.copy() first_blurred_img = c.convolve(image, first_kernel) second_blurred_img = c.convolve(image, second_kernel) for i in range(image.shape[0]): for j in range(image.shape[1]): out_img[i, j] = ((1 + p) * first_blurred_img[i, j]) -(p * second_blurred_img[i, j]) cv2.imshow('first blurred image', first_blurred_img) cv2.imshow('second blurred image', second_blurred_img) cv2.imshow('sharpend result', out_img) cv2.waitKey(0) cv2.destroyAllWindows()
def find_n_pc(u, factor=0.5): """Find number of principal components This method finds the minimum number of principal components required Parameters ---------- u : np.ndarray Left singular vector factor : float, optional Factor for testing the auto correlation (default is '0.5') Returns ------- int number of principal components """ # Get the shape of the galaxy images. gal_shape = np.repeat(np.int(np.sqrt(u.shape[0])), 2) # Find the auto correlation of the left singular vector. u_auto = [convolve(a.reshape(gal_shape), np.rot90(a.reshape(gal_shape), 2)) for a in u.T] # Return the required number of principal components. return np.sum(((a[zip(gal_shape / 2)] ** 2 <= factor * np.sum(a ** 2)) for a in u_auto))
def itilbert(x,h,period=None, _cache = _cache): """ itilbert(x, h, period=2*pi) -> y Return inverse h-Tilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j y_0 = 0 Optional input: see tilbert.__doc__ """ tmp = asarray(x) if iscomplexobj(tmp): return itilbert(tmp.real,h,period)+\ 1j*itilbert(tmp.imag,h,period) if period is not None: h = h*2*pi/period n = len(x) omega = _cache.get((n,h)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,h=h): if k: return -tanh(h*k) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[(n,h)] = omega overwrite_x = tmp is not x and not hasattr(x,'__array__') return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def itilbert(x,h,period=None, _cache = _cache): """ itilbert(x, h, period=2*pi) -> y Return inverse h-Tilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j y_0 = 0 Optional input: see tilbert.__doc__ """ tmp = asarray(x) if iscomplexobj(tmp): return itilbert(tmp.real,h,period)+\ 1j*itilbert(tmp.imag,h,period) if period is not None: h = h*2*pi/period n = len(x) omega = _cache.get((n,h)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,h=h): if k: return -tanh(h*k) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[(n,h)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def hilbert(x, _cache=_cache): """ hilbert(x) -> y Return Hilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = sqrt(-1)*sign(j) * x_j y_0 = 0 Notes: If sum(x,axis=0)==0 then hilbert(ihilbert(x)) == x For even len(x), the Nyquist mode of x is taken zero. """ tmp = asarray(x) if iscomplexobj(tmp): return hilbert(tmp.real)+1j*hilbert(tmp.imag) n = len(x) omega = _cache.get(n) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k): if k>0: return 1.0 elif k<0: return -1.0 return 0.0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[n] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def hilbert(x, _cache=_cache): """ hilbert(x) -> y Return Hilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = sqrt(-1)*sign(j) * x_j y_0 = 0 Notes: If sum(x,axis=0)==0 then hilbert(ihilbert(x)) == x For even len(x), the Nyquist mode of x is taken zero. """ tmp = asarray(x) if iscomplexobj(tmp): return hilbert(tmp.real)+1j*hilbert(tmp.imag) n = len(x) omega = _cache.get(n) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k): if k>0: return 1.0 elif k<0: return -1.0 return 0.0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[n] = omega overwrite_x = tmp is not x and not hasattr(x,'__array__') return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def itilbert(x,h,period=None, _cache=_cache): """ Return inverse h-Tilbert transform of a periodic sequence x. If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x and y, respectively, then:: y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j y_0 = 0 For more details, see `tilbert`. """ tmp = asarray(x) if iscomplexobj(tmp): return itilbert(tmp.real,h,period)+\ 1j*itilbert(tmp.imag,h,period) if period is not None: h = h*2*pi/period n = len(x) omega = _cache.get((n,h)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,h=h): if k: return -tanh(h*k) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[(n,h)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def hilbert(x, _cache=_cache): """ hilbert(x) -> y Return Hilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = sqrt(-1)*sign(j) * x_j y_0 = 0 Parameters ---------- x : array_like The input array, should be periodic. _cache : dict, optional Dictionary that contains the kernel used to do a convolution with. Returns ------- y : ndarray The transformed input. Notes ----- If ``sum(x, axis=0) == 0`` then ``hilbert(ihilbert(x)) == x``. For even len(x), the Nyquist mode of x is taken zero. The sign of the returned transform does not have a factor -1 that is more often than not found in the definition of the Hilbert transform. Note also that ``scipy.signal.hilbert`` does have an extra -1 factor compared to this function. """ tmp = asarray(x) if iscomplexobj(tmp): return hilbert(tmp.real) + 1j * hilbert(tmp.imag) n = len(x) omega = _cache.get(n) if omega is None: if len(_cache) > 20: while _cache: _cache.popitem() def kernel(k): if k > 0: return 1.0 elif k < 0: return -1.0 return 0.0 omega = convolve.init_convolution_kernel(n, kernel, d=1) _cache[n] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp, omega, swap_real_imag=1, overwrite_x=overwrite_x)
def diff(x, order=1, period=None, _cache=_cache): """ diff(x, order=1, period=2*pi) -> y Return k-th derivative (or integral) of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j y_0 = 0 if order is not 0. Optional input: order The order of differentiation. Default order is 1. If order is negative, then integration is carried out under the assumption that x_0==0. period The assumed period of the sequence. Default is 2*pi. Notes: If sum(x,axis=0)=0 then diff(diff(x,k),-k)==x (within numerical accuracy) For odd order and even len(x), the Nyquist mode is taken zero. """ tmp = asarray(x) if order == 0: return tmp if iscomplexobj(tmp): return diff(tmp.real, order, period) + 1j * diff(tmp.imag, order, period) if period is not None: c = 2 * pi / period else: c = 1.0 n = len(x) omega = _cache.get((n, order, c)) if omega is None: if len(_cache) > 20: while _cache: _cache.popitem() def kernel(k, order=order, c=c): if k: return pow(c * k, order) return 0 omega = convolve.init_convolution_kernel(n, kernel, d=order, zero_nyquist=1) _cache[(n, order, c)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp, omega, swap_real_imag=order % 2, overwrite_x=overwrite_x)
def diff(x,order=1,period=None, _cache=_cache): """ Return k-th derivative (or integral) of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then:: y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j y_0 = 0 if order is not 0. Parameters ---------- x : array_like order : int, optional The order of differentiation. Default order is 1. If order is negative, then integration is carried out under the assumption that ``x_0 == 0``. period : float, optional The assumed period of the sequence. Default is ``2*pi``. Notes ----- If ``sum(x, axis=0) = 0`` then ``diff(diff(x, k), -k) == x`` (within numerical accuracy). For odd order and even ``len(x)``, the Nyquist mode is taken zero. """ tmp = asarray(x) if order==0: return tmp if iscomplexobj(tmp): return diff(tmp.real,order,period)+1j*diff(tmp.imag,order,period) if period is not None: c = 2*pi/period else: c = 1.0 n = len(x) omega = _cache.get((n,order,c)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,order=order,c=c): if k: return pow(c*k,order) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=order, zero_nyquist=1) _cache[(n,order,c)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=order%2, overwrite_x=overwrite_x)
def find_n_pc(u, factor=0.5): # Get the shape of the galaxy images. gal_shape = np.repeat(np.int(np.sqrt(u.shape[0])), 2) # Find the auto correlation of the left singular vector. u_auto = [convolve(a.reshape(gal_shape), np.rot90(a.reshape(gal_shape), 2)) for a in u.T] # Return the required number of principal components. return np.sum([(a[zip(gal_shape / 2)] ** 2 <= factor * np.sum(a ** 2)) for a in u_auto])
def hilbert(x, _cache=_cache): """ hilbert(x) -> y Return Hilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = sqrt(-1)*sign(j) * x_j y_0 = 0 Parameters ---------- x : array_like The input array, should be periodic. _cache : dict, optional Dictionary that contains the kernel used to do a convolution with. Returns ------- y : ndarray The transformed input. Notes ----- If ``sum(x, axis=0) == 0`` then ``hilbert(ihilbert(x)) == x``. For even len(x), the Nyquist mode of x is taken zero. The sign of the returned transform does not have a factor -1 that is more often than not found in the definition of the Hilbert transform. Note also that ``scipy.signal.hilbert`` does have an extra -1 factor compared to this function. """ tmp = asarray(x) if iscomplexobj(tmp): return hilbert(tmp.real)+1j*hilbert(tmp.imag) n = len(x) omega = _cache.get(n) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k): if k>0: return 1.0 elif k<0: return -1.0 return 0.0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[n] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def sc_diff(x, a, b, period=None, _cache=_cache): """ Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then:: y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j y_0 = 0 Parameters ---------- x : array_like Input array. a,b : float Defines the parameters of the sinh/cosh pseudo-differential operator. period : float, optional The period of the sequence x. Default is 2*pi. Notes ----- ``sc_diff(cs_diff(x,a,b),b,a) == x`` For even ``len(x)``, the Nyquist mode of x is taken as zero. """ tmp = asarray(x) if iscomplexobj(tmp): return sc_diff(tmp.real,a,b,period)+\ 1j*sc_diff(tmp.imag,a,b,period) if period is not None: a = a * 2 * pi / period b = b * 2 * pi / period n = len(x) omega = _cache.get((n, a, b)) if omega is None: if len(_cache) > 20: while _cache: _cache.popitem() def kernel(k, a=a, b=b): if k: return sinh(a * k) / cosh(b * k) return 0 omega = convolve.init_convolution_kernel(n, kernel, d=1) _cache[(n, a, b)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp, omega, swap_real_imag=1, overwrite_x=overwrite_x)
def calculate_features(image, fov_mask, mask_list, k_size): """ Calculate line score, orthogonal line score, and inverse green-channel intensity vectors """ inverse_green = as_inverse_green( image) # Line detector applied to inverse green function = lambda x, y: line_score( x, y, mask_list) # Function to apply to each neighborhood result = convolve(inverse_green, k_size, function, fov_mask, 2) vectors = np.dstack( (result, inverse_green)) # Union of all feature vectors vectors = normalize_features(vectors) return vectors
def tilbert(x, h, period=None, _cache=_cache): """ tilbert(x, h, period=2*pi) -> y Return h-Tilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j y_0 = 0 Input: h Defines the parameter of the Tilbert transform. period The assumed period of the sequence. Default period is 2*pi. Notes: If sum(x,axis=0)==0 and n=len(x) is odd then tilbert(itilbert(x)) == x If 2*pi*h/period is approximately 10 or larger then numerically tilbert == hilbert (theoretically oo-Tilbert == Hilbert). For even len(x), the Nyquist mode of x is taken zero. """ tmp = asarray(x) if iscomplexobj(tmp): return tilbert(tmp.real,h,period)+\ 1j*tilbert(tmp.imag,h,period) if period is not None: h = h * 2 * pi / period n = len(x) omega = _cache.get((n, h)) if omega is None: if len(_cache) > 20: while _cache: _cache.popitem() def kernel(k, h=h): if k: return 1.0 / tanh(h * k) return 0 omega = convolve.init_convolution_kernel(n, kernel, d=1) _cache[(n, h)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp, omega, swap_real_imag=1, overwrite_x=overwrite_x)
def diff(x,order=1,period=None, _cache = _cache): """ diff(x, order=1, period=2*pi) -> y Return k-th derivative (or integral) of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j y_0 = 0 if order is not 0. Optional input: order The order of differentiation. Default order is 1. If order is negative, then integration is carried out under the assumption that x_0==0. period The assumed period of the sequence. Default is 2*pi. Notes: If sum(x,axis=0)=0 then diff(diff(x,k),-k)==x (within numerical accuracy) For odd order and even len(x), the Nyquist mode is taken zero. """ tmp = asarray(x) if order==0: return tmp if iscomplexobj(tmp): return diff(tmp.real,order,period)+1j*diff(tmp.imag,order,period) if period is not None: c = 2*pi/period else: c = 1.0 n = len(x) omega = _cache.get((n,order,c)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,order=order,c=c): if k: return pow(c*k,order) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=order, zero_nyquist=1) _cache[(n,order,c)] = omega overwrite_x = tmp is not x and not hasattr(x,'__array__') return convolve.convolve(tmp,omega,swap_real_imag=order%2, overwrite_x=overwrite_x)
def sc_diff(x, a, b, period=None, _cache=_cache): """ sc_diff(x, a, b, period=2*pi) -> y Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j y_0 = 0 Input: a,b Defines the parameters of the sinh/cosh pseudo-differential operator. period The period of the sequence x. Default is 2*pi. Notes: sc_diff(cs_diff(x,a,b),b,a) == x For even len(x), the Nyquist mode of x is taken zero. """ tmp = asarray(x) if iscomplexobj(tmp): return sc_diff(tmp.real,a,b,period)+\ 1j*sc_diff(tmp.imag,a,b,period) if period is not None: a = a * 2 * pi / period b = b * 2 * pi / period n = len(x) omega = _cache.get((n, a, b)) if omega is None: if len(_cache) > 20: while _cache: _cache.popitem() def kernel(k, a=a, b=b): if k: return sinh(a * k) / cosh(b * k) return 0 omega = convolve.init_convolution_kernel(n, kernel, d=1) _cache[(n, a, b)] = omega overwrite_x = tmp is not x and not hasattr(x, '__array__') return convolve.convolve(tmp, omega, swap_real_imag=1, overwrite_x=overwrite_x)
def sc_diff(x, a, b, period=None, _cache = _cache): """ Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then:: y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j y_0 = 0 Parameters ---------- x : array_like Input array. a,b : float Defines the parameters of the sinh/cosh pseudo-differential operator. period : float, optional The period of the sequence x. Default is 2*pi. Notes ----- ``sc_diff(cs_diff(x,a,b),b,a) == x`` For even ``len(x)``, the Nyquist mode of x is taken as zero. """ tmp = asarray(x) if iscomplexobj(tmp): return sc_diff(tmp.real,a,b,period)+\ 1j*sc_diff(tmp.imag,a,b,period) if period is not None: a = a*2*pi/period b = b*2*pi/period n = len(x) omega = _cache.get((n,a,b)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,a=a,b=b): if k: return sinh(a*k)/cosh(b*k) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[(n,a,b)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def _blur_buf(d_g, width=(4.0, 4.0), res_g=None): Ns = [3 * s + 1 for s in width] sigmas = [0.5 * s for s in width] hs = [np.exp(-0.5 / s ** 2 * np.linspace(-N / 2, N / 2, N) ** 2) for s, N in zip(sigmas, Ns)] h_gs = [OCLArray.from_array(h.astype(np.float32)) for h in hs][::-1] if len(d_g.shape) == 1: return convolve(d_g, *h_gs, res_g=res_g) elif len(d_g.shape) == 2: return convolve_sep2(d_g, *h_gs, res_g=res_g) elif len(d_g.shape) == 3: return convolve_sep3(d_g, *h_gs, res_g=res_g) else: pass
def ss_diff(x, a, b, period=None, _cache=_cache): """ Return (a,b)-sinh/sinh pseudo-derivative of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then:: y_j = sinh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j y_0 = a/b * x_0 Parameters ---------- x : array_like The array to take the pseudo-derivative from. a,b Defines the parameters of the sinh/sinh pseudo-differential operator. period : float, optional The period of the sequence x. Default is ``2*pi``. Notes ----- ``ss_diff(ss_diff(x,a,b),b,a) == x`` """ tmp = asarray(x) if iscomplexobj(tmp): return ss_diff(tmp.real,a,b,period)+\ 1j*ss_diff(tmp.imag,a,b,period) if period is not None: a = a*2*pi/period b = b*2*pi/period n = len(x) omega = _cache.get((n,a,b)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,a=a,b=b): if k: return sinh(a*k)/sinh(b*k) return float(a)/b omega = convolve.init_convolution_kernel(n,kernel) _cache[(n,a,b)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,overwrite_x=overwrite_x)
def tilbert(x,h,period=None, _cache = _cache): """ tilbert(x, h, period=2*pi) -> y Return h-Tilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j y_0 = 0 Input: h Defines the parameter of the Tilbert transform. period The assumed period of the sequence. Default period is 2*pi. Notes: If sum(x,axis=0)==0 and n=len(x) is odd then tilbert(itilbert(x)) == x If 2*pi*h/period is approximately 10 or larger then numerically tilbert == hilbert (theoretically oo-Tilbert == Hilbert). For even len(x), the Nyquist mode of x is taken zero. """ tmp = asarray(x) if iscomplexobj(tmp): return tilbert(tmp.real,h,period)+\ 1j*tilbert(tmp.imag,h,period) if period is not None: h = h*2*pi/period n = len(x) omega = _cache.get((n,h)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,h=h): if k: return 1.0/tanh(h*k) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[(n,h)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def cc_diff(x, a, b, period=None, _cache=_cache): """ cc_diff(x, a, b, period=2*pi) -> y Return (a,b)-cosh/cosh pseudo-derivative of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = cosh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j Input: a,b Defines the parameters of the sinh/sinh pseudo-differential operator. Optional input: period The period of the sequence x. Default is 2*pi. Notes: cc_diff(cc_diff(x,a,b),b,a) == x """ tmp = asarray(x) if iscomplexobj(tmp): return cc_diff(tmp.real,a,b,period)+\ 1j*cc_diff(tmp.imag,a,b,period) if period is not None: a = a * 2 * pi / period b = b * 2 * pi / period n = len(x) omega = _cache.get((n, a, b)) if omega is None: if len(_cache) > 20: while _cache: _cache.popitem() def kernel(k, a=a, b=b): return cosh(a * k) / cosh(b * k) omega = convolve.init_convolution_kernel(n, kernel) _cache[(n, a, b)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp, omega, overwrite_x=overwrite_x)
def convolve_dir_filters(data, filters): """Convolve with directional filters This method convolves the input data with the provided filters Parameters ---------- data : np.ndarray Input data array filters : np.ndarray 3D array of filters Returns ------- np.ndarray of convolved data """ return np.array([convolve(data, f) for f in filters])
def sc_diff(x, a, b, period=None, _cache = _cache): """ sc_diff(x, a, b, period=2*pi) -> y Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j y_0 = 0 Input: a,b Defines the parameters of the sinh/cosh pseudo-differential operator. period The period of the sequence x. Default is 2*pi. Notes: sc_diff(cs_diff(x,a,b),b,a) == x For even len(x), the Nyquist mode of x is taken zero. """ tmp = asarray(x) if iscomplexobj(tmp): return sc_diff(tmp.real,a,b,period)+\ 1j*sc_diff(tmp.imag,a,b,period) if period is not None: a = a*2*pi/period b = b*2*pi/period n = len(x) omega = _cache.get((n,a,b)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,a=a,b=b): if k: return sinh(a*k)/cosh(b*k) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[(n,a,b)] = omega overwrite_x = tmp is not x and not hasattr(x,'__array__') return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def cs_diff(x, a, b, period=None, _cache=_cache): """ Return (a,b)-cosh/sinh pseudo-derivative of a periodic sequence x. If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x and y, respectively, then:: y_j = -sqrt(-1)*cosh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j y_0 = 0 Parameters ---------- x : array_like The array to take the pseudo-derivative from. a, b : float Defines the parameters of the cosh/sinh pseudo-differential operator. period : float, optional The period of the sequence. Default period is ``2*pi``. Notes: For even len(x), the Nyquist mode of x is taken zero. """ tmp = asarray(x) if iscomplexobj(tmp): return cs_diff(tmp.real,a,b,period)+\ 1j*cs_diff(tmp.imag,a,b,period) if period is not None: a = a*2*pi/period b = b*2*pi/period n = len(x) omega = _cache.get((n,a,b)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,a=a,b=b): if k: return -cosh(a*k)/sinh(b*k) return 0 omega = convolve.init_convolution_kernel(n,kernel,d=1) _cache[(n,a,b)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
def cc_diff(x, a, b, period=None, _cache = _cache): """ cc_diff(x, a, b, period=2*pi) -> y Return (a,b)-cosh/cosh pseudo-derivative of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then y_j = cosh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j Input: a,b Defines the parameters of the sinh/sinh pseudo-differential operator. Optional input: period The period of the sequence x. Default is 2*pi. Notes: cc_diff(cc_diff(x,a,b),b,a) == x """ tmp = asarray(x) if iscomplexobj(tmp): return cc_diff(tmp.real,a,b,period)+\ 1j*cc_diff(tmp.imag,a,b,period) if period is not None: a = a*2*pi/period b = b*2*pi/period n = len(x) omega = _cache.get((n,a,b)) if omega is None: if len(_cache)>20: while _cache: _cache.popitem() def kernel(k,a=a,b=b): return cosh(a*k)/cosh(b*k) omega = convolve.init_convolution_kernel(n,kernel) _cache[(n,a,b)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,overwrite_x=overwrite_x)
def _blur_buf(d_g, width=(4., 4.), res_g=None): Ns = [3 * s + 1 for s in width] sigmas = [.5 * s for s in width] hs = [ np.exp(-.5 / s**2 * np.linspace(-N / 2, N / 2, N)**2) for s, N in zip(sigmas, Ns) ] h_gs = [OCLArray.from_array(h.astype(np.float32)) for h in hs][::-1] if len(d_g.shape) == 1: return convolve(d_g, *h_gs, res_g=res_g) elif len(d_g.shape) == 2: return convolve_sep2(d_g, *h_gs, res_g=res_g) elif len(d_g.shape) == 3: return convolve_sep3(d_g, *h_gs, res_g=res_g) else: pass
def basic(indata, threshold=0, norm=0): x = convolve(indata, BASIC_X, threshold, norm) y = convolve(indata, BASIC_Y, threshold, norm) return x, y
def roberts(indata, threshold=0, norm=0): get_new_image(convolve(indata, ROBERTS, threshold, norm), "L").show() sys.exit(0)
def sobel(indata, threshold=0, norm=0): """Pass to gradient_images(): Convolves Sobel operator with image data""" sx = convolve(indata, SOBEL_GX, threshold, norm) sy = convolve(indata, SOBEL_GY, threshold, norm) return sx, sy
def prewitt(indata, threshold=0, norm=0): """Pass to gradient_images(): Convolves Prewitt operator with image data""" px = convolve(indata, PREWITT_GX, threshold, norm) py = convolve(indata, PREWITT_GY, threshold, norm) return px, py
def scharr(indata, threshold=0, norm=0): """Pass to gradient_images(): Convolves common Scharr operator with image data""" scx = convolve(indata, SCHARR_GX, threshold, norm) scy = convolve(indata, SCHARR_GY, threshold, norm) return scx, scy
def optimal(indata, threshold=0, norm=0): """Pass to gradient_images(): Convolves optimal Scharr operator with images data""" ox = convolve(indata, OPTIMAL_GX, threshold, norm) oy = convolve(indata, OPTIMAL_GY, threshold, norm) return ox, oy
# SOUND GENERATION def generate_sin (num_samples, sample_rate, frequency_Hz, amplitude=1.0, phase_shift=0): x = np.arange(num_samples).astype(np.float32) y = float(amplitude)*np.sin(2*np.pi*frequency_Hz*x/sample_rate + phase_shift) return y def generate_simple_harmony (notes, num_samples, sample_rate, total_amplitude=1.0, phase_shift=0): waves = [] for note in notes: waves.append(generate_sin(num_samples, sample_rate, note_to_freq[note], amplitude=float(total_amplitude)/(len(notes)), phase_shift=phase_shift)) return sum(waves) # END SOUND GENERATION if __name__ == '__main__': c4 = generate_sin(48000, 48000, note_to_freq['C4']) c5 = generate_sin(48000, 48000, note_to_freq['C5']) d5 = generate_sin(48000, 48000, note_to_freq['D5']) g5 = generate_sin(48000, 48000, note_to_freq['G5']) y = np.concatenate((c4, c5, d5, g5, c4)) wavfile.write('melody.wav', 48000, y) sr0, y0 = wavfile.read('rachel.wav') sr1, y1 = wavfile.read('../ir_3_L_7.wav') y = convolve(y0, sr0, y1, sr1) wavfile.write('rachel_reverb.wav', sr0, y)
#!/usr/bin/env python import histogram.hdf as hh, os # exp = hh.load(os.path.expanduser("/SNS/users/linjiao/simulations/ARCS/He4/2015/exp/11315/m2-focused.h5")) exp = hh.load(os.path.expanduser("/SNS/users/linjiao/simulations/ARCS/mod2sample/686.62meV/exp/m1-focused.h5")) sim = hh.load("out/mon1-itof-focused.h5") oldsim = hh.load("../../fitmonitor/n1e9/out/mon1-itof-focused.h5") tofmin, tofmax = 1021, 1055 us = 1.e-6 exp = exp[(tofmin, tofmax)] sim = sim[(tofmin*us, tofmax*us)] oldsim = oldsim[(tofmin*us, tofmax*us)] from convolve import convolve sim2I = convolve(sim, 1.1*us, N=20) import pylab pylab.plot(exp.tof, exp.I, label="exp") # pylab.plot(sim.tof*1e6+3, sim.I*.000031, label="sim") # pylab.plot(sim.tof/us-.7, sim.I*.78, label="sim") # pylab.plot(sim.tof/us-.7, sim2I*.85, label="sim with monitor thickness effect") pylab.plot(sim.tof/us-.7, sim2I*.85, label="sim") pylab.plot(oldsim.tof/us+3.1, oldsim.I*.49, label="old sim") pylab.xlim(1020, 1055) pylab.legend() pylab.show()
def feat(data): """ Returns an array of features for a given patch. Standard dimensions: patch_size_0 x patch_size_1 x # of kernels used. """ fi = [] names = [] t0 = time.time() # print 'entered feat' # gaussian 57 - 68 fi.append(data) names.append('initial picture: intesity values') g05 = convolve(data,gauss(sigma_x = 0.5, sigma_y = 0.5)) g1 = convolve(data,gauss(sigma_x = 1.0, sigma_y = 1.0)) g2 = convolve(data,gauss(sigma_x = 2.0, sigma_y = 2.0)) g3 = convolve(data,gauss(sigma_x = 3.0, sigma_y = 3.0)) g4 = convolve(data,gauss(sigma_x = 4.0, sigma_y = 4.0)) g5 = convolve(data,gauss(sigma_x = 5.0, sigma_y = 5.0)) g6 = convolve(data,gauss(sigma_x = 6.0, sigma_y = 6.0)) g7 = convolve(data,gauss(sigma_x = 7.0, sigma_y = 7.0)) g10 = convolve(data,gauss(sigma_x = 10.0, sigma_y = 10.0)) g12 = convolve(data,gauss(sigma_x = 12.0, sigma_y = 12.0)) g14 = convolve(data,gauss(sigma_x = 14.0, sigma_y = 14.0)) g16 = convolve(data,gauss(sigma_x = 16.0, sigma_y = 16.0)) fi.append(g05) names.append('gauss(sigma_x = 0.5, sigma_y = 0.5))') fi.append(g1) names.append('gauss(sigma_x = 1., sigma_y = 1.))') fi.append(g2) names.append('gauss(sigma_x = 2., sigma_y = 2.))') fi.append(g3) names.append('gauss(sigma_x = 3., sigma_y = 3.)') fi.append(g4) names.append('gauss(sigma_x = 4., sigma_y = 4.)') fi.append(g5) names.append('gauss(sigma_x = 5., sigma_y = 5.)') fi.append(g6) names.append('gauss(sigma_x = 6., sigma_y = 6.)') fi.append(g7) names.append('gauss(sigma_x = 7., sigma_y = 7.)') fi.append(g10) names.append('gauss(sigma_x = 10., sigma_y = 10.)') fi.append(g12) names.append('gauss(sigma_x = 12., sigma_y = 12.)') fi.append(g14) names.append('gauss(sigma_x = 14., sigma_y = 14.)') fi.append(g16) names.append('gauss(sigma_x = 16., sigma_y = 16.)') tg = time.time() # print 'performed gaussian blurs - '+str(tg-t0) # bilateral 0 - 15 fi.append(gpu_bilateral(data, Nx=10, Ny=10, sigma_int=1.0, sigma_dist=1.0)) names.append('gpu_bilateral(data, Nx=10, Ny=10, sigma_int=1.0, sigma_dist=1.0)') fi.append(gpu_bilateral(data, Nx=10, Ny=10, sigma_int=5.0, sigma_dist=1.0)) names.append('gpu_bilateral(data, Nx=10, Ny=10, sigma_int=5.0, sigma_dist=1.0)') fi.append(gpu_bilateral(data, Nx=10, Ny=10, sigma_int=10.0, sigma_dist=1.0)) names.append('gpu_bilateral(data, Nx=10, Ny=10, sigma_int=10.0, sigma_dist=1.0)') # fi.append(gpu_bilateral(data, Nx=20, Ny=20, sigma_int=2.0, sigma_dist=5.0)) # names.append('gpu_bilateral(data, Nx=20, Ny=20, sigma_int=2.0, sigma_dist=5.0)') # fi.append(gpu_bilateral(data, Nx=20, Ny=20, sigma_int=5.0, sigma_dist=5.0)) # names.append('gpu_bilateral(data, Nx=20, Ny=20, sigma_int=5.0, sigma_dist=5.0)') # fi.append(gpu_bilateral(data, Nx=20, Ny=20, sigma_int=7.0, sigma_dist=5.0)) # names.append('gpu_bilateral(data, Nx=20, Ny=20, sigma_int=7.0, sigma_dist=5.0)') # fi.append(gpu_bilateral(data, Nx=20, Ny=20, sigma_int=10.0, sigma_dist=5.0)) # names.append('gpu_bilateral(data, Nx=20, Ny=20, sigma_int=10.0, sigma_dist=5.0)') # fi.append(gpu_bilateral(data, Nx=20, Ny=20, sigma_int=2.0, sigma_dist=7.0)) # names.append('gpu_bilateral(data, Nx=20, Ny=20, sigma_int=2.0, sigma_dist=7.0)') # fi.append(gpu_bilateral(data, Nx=20, Ny=20, sigma_int=5.0, sigma_dist=7.0)) # names.append('gpu_bilateral(data, Nx=20, Ny=20, sigma_int=5.0, sigma_dist=7.0)') # fi.append(gpu_bilateral(data, Nx=20, Ny=20, sigma_int=7.0, sigma_dist=7.0)) # names.append('gpu_bilateral(data, Nx=20, Ny=20, sigma_int=7.0, sigma_dist=7.0)') # fi.append(gpu_bilateral(data, Nx=20, Ny=20, sigma_int=10.0, sigma_dist=7.0)) # names.append('gpu_bilateral(data, Nx=20, Ny=20, sigma_int=10.0, sigma_dist=7.0)') # fi.append(gpu_bilateral(data, Nx=50, Ny=50, sigma_int=10.0, sigma_dist=10.0)) # names.append('gpu_bilateral(data, Nx=50, Ny=50, sigma_int=10.0, sigma_dist=10.0)') # fi.append(gpu_bilateral(data, Nx=50, Ny=50, sigma_int=10.0, sigma_dist=20.0)) # names.append('gpu_bilateral(data, Nx=50, Ny=50, sigma_int=10.0, sigma_dist=20.0)') # fi.append(gpu_bilateral(data, Nx=50, Ny=50, sigma_int=10.0, sigma_dist=30.0)) # names.append('gpu_bilateral(data, Nx=50, Ny=50, sigma_int=10.0, sigma_dist=30.0)') # fi.append(gpu_bilateral(data, Nx=50, Ny=50, sigma_int=20.0, sigma_dist=10.0)) # names.append('gpu_bilateral(data, Nx=50, Ny=50, sigma_int=20.0, sigma_dist=10.0)') # fi.append(gpu_bilateral(data, Nx=50, Ny=50, sigma_int=30.0, sigma_dist=10.0)) # names.append('gpu_bilateral(data, Nx=50, Ny=50, sigma_int=30.0, sigma_dist=10.0)') # entropy 16 - 20 fi.append(gpu_entropy(data, Nx=2,Ny=2)) names.append('gpu_entropy(data, Nx=2,Ny=2)') fi.append(gpu_entropy(data, Nx=3,Ny=3)) names.append('gpu_entropy(data, Nx=2,Ny=2)') fi.append(gpu_entropy(data, Nx=5,Ny=4)) names.append('gpu_entropy(data, Nx=2,Ny=2)') fi.append(gpu_entropy(data, Nx=5,Ny=5)) names.append('gpu_entropy(data, Nx=2,Ny=2)') fi.append(gpu_entropy(data, Nx=7,Ny=7)) names.append('gpu_entropy(data, Nx=2,Ny=2)') # hessian 21 - 28 H = gpu_hessian(data) fi.append(H[:,:,0]) names.append('H[:,:,0]') fi.append(H[:,:,1]) names.append('H[:,:,1]') fi.append(H[:,:,2]) names.append('H[:,:,2]') fi.append(H[:,:,3]) names.append('H[:,:,3]') fi.append(H[:,:,4]) names.append('H[:,:,4]') fi.append(H[:,:,5]) names.append('H[:,:,5]') fi.append(H[:,:,6]) names.append('H[:,:,6]') fi.append(H[:,:,7]) names.append('H[:,:,7]') H = gpu_hessian(g05) fi.append(H[:,:,0]) names.append('H05[:,:,0]') fi.append(H[:,:,1]) names.append('H05[:,:,1]') fi.append(H[:,:,2]) names.append('H05[:,:,2]') fi.append(H[:,:,3]) names.append('H05[:,:,3]') fi.append(H[:,:,4]) names.append('H05[:,:,4]') fi.append(H[:,:,5]) names.append('H05[:,:,5]') fi.append(H[:,:,6]) names.append('H05[:,:,6]') fi.append(H[:,:,7]) names.append('H05[:,:,7]') H = gpu_hessian(g1) fi.append(H[:,:,0]) names.append('H1[:,:,0]') fi.append(H[:,:,1]) names.append('H1[:,:,1]') fi.append(H[:,:,2]) names.append('H1[:,:,2]') fi.append(H[:,:,3]) names.append('H1[:,:,3]') fi.append(H[:,:,4]) names.append('H1[:,:,4]') fi.append(H[:,:,5]) names.append('H1[:,:,5]') fi.append(H[:,:,6]) names.append('H1[:,:,6]') fi.append(H[:,:,7]) names.append('H1[:,:,7]') H = gpu_hessian(g2) fi.append(H[:,:,0]) names.append('H2[:,:,0]') fi.append(H[:,:,1]) names.append('H2[:,:,1]') fi.append(H[:,:,2]) names.append('H2[:,:,2]') fi.append(H[:,:,3]) names.append('H2[:,:,3]') fi.append(H[:,:,4]) names.append('H2[:,:,4]') fi.append(H[:,:,5]) names.append('H2[:,:,5]') fi.append(H[:,:,6]) names.append('H2[:,:,6]') fi.append(H[:,:,7]) names.append('H2[:,:,7]') H = gpu_hessian(g3) fi.append(H[:,:,0]) names.append('H3[:,:,0]') fi.append(H[:,:,1]) names.append('H3[:,:,1]') fi.append(H[:,:,2]) names.append('H3[:,:,2]') fi.append(H[:,:,3]) names.append('H3[:,:,3]') fi.append(H[:,:,4]) names.append('H3[:,:,4]') fi.append(H[:,:,5]) names.append('H3[:,:,5]') fi.append(H[:,:,6]) names.append('H3[:,:,6]') fi.append(H[:,:,7]) names.append('H3[:,:,7]') H = gpu_hessian(g4) fi.append(H[:,:,0]) names.append('H4[:,:,0]') fi.append(H[:,:,1]) names.append('H4[:,:,1]') fi.append(H[:,:,2]) names.append('H4[:,:,2]') fi.append(H[:,:,3]) names.append('H4[:,:,3]') fi.append(H[:,:,4]) names.append('H4[:,:,4]') fi.append(H[:,:,5]) names.append('H4[:,:,5]') fi.append(H[:,:,6]) names.append('H4[:,:,6]') fi.append(H[:,:,7]) names.append('H4[:,:,7]') H = gpu_hessian(g5) fi.append(H[:,:,0]) names.append('H5[:,:,0]') fi.append(H[:,:,1]) names.append('H5[:,:,1]') fi.append(H[:,:,2]) names.append('H5[:,:,2]') fi.append(H[:,:,3]) names.append('H5[:,:,3]') fi.append(H[:,:,4]) names.append('H5[:,:,4]') fi.append(H[:,:,5]) names.append('H5[:,:,5]') fi.append(H[:,:,6]) names.append('H5[:,:,6]') fi.append(H[:,:,7]) names.append('H5[:,:,7]') H = gpu_hessian(g6) fi.append(H[:,:,0]) names.append('H6[:,:,0]') fi.append(H[:,:,1]) names.append('H6[:,:,1]') fi.append(H[:,:,2]) names.append('H6[:,:,2]') fi.append(H[:,:,3]) names.append('H6[:,:,3]') fi.append(H[:,:,4]) names.append('H6[:,:,4]') fi.append(H[:,:,5]) names.append('H6[:,:,5]') fi.append(H[:,:,6]) names.append('H6[:,:,6]') fi.append(H[:,:,7]) names.append('H6[:,:,7]') H = gpu_hessian(g7) fi.append(H[:,:,0]) names.append('H7[:,:,0]') fi.append(H[:,:,1]) names.append('H7[:,:,1]') fi.append(H[:,:,2]) names.append('H7[:,:,2]') fi.append(H[:,:,3]) names.append('H7[:,:,3]') fi.append(H[:,:,4]) names.append('H7[:,:,4]') fi.append(H[:,:,5]) names.append('H7[:,:,5]') fi.append(H[:,:,6]) names.append('H7[:,:,6]') fi.append(H[:,:,7]) names.append('H7[:,:,7]') H = gpu_hessian(g10) fi.append(H[:,:,0]) names.append('H10[:,:,0]') fi.append(H[:,:,1]) names.append('H10[:,:,1]') fi.append(H[:,:,2]) names.append('H10[:,:,2]') fi.append(H[:,:,3]) names.append('H10[:,:,3]') fi.append(H[:,:,4]) names.append('H10[:,:,4]') fi.append(H[:,:,5]) names.append('H10[:,:,5]') fi.append(H[:,:,6]) names.append('H10[:,:,6]') fi.append(H[:,:,7]) names.append('H10[:,:,7]') H = gpu_hessian(g12) fi.append(H[:,:,0]) names.append('H12[:,:,0]') fi.append(H[:,:,1]) names.append('H12[:,:,1]') fi.append(H[:,:,2]) names.append('H12[:,:,2]') fi.append(H[:,:,3]) names.append('H12[:,:,3]') fi.append(H[:,:,4]) names.append('H12[:,:,4]') fi.append(H[:,:,5]) names.append('H12[:,:,5]') fi.append(H[:,:,6]) names.append('H12[:,:,6]') fi.append(H[:,:,7]) names.append('H12[:,:,7]') H = gpu_hessian(g14) fi.append(H[:,:,0]) names.append('H14[:,:,0]') fi.append(H[:,:,1]) names.append('H14[:,:,1]') fi.append(H[:,:,2]) names.append('H14[:,:,2]') fi.append(H[:,:,3]) names.append('H14[:,:,3]') fi.append(H[:,:,4]) names.append('H14[:,:,4]') fi.append(H[:,:,5]) names.append('H14[:,:,5]') fi.append(H[:,:,6]) names.append('H14[:,:,6]') fi.append(H[:,:,7]) names.append('H14[:,:,7]') H = gpu_hessian(g16) fi.append(H[:,:,0]) names.append('H16[:,:,0]') fi.append(H[:,:,1]) names.append('H16[:,:,1]') fi.append(H[:,:,2]) names.append('H16[:,:,2]') fi.append(H[:,:,3]) names.append('H16[:,:,3]') fi.append(H[:,:,4]) names.append('H16[:,:,4]') fi.append(H[:,:,5]) names.append('H16[:,:,5]') fi.append(H[:,:,6]) names.append('H16[:,:,6]') fi.append(H[:,:,7]) names.append('H16[:,:,7]') # kuwahara 29 - 33 fi.append(gpu_kuwahara(data, N=3)) names.append('gpu_kuwahara(data, N=3)') fi.append(gpu_kuwahara(data, N=5)) names.append('gpu_kuwahara(data, N=5)') fi.append(gpu_kuwahara(data, N=7)) names.append('gpu_kuwahara(data, N=7)') fi.append(gpu_kuwahara(data, N=9)) names.append('gpu_kuwahara(data, N=9)') fi.append(gpu_kuwahara(data, N=11)) names.append('gpu_kuwahara(data, N=11)') # maximum 34 - 38 fi.append(gpu_maximum(data, Nx=2, Ny=2)) names.append('gpu_maximum(data, Nx=2, Ny=2)') fi.append(gpu_maximum(data, Nx=3, Ny=3)) names.append('gpu_maximum(data, Nx=3, Ny=3)') fi.append(gpu_maximum(data, Nx=4, Ny=4)) names.append('gpu_maximum(data, Nx=4, Ny=4)') fi.append(gpu_maximum(data, Nx=5, Ny=5)) names.append('gpu_maximum(data, Nx=5, Ny=5)') fi.append(gpu_maximum(data, Nx=7, Ny=7)) names.append('gpu_maximum(data, Nx=7, Ny=7)') # median 39 - 43 fi.append(gpu_median(data, Nx=2, Ny=2)) names.append('gpu_median(data, Nx=2, Ny=2)') fi.append(gpu_median(data, Nx=3, Ny=3)) names.append('gpu_median(data, Nx=3, Ny=3)') fi.append(gpu_median(data, Nx=4, Ny=4)) names.append('gpu_median(data, Nx=4, Ny=4)') fi.append(gpu_median(data, Nx=5, Ny=5)) names.append('gpu_median(data, Nx=5, Ny=5)') fi.append(gpu_median(data, Nx=7, Ny=7)) names.append('gpu_median(data, Nx=7, Ny=7)') # minimum 44 - 48 fi.append(gpu_minimum(data, Nx=2, Ny=2)) names.append('gpu_minimum(data, Nx=2, Ny=2)') fi.append(gpu_minimum(data, Nx=3, Ny=3)) names.append('(gpu_minimum(data, Nx=3, Ny=3)') fi.append(gpu_minimum(data, Nx=4, Ny=4)) names.append('gpu_minimum(data, Nx=4, Ny=4)') fi.append(gpu_minimum(data, Nx=5, Ny=5)) names.append('gpu_minimum(data, Nx=5, Ny=5)') fi.append(gpu_minimum(data, Nx=7, Ny=7)) names.append('gpu_minimum(data, Nx=7, Ny=7)') # structure 49 - 50 S = gpu_structure(data) fi.append(S[:,:,0]) names.append('S[:,:,0]') fi.append(S[:,:,1]) names.append('S[:,:,1]') S_g05 = gpu_structure(g05) fi.append(S_g05[:,:,0]) names.append('S_g05[:,:,0]') fi.append(S_g05[:,:,1]) names.append('S_g05[:,:,1]') S_g1 = gpu_structure(g1) fi.append(S_g1[:,:,0]) names.append('S_g1[:,:,0]') fi.append(S_g1[:,:,1]) names.append('S_g1[:,:,1]') S_g2 = gpu_structure(g2) fi.append(S_g2[:,:,0]) names.append('S_g2[:,:,0]') fi.append(S_g2[:,:,1]) names.append('S_g2[:,:,1]') S_g3 = gpu_structure(g3) fi.append(S_g3[:,:,0]) names.append('S_g3[:,:,0]') fi.append(S_g3[:,:,1]) names.append('S_g3[:,:,1]') S_g4 = gpu_structure(g4) fi.append(S_g4[:,:,0]) names.append('S_g4[:,:,0]') fi.append(S_g4[:,:,1]) names.append('S_g4[:,:,1]') S_g5 = gpu_structure(g5) fi.append(S_g5[:,:,0]) names.append('S_g5[:,:,0]') fi.append(S_g5[:,:,1]) names.append('S_g5[:,:,1]') S_g6 = gpu_structure(g6) fi.append(S_g6[:,:,0]) names.append('S_g6[:,:,0]') fi.append(S_g6[:,:,1]) names.append('S_g6[:,:,1]') S_g7 = gpu_structure(g7) fi.append(S_g7[:,:,0]) names.append('S_g7[:,:,0]') fi.append(S_g7[:,:,1]) names.append('S_g7[:,:,1]') S_g10 = gpu_structure(g10) fi.append(S_g10[:,:,0]) names.append('S_g10[:,:,0]') fi.append(S_g10[:,:,1]) names.append('S_g10[:,:,1]') S_g12 = gpu_structure(g12) fi.append(S_g12[:,:,0]) names.append('S_g12[:,:,0]') fi.append(S_g12[:,:,1]) names.append('S_g12[:,:,1]') S_g14 = gpu_structure(g14) fi.append(S_g14[:,:,0]) names.append('S_g14[:,:,0]') fi.append(S_g14[:,:,1]) names.append('S_g14[:,:,1]') S_g16 = gpu_structure(g16) fi.append(S_g16[:,:,0]) names.append('S_g16[:,:,0]') fi.append(S_g16[:,:,1]) names.append('S_g16[:,:,1]') # variance 51 - 56 fi.append(gpu_variance(data, Nx=2, Ny=2)) names.append('gpu_variance(data, Nx=2, Ny=2)') fi.append(gpu_variance(data, Nx=3, Ny=3)) names.append('gpu_variance(data, Nx=3, Ny=3)') fi.append(gpu_variance(data, Nx=4, Ny=4)) names.append('gpu_variance(data, Nx=4, Ny=4)') fi.append(gpu_variance(data, Nx=5, Ny=5)) names.append('gpu_variance(data, Nx=5, Ny=5)') fi.append(gpu_variance(data, Nx=6, Ny=6)) names.append('gpu_variance(data, Nx=6, Ny=6)') fi.append(gpu_variance(data, Nx=7, Ny=7)) names.append('gpu_variance(data, Nx=7, Ny=7)') # difference of gaussians 69 - 83 fi.append(convolve(data,gauss(sigma_x = 0.5, sigma_y = 0.5) - gauss())) names.append('gauss(sigma_x = 0.5, sigma_y = 0.5) - gauss()') fi.append(convolve(data,gauss(sigma_x = 1., sigma_y = 1.) - gauss())) names.append('gauss(sigma_x = 1., sigma_y = 1.) - gauss()') fi.append(convolve(data,gauss(sigma_x = 2., sigma_y = 2.) - gauss())) names.append('gauss(sigma_x = 2., sigma_y = 2.) - gauss()') fi.append(convolve(data,gauss(sigma_x = 4., sigma_y = 4.) - gauss(sigma_x = 2., sigma_y = 2.))) names.append('gauss(sigma_x = 4., sigma_y = 4.) - gauss(sigma_x = 2., sigma_y = 2.)') fi.append(convolve(data,gauss(sigma_x = 6., sigma_y = 6.) - gauss(sigma_x = 2., sigma_y = 2.))) names.append('gauss(sigma_x = 6., sigma_y = 6.) - gauss(sigma_x = 2., sigma_y = 2.)') fi.append(convolve(data,gauss(sigma_x = 8., sigma_y = 8.) - gauss(sigma_x = 2., sigma_y = 2.))) names.append('gauss(sigma_x = 8., sigma_y = 8.) - gauss(sigma_x = 2., sigma_y = 2.)') fi.append(convolve(data,gauss(sigma_x = 10., sigma_y = 10.) - gauss(sigma_x = 2., sigma_y = 2.))) names.append('gauss(sigma_x = 10., sigma_y = 10.) - gauss(sigma_x = 2., sigma_y = 2.)') fi.append(convolve(data,gauss(sigma_x = 6., sigma_y = 6.) - gauss(sigma_x = 4., sigma_y = 4.))) names.append('gauss(sigma_x = 6., sigma_y = 6.) - gauss(sigma_x = 4., sigma_y = 4.)') fi.append(convolve(data,gauss(sigma_x = 8., sigma_y = 8.) - gauss(sigma_x = 4., sigma_y = 4.))) names.append('gauss(sigma_x = 8., sigma_y = 8.) - gauss(sigma_x = 4., sigma_y = 4.)') fi.append(convolve(data,gauss(sigma_x = 10., sigma_y = 10.) - gauss(sigma_x = 4., sigma_y = 4.))) names.append('gauss(sigma_x = 10., sigma_y = 10.) - gauss(sigma_x = 4., sigma_y = 4.)') fi.append(convolve(data,gauss(sigma_x = 12., sigma_y = 12.) - gauss(sigma_x = 4., sigma_y = 4.))) names.append('gauss(sigma_x = 12., sigma_y = 12.) - gauss(sigma_x = 4., sigma_y = 4.)') fi.append(convolve(data,gauss(sigma_x = 14., sigma_y = 14.) - gauss(sigma_x = 4., sigma_y = 4.))) names.append('gauss(sigma_x = 14., sigma_y = 14.) - gauss(sigma_x = 4., sigma_y = 4.)') fi.append(convolve(data,gauss(sigma_x = 10., sigma_y = 10.) - gauss(sigma_x = 6., sigma_y = 6.))) names.append('gauss(sigma_x = 10., sigma_y = 10.) - gauss(sigma_x = 6., sigma_y = 6.)') fi.append(convolve(data,gauss(sigma_x = 12., sigma_y = 12.) - gauss(sigma_x = 6., sigma_y = 6.))) names.append('gauss(sigma_x = 12., sigma_y = 12.) - gauss(sigma_x = 6., sigma_y = 6.)') fi.append(convolve(data,gauss(sigma_x = 14., sigma_y = 14.) - gauss(sigma_x = 6., sigma_y = 6.))) names.append('gauss(sigma_x = 14., sigma_y = 14.) - gauss(sigma_x = 6., sigma_y = 6.)') # mean 84 - 89 fi.append(convolve(data,Mean(Nx=2, Ny=2))) names.append('Mean(Nx=2, Ny=2)') fi.append(convolve(data,Mean(Nx=3, Ny=3))) names.append('Mean(Nx=3, Ny=3)') fi.append(convolve(data,Mean(Nx=4, Ny=4))) names.append('Mean(Nx=4, Ny=4)') fi.append(convolve(data,Mean(Nx=5, Ny=5))) names.append('Mean(Nx=5, Ny=5)') fi.append(convolve(data,Mean(Nx=6, Ny=6))) names.append('Mean(Nx=6, Ny=6)') fi.append(convolve(data,Mean(Nx=7, Ny=7))) names.append('Mean(Nx=7, Ny=7)') # laplacian 90 fi.append(convolve(data,laplacian())) names.append('laplacian') fi.append(convolve(g05,laplacian())) names.append('laplacian_g05') fi.append(convolve(g1,laplacian())) names.append('laplacian_g1') fi.append(convolve(g2,laplacian())) names.append('laplacian_g2') fi.append(convolve(g3,laplacian())) names.append('laplacian_g3') fi.append(convolve(g4,laplacian())) names.append('laplacian_g4') fi.append(convolve(g5,laplacian())) names.append('laplacian_g5') fi.append(convolve(g6,laplacian())) names.append('laplacian_g6') fi.append(convolve(g7,laplacian())) names.append('laplacian_g7') fi.append(convolve(g10,laplacian())) names.append('laplacian_g10') fi.append(convolve(g12,laplacian())) names.append('laplacian_g12') fi.append(convolve(g14,laplacian())) names.append('laplacian_g14') fi.append(convolve(g16,laplacian())) names.append('laplacian_g16') # gabor 91 - 198 t = np.linspace(0,2.0*np.pi,18) KX =np. cos(t) KY = np.sin(t) for i in range(KX.size): fi.append(convolve(data,gabor(kx=KX[i], ky=KY[i], sigma_x=2., sigma_y=2.))) names.append('gabor(kx=KX'+str(i)+'], ky=KY['+str(i)+'], sigma_x=2., sigma_y=2.)') for i in range(KX.size): fi.append(convolve(data,gabor(kx=KX[i], ky=KY[i], sigma_x=2., sigma_y=2., P=0.1))) names.append('gabor(kx=KX'+str(i)+'], ky=KY['+str(i)+'], sigma_x=2., sigma_y=2., P=0.1)') for i in range(KX.size): fi.append(convolve(data,gabor(kx=KX[i], ky=KY[i], sigma_x=2., sigma_y=2., P=0.5))) names.append('gabor(kx=KX'+str(i)+'], ky=KY['+str(i)+'], sigma_x=2., sigma_y=2., P=0.5)') # sobel 199 fi.append(np.sqrt((convolve(data,sobel()[0]))**2 + (convolve(data,sobel()[1]))**2)) names.append('sobel') fi.append(np.sqrt((convolve(g05,sobel()[0]))**2 + (convolve(g05,sobel()[1]))**2)) names.append('sobel_g05') fi.append(np.sqrt((convolve(g1,sobel()[0]))**2 + (convolve(g1,sobel()[1]))**2)) names.append('sobel_g1') fi.append(np.sqrt((convolve(g2,sobel()[0]))**2 + (convolve(g2,sobel()[1]))**2)) names.append('sobel_g2') fi.append(np.sqrt((convolve(g3,sobel()[0]))**2 + (convolve(g3,sobel()[1]))**2)) names.append('sobel_g3') fi.append(np.sqrt((convolve(g4,sobel()[0]))**2 + (convolve(g4,sobel()[1]))**2)) names.append('sobel_g4') fi.append(np.sqrt((convolve(g5,sobel()[0]))**2 + (convolve(g5,sobel()[1]))**2)) names.append('sobel_g5') fi.append(np.sqrt((convolve(g6,sobel()[0]))**2 + (convolve(g6,sobel()[1]))**2)) names.append('sobel_g6') fi.append(np.sqrt((convolve(g7,sobel()[0]))**2 + (convolve(g7,sobel()[1]))**2)) names.append('sobel_g7') fi.append(np.sqrt((convolve(g10,sobel()[0]))**2 + (convolve(g10,sobel()[1]))**2)) names.append('sobel_g10') fi.append(np.sqrt((convolve(g12,sobel()[0]))**2 + (convolve(g12,sobel()[1]))**2)) names.append('sobel_g12') fi.append(np.sqrt((convolve(g14,sobel()[0]))**2 + (convolve(g14,sobel()[1]))**2)) names.append('sobel_g14') fi.append(np.sqrt((convolve(g16,sobel()[0]))**2 + (convolve(g16,sobel()[1]))**2)) names.append('sobel_g16') # --- Membrane Projections --- # 200 - 205 mp = np.zeros((data.shape[0],data.shape[1],30)) for i in range(30): mp[:,:,i] = convolve(data, proj(angle=i*6.0)) fi.append(np.sum(mp, axis=2)) names.append('mj_sum') fi.append(np.mean(mp, axis=2)) names.append('mj_mean') fi.append(np.std(mp, axis=2)) names.append('mj_std') fi.append(np.median(mp, axis=2)) names.append('mj_median') fi.append(np.max(mp, axis=2)) names.append('mj_max') fi.append(np.min(mp, axis=2)) names.append('mj_min') # convert list fi to array fi fi = np.asarray(fi) return fi, names
def tilbert(x, h, period=None, _cache=_cache): """ Return h-Tilbert transform of a periodic sequence x. If x_j and y_j are Fourier coefficients of periodic functions x and y, respectively, then:: y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j y_0 = 0 Parameters ---------- x : array_like The input array to transform. h : float Defines the parameter of the Tilbert transform. period : float, optional The assumed period of the sequence. Default period is ``2*pi``. Returns ------- tilbert : ndarray The result of the transform. Notes ----- If ``sum(x, axis=0) == 0`` and ``n = len(x)`` is odd then ``tilbert(itilbert(x)) == x``. If ``2 * pi * h / period`` is approximately 10 or larger, then numerically ``tilbert == hilbert`` (theoretically oo-Tilbert == Hilbert). For even ``len(x)``, the Nyquist mode of ``x`` is taken zero. """ tmp = asarray(x) if iscomplexobj(tmp): return tilbert(tmp.real, h, period) + \ 1j * tilbert(tmp.imag, h, period) if period is not None: h = h * 2 * pi / period n = len(x) omega = _cache.get((n, h)) if omega is None: if len(_cache) > 20: while _cache: _cache.popitem() def kernel(k, h=h): if k: return 1.0/tanh(h*k) return 0 omega = convolve.init_convolution_kernel(n, kernel, d=1) _cache[(n,h)] = omega overwrite_x = _datacopied(tmp, x) return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
# Create mass (or lensing) objects lensMass = MassModels.PowerLaw('lens',{'x':para[i,14],'y':para[i,15],'b':para[i,16],'q':para[i,17],'pa':para[i,18],'eta':para[i,19]}) shear = MassModels.ExtShear('shear',{'x':para[i,20],'y':para[i,21],'b':para[i,22],'pa':para[i,23]}) lenses = [lensMass,shear] #create the coordinate grids that the image will be evaluated on y,x = iT.coords((360,360)) #with higher resolution 60*6 # create a PSF import pyfits psf = pyfits.open('sub600.fits')[0].data.copy() psf /= psf.sum() # Form the image and convolve (convolution returns the image and FFT'd PSF) img,psf = convolve.convolve(lensGal.pixeval(x,y),psf) xl,yl = pylens.getDeflections(lenses,[x,y]) img +=convolve.convolve(src.pixeval(xl,yl),psf,False)[0] #pylab.imshow(img,origin='lower',interpolation='nearest') #pylab.colorbar() #pylab.show() pyfits.PrimaryHDU(img).writeto('../fits/HE_arc-{0}.fits'.format(i+1),clobber=True)