Search code examples
pythonarraysnumpyctypesmemmove

memove a masked array - python


I have a numpy array which contains no data values. I mask those no data values so that they do not influence my calculations using:

    array = numpy.ma.masked_values(array, options['ndv'], copy=False)

I then use memmove to get the numpy array into a shared ctypes array using:

def ndarray_to_shmem(array):
    """ Converts a numpy.ndarray to a multiprocessing.Array object.

    The memory is copied, and the array is flattened.
    """
    arr = array.reshape((-1, ))
    data = RawArray(_numpy_to_ctypes[array.dtype.type], 
                                    arr.size)
    ctypes.memmove(data, array.data[:], len(array.data))
    return data

Which returns the following stack trace:

ctypes.memmove(data, array.data[:], len(array.data))
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: wrong type

Is it possible to use memmove to move the masked array into a shared, ctypes array?


Solution

  • First of all, you need to change this line:

    ctypes.memmove(data, array.data[:], len(array.data))
    

    to look like this:

    ctypes.memmove(data, array.data[:].ctypes.data, len(array.data))
    

    Second, ctypes.memmove has no understanding of masked arrays. Instead, just make a copy with the masked areas set to nan:

    masked = array.copy()
    masked[array == options['ndv']] = np.nan
    
    ...
    
    ctypes.memmove(data, masked.ctypes.data, len(masked))