Ejemplo n.º 1
0
def asNumpyArray(*params):
    '''
    Given a CLR `System.Array` returns a `numpy.ndarray`.  See _MAP_NET_NP for 
    the mapping of CLR types to Numpy dtypes.

    PS:add 'Length' Param to partial ref one dim array
    '''
    netArray = params[0]
    dims = np.empty(netArray.Rank, dtype=int)
    for I in range(netArray.Rank):
        dims[I] = netArray.GetLength(I)
    netType = netArray.GetType().GetElementType().Name

    try:
        if (len(params) == 2):
            npArray = np.empty(params[1],
                               order='C',
                               dtype=_MAP_NET_NP[netType])
        else:
            npArray = np.empty(dims, order='C', dtype=_MAP_NET_NP[netType])
    except KeyError:
        raise NotImplementedError(
            "asNumpyArray does not yet support System type {}".format(netType))

    try:  # Memmove
        sourceHandle = GCHandle.Alloc(netArray, GCHandleType.Pinned)
        sourcePtr = sourceHandle.AddrOfPinnedObject().ToInt64()
        destPtr = npArray.__array_interface__['data'][0]
        ctypes.memmove(destPtr, sourcePtr, npArray.nbytes)
    finally:
        if sourceHandle.IsAllocated: sourceHandle.Free()
    return npArray
Ejemplo n.º 2
0
def test_correct_overload_selection():
    """Test correct overloading selection for common types."""
    from System import (String, Double, Single,
                        Int16, Int32, Int64)
    from System import Math

    substr = String("substring")
    assert substr.Substring(2) == substr.Substring.__overloads__[Int32](
        Int32(2))
    assert substr.Substring(2, 3) == substr.Substring.__overloads__[Int32, Int32](
        Int32(2), Int32(3))

    for atype, value1, value2 in zip([Double, Single, Int16, Int32, Int64],
                                     [1.0, 1.0, 1, 1, 1],
                                     [2.0, 0.5, 2, 0, -1]):
        assert Math.Abs(atype(value1)) == Math.Abs.__overloads__[atype](atype(value1))
        assert Math.Abs(value1) == Math.Abs.__overloads__[atype](atype(value1))
        assert Math.Max(atype(value1),
                        atype(value2)) == Math.Max.__overloads__[atype, atype](
            atype(value1), atype(value2))
        assert Math.Max(atype(value1),
                        value2) == Math.Max.__overloads__[atype, atype](
            atype(value1), atype(value2))

    clr.AddReference("System.Runtime.InteropServices")
    from System.Runtime.InteropServices import GCHandle, GCHandleType
    from System import Array, Byte
    cs_array = Array.CreateInstance(Byte, 1000)
    handler = GCHandle.Alloc(cs_array, GCHandleType.Pinned)
Ejemplo n.º 3
0
def to_numpy(src):
    """
    Convert .NET array to numpy array

    Parameters
    ----------
    src : System.Array
        
    Returns
    -------
    np.ndarray
        
    """

    src_hndl = GCHandle.Alloc(src, GCHandleType.Pinned)
    try:
        src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
        bufType = ctypes.c_float * len(src)
        cbuf = bufType.from_address(src_ptr)
        d = np.frombuffer(cbuf, dtype=cbuf._type_)
    finally:
        if src_hndl.IsAllocated:
            src_hndl.Free()

    return d
Ejemplo n.º 4
0
def imageframe_to_numpy(frame):
    '''
    Retrieve data from LightField DisplaySource.
    
    Parameters
    ----------
    frame : 
        LightField display source. Could be the live view or a loaded file.

    Returns
    -------
    data
        numpy array.
    '''
    buffer = frame.GetData()
    image_format = frame.Format
    src_hndl = GCHandle.Alloc(buffer, GCHandleType.Pinned)
    try:
        src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
        # Possible data types returned from acquisition
        dtypes = {
            ImageDataFormat.MonochromeUnsigned16: ctypes.c_ushort,
            ImageDataFormat.MonochromeUnsigned32: ctypes.c_uint,
            ImageDataFormat.MonochromeFloating32: ctypes.c_float
        }
        buf_type = dtypes[image_format] * len(buffer)
        cbuf = buf_type.from_address(src_ptr)
        image = np.frombuffer(cbuf, dtype=cbuf._type_).copy()
        image = np.rot90(image.reshape(frame.Height, frame.Width), -1).T
    finally:
        if src_hndl.IsAllocated:
            src_hndl.Free()
    return image
