Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

BUG: Trac ticket 1491: ndimage.label ignores output keyword #240

Closed
wants to merge 1 commit into from

3 participants

@thouis

These changes make ndimage.label() behave as described in the docstring
with regards to the output dtype, which can now be an array or dtype
as long as its compatible with assignment from int32.

@thouis thouis BUG: Trac ticket 1491: ndimage.label ignores output keyword
These changes make ndimage.label() behave as described in the docstring
with regards to the output dtype, which can now be an array or dtype
as long as its compatible with assignment from int32.
ceaa78b
@jjhelmus

Thouis thanks for addressing this ticket!

With this pull ndimage.label is doing what the documentation describes, but unless I am mistaken a int32 array is still used for the labeling not the passed array. This is going to result in more memory usage than necessary. It seems to me that a better solution would be to have NI_Label in ni_measure.c write to the passed output array in a manner similar to NI_ZoomShift in ni_interpolation.c (there are probably other example but this is the one I am most familar with). This will oviously be a much more involved patch, but I though I should mention it.

Again, Thouis thanks your work, it is nice to have a solution to this problem.

@thouis

This seems like something to address by rewriting in Cython using fused types (or some other generic), once its support has stabilized.

@jjhelmus

Agreed, Cython's fused types or another method of generic programming would be great here.

@pv pv referenced this pull request from a commit
@pv pv BUG: ndimage: Merge PR #240 from 'thouis/ticket-1491'
Fix ndimage.label ignoring its output keyword.
5f0b796
@pv
Owner
pv commented

Thanks, merged in 5f0b796

Type generics could be nice, but rewriting ndimage is a bit of a chore...

@pv pv closed this
@thouis
@pv
Owner
pv commented

Wow, good luck (and big thanks :))

@jnothman jnothman referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 5, 2012
  1. @thouis

    BUG: Trac ticket 1491: ndimage.label ignores output keyword

    thouis authored
    These changes make ndimage.label() behave as described in the docstring
    with regards to the output dtype, which can now be an array or dtype
    as long as its compatible with assignment from int32.
This page is out of date. Refresh to see the latest.
View
19 scipy/ndimage/measurements.py
@@ -148,16 +148,33 @@ def label(input, structure=None, output=None):
raise RuntimeError('structure dimensions must be equal to 3')
if not structure.flags.contiguous:
structure = structure.copy()
+ requested_output = None
+ requested_dtype = None
if isinstance(output, numpy.ndarray):
if output.dtype.type != numpy.int32:
- raise RuntimeError('output type must be int32')
+ if output.shape != input.shape:
+ raise RuntimeError("output shape not correct")
+ # _ndimage.label() needs np.int32
+ requested_output = output
+ output = numpy.int32
+ else:
+ # output will be written directly
+ pass
else:
+ requested_dtype = output
output = numpy.int32
output, return_value = _ni_support._get_output(output, input)
max_label = _nd_image.label(input, structure, output)
if return_value is None:
+ # result was written in-place
+ return max_label
+ elif requested_output is not None:
+ # original output was not int32
+ requested_output[...] = output[...]
return max_label
else:
+ if requested_dtype is not None:
+ return_value = return_value.astype(requested_dtype)
return return_value, max_label
def find_objects(input, max_label=0):
View
26 scipy/ndimage/tests/test_measurements.py
@@ -1,6 +1,6 @@
from numpy.testing import assert_, assert_array_almost_equal, assert_equal, \
assert_almost_equal, assert_array_equal, \
- run_module_suite, TestCase
+ assert_raises, run_module_suite, TestCase
import numpy as np
import scipy.ndimage as ndimage
@@ -261,6 +261,30 @@ def test_label13():
assert_array_almost_equal(out, expected)
assert_equal(n, 1)
+def test_label_output_typed():
+ "test label with specified output with type"
+ data = np.ones([5])
+ for t in types:
+ output = np.zeros([5], dtype=t)
+ n = ndimage.label(data, output=output)
+ assert_array_almost_equal(output, 1)
+ assert_equal(n, 1)
+
+def test_label_output_dtype():
+ "test label with specified output dtype"
+ data = np.ones([5])
+ for t in types:
+ output, n = ndimage.label(data, output=t)
+ assert_array_almost_equal(output, 1)
+ assert output.dtype == t
+
+def test_label_output_wrong_size():
+ "test label with output of wrong size"
+ data = np.ones([5])
+ for t in types:
+ output = np.zeros([10], t)
+ assert_raises(RuntimeError, ndimage.label, data, output=output)
+
def test_find_objects01():
"find_objects 1"
data = np.ones([], dtype=int)
Something went wrong with that request. Please try again.