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

Initialising NDData objects with a numpy masked array object fails if there are no masked entries #2784

Closed
AnthonyHorton opened this issue Jul 23, 2014 · 1 comment

Comments

@AnthonyHorton
Copy link
Contributor

@AnthonyHorton AnthonyHorton commented Jul 23, 2014

If you attempt to initialise an NDData object using a numpy masked array object which happens to have no masked entries it raises an exception, e.g.

maskedarray = numpy.ma.array(numpy.ones((10,10)))
astropy.nddata.NDData(maskedarray)

results in ValueError: dimensions of mask do not match data (confirmed with Python 3.4.1, numpy 1.8.1 and astropy 0.4).

The problem is the mask setter code in nddata.py:

@mask.setter
def mask(self, value):
    if value is not None:
        mask = np.array(value, dtype=np.bool_, copy=False)
        if mask.shape != self.shape:
            raise ValueError("dimensions of mask do not match data")
        else:
            self._mask = mask
    else:
        # internal representation should be one numpy understands
        self._mask = np.ma.nomask

The 3rd line tests whether the provided mask value is equal to None but the mask of a numpy masked array with no masked elements is not None, it is numpy.ma.nomask (which evaluates to False). As a result the expression in the if statement evaluates as True and the setter goes ahead to convert numpy.ma.nomask to a single element boolean array [False,] before raising an exception because the shape doesn't match that of the data.

I think all that is required here is to replace

if value is not None:

with

if (value is not None) and (value != np.ma.nomask):

@embray embray added Bug labels Jul 24, 2014
@AnthonyHorton

This comment has been minimized.

Copy link
Contributor Author

@AnthonyHorton AnthonyHorton commented Jul 25, 2014

Confirmed in current dev version:

Python 3.4.0 (default, May 22 2014, 17:22:29) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import astropy
>>> astropy.__version__
'1.0.dev9488'
>>> import astropy.nddata as ndd
>>> m = np.ma.array(np.ones((10,10)))
>>> ndd.NDData(m)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ajh/Build/astropy/build/lib.linux-x86_64-3.4/astropy/nddata/nddata.py", line 160, in __init__
    self.mask = data.mask
  File "/home/ajh/Build/astropy/build/lib.linux-x86_64-3.4/astropy/nddata/nddata.py", line 205, in mask
    raise ValueError("dimensions of mask do not match data")
ValueError: dimensions of mask do not match data
>>> m[2,5] = np.ma.masked
>>> ndd.NDData(m)
NDData([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])
AnthonyHorton pushed a commit to AnthonyHorton/astropy that referenced this issue Jul 25, 2014
AnthonyHorton pushed a commit to AnthonyHorton/astropy that referenced this issue Jul 25, 2014
AnthonyHorton pushed a commit to AnthonyHorton/astropy that referenced this issue Aug 1, 2014
AnthonyHorton pushed a commit to AnthonyHorton/astropy that referenced this issue Aug 1, 2014
@embray embray closed this in 465fa1c Aug 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.