The following code gives me a compile time error : import arraymancer import nimpy import nimpy/raw_buffers proc `+`[T](p: ptr T, val: int) : ptr T {.inline.}= cast[ptr T](cast[uint](p) + cast[uint](val * sizeof(T))) proc pyBufToTensor[T](ndArray: PyObject): Tensor[T]= # Convert PyObject to RawPyBuffer var aBuf: RawPyBuffer ndArray.getBuffer(aBuf, PyBUF_WRITABLE or PyBUF_ND) aBuf.release() # Copy buffer into Tensor var shape: seq[int] for i in 0..<aBuf.ndim: shape.add((aBuf.shape+i)[]) # Get memory asdress of buffer var bufPtr = cast[ptr UncheckedArray[T]](aBuf.buf) # Get underlying data buffer of Tensor result = newTensor[T](shape) var tensorDataPtr = cast[ptr UncheckedArray[T]](result.get_data_ptr) # Copy Buffer into Tensor var length = shape.foldl(a*b) copyMem(tensorDataPtr, bufPtr, length*sizeof(T)) let np = pyImport("numpy") proc nimfftshift[T](t: Tensor[T]): Tensor[T]= ## Reshape PyObject to Arraymancer Tensor #var ndArray = wrappy.w_fftshift(t.toSeq, t.shape.toSeq) var shape = np.array(t.shape.toSeq) var ndArray = np.array(t.toSeq) ndArray = np.reshape(ndArray, shape) ndArray = np.fft.fftshift(ndArray) # Convert RawPyBuffer to Tensor result = pyBufToTensor[T](ndArray) Run
Error message : but expected one of: proc reshape(t: Tensor; new_shape: MetadataArray): Tensor first type mismatch at position: 1 required type for t: Tensor but expression 'np' is of type: PyObject proc reshape(t: Tensor; new_shape: varargs[int]): Tensor first type mismatch at position: 1 required type for t: Tensor but expression 'np' is of type: PyObject proc reshape[TT](a: Variable[TT]; shape: MetadataArray): Variable[TT] first type mismatch at position: 1 required type for a: Variable[reshape.TT] but expression 'np' is of type: PyObject proc reshape[TT](a: Variable[TT]; shape: varargs[int]): Variable[TT] first type mismatch at position: 1 required type for a: Variable[reshape.TT] but expression 'np' is of type: PyObject expression: reshape(np, ndArray, shape) Run > My plan was to introduce zero-copy interop with Numpy That would be awesome !