def test_mask(): length = 100 ramps = [ np.linspace(0, 4 * np.pi, length), np.linspace(0, 8 * np.pi, length), np.linspace(0, 6 * np.pi, length) ] image = np.vstack(ramps) mask_1d = np.ones((length, ), dtype=np.bool) mask_1d[0] = mask_1d[-1] = False for i in range(len(ramps)): # mask all ramps but the i'th one mask = np.zeros(image.shape, dtype=np.bool) mask |= mask_1d.reshape(1, -1) mask[i, :] = False # unmask i'th ramp image_wrapped = np.ma.array(np.angle(np.exp(1j * image)), mask=mask) image_unwrapped = unwrap_phase(image_wrapped) image_unwrapped -= image_unwrapped[0, 0] # remove phase shift # The end of the unwrapped array should have value equal to the # endpoint of the unmasked ramp assert_array_almost_equal_nulp(image_unwrapped[:, -1], image[i, -1]) assert_(np.ma.isMaskedArray(image_unwrapped)) # Same tests, but forcing use of the 3D unwrapper by reshaping with expected_warnings(['length 1 dimension']): shape = (1, ) + image_wrapped.shape image_wrapped_3d = image_wrapped.reshape(shape) image_unwrapped_3d = unwrap_phase(image_wrapped_3d) # remove phase shift image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0] assert_array_almost_equal_nulp(image_unwrapped_3d[:, :, -1], image[i, -1])
def test_mask(): length = 100 ramps = [np.linspace(0, 4 * np.pi, length), np.linspace(0, 8 * np.pi, length), np.linspace(0, 6 * np.pi, length)] image = np.vstack(ramps) mask_1d = np.ones((length,), dtype=np.bool) mask_1d[0] = mask_1d[-1] = False for i in range(len(ramps)): # mask all ramps but the i'th one mask = np.zeros(image.shape, dtype=np.bool) mask |= mask_1d.reshape(1, -1) mask[i, :] = False # unmask i'th ramp image_wrapped = np.ma.array(np.angle(np.exp(1j * image)), mask=mask) image_unwrapped = unwrap_phase(image_wrapped) image_unwrapped -= image_unwrapped[0, 0] # remove phase shift # The end of the unwrapped array should have value equal to the # endpoint of the unmasked ramp assert_array_almost_equal_nulp(image_unwrapped[:, -1], image[i, -1]) assert_(np.ma.isMaskedArray(image_unwrapped)) # Same tests, but forcing use of the 3D unwrapper by reshaping with expected_warnings(['length 1 dimension']): shape = (1,) + image_wrapped.shape image_wrapped_3d = image_wrapped.reshape(shape) image_unwrapped_3d = unwrap_phase(image_wrapped_3d) # remove phase shift image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0] assert_array_almost_equal_nulp(image_unwrapped_3d[:, :, -1], image[i, -1])
def assert_phase_almost_equal(a, b, *args, **kwargs): """An assert_almost_equal insensitive to phase shifts of n*2*pi.""" shift = 2 * np.pi * np.round((b.mean() - a.mean()) / (2 * np.pi)) with expected_warnings([ r'invalid value encountered|\A\Z', r'divide by zero encountered|\A\Z' ]): print('assert_phase_allclose, abs', np.max(np.abs(a - (b - shift)))) print('assert_phase_allclose, rel', np.max(np.abs((a - (b - shift)) / a))) if np.ma.isMaskedArray(a): assert_(np.ma.isMaskedArray(b)) assert_array_equal(a.mask, b.mask) assert_(a.fill_value == b.fill_value) au = np.asarray(a) bu = np.asarray(b) with expected_warnings([ r'invalid value encountered|\A\Z', r'divide by zero encountered|\A\Z' ]): print('assert_phase_allclose, no mask, abs', np.max(np.abs(au - (bu - shift)))) print('assert_phase_allclose, no mask, rel', np.max(np.abs((au - (bu - shift)) / au))) assert_array_almost_equal_nulp(a + shift, b, *args, **kwargs)
def assert_phase_almost_equal(a, b, *args, **kwargs): """An assert_almost_equal insensitive to phase shifts of n*2*pi.""" shift = 2 * np.pi * np.round((b.mean() - a.mean()) / (2 * np.pi)) with warnings.catch_warnings(): warnings.simplefilter("ignore") print('assert_phase_allclose, abs', np.max(np.abs(a - (b - shift)))) print('assert_phase_allclose, rel', np.max(np.abs((a - (b - shift)) / a))) if np.ma.isMaskedArray(a): assert_(np.ma.isMaskedArray(b)) assert_array_equal(a.mask, b.mask) au = np.asarray(a) bu = np.asarray(b) with warnings.catch_warnings(): warnings.simplefilter("ignore") print('assert_phase_allclose, no mask, abs', np.max(np.abs(au - (bu - shift)))) print('assert_phase_allclose, no mask, rel', np.max(np.abs((au - (bu - shift)) / au))) assert_array_almost_equal_nulp(a + shift, b, *args, **kwargs)
def assert_phase_almost_equal(a, b, *args, **kwargs): """An assert_almost_equal insensitive to phase shifts of n*2*pi.""" shift = 2 * np.pi * np.round((b.mean() - a.mean()) / (2 * np.pi)) with warnings.catch_warnings(): warnings.simplefilter("ignore") print('assert_phase_allclose, abs', np.max(np.abs(a - (b - shift)))) print('assert_phase_allclose, rel', np.max(np.abs((a - (b - shift)) / a))) if np.ma.isMaskedArray(a): assert_(np.ma.isMaskedArray(b)) assert_array_equal(a.mask, b.mask) au = np.asarray(a) bu = np.asarray(b) with warnings.catch_warnings(): warnings.simplefilter("ignore") print('assert_phase_allclose, no mask, abs', np.max(np.abs(au - (bu - shift)))) print('assert_phase_allclose, no mask, rel', np.max(np.abs((au - (bu - shift)) / au))) assert_array_almost_equal_nulp(a + shift, b, *args, **kwargs)
def assert_phase_almost_equal(a, b, *args, **kwargs): """An assert_almost_equal insensitive to phase shifts of n*2*pi.""" shift = 2 * np.pi * np.round((b.mean() - a.mean()) / (2 * np.pi)) with expected_warnings([r'invalid value encountered|\A\Z', r'divide by zero encountered|\A\Z']): print('assert_phase_allclose, abs', np.max(np.abs(a - (b - shift)))) print('assert_phase_allclose, rel', np.max(np.abs((a - (b - shift)) / a))) if np.ma.isMaskedArray(a): assert_(np.ma.isMaskedArray(b)) assert_array_equal(a.mask, b.mask) assert_(a.fill_value == b.fill_value) au = np.asarray(a) bu = np.asarray(b) with expected_warnings([r'invalid value encountered|\A\Z', r'divide by zero encountered|\A\Z']): print('assert_phase_allclose, no mask, abs', np.max(np.abs(au - (bu - shift)))) print('assert_phase_allclose, no mask, rel', np.max(np.abs((au - (bu - shift)) / au))) assert_array_almost_equal_nulp(a + shift, b, *args, **kwargs)