mdhaber / marray

Masked versions of array API compatible arrays
MIT License
4 stars 1 forks source link

easier way to create arrays #6

Open keewis opened 1 day ago

keewis commented 1 day ago

While the idea of using a function to create a nested namespace is neat, it makes creating arrays harder:

import marray
import numpy as np

xp = marray.masked_array(np)

a = xp.asarray(np.arange(10))
a.mask[...] = np.arange(10) % 2 == 0

What I'd like to have (but don't quite know how easy it is to support, nor if it actually is a good idea) is something like this:

import marray
import numpy as np

a = marray.MaskedArray(data=np.arange(10), mask=np.arange(10) % 2 == 0)
xp = a.__array_namespace__()  # nested namespace

Alternatively, this could also work (since the Array API doesn't forbid adding non-standard things to the namespace):

xp = marray.masked_array(np)
a = xp.MaskedArray(data=np.arange(10), mask=np.arange(10) % 2 == 0)

However, I would imagine that this makes creating / composing arrays a bit harder. And since we don't subclass arrays classes anymore, maybe we don't even need the dynamic namespace (and thus the meta-programming)?

mdhaber commented 1 day ago

Your second example should work. And asarray also accepts the mask. Have you tried

import marray
import numpy as np

xp = marray.masked_array(np)
a = xp.asarray(np.arange(10), mask=(np.arange(10) % 2 == 0))

?

Currently mask doesn't appear in the documentation, because it's just copied from NumPy, but that can come later.

keewis commented 1 day ago

That's interesting, I totally missed that. I was under the (quite possibly mistaken?) assumption that the Array API didn't allow adding additional arguments, so I didn't even bother to check.

mdhaber commented 1 day ago

I'm not certain whether the standard disallows that sort of thing, but NumPy 2.1 asarray has an order argument that is not in the standard. I'm sure there are many more examples.