Esempio n. 1
0
    def from_memoryview(cls, mview: memoryview) -> VT:
        """Create an R vector/array from a memoryview.

        The memoryview must be contiguous, and the C representation
        for the vector must be compatible between R and Python. If
        not the case, a :class:`ValueError` exception with will be
        raised."""
        if not embedded.isready():
            raise embedded.RNotReadyError('Embedded R is not ready to use.')
        if not mview.contiguous:
            raise ValueError('The memory view must be contiguous.')
        if not cls._check_C_compatible(mview):
            cls._raise_incompatible_C_size(mview)

        r_vector = None
        n = len(mview)
        with memorymanagement.rmemory() as rmemory:
            r_vector = rmemory.protect(
                openrlib.rlib.Rf_allocVector(
                    cls._R_TYPE, n)
            )
            dest_ptr = cls._R_GET_PTR(r_vector)
            src_ptr = _rinterface.ffi.from_buffer(mview)
            nbytes = n * mview.itemsize
            _rinterface.ffi.memmove(dest_ptr, src_ptr, nbytes)
        return r_vector
Esempio n. 2
0
    def from_memoryview(cls, mview: memoryview) -> VT:
        """Create an R vector/array from a memoryview.

        The memoryview must be contiguous, and the C representation
        for the vector must be compatible between R and Python. If
        not the case, a :class:`ValueError` exception with will be
        raised."""
        if not embedded.isready():
            raise embedded.RNotReadyError('Embedded R is not ready to use.')
        if not mview.contiguous:
            raise ValueError('The memory view must be contiguous.')
        if mview.itemsize != cls._R_SIZEOF_ELT:
            msg = (
                'Incompatible C type sizes. '
                'The R array type is {r_size} bytes '
                'while the Python array type is {py_size} '
                'bytes.'
                .format(r_size=cls._R_SIZEOF_ELT,
                        py_size=mview.itemsize)
            )
            raise ValueError(msg)
        r_vector = None
        n = len(mview)
        with memorymanagement.rmemory() as rmemory:
            r_vector = rmemory.protect(
                openrlib.rlib.Rf_allocVector(
                    cls._R_TYPE, n)
            )
            dest_ptr = cls._R_GET_PTR(r_vector)
            src_ptr = _rinterface.ffi.from_buffer(mview)
            nbytes = n * mview.itemsize
            _rinterface.ffi.memmove(dest_ptr, src_ptr, nbytes)
        return r_vector
Esempio n. 3
0
 def from_iterable(cls,
                   iterable,
                   populate_func=None,
                   set_elt=None,
                   cast_value=None) -> VT:
     """Create an R vector/array from an iterable."""
     if not embedded.isready():
         raise embedded.RNotReadyError('Embedded R is not ready to use.')
     if populate_func is None:
         populate_func = _populate_r_vector
     if set_elt is None:
         set_elt = cls._R_SET_VECTOR_ELT
     if cast_value is None:
         cast_value = cls._CAST_IN
     n = len(iterable)
     with memorymanagement.rmemory() as rmemory:
         r_vector = rmemory.protect(
             openrlib.rlib.Rf_allocVector(cls._R_TYPE, n))
         populate_func(iterable, r_vector, set_elt, cast_value)
     return r_vector