<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Sat, Nov 10, 2018 at 10:45 PM Eric Firing <<a href="mailto:efiring@hawaii.edu" target="_blank">efiring@hawaii.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2018/11/10 12:39 PM, Stephan Hoyer wrote:<br>
> On Sat, Nov 10, 2018 at 2:22 PM Hameer Abbasi <<a href="mailto:einstein.edison@gmail.com" target="_blank">einstein.edison@gmail.com</a> <br>
> <mailto:<a href="mailto:einstein.edison@gmail.com" target="_blank">einstein.edison@gmail.com</a>>> wrote:<br>
> <br>
>         To summarize, I think these are our options:<br>
> <br>
>         1. Change the behavior of np.anyarray() to check for an<br>
>         __anyarray__() protocol. Change np.matrix.__anyarray__() to<br>
>         return a base numpy array (this is a minor backwards<br>
>         compatibility break, but probably for the best). Start issuing a<br>
>         FutureWarning for any MaskedArray operations that violate Liskov<br>
>         and add a skipna argument that in the future will default to<br>
>         skipna=False.<br>
> <br>
>         2. Introduce a new coercion function, e.g., np.duckarray(). This<br>
>         is the easiest option because we don't need to cleanup NumPy's<br>
>         existing ndarray subclasses.<br>
> <br>
> <br>
>     My vote is still for 1. I don’t have an issue for PyData/Sparse<br>
>     depending on recent-ish NumPy versions — It’ll need a lot of the<br>
>     recent protocols anyway, although I could be convinced otherwise if<br>
>     major package devs (scikits, SciPy, Dask) were to weigh in and say<br>
>     they’ll jump on it (which seems unlikely given SciPy’s policy to<br>
>     support old NumPy versions).<br>
> <br>
> <br>
> I agree that option (1) is fine for PyData/sparse. The bigger issue is <br>
> that this change should be conditional on making breaking changes (at <br>
> least raising FutureWarning for now) to np.ma.MaskedArray.<br>
> <br>
> I don't know how people who currently use MaskedArray would feel about <br>
> that. I would love to hear their thoughts.<br>
<br>
Thank you.  I am a user of masked arrays, and have been since pre-numpy <br>
days.  I introduced their extensive use in matplotlib long ago.  I have <br>
been a bit concerned, indeed, that all of the discussion of modifying <br>
masked arrays seems to be by people who don't actually use them <br>
explicitly (though they might be using them without knowing it via <br>
internal operations in matplotlib, or they might be quickly getting rid <br>
of them after they are yielded by netCDF4.Dataset()).<br>
<br>
I think that those of us who do use masked arrays recognize that they <br>
are not perfect; they have some quirks and gotchas, and one has to be <br>
careful to use <a href="http://numpy.ma" rel="noreferrer" target="_blank">numpy.ma</a> functions instead of numpy functions in most <br>
cases.  But we use them because they have real advantages over the <br>
alternatives, which are using nans and/or manually tracking independent <br>
masks throughout calculations.  These advantages are largely because <br>
masked values *don't* behave like nan, *don't* propagate.  This is <br>
fundamental to the design, and motivated by real-life use cases.<br>
<br>
The proposal to add a skipna kwarg to MaskedArray looks to me like it is <br>
giving purity priority over practicality.  It will force ma users to <br>
insert skipna kwargs all over the place--because the default will be <br>
contrary to the primary purposes of using masked arrays, in most cases. <br>
How many people will it actually benefit?  How many people are being <br>
bitten, and how badly, by masked array behavior?<br>
<br>
If there were a prospect of truly integrating missing/masked value <br>
handling into numpy, simplifying or phasing out <a href="http://numpy.ma" rel="noreferrer" target="_blank">numpy.ma</a>, I would be <br>
delighted--I think it is the biggest single fundamental improvement that <br>
could be made, from the user's standpoint.  I was sad to see Mark <br>
Wiebe's work in that direction come to grief.<br>
<br>
If there are ways of gradually improving <a href="http://numpy.ma" rel="noreferrer" target="_blank">numpy.ma</a> and its <br>
interoperability with the rest of numpy and with the proliferation of <br>
duck arrays, I'm all in favor--so long as they don't effectively wreck <br>
<a href="http://numpy.ma" rel="noreferrer" target="_blank">numpy.ma</a> for its present intended purposes.</blockquote><div><br></div></div><div dir="ltr"><div class="gmail_quote"><div>Eric -- thank you for sharing your perspective! I guess it should not be surprising that the semantics of MaskedArray intentionally deviate from the semantics of base NumPy arrays.</div><div><br></div><div><div>This deviation is fortunately less severe than than deviations in the behavior of np.matrix, but it still presents some difficulties for duck typing. We're in a position to reduce (but still not eliminate) these differences with new protocols like __array_function__.</div></div><div><br></div><div>I think Nathaniel actually summarized these issues pretty well in NEP 16 (<a href="http://www.numpy.org/neps/nep-0016-abstract-array.html" target="_blank">http://www.numpy.org/neps/nep-0016-abstract-array.html</a>). If we want a coercion function that guarantees an object is a "full duck array", then it can't pass on either np.matrix or MaskedArray in their current state. Anything less than full compatibility provides a shaky foundation for use in downstream projects or inside NumPy itself.</div><br class="inbox-inbox-Apple-interchange-newline"><div>In theory (certainly if we were starting from scratch) it would make sense to make asabstractarray() pass on any ndarray subclass, but this would require willingness to make breaking changes to both np.matrix and MaskedArray.</div><div><br></div><div>I would suggest adopting a variation of the proposal in NEP 16, except using a protocol rather an abstract base class per NEP 22, e.g.,</div><div><br></div><div># names still to be determined</div><div>def asabstractarray(array, dtype):</div><div>    if hasattr(array, '__abstractarray__'):</div><div>        return array.__abstractarray__(array, dtype=dtype)</div><div>    return asarray(array, dtype)</div><div><br></div><div><br></div></div></div></div>