Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ndimage measurement's median provides result for missing labels #7706

Open
jakirkham opened this issue Aug 3, 2017 · 2 comments
Open

ndimage measurement's median provides result for missing labels #7706

jakirkham opened this issue Aug 3, 2017 · 2 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.ndimage

Comments

@jakirkham
Copy link
Contributor

It appears that in some cases SciPy ndimage measurement's median function returns a result (other than nan) when a label doesn't exist. A reproducer of this odd behavior is provided below along with an environment file for use with conda.

Code snippet:

In [1]: import numpy as np

In [2]: import scipy.ndimage as spnd

In [3]: np.random.seed(0)

In [4]: a = np.random.random((4, 4))

In [5]: a
Out[5]: 
array([[ 0.5488135 ,  0.71518937,  0.60276338,  0.54488318],
       [ 0.4236548 ,  0.64589411,  0.43758721,  0.891773  ],
       [ 0.96366276,  0.38344152,  0.79172504,  0.52889492],
       [ 0.56804456,  0.92559664,  0.07103606,  0.0871293 ]])

In [6]: labels, labels_nb = spnd.label(a < 0.5)

In [7]: labels
Out[7]: 
array([[0, 0, 0, 0],
       [1, 0, 2, 0],
       [0, 3, 0, 0],
       [0, 0, 4, 4]], dtype=int32)

In [8]: np.unique(labels)
Out[8]: array([0, 1, 2, 3, 4], dtype=int32)

In [9]: spnd.median(a, labels, [1, 100])
Out[9]: array([ 0.4236548 ,  0.52889492])

Environment:

name: test
channels:
- conda-forge
- defaults
dependencies:
- appnope=0.1.0=py27_0
- backports.shutil_get_terminal_size=1.0.0=py27_1
- blas=1.1=openblas
- ca-certificates=2017.7.27.1=0
- certifi=2017.7.27.1=py27_0
- decorator=4.1.2=py27_0
- enum34=1.1.6=py27_1
- ipython=5.4.1=py27_0
- ipython_genutils=0.2.0=py27_0
- libgfortran=3.0.0=0
- ncurses=5.9=10
- numpy=1.13.1=py27_blas_openblas_200
- openblas=0.2.19=2
- openssl=1.0.2l=0
- pathlib2=2.3.0=py27_0
- pexpect=4.2.1=py27_0
- pickleshare=0.7.3=py27_0
- pip=9.0.1=py27_0
- prompt_toolkit=1.0.14=py27_0
- ptyprocess=0.5.2=py27_0
- pygments=2.2.0=py27_0
- python=2.7.13=1
- readline=6.2=0
- scandir=1.5=py27_1
- scipy=0.19.1=py27_blas_openblas_201
- setuptools=36.2.2=py27_0
- simplegeneric=0.8.1=py27_0
- six=1.10.0=py27_1
- sqlite=3.13.0=1
- tk=8.5.19=2
- traitlets=4.3.2=py27_0
- wcwidth=0.1.7=py27_0
- wheel=0.29.0=py27_0
- zlib=1.2.11=0
- pip:
  - backports.shutil-get-terminal-size==1.0.0
  - ipython-genutils==0.2.0
  - prompt-toolkit==1.0.14
@vbasu
Copy link
Contributor

vbasu commented Aug 13, 2017

I looked at the implementation and it appears that any index that's not a valid label (not between 0 and the max label, not a positive value, not an integer value) will simply return the smallest value with label 0. Perhaps there is a reason for this, I don't know. I don't think it would be too difficult to alter this behavior, but I'm not sure what would be appropriate. Should it return nan? Should there be a warning?

@WarrenWeckesser WarrenWeckesser added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label May 14, 2018
@WarrenWeckesser
Copy link
Member

If both labels and index are given, and there is a value in index that is not in labels, I think an error should be raised. What it does now makes no sense, so I'm calling it a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.ndimage
Projects
None yet
Development

No branches or pull requests

4 participants