Question

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?

Was it helpful?

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))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top