Ejemplo n.º 5
0
def dotnet_arr_to_ndarr(dotnet_arr):
    """
    Converts data read in with dotNet (i.e. reading dfsu through DHI .NET
    libraries) and efficiently converts to numpy ndarray

    Parameters
    ----------
    dotnet_array : either: System.Double, System.Single, System.Int32
        .NET array read in using pythonnet

    Returns
    -------
    ndarray : dotnet_array converted to ndarray

    """

    src_hndl = GCHandle.Alloc(dotnet_arr, GCHandleType.Pinned)

    try:
        src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
        dtype = dotnet_arr.GetType().get_Name()

        if dtype == 'Single[]':
            bufType = ctypes.c_float * len(dotnet_arr)
        elif dtype == 'Int32[]':
            bufType = ctypes.c_int * len(dotnet_arr)
        elif dtype == 'Double[]':
            bufType = ctypes.c_double * len(dotnet_arr)
        cbuf = bufType.from_address(src_ptr)
        ndarray = np.frombuffer(cbuf, dtype=cbuf._type_)
    finally:
        if src_hndl.IsAllocated:
            src_hndl.Free()
    return ndarray
Ejemplo n.º 6
0
def as_numpy_array(netArray):
    '''
    Given a CLR `System.Array` returns a `numpy.ndarray`.
    '''
    dims = np.empty(netArray.Rank, dtype=int)
    for I in range(netArray.Rank):
        dims[I] = netArray.GetLength(I)
    netType = netArray.GetType().GetElementType().Name

    try:
        npArray = np.empty(dims, order='C', dtype=_MAP_NET_NP[netType])
    except KeyError:
        raise NotImplementedError(
            "as_numpy_array does not yet support System type "
            "{}".format(netType))

    try:  # Memmove
        sourceHandle = GCHandle.Alloc(netArray, GCHandleType.Pinned)
        sourcePtr = sourceHandle.AddrOfPinnedObject().ToInt64()
        destPtr = npArray.__array_interface__['data'][0]
        ctypes.memmove(destPtr, sourcePtr, npArray.nbytes)
    finally:
        if sourceHandle.IsAllocated:
            sourceHandle.Free()
    return npArray
Ejemplo n.º 7
0
def dn2np(src):
    #Arg src : the source dotNEt source Array
    #Retrun dest : the copy of src into a numpy array

    #Get GCHandle to handle managed memory with non-managed code
    src_hndl = GCHandle.Alloc(src, GCHandleType.Pinned)

    #Get numpy type from dotNet Array
    array_type = src.GetType()
    if array_type.ToString() == "System.Byte[]":
        ctype = ctypes.c_ubyte
    elif array_type.ToString() == "System.Single[]":
        ctype = ctypes.c_float
    elif array_type.ToString() == "System.Double[]":
        ctype = ctypes.c_double
    else:
        print("Array type", array_type.ToString(),
              "does not match with numpy type")
        return

    try:
        #Get pointer to source
        src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
        bufType = ctype * len(src)
        #Copy src to dst
        cbuf = bufType.from_address(src_ptr)
        dest = np.frombuffer(cbuf, dtype=cbuf._type_)
    finally:
        #Allow the garbage collector to free managed memory
        if src_hndl.IsAllocated: src_hndl.Free()
    return dest
Ejemplo n.º 8
0
def net2numpy(net_array):
    src_hndl = GCHandle.Alloc(net_array, GCHandleType.Pinned)
    try:
        src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
        bufType = ctypes.c_float * len(net_array)
        cbuf = bufType.from_address(src_ptr)
        np_array = np.frombuffer(cbuf, dtype=cbuf._type_)
    finally:
        if src_hndl.IsAllocated: src_hndl.Free()

    return np_array.reshape(net_array.GetLength(0), net_array.GetLength(1))
Ejemplo n.º 9
0
def to_ndarray(src, dtype):
    '''converts a blitable .NET array of type dtype to a numpy array of type dtype'''
    src_hndl = GCHandle.Alloc(src, GCHandleType.Pinned)
    try:
        src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
        dest = np.frombuffer(string_at(src_ptr, len(src) * sizeof(dtype)), dtype=dtype)
    finally:
        if src_hndl.IsAllocated: src_hndl.Free()
    if SAFE_MODE:
        check_arrays(src, dest)
    return dest
Ejemplo n.º 10
0
def to_numpy(src):

    src_hndl = GCHandle.Alloc(src, GCHandleType.Pinned)
    try:
        src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
        bufType = ctypes.c_float * len(src)
        cbuf = bufType.from_address(src_ptr)
        d = np.frombuffer(cbuf, dtype=cbuf._type_)
    finally:
        if src_hndl.IsAllocated: src_hndl.Free()

    return d
