def curvelet_transform(x, num_bands, num_angles = 8, all_curvelets = True, as_complex = False): ndims = len(x.shape) # This file requires Curvelab and the PyCurveLab packages be installed on your system. try: import pyct except ImportError: raise NotImplementedError("Use of curvelets requires installation of CurveLab and the PyCurveLab package.\nSee: http://curvelet.org/ and https://www.slim.eos.ubc.ca/SoftwareLicensed/") if ndims == 2: ct = pyct.fdct2( n = x.shape, nbs = num_bands, # Number of bands nba = num_angles, # Number of discrete angles ac = all_curvelets,# Return curvelets at the finest detail level vec = False, # Return results as nested python vectors cpx = as_complex) # Disable complex-valued curvelets elif ndims == 3: ct = pyct.fdct3( n = x.shape, nbs = num_bands, # Number of bands nba = num_angles, # Number of discrete angles ac = all_curvelets,# Return curvelets at the finest detail level vec = False, # Return results as nested python vectors cpx = as_complex) # Disable complex-valued curvelets else: raise NotImplementedError("%dD Curvelets are not supported." % (ndims)) result = ct.fwd(x) del ct return result
def test_FDCT3D_3dsignal(par): """ Tests for FDCT3D operator for 3d signal. """ x = np.random.normal(0., 1., (par['nx'], par['ny'], par['nz'])) + \ np.random.normal(0., 1., (par['nx'], par['ny'], par['nz'])) * \ par['imag'] FDCTop = FDCT3D(dims=(par['nx'], par['ny'], par['nz']), dtype=par['dtype']) assert dottest(FDCTop, *FDCTop.shape, tol=1e-12, complexflag=0 if par['imag'] == 0 else 3) y = FDCTop * x.ravel() xinv = FDCTop.H * y np.testing.assert_array_almost_equal(xinv.reshape(*x.shape), x, decimal=14) if PYCT: FDCTct = ct.fdct3(x.shape, FDCTop.nbscales, FDCTop.nbangles_coarse, FDCTop.allcurvelets, cpx=False if par['imag'] == 0 else True) y_ct = np.array(FDCTct.fwd(x)).ravel() np.testing.assert_array_almost_equal(y, y_ct, decimal=64) assert y.dtype == y_ct.dtype
def normtest(dim=2, clen=10): for i in range(clen): print("-----------------------------------") if dim == 2: sz = np.arange(256, 513) else: sz = np.arange(64, 128) np.random.shuffle(sz) print(sz[:dim]) iscplx = [True, False] np.random.shuffle(iscplx) isac = [True, False] np.random.shuffle(isac) if isac[0]: print("All curvelets") else: print("Wavelets at finest scale") if dim == 2: A = ct.fdct2(sz[:2], 6, 32, isac[0], norm=True, cpx=iscplx[0]) pos = np.arange(A.range()) np.random.shuffle(pos) if iscplx[0]: print("Complex input") x = np.zeros(A.range(), dtype='complex') v = np.random.rand() x[pos[0]] = v + np.sqrt(1 - v**2) * 1j else: print("Real input") x = np.zeros(A.range()) x[pos[0]] = 1. elif dim == 3: A = ct.fdct3(sz[:3], 4, 8, isac[0], norm=True, cpx=iscplx[0]) pos = np.arange(A.range()) np.random.shuffle(pos) if iscplx[0]: print("Complex input") x = np.zeros(A.range(), dtype='complex') v = np.random.rand() x[pos[0]] = v + np.sqrt(1 - v**2) * 1j else: print("Real input") x = np.zeros(A.range()) x[pos[0]] = 1. f = A.inv(x) if np.allclose(norm(f.flatten(), ord=2), norm(x, ord=2)): print('Norm check ok!') else: print('Problem w norm test') print("||f|| = ", norm(f.flatten(), ord=2), f.dtype) print("||x|| = ", norm(x, ord=2), x.dtype)
def test(dim=2,clen=10): for i in xrange(clen): print "-----------------------------------" if dim==2: sz = np.arange(256,513) elif dim==3: sz = np.arange(64,128) np.random.shuffle(sz) iscplx = [True,False] np.random.shuffle(iscplx) if iscplx[0]: print "Complex input" f = np.array(sc.randn(*sz[:dim])+sc.randn(*sz[:dim])*1j) else: print "Real input" f = np.array(sc.randn(*sz[:dim])) isac = [True,False] np.random.shuffle(isac) if isac[0]: print "All curvelets" else: print "Wavelets at finest scale" print f.shape if dim==2: A = ct.fdct2(f.shape,6,32,isac[0],cpx=iscplx[0]) elif dim==3: A = ct.fdct3(f.shape,4,8,isac[0],cpx=iscplx[0]) x = A.fwd(f) if np.allclose(norm(f.flatten(),ord=2),norm(x,ord=2) ): print 'Energy check ok!' else: print 'Problem w energy test' fr = A.inv(x) if np.allclose(f.flatten(),fr.flatten() ): print 'Inverse check ok!' else: print 'Problem w inverse test' print "||f|| = ",norm(f.flatten(),ord=2),f.dtype print "||x|| = ",norm(x,ord=2),x.dtype print "||fr|| = ",norm(fr.flatten(),ord=2),fr.dtype
def curvelet_decomposition(vol, n_scales, n_angles): # Create a Curvelet object A = ct.fdct3(vol.shape, nbs=n_scales, nba=n_angles, ac=True, norm=False, vec=True, cpx=False) # Apply curvelet to volume f = A.fwd(vol) # Assembly feature vector feats = pd.Series() for scale in range(n_scales): for angle in range(n_angles): # Get values in respective scale/angle try: ix = A.index(scale, angle) data = f[ix[0]:ix[1]] # Extract magnitude # Extract several statistics n, (mi, ma), mea, var, skew, kurt = stats.describe(data) # Assign to series feats['%d_%d_n' % (scale, angle)] = n feats['%d_%d_min' % (scale, angle)] = mi feats['%d_%d_max' % (scale, angle)] = ma feats['%d_%d_mean' % (scale, angle)] = mea feats['%d_%d_var' % (scale, angle)] = var feats['%d_%d_skew' % (scale, angle)] = skew feats['%d_%d_kurtosis' % (scale, angle)] = kurt except IndexError: pass return feats
def inverse_curvelet_transform(coefs, x_shape, num_bands, num_angles, all_curvelets, as_complex): # This file requires Curvelab and the PyCurveLab packages be installed on your system. try: import pyct except ImportError: raise NotImplementedError("Use of curvelets requires installation of CurveLab and the PyCurveLab package.\nSee: http://curvelet.org/ and https://www.slim.eos.ubc.ca/SoftwareLicensed/") if len(x_shape) == 2: ct = pyct.fdct2( n = x_shape, nbs = num_bands, # Number of bands nba = num_angles, ac = all_curvelets, vec = False, cpx = as_complex) else: ct = pyct.fdct3( n = x_shape, nbs = num_bands, # Number of bands nba = num_angles, ac = all_curvelets, vec = False, cpx = as_complex) result = ct.inv(coefs) del ct return result
filename = os.path.join(root, 'param', 'fsaverage', 'brainmask.mgz') img = nb.load(filename).get_data().astype(np.float) img = img[50:150, 50:150, 50:150] print('Image shape: ', img.shape) print('Number of elements: ', np.prod(img.shape)) # Parameters number_of_scales = 7 number_of_angles = 4 # Setup curvelet params print('Applying Curvelet Transform...') A = ct.fdct3(img.shape, nbs=number_of_scales, nba=number_of_angles, ac=True, norm=False, vec=True, cpx=False) # Apply curvelet to the image f = A.fwd(img) print('Coefficients array:\n\t', f) print('Shape: ', f.shape) # Print Information for scale in range(0, number_of_scales): if scale == 0: angles = [0] elif scale == 1: