def _level1_xfm_no_highpass(X, h0o, h1o, ext_mode): """Perform level 1 of the 3d transform discarding highpass subbands. """ # Check shape of input according to ext_mode. Note that shape of X is # double original input in each direction. if ext_mode == 4 and np.any(np.fmod(X.shape, 2) != 0): raise ValueError( 'Input shape should be a multiple of 2 in each direction when self.ext_mode == 4' ) elif ext_mode == 8 and np.any(np.fmod(X.shape, 4) != 0): raise ValueError( 'Input shape should be a multiple of 4 in each direction when self.ext_mode == 8' ) out = np.zeros_like(X) # Loop over 2nd dimension extracting 2D slice from first and 3rd dimensions for f in xrange(X.shape[1]): # extract slice y = X[:, f, :].T out[:, f, :] = colfilter(y, h0o).T # Loop over 3rd dimension extracting 2D slice from first and 2nd dimensions for f in xrange(X.shape[2]): y = colfilter(out[:, :, f].T, h0o).T out[:, :, f] = colfilter(y, h0o) return out
def _level1_xfm_no_highpass(X, h0o, h1o, ext_mode): """Perform level 1 of the 3d transform discarding highpass subbands. """ # Check shape of input according to ext_mode. Note that shape of X is # double original input in each direction. if ext_mode == 4 and np.any(np.fmod(X.shape, 2) != 0): raise ValueError('Input shape should be a multiple of 2 in each direction when self.ext_mode == 4') elif ext_mode == 8 and np.any(np.fmod(X.shape, 4) != 0): raise ValueError('Input shape should be a multiple of 4 in each direction when self.ext_mode == 8') out = np.zeros_like(X) # Loop over 2nd dimension extracting 2D slice from first and 3rd dimensions for f in xrange(X.shape[1]): # extract slice y = X[:, f, :].T out[:, f, :] = colfilter(y, h0o).T # Loop over 3rd dimension extracting 2D slice from first and 2nd dimensions for f in xrange(X.shape[2]): y = colfilter(out[:, :, f].T, h0o).T out[:, :, f] = colfilter(y, h0o) return out
def test_even_size(): y = colfilter(np.zeros_like(mandrill), (-1,1)) assert y.shape == (mandrill.shape[0]+1, mandrill.shape[1]) assert not np.any(y[:] != 0.0) z = colfilter_gold(np.zeros_like(mandrill), (-1,1)) assert_almost_equal(y, z)
def test_even_size(): y = colfilter(np.zeros_like(mandrill), (-1, 1)) assert y.shape == (mandrill.shape[0] + 1, mandrill.shape[1]) assert not np.any(y[:] != 0.0) z = colfilter_gold(np.zeros_like(mandrill), (-1, 1)) assert_almost_equal(y, z)
def _level1_ifm_no_highpass(Yl, g0o, g1o): """Perform level 1 of the inverse 3d transform assuming highpass coefficients are zero. """ # Create work area output = np.zeros_like(Yl) for f in xrange(Yl.shape[2]): y = colfilter(Yl[:, :, f].T, g0o) output[:, :, f] = colfilter(y.T, g0o) for f in xrange(Yl.shape[1]): y = output[:, f, :].T.copy() output[:, f, :] = colfilter(y, g0o) return output
def _level1_ifm(Yl, Yh, g0o, g1o): """Perform level 1 of the inverse 3d transform. """ # Create work area work = np.zeros(np.asanyarray(Yl.shape) * 2, dtype=Yl.dtype) # Work out shape of output Xshape = np.asanyarray(work.shape) >> 1 if g0o.shape[0] % 2 == 0: # if we have an even length filter, we need to shrink the output by 1 # to compensate for the addition of an extra row/column/slice in # the forward transform Xshape -= 1 # Form some useful slices s0a = slice(None, work.shape[0] >> 1) s1a = slice(None, work.shape[1] >> 1) s2a = slice(None, work.shape[2] >> 1) s0b = slice(work.shape[0] >> 1, None) s1b = slice(work.shape[1] >> 1, None) s2b = slice(work.shape[2] >> 1, None) x0a = slice(None, Xshape[0]) x1a = slice(None, Xshape[1]) x2a = slice(None, Xshape[2]) x0b = slice(work.shape[0] >> 1, (work.shape[0] >> 1) + Xshape[0]) x1b = slice(work.shape[1] >> 1, (work.shape[1] >> 1) + Xshape[1]) x2b = slice(work.shape[2] >> 1, (work.shape[2] >> 1) + Xshape[2]) # Assign regions of work area work[s0a, s1a, s2a] = Yl work[x0a, x1b, x2a] = c2cube(Yh[:, :, :, 0:4]) work[x0b, x1a, x2a] = c2cube(Yh[:, :, :, 4:8]) work[x0b, x1b, x2a] = c2cube(Yh[:, :, :, 8:12]) work[x0a, x1a, x2b] = c2cube(Yh[:, :, :, 12:16]) work[x0a, x1b, x2b] = c2cube(Yh[:, :, :, 16:20]) work[x0b, x1a, x2b] = c2cube(Yh[:, :, :, 20:24]) work[x0b, x1b, x2b] = c2cube(Yh[:, :, :, 24:28]) for f in xrange(work.shape[2]): # Do odd top-level filters on rows. y = colfilter(work[:, x1a, f].T, g0o) + colfilter( work[:, x1b, f].T, g1o) # Do odd top-level filters on columns. work[s0a, s1a, f] = colfilter(y[:, x0a].T, g0o) + colfilter(y[:, x0b].T, g1o) for f in xrange(work.shape[1] >> 1): # Do odd top-level filters on 3rd dim. y = work[s0a, f, :].T work[s0a, f, s2a] = (colfilter(y[x2a, :], g0o) + colfilter(y[x2b, :], g1o)).T if g0o.shape[0] % 2 == 0: return work[1:(work.shape[0] >> 1), 1:(work.shape[1] >> 1), 1:(work.shape[2] >> 1)] else: return work[s0a, s1a, s2a]
def _level1_ifm(Yl, Yh, g0o, g1o): """Perform level 1 of the inverse 3d transform. """ # Create work area work = np.zeros(np.asanyarray(Yl.shape) * 2, dtype=Yl.dtype) # Work out shape of output Xshape = np.asanyarray(work.shape) >> 1 if g0o.shape[0] % 2 == 0: # if we have an even length filter, we need to shrink the output by 1 # to compensate for the addition of an extra row/column/slice in # the forward transform Xshape -= 1 # Form some useful slices s0a = slice(None, work.shape[0] >> 1) s1a = slice(None, work.shape[1] >> 1) s2a = slice(None, work.shape[2] >> 1) s0b = slice(work.shape[0] >> 1, None) s1b = slice(work.shape[1] >> 1, None) s2b = slice(work.shape[2] >> 1, None) x0a = slice(None, Xshape[0]) x1a = slice(None, Xshape[1]) x2a = slice(None, Xshape[2]) x0b = slice(work.shape[0] >> 1, (work.shape[0] >> 1) + Xshape[0]) x1b = slice(work.shape[1] >> 1, (work.shape[1] >> 1) + Xshape[1]) x2b = slice(work.shape[2] >> 1, (work.shape[2] >> 1) + Xshape[2]) # Assign regions of work area work[s0a, s1a, s2a] = Yl work[x0a, x1b, x2a] = c2cube(Yh[:,:,:, 0:4 ]) work[x0b, x1a, x2a] = c2cube(Yh[:,:,:, 4:8 ]) work[x0b, x1b, x2a] = c2cube(Yh[:,:,:, 8:12]) work[x0a, x1a, x2b] = c2cube(Yh[:,:,:,12:16]) work[x0a, x1b, x2b] = c2cube(Yh[:,:,:,16:20]) work[x0b, x1a, x2b] = c2cube(Yh[:,:,:,20:24]) work[x0b, x1b, x2b] = c2cube(Yh[:,:,:,24:28]) for f in xrange(work.shape[2]): # Do odd top-level filters on rows. y = colfilter(work[:, x1a, f].T, g0o) + colfilter(work[:, x1b, f].T, g1o) # Do odd top-level filters on columns. work[s0a, s1a, f] = colfilter(y[:, x0a].T, g0o) + colfilter(y[:, x0b].T, g1o) for f in xrange(work.shape[1]>>1): # Do odd top-level filters on 3rd dim. y = work[s0a, f, :].T work[s0a, f, s2a] = (colfilter(y[x2a, :], g0o) + colfilter(y[x2b, :], g1o)).T if g0o.shape[0] % 2 == 0: return work[1:(work.shape[0]>>1), 1:(work.shape[1]>>1), 1:(work.shape[2]>>1)] else: return work[s0a, s1a, s2a]
def test_qshift(): y = colfilter(mandrill, qshift('qshift_a')[0]) assert y.shape == (mandrill.shape[0] + 1, mandrill.shape[1]) z = colfilter_gold(mandrill, qshift('qshift_a')[0]) assert_almost_equal(y, z)
def _level1_xfm(X, h0o, h1o, ext_mode): """Perform level 1 of the 3d transform. """ # Check shape of input according to ext_mode. Note that shape of X is # double original input in each direction. if ext_mode == 4 and np.any(np.fmod(X.shape, 2) != 0): raise ValueError( 'Input shape should be a multiple of 2 in each direction when self.ext_mode == 4' ) elif ext_mode == 8 and np.any(np.fmod(X.shape, 4) != 0): raise ValueError( 'Input shape should be a multiple of 4 in each direction when self.ext_mode == 8' ) # Create work area work_shape = np.asanyarray(X.shape) * 2 # We need one extra row per octant if filter length is even if h0o.shape[0] % 2 == 0: work_shape += 2 work = np.zeros(work_shape, dtype=X.dtype) # Form some useful slices s0a = slice(None, work.shape[0] >> 1) s1a = slice(None, work.shape[1] >> 1) s2a = slice(None, work.shape[2] >> 1) s0b = slice(work.shape[0] >> 1, None) s1b = slice(work.shape[1] >> 1, None) s2b = slice(work.shape[2] >> 1, None) x0a = slice(None, X.shape[0]) x1a = slice(None, X.shape[1]) x2a = slice(None, X.shape[2]) x0b = slice(work.shape[0] >> 1, (work.shape[0] >> 1) + X.shape[0]) x1b = slice(work.shape[1] >> 1, (work.shape[1] >> 1) + X.shape[1]) x2b = slice(work.shape[2] >> 1, (work.shape[2] >> 1) + X.shape[2]) # Assign input if h0o.shape[0] % 2 == 0: work[:X.shape[0], :X.shape[1], :X.shape[2]] = X # Copy last rows/cols/slices work[X.shape[0], :X.shape[1], :X.shape[2]] = X[-1, :, :] work[:X.shape[0], X.shape[1], :X.shape[2]] = X[:, -1, :] work[:X.shape[0], :X.shape[1], X.shape[2]] = X[:, :, -1] work[X.shape[0], X.shape[1], X.shape[2]] = X[-1, -1, -1] else: work[s0a, s1a, s2a] = X # Loop over 2nd dimension extracting 2D slice from first and 3rd dimensions for f in xrange(work.shape[1] >> 1): # extract slice y = work[s0a, f, x2a].T # Do odd top-level filters on 3rd dim. The order here is important # since the second filtering will modify the elements of y as well # since y is merely a view onto work. work[s0a, f, s2b] = colfilter(y, h1o).T work[s0a, f, s2a] = colfilter(y, h0o).T # Loop over 3rd dimension extracting 2D slice from first and 2nd dimensions for f in xrange(work.shape[2]): # Do odd top-level filters on rows. y1 = work[x0a, x1a, f].T y2 = np.vstack((colfilter(y1, h0o), colfilter(y1, h1o))).T # Do odd top-level filters on columns. work[s0a, :, f] = colfilter(y2, h0o) work[s0b, :, f] = colfilter(y2, h1o) # Return appropriate slices of output return ( work[s0a, s1a, s2a], # LLL np.concatenate( ( cube2c(work[x0a, x1b, x2a]), # HLL cube2c(work[x0b, x1a, x2a]), # LHL cube2c(work[x0b, x1b, x2a]), # HHL cube2c(work[x0a, x1a, x2b]), # LLH cube2c(work[x0a, x1b, x2b]), # HLH cube2c(work[x0b, x1a, x2b]), # LHH cube2c(work[x0b, x1b, x2b]), # HLH ), axis=3))
def test_even_size_non_array(): y = colfilter(mandrill.tolist(), (-1,1)) assert y.shape == (mandrill.shape[0]+1, mandrill.shape[1]) z = colfilter_gold(mandrill.tolist(), (-1,1)) assert_almost_equal(y, z)
def test_odd_size_non_array(): y = colfilter(mandrill.tolist(), (-1,2,-1)) assert y.shape == mandrill.shape z = colfilter_gold(mandrill.tolist(), (-1,2,-1)) assert_almost_equal(y, z)
def test_biort(): y = colfilter(mandrill, biort('antonini')[0]) assert y.shape == mandrill.shape z = colfilter_gold(mandrill, biort('antonini')[0]) assert_almost_equal(y, z)
def test_even_size_non_array(): y = colfilter(mandrill.tolist(), (-1, 1)) assert y.shape == (mandrill.shape[0] + 1, mandrill.shape[1]) z = colfilter_gold(mandrill.tolist(), (-1, 1)) assert_almost_equal(y, z)
def _level1_xfm(X, h0o, h1o, ext_mode): """Perform level 1 of the 3d transform. """ # Check shape of input according to ext_mode. Note that shape of X is # double original input in each direction. if ext_mode == 4 and np.any(np.fmod(X.shape, 2) != 0): raise ValueError('Input shape should be a multiple of 2 in each direction when self.ext_mode == 4') elif ext_mode == 8 and np.any(np.fmod(X.shape, 4) != 0): raise ValueError('Input shape should be a multiple of 4 in each direction when self.ext_mode == 8') # Create work area work_shape = np.asanyarray(X.shape) * 2 # We need one extra row per octant if filter length is even if h0o.shape[0] % 2 == 0: work_shape += 2 work = np.zeros(work_shape, dtype=X.dtype) # Form some useful slices s0a = slice(None, work.shape[0] >> 1) s1a = slice(None, work.shape[1] >> 1) s2a = slice(None, work.shape[2] >> 1) s0b = slice(work.shape[0] >> 1, None) s1b = slice(work.shape[1] >> 1, None) s2b = slice(work.shape[2] >> 1, None) x0a = slice(None, X.shape[0]) x1a = slice(None, X.shape[1]) x2a = slice(None, X.shape[2]) x0b = slice(work.shape[0] >> 1, (work.shape[0] >> 1) + X.shape[0]) x1b = slice(work.shape[1] >> 1, (work.shape[1] >> 1) + X.shape[1]) x2b = slice(work.shape[2] >> 1, (work.shape[2] >> 1) + X.shape[2]) # Assign input if h0o.shape[0] % 2 == 0: work[:X.shape[0], :X.shape[1], :X.shape[2]] = X # Copy last rows/cols/slices work[ X.shape[0], :X.shape[1], :X.shape[2]] = X[-1, :, :] work[:X.shape[0], X.shape[1], :X.shape[2]] = X[:, -1, :] work[:X.shape[0], :X.shape[1], X.shape[2]] = X[:, :, -1] work[X.shape[0], X.shape[1], X.shape[2]] = X[-1,-1,-1] else: work[s0a, s1a, s2a] = X # Loop over 2nd dimension extracting 2D slice from first and 3rd dimensions for f in xrange(work.shape[1] >> 1): # extract slice y = work[s0a, f, x2a].T # Do odd top-level filters on 3rd dim. The order here is important # since the second filtering will modify the elements of y as well # since y is merely a view onto work. work[s0a, f, s2b] = colfilter(y, h1o).T work[s0a, f, s2a] = colfilter(y, h0o).T # Loop over 3rd dimension extracting 2D slice from first and 2nd dimensions for f in xrange(work.shape[2]): # Do odd top-level filters on rows. y1 = work[x0a, x1a, f].T y2 = np.vstack((colfilter(y1, h0o), colfilter(y1, h1o))).T # Do odd top-level filters on columns. work[s0a, :, f] = colfilter(y2, h0o) work[s0b, :, f] = colfilter(y2, h1o) # Return appropriate slices of output return ( work[s0a, s1a, s2a], # LLL np.concatenate(( cube2c(work[x0a, x1b, x2a]), # HLL cube2c(work[x0b, x1a, x2a]), # LHL cube2c(work[x0b, x1b, x2a]), # HHL cube2c(work[x0a, x1a, x2b]), # LLH cube2c(work[x0a, x1b, x2b]), # HLH cube2c(work[x0b, x1a, x2b]), # LHH cube2c(work[x0b, x1b, x2b]), # HLH ), axis=3) )
def test_even_size(): y = colfilter(mandrill, (-1,1)) assert y.shape == (mandrill.shape[0]+1, mandrill.shape[1]) z = colfilter_gold(mandrill, (-1,1)) assert_almost_equal(y, z)
def test_biort(): y = colfilter(lena, biort('antonini')[0]) assert y.shape == lena.shape z = colfilter_gold(lena, biort('antonini')[0]) assert_almost_equal(y, z)
def test_qshift(): y = colfilter(lena, qshift('qshift_a')[0]) assert y.shape == (lena.shape[0]+1, lena.shape[1]) z = colfilter_gold(lena, qshift('qshift_a')[0]) assert_almost_equal(y, z)
def test_odd_size(): y = colfilter(lena, (-1,2,-1)) assert y.shape == lena.shape z = colfilter_gold(lena, (-1,2,-1)) assert_almost_equal(y, z)
def test_even_size(): y = colfilter(lena, (-1,1)) assert y.shape == (lena.shape[0]+1, lena.shape[1]) z = colfilter_gold(lena, (-1,1)) assert_almost_equal(y, z)
def test_odd_size(): y = colfilter(mandrill, (-1, 2, -1)) assert y.shape == mandrill.shape z = colfilter_gold(mandrill, (-1, 2, -1)) assert_almost_equal(y, z)
def test_odd_size(): y = colfilter(mandrill, (-1,2,-1)) assert y.shape == mandrill.shape z = colfilter_gold(mandrill, (-1,2,-1)) assert_almost_equal(y, z)
def test_odd_size_non_array(): y = colfilter(mandrill.tolist(), (-1, 2, -1)) assert y.shape == mandrill.shape z = colfilter_gold(mandrill.tolist(), (-1, 2, -1)) assert_almost_equal(y, z)
def test_qshift(): y = colfilter(mandrill, qshift('qshift_a')[0]) assert y.shape == (mandrill.shape[0]+1, mandrill.shape[1]) z = colfilter_gold(mandrill, qshift('qshift_a')[0]) assert_almost_equal(y, z)
def test_even_size(): y = colfilter(mandrill, (-1, 1)) assert y.shape == (mandrill.shape[0] + 1, mandrill.shape[1]) z = colfilter_gold(mandrill, (-1, 1)) assert_almost_equal(y, z)