Ejemplo n.º 11
0
    def __grab_image(self):  # Method of geting array from buffer (fast)
        arr = BSDK.camera.camImg.GetLastImageArray()
        src_hndl = GCHandle.Alloc(arr, GCHandleType.Pinned)
        try:
            src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
            dest = np.fromstring(string_at(src_ptr,
                                           len(arr) *
                                           8))  # note: 8 is size of double...
        finally:
            if src_hndl.IsAllocated:
                src_hndl.Free()
                #self.stop()

        return dest.reshape(self.height, self.width)
Ejemplo n.º 12
0
def convert_numpy_array_to_cnet(nparray):
    '''
    Given a `numpy.ndarray` returns a CLR `System.Array`.  See _MAP_NP_NET for
    the mapping of Numpy dtypes to CLR types.

    Note: `complex64` and `complex128` arrays are converted to `float32`
    and `float64` arrays respectively with shape [m,n,...] -> [m,n,...,2]
    Reference: https://github.com/pythonnet/pythonnet/issues/514
    '''
    dims = nparray.shape
    dtype = nparray.dtype
    # For complex arrays, we must make a view of the array as its corresponding
    # float type.
    if dtype == np.complex64:
        dtype = np.dtype('float32')
        dims.append(2)
        nparray = nparray.view(np.float32).reshape(dims)
    elif dtype == np.complex128:
        dtype = np.dtype('float64')
        dims.append(2)
        nparray = nparray.view(np.float64).reshape(dims)

    cnet_dims = Array.CreateInstance(Int32, nparray.ndim)
    for I in range(nparray.ndim):
        cnet_dims[I] = Int32(dims[I])

    if not nparray.flags.c_contiguous:
        nparray = nparray.copy(order='C')
    assert nparray.flags.c_contiguous

    try:
        cnet_array = Array.CreateInstance(_MAP_NP_NET[dtype], cnet_dims)
    except KeyError:
        raise NotImplementedError(
            "asNetArray does not yet support dtype {}".format(dtype))

    try:  # Memmove
        dest_handle = GCHandle.Alloc(cnet_array, GCHandleType.Pinned)
        src_ptr = nparray.__array_interface__['data'][0]
        dest_ptr = dest_handle.AddrOfPinnedObject().ToInt64()
        ctypes.memmove(dest_ptr, src_ptr, nparray.nbytes)
    finally:
        if dest_handle.IsAllocated: dest_handle.Free()
    return cnet_array
Ejemplo n.º 13
0
def to_dotnet_array(x):
    """
    Convert numpy array to .NET array with same data type (single, double,...)

    Parameters
    ----------
    x: np.array

    Returns
    -------
    System.Array
        
    Notes
    -----
    Given a `numpy.ndarray` returns a CLR `System.Array`.  See _MAP_NP_NET for 
    the mapping of Numpy dtypes to CLR types.
    """
    dims = x.shape
    dtype = x.dtype

    netDims = System.Array.CreateInstance(System.Int32, x.ndim)
    for I in range(x.ndim):
        netDims[I] = System.Int32(dims[I])

    if not x.flags.c_contiguous:
        x = x.copy(order="C")
    assert x.flags.c_contiguous

    try:
        netArray = System.Array.CreateInstance(_MAP_NP_NET[dtype], netDims)
    except KeyError:
        raise NotImplementedError(
            "asNetArray does not yet support dtype {}".format(dtype))

    try:  # Memmove
        destHandle = GCHandle.Alloc(netArray, GCHandleType.Pinned)
        sourcePtr = x.__array_interface__["data"][0]
        destPtr = destHandle.AddrOfPinnedObject().ToInt64()
        ctypes.memmove(destPtr, sourcePtr, x.nbytes)
    finally:
        if destHandle.IsAllocated:
            destHandle.Free()
    return netArray
