From 3d4fb9559c8bf068d6b24aabe20bc79a9683cb26 Mon Sep 17 00:00:00 2001 From: Gregor Thalhammer Date: Sun, 13 May 2012 18:44:53 +0200 Subject: [PATCH] unwrap: moved pure python code from unwrap2D.pyx to unwrap.py, allocate output array in pure python --- unwrap2D/setup.py | 1 - unwrap2D/test_unwrap.py | 2 +- unwrap2D/unwrap.py | 20 ++++++++++++++++++++ unwrap2D/unwrap2D.pyx | 27 ++++----------------------- 4 files changed, 25 insertions(+), 25 deletions(-) create mode 100644 unwrap2D/unwrap.py diff --git a/unwrap2D/setup.py b/unwrap2D/setup.py index 284d8374..a8be8565 100644 --- a/unwrap2D/setup.py +++ b/unwrap2D/setup.py @@ -7,7 +7,6 @@ import numpy as np ext_modules = [ Extension('unwrap2D', ['unwrap2D.pyx', - #'Miguel_2D_unwrapper_with_mask_and_wrap_around_option.cpp', 'Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c', ], include_dirs = [np.get_include(),], diff --git a/unwrap2D/test_unwrap.py b/unwrap2D/test_unwrap.py index 930cf18b..af94c934 100644 --- a/unwrap2D/test_unwrap.py +++ b/unwrap2D/test_unwrap.py @@ -1,5 +1,5 @@ from numpy.testing import * -from unwrap2D import unwrap2D +from unwrap import unwrap2D import numpy as np from numpy import outer, arange, ones, abs, empty, power, indices diff --git a/unwrap2D/unwrap.py b/unwrap2D/unwrap.py new file mode 100644 index 00000000..8a61a779 --- /dev/null +++ b/unwrap2D/unwrap.py @@ -0,0 +1,20 @@ +import numpy as np +from unwrap2D import _unwrap2D + +def unwrap2D(wrapped_array, wrap_around_x = False, wrap_around_y = False): + wrapped_array = np.require(wrapped_array, np.float32, ['C']) + wrapped_array_masked = np.ma.asarray(wrapped_array) + unwrapped_array = np.empty_like(wrapped_array_masked.data) + + _unwrap2D(wrapped_array_masked.data, + np.ma.getmaskarray(wrapped_array_masked).astype(np.uint8), + unwrapped_array, + wrap_around_x, wrap_around_y) + if np.ma.isMaskedArray(wrapped_array): + return np.ma.array(unwrapped_array, mask = wrapped_array_masked.mask) + else: + return unwrapped_array + + #TODO: set_fill to minimum value + #TODO: check for empty mask, not a single contiguous pixel + diff --git a/unwrap2D/unwrap2D.pyx b/unwrap2D/unwrap2D.pyx index 9d734dec..4d2a7be0 100644 --- a/unwrap2D/unwrap2D.pyx +++ b/unwrap2D/unwrap2D.pyx @@ -1,41 +1,22 @@ -import numpy as np -cimport numpy as np - -import numpy.ma - +#import numpy as np +#cimport numpy as np cdef extern int unwrap(float* wrapped_image, float* unwrapped_image, unsigned char* input_mask, int image_width, int image_height, int wrap_around_x, int wrap_around_y) -def unwrap2D(input, wrap_around_x = False, wrap_around_y = False): - - masked_array = numpy.ma.asarray(input, dtype = np.float32) - unwrapped_array = _unwrap2D(masked_array.data, - numpy.ma.getmaskarray(masked_array).astype(np.uint8), - wrap_around_x, wrap_around_y) - if numpy.ma.isarray(input): - return numpy.ma.array(unwrapped_array, mask = input.mask) - else: - return unwrapped_array - - #TODO: set_fill to minimum value - - -cdef _unwrap2D(float[:,::1] array, +def _unwrap2D(float[:,::1] array, unsigned char[:,::1] mask, + float[:,::1] unwrapped_array, wrap_around_x, wrap_around_y): - cdef float[:,::1] unwrapped_array = np.empty_like(array) cdef int h = array.shape[0] cdef int w = array.shape[1] - #TODO: check for masked array/ unwrap(&array[0,0], &unwrapped_array[0,0], &mask[0,0], array.shape[0], array.shape[1], wrap_around_x, wrap_around_y, ) - return np.asarray(unwrapped_array)