From ea15eb7647923685e39a1fda6890733d8a06629a Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Sat, 10 Mar 2012 17:27:54 -0800 Subject: [PATCH] ENH: Allow forced copy of data upon type conversion. --- skimage/util/dtype.py | 30 +++++++++++++++++++++--------- skimage/util/tests/test_dtype.py | 8 ++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/skimage/util/dtype.py b/skimage/util/dtype.py index 76ce3ca0..e9bc3036 100644 --- a/skimage/util/dtype.py +++ b/skimage/util/dtype.py @@ -24,7 +24,7 @@ if np.__version__ >= "1.6.0": _supported_types += (np.float16, ) -def convert(image, dtype): +def convert(image, dtype, force_copy=False): """ Convert an image to the requested data-type. @@ -40,6 +40,8 @@ def convert(image, dtype): Input image. dtype : dtype Target data-type. + force_copy : bool + Force a copy of the data, irrespective of its current dtype. """ image = np.asarray(image) @@ -53,6 +55,8 @@ def convert(image, dtype): itemsize_in = dtypeobj_in.itemsize if dtype_in == dtype: + if force_copy: + image = image.copy() return image if not (dtype_in in _supported_types and dtype in _supported_types): @@ -159,13 +163,15 @@ def convert(image, dtype): return dtype(result) -def img_as_float(image): +def img_as_float(image, force_copy=False): """Convert an image to double-precision floating point format. Parameters ---------- image : ndarray Input image. + force_copy : bool + Force a copy of the data, irrespective of its current dtype. Returns ------- @@ -178,16 +184,18 @@ def img_as_float(image): Negative input values will be shifted to the positive domain. """ - return convert(image, np.float64) + return convert(image, np.float64, force_copy) -def img_as_uint(image): +def img_as_uint(image, force_copy=False): """Convert an image to 16-bit unsigned integer format. Parameters ---------- image : ndarray Input image. + force_copy : bool + Force a copy of the data, irrespective of its current dtype. Returns ------- @@ -199,16 +207,18 @@ def img_as_uint(image): Negative input values will be shifted to the positive domain. """ - return convert(image, np.uint16) + return convert(image, np.uint16, force_copy) -def img_as_int(image): +def img_as_int(image, force_copy=False): """Convert an image to 16-bit signed integer format. Parameters ---------- image : ndarray Input image. + force_copy : bool + Force a copy of the data, irrespective of its current dtype. Returns ------- @@ -221,16 +231,18 @@ def img_as_int(image): the output image will still only have positive values. """ - return convert(image, np.int16) + return convert(image, np.int16, force_copy) -def img_as_ubyte(image): +def img_as_ubyte(image, force_copy=False): """Convert an image to 8-bit unsigned integer format. Parameters ---------- image : ndarray Input image. + force_copy : bool + Force a copy of the data, irrespective of its current dtype. Returns ------- @@ -243,4 +255,4 @@ def img_as_ubyte(image): the output image will still only have positive values. """ - return convert(image, np.uint8) + return convert(image, np.uint8, force_copy) diff --git a/skimage/util/tests/test_dtype.py b/skimage/util/tests/test_dtype.py index 11dd7248..4d0ed750 100644 --- a/skimage/util/tests/test_dtype.py +++ b/skimage/util/tests/test_dtype.py @@ -74,6 +74,14 @@ def test_float_out_of_range(): assert_raises(ValueError, img_as_int, too_low) +def test_copy(): + x = np.array([1], dtype=float) + y = img_as_float(x) + z = img_as_float(x, force_copy=True) + + assert y is x + assert z is not x + if __name__ == '__main__': np.testing.run_module_suite()