Ejemplo n.º 14
0
    def testCorrectOverloadSelection(self):
        """
        Test correct overloading selection for common types.
        """
        from System.Drawing import Font

        from System import (String, Double, Single, Int16, Int32, Int64)
        from System import Math

        substr = String("substring")
        self.assertTrue(
            substr.Substring(2) == substr.Substring.__overloads__[Int32](Int32(
                2)))
        self.assertTrue(
            substr.Substring(2, 3) == substr.Substring.__overloads__[
                Int32, Int32](Int32(2), Int32(3)))

        for atype, value1, value2 in zip([Double, Single, Int16, Int32, Int64],
                                         [1.0, 1.0, 1, 1, 1],
                                         [2.0, 0.5, 2, 0, -1]):
            self.assertTrue(
                Math.Abs(atype(value1)) == Math.Abs.__overloads__[atype](atype(
                    value1)))
            self.assertTrue(
                Math.Abs(value1) == Math.Abs.__overloads__[atype](atype(
                    value1)))
            self.assertTrue(
                Math.Max(atype(value1), atype(value2)) ==
                Math.Max.__overloads__[atype,
                                       atype](atype(value1), atype(value2)))
            if (atype is Int64) and six.PY2:
                value2 = long(value2)
            self.assertTrue(
                Math.Max(atype(value1), value2) == Math.Max.__overloads__[
                    atype, atype](atype(value1), atype(value2)))

        clr.AddReference("System.Runtime.InteropServices")
        from System.Runtime.InteropServices import GCHandle, GCHandleType
        from System import Array, Byte
        CSArray = Array.CreateInstance(Byte, 1000)
        handler = GCHandle.Alloc(CSArray, GCHandleType.Pinned)
Ejemplo n.º 15
0
def convert_buffer(net_array, image_format):
    src_hndl = GCHandle.Alloc(net_array, GCHandleType.Pinned)
    try:
        src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()

        # Possible data types returned from acquisition
        if (image_format == ImageDataFormat.MonochromeUnsigned16):
            buf_type = ctypes.c_ushort * len(net_array)
        elif (image_format == ImageDataFormat.MonochromeUnsigned32):
            buf_type = ctypes.c_uint * len(net_array)
        elif (image_format == ImageDataFormat.MonochromeFloating32):
            buf_type = ctypes.c_float * len(net_array)

        cbuf = buf_type.from_address(src_ptr)
        resultArray = np.frombuffer(cbuf, dtype=cbuf._type_)

    # Free the handle
    finally:
        if src_hndl.IsAllocated: src_hndl.Free()

    # Make a copy of the buffer
    return np.copy(resultArray)
Ejemplo n.º 16
0
def convert_to_numpy_array(cnet_array):
    """
    Converts CLR System.Array into numpy.ndarray.
    See _MAP_NET_NP for the mapping of CLR types to Numpy dtypes.
    Reference: https://github.com/pythonnet/pythonnet/issues/514

    Parameters
    ----------
    cnet_array : System.Array
        Array from C#

    Returns
    -------
    ndarray

    """
    dims = np.empty(cnet_array.Rank, dtype=int)
    for I in range(cnet_array.Rank):
        dims[I] = cnet_array.GetLength(I)
    cnet_type = cnet_array.GetType().GetElementType().Name

    try:
        np_array = np.empty(dims, order='C', dtype=_MAP_NET_NP[cnet_type])
    except KeyError:
        raise NotImplementedError(
            "asNumpyArray does not yet support System type {}".format(
                cnet_type))

    try:  # Memmove
        src_handle = GCHandle.Alloc(cnet_array, GCHandleType.Pinned)
        src_ptr = src_handle.AddrOfPinnedObject().ToInt64()
        dest_ptr = np_array.__array_interface__['data'][0]
        ctypes.memmove(dest_ptr, src_ptr, np_array.nbytes)
    finally:
        if src_handle.IsAllocated:
            src_handle.Free()

    return np_array
Ejemplo n.º 17
0
def asNumpyArray(x):
    """
    Convert .NET array to numpy array

    Parameters
    ----------
    x : System.Array
        
    Returns
    -------
    np.ndarray
        
    Notes
    -----
    Given a CLR `System.Array` returns a `numpy.ndarray`.  See _MAP_NET_NP for 
    the mapping of CLR types to Numpy dtypes.
    """
    dims = np.empty(x.Rank, dtype=int)
    for I in range(x.Rank):
        dims[I] = x.GetLength(I)
    netType = x.GetType().GetElementType().Name

    try:
        npArray = np.empty(dims, order="C", dtype=_MAP_NET_NP[netType])
    except KeyError:
        raise NotImplementedError(
            "asNumpyArray does not yet support System type {}".format(netType))

    try:  # Memmove
        sourceHandle = GCHandle.Alloc(x, GCHandleType.Pinned)
        sourcePtr = sourceHandle.AddrOfPinnedObject().ToInt64()
        destPtr = npArray.__array_interface__["data"][0]
        ctypes.memmove(destPtr, sourcePtr, npArray.nbytes)
    finally:
        if sourceHandle.IsAllocated:
            sourceHandle.Free()
    return npArray