[Numpy-discussion] ravel() in ma/core.py

Eric Firing efiring@hawaii....
Wed Aug 13 12:54:47 CDT 2008


Christoph T. Weidemann wrote:
> Hi!
> 
> I'm working with a subclass of ndarray and ran into an issue that
> seems to be caused by a line in numpy/ma/core.py
> The offending line is no. 1837 in version 1.1.0 or 2053 in the latest
> SVN version (revision 5635):
> r = ndarray.ravel(self._data).view(type(self))
> 
> The problem is that my subclass of ndarray has its own implementation
> of the ravel method that takes care of some important things before
> calling the ndarray.ravel class. Calling ndarray.ravel directly on
> this data structure doesn't allow for the necessary house-keeping to
> take place and my data structure complains by throwing an exception.I
> believe this issue would be solved if the above line instead read:
> r = self.ravel()
> or alternatively
> r = np.ravel(self)
> or something along those lines -- can anybody here please take a look
> and see if these (or something else) would be a viable alternative
> that would cause the subclass's ravel function to be called instead of
> ndarray.ravel()?
> 
> Thanks,
> CTW
> 
> PS: I ran into this issue while trying to plot my data with the imshow
> function in matplotlib. If vmin and/or vmax are not specified, a
> masked array version of the input array is generated in
> matplotlib/colors.py in this line:
> val = ma.asarray(value).astype(np.float)
> Then ma.minimum() and ma.maximum() are called and eventually the line
> highlighted above gets executed. I don't know if/why this conversion
> to masked arrays is necessary in the matplotlib code, but it seems
> pretty clear that the source of the problem is the application of the
> ndarray.ravel() method rather than that for the subclass in the line
> highlighted above.

We are using masked arrays because we need to allow for missing values; 
we need to be able to find the max and min of the valid values.  Using 
ma.asarray etc. is the simplest way to do this, assuming the user will 
be using masked arrays when input data have invalid values.
With more code we could avoid the conversion when an input array is not 
masked.  The strategy for supporting masked arrays, nans, and infs in 
mpl is not completely settled yet.

Eric


More information about the Numpy-discussion mailing list