From 6ba69a3167c67d93c7a32968858e3f06654b2eb3 Mon Sep 17 00:00:00 2001 From: Gregor Thalhammer Date: Fri, 18 May 2012 22:44:55 +0200 Subject: [PATCH] unwrap: added numpy test cases --- unwrap2D/test_unwrap.py | 189 ++++++++++++++++++++++------------------ 1 file changed, 102 insertions(+), 87 deletions(-) diff --git a/unwrap2D/test_unwrap.py b/unwrap2D/test_unwrap.py index eb2c3ee1..f371494f 100644 --- a/unwrap2D/test_unwrap.py +++ b/unwrap2D/test_unwrap.py @@ -1,107 +1,122 @@ -from numpy.testing import * +from numpy.testing import run_module_suite, TestCase, assert_array_almost_equal from unwrap import unwrap - import numpy as np -def test_unwrap2D(): - nx, ny = 16, 32 - x = np.arange(nx) - y = np.arange(ny) - x.shape = (-1,1) - y.shape = (1,-1) +class test_unwrap(TestCase): - z = np.exp(1j*x*0.2*np.pi) * np.exp(1j*y*0.1*np.pi) - phi_w = np.angle(z) - phi = unwrap(phi_w, - wrap_around_axis_0 = False) + def test_unwrap2D(self): + x, y = np.ogrid[:8, :16] + phi = 2*np.pi*(x*0.2 + y*0.1) + phi_wrapped = np.angle(np.exp(1j*phi)) + phi_unwrapped = unwrap(phi_wrapped) - mask = 0*np.ones((nx, ny), dtype = np.uint8) - mask[4:16, 4:16] = 1 - phi_w_ma = np.ma.array(phi_w, dtype = np.float32, mask = mask) - phi_ma = unwrap(phi_w_ma) + s = np.round(phi_unwrapped[0,0]/(2*np.pi)) + assert_array_almost_equal(phi, phi_unwrapped - s*2*np.pi) - return (phi/(np.pi*2), - phi_w/(np.pi*2), phi/(np.pi*2), - phi_w_ma/(np.pi*2), phi_ma/(np.pi*2),) + def test_unwrap2D_masked(self): + x, y = np.ogrid[:8, :16] + phi = 2*np.pi*(x*0.2 + y*0.1) + + mask = np.zeros_like(phi, dtype = np.uint8) + mask[4:6, 4:8] = 1 + + phi_wrapped = np.angle(np.exp(1j*phi)) + phi_wrapped_masked = np.ma.array(phi_wrapped, dtype = np.float32, mask = mask) + phi_unwrapped_masked = unwrap(phi_wrapped_masked) + + s = np.round(phi_unwrapped_masked[0,0]/(2*np.pi)) + assert_array_almost_equal(phi + 2*np.pi*s, phi_unwrapped_masked) + + def test_unwrap3D(self): + x, y, z = np.ogrid[:8, :12, :4] + phi = 2*np.pi*(x*0.2 + y*0.1 + z*0.05) + phi_wrapped = np.angle(np.exp(1j*phi)) + phi_unwrapped = unwrap(phi_wrapped) + + s = np.round(phi_unwrapped[0,0]/(2*np.pi)) + assert_array_almost_equal(phi, phi_unwrapped - s*2*np.pi) + + def test_unwrap3D_masked(self): + x, y, z = np.ogrid[:8, :12, :4] + phi = 2*np.pi*(x*0.2 + y*0.1 + z*0.05) + phi_wrapped = np.angle(np.exp(1j*phi)) + mask = np.zeros_like(phi, dtype = np.uint8) + mask[4:6, 4:6, 1:3] = 1 + phi_wrapped_masked = np.ma.array(phi_wrapped, dtype = np.float32, mask = mask) + phi_unwrapped_masked = unwrap(phi_wrapped_masked) + + s = np.round(phi_unwrapped_masked[0,0,0]/(2*np.pi)) + assert_array_almost_equal(phi + 2*np.pi*s, phi_unwrapped_masked) + +def unwrap_plots(): -def test_unwrap3D(): - x, y, z = np.ogrid[:8, :12, :4] + x, y = np.ogrid[:32, :32] + phi = 2*np.pi*(x*0.2 + y*0.1) - phi = 2*np.pi*(x*0.2 + y*0.1 + z*0.05) + #phi = 1*np.arctan2(x-14.3, y-6.3) - 2*np.arctan2(x-18.3, y-22.1) + + phi[8,8] = np.NaN + phi_wrapped = np.angle(np.exp(1j*phi)) - phi_unwrapped = unwrap(phi_wrapped) + phi_unwrapped = unwrap(phi_wrapped, + #wrap_around_axis_0 = True, + #wrap_around_axis_1 = True, + ) mask = np.zeros_like(phi, dtype = np.uint8) - mask[4:6, 4:6, 1:3] = 1 + #mask[10:22, 4:10] = 1 phi_wrapped_masked = np.ma.array(phi_wrapped, dtype = np.float32, mask = mask) phi_unwrapped_masked = unwrap(phi_wrapped_masked) - - s = np.round(phi_unwrapped[0,0,0]/(2*np.pi)) - assert_array_almost_equal(phi/(2*np.pi), phi_unwrapped/(2*np.pi) - s, decimal = 5) - assert_array_almost_equal(phi + 2*np.pi*s, phi_unwrapped_masked, decimal = 5) - - return (phi/(np.pi*2), - phi_wrapped/(np.pi*2), phi_unwrapped/(np.pi*2), - phi_wrapped_masked/(np.pi*2), phi_unwrapped_masked/(np.pi*2),) - - - -# class test_unwrap(TestCase): -# def test_simple2d(self, level=1): -# grid = outer(ones(64), arange(-32,32)) + \ -# 1.j * outer(arange(-32,32), ones(64)) -# pgrid = abs(grid) -# wr_grid = normalize_angle(pgrid) -# uw_grid = unwrap2D(wr_grid) -# uw_grid += (pgrid[32,32] - uw_grid[32,32]) - -# assert_array_almost_equal(pgrid, uw_grid, decimal=5) - -# def test_simple3d(self): -# grid = indices((64,64,64)) -# grid[0] -= 32 -# grid[1] -= 32 -# grid[2] -= 32 -# # get distance of each point in the grid from 0 -# grid = power(power(grid, 2.0).sum(axis=0), 0.5) -# wr_grid = normalize_angle(grid) -# uw_grid = unwrap3D(wr_grid) -# uw_grid += (grid[32,32,32] - uw_grid[32,32,32]) -# assert_array_almost_equal(grid, uw_grid, decimal=5) - -if __name__=="__main__": - #NumpyTest().run() import matplotlib.pyplot as plt - - p0,p1,p2,p3,p4 = test_unwrap2D() plt.figure(1) plt.clf() - plt.subplot(322) - plt.imshow(p0,interpolation = 'nearest') - plt.subplot(323) - plt.imshow(p1,interpolation = 'nearest') - plt.subplot(324) - plt.imshow(p2, interpolation = 'nearest') - plt.subplot(325) - plt.imshow(p3, interpolation = 'nearest') - plt.subplot(326) - plt.imshow(p4, interpolation = 'nearest') - plt.draw() + plt.gray() + plt.subplot(221) + plt.imshow(phi, interpolation = 'nearest') + plt.subplot(222) + plt.imshow(phi_wrapped, interpolation = 'nearest') + plt.subplot(223) + plt.imshow(phi_unwrapped, interpolation = 'nearest') + plt.subplot(224) + plt.imshow(phi_unwrapped_masked, interpolation = 'nearest') - - p0,p1,p2,p3,p4 = test_unwrap3D() - plt.figure(2) - plt.clf() - plt.subplot(322) - plt.imshow(p0[:,:,0],interpolation = 'nearest') - plt.subplot(323) - plt.imshow(p1[:,:,0],interpolation = 'nearest') - plt.subplot(324) - plt.imshow(p2[:,:,0], interpolation = 'nearest') - plt.subplot(325) - plt.imshow(p3[:,:,0], interpolation = 'nearest') - plt.subplot(326) - plt.imshow(p4[:,:,0], interpolation = 'nearest') plt.draw() plt.show() + +if __name__=="__main__": + run_module_suite() + + unwrap_plots() + + # p0,p1,p2,p3,p4 = test_unwrap2D() + # plt.figure(1) + # plt.clf() + # plt.subplot(322) + # plt.imshow(p0,interpolation = 'nearest') + # plt.subplot(323) + # plt.imshow(p1,interpolation = 'nearest') + # plt.subplot(324) + # plt.imshow(p2, interpolation = 'nearest') + # plt.subplot(325) + # plt.imshow(p3, interpolation = 'nearest') + # plt.subplot(326) + # plt.imshow(p4, interpolation = 'nearest') + # plt.draw() + + + # p0,p1,p2,p3,p4 = test_unwrap3D() + # plt.figure(2) + # plt.clf() + # plt.subplot(322) + # plt.imshow(p0[:,:,0],interpolation = 'nearest') + # plt.subplot(323) + # plt.imshow(p1[:,:,0],interpolation = 'nearest') + # plt.subplot(324) + # plt.imshow(p2[:,:,0], interpolation = 'nearest') + # plt.subplot(325) + # plt.imshow(p3[:,:,0], interpolation = 'nearest') + # plt.subplot(326) + # plt.imshow(p4[:,:,0], interpolation = 'nearest') + # plt.draw() + # plt.show()