-2

numpy.ma.split_array does not exist.

Consequently, does the following code works as intended if arr is a masked array?

np.array_split(arr, multiprocessing.cpu_count())

If not, how should I define a function split_masked_array to achieve similar behavior?

WaterFox
  • 850
  • 6
  • 18
  • 1
    "does the following code works as intended" why are you asking us? Why not try it and see for yourself? – ddejohn Mar 25 '22 at 22:14
  • 1
    `masked arrays` are a subclass of `ndarray`. They define theIr own methods as needed. Also a slew of `np.ma` functions. Use those where available. Many `np` functions dont work right, unless they use the methods. Or they just use the `data` part of tbe `ma`, ignoring the mask. I don't know any general purpose rules. Read the docs and test. – hpaulj Mar 26 '22 at 00:32
  • Thank your answers! Yes I read the doc, but np.split_array did not say anything about ma.arrays and np.ma doc did not say anything about split_array! And I was scared to test things in a silly way! – WaterFox Mar 26 '22 at 04:29

1 Answers1

1

Not sure why you didn't just try. Seems to work, but it's hard to tell for sure since you didn't provide a minimal reproducible example so I don't know what "works as intended" means.

In [5]: x = np.ma.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])

In [6]: x
Out[6]:
masked_array(
  data=[[--, 2, 3],
        [4, --, 6],
        [7, 8, --]],
  mask=[[ True, False, False],
        [False,  True, False],
        [False, False,  True]],
  fill_value=999999)

In [7]: np.array_split(x, 3)
Out[7]:
[masked_array(data=[[--, 2, 3]],
              mask=[[ True, False, False]],
        fill_value=999999),
 masked_array(data=[[4, --, 6]],
              mask=[[False,  True, False]],
        fill_value=999999),
 masked_array(data=[[7, 8, --]],
              mask=[[False, False,  True]],
        fill_value=999999)]
ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • Thank you for your time! It was more a question about general culture of python interfaces. The exact reason is that I am reusing a code that was designed for np.arrays, and feeding it with masked arrays leads downstream to `ValueError: zero-dimensional arrays cannot be concatenated`. So rather than pasting a big blob of code with a cryptic error, I wanted to make sure that this particular interface was ok before to test further :) And also I did not try because I've little idea of what the code internals is supposed to do :/ – WaterFox Mar 25 '22 at 22:23
  • 1
    `np.array_split` just does repeated slices of the right size (and dimension), The code is python, and easily read. Masked arrays indexing behaves basically the same. As for your concatenation error, try to come up with a minimal example, with just enough code. `concatenate` is one of those cases where the `np.ma` version should be used (try it). – hpaulj Mar 26 '22 at 02:27
  • Thanks! Yes I was already using `np.ma.concatenate`, that's why my next suspicion went to `np.split_array` somehow mistreating the mask, and I was not sure how to test this. – WaterFox Mar 26 '22 at 04:33
  • @ddejohn, this example with constant False mask is not proof. Use a mask that is a mix of True and False – hpaulj Mar 26 '22 at 14:26