LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
arma_npy.h
Go to the documentation of this file.
1#pragma once
2#include "lasp_mathtypes.h"
3#include <cstring>
4#include <pybind11/numpy.h>
5#include <type_traits>
6
7namespace py = pybind11;
8
9template<typename T>
10using pyarray = py::array_t<T, py::array::f_style | py::array::forcecast>;
13
14
23template <typename T> py::array_t<T> ColToNpy(const arma::Col<T> &data) {
24
25 const us Tsize = sizeof(T);
26
27 const auto nrows = static_cast<ssize_t>(data.n_rows);
28
29 const us size = nrows;
30
31 py::array_t<T> result({nrows}, {Tsize});
32
33 // Copy over memory
34 if (size > 0) {
35 memcpy(result.mutable_data(0), data.memptr(), sizeof(T) * nrows);
36 }
37
38 return result;
39}
40
49template <typename T> py::array_t<T> MatToNpy(const arma::Mat<T> &data) {
50
51 const us Tsize = sizeof(T);
52
53 const auto nrows = static_cast<ssize_t>(data.n_rows);
54 const auto ncols = static_cast<ssize_t>(data.n_cols);
55
56 const us size = nrows;
57
58 py::array_t<T> result({nrows, ncols}, {Tsize, nrows * Tsize});
59
60 // Copy over memory
61 if (size > 0) {
62 memcpy(result.mutable_data(0, 0), data.memptr(), sizeof(T) * nrows * ncols);
63 }
64
65 return result;
66}
67
76template <typename T> py::array_t<T> CubeToNpy(const arma::Cube<T> &data) {
77
78 const us Tsize = sizeof(T);
79
80 const auto nrows = static_cast<ssize_t>(data.n_rows);
81 const auto ncols = static_cast<ssize_t>(data.n_cols);
82 const auto nslices = static_cast<ssize_t>(data.n_slices);
83
84 const us size = nrows * ncols * nslices;
85
86 py::array_t<T> result({nrows, ncols, nslices},
87 {Tsize, nrows * Tsize, nrows * ncols * Tsize});
88
89 // Copy over memory
90 if (size > 0) {
91 memcpy(result.mutable_data(0, 0, 0), data.memptr(),
92 sizeof(T) * nrows * ncols * nslices);
93 }
94
95 return result;
96}
97
99
109template <typename T, bool copy = true>
110arma::Mat<T> NpyToMat(pyarray<T> data) {
111 if (data.ndim() != 2) {
112 throw std::runtime_error("Expects a 2D array");
113 }
114 return arma::Mat<T>(data.mutable_data(0,0), data.shape(0), data.shape(1), copy);
115}
116
126template <typename T, bool copy = true>
127arma::Mat<T> NpyToCol(pyarray<T> data) {
128 if (data.ndim() != 1) {
129 throw std::runtime_error("Expects a 1D array");
130 }
131 return arma::Col<T>(data.mutable_data(0), data.size(), copy);
132}
133
py::array_t< T > ColToNpy(const arma::Col< T > &data)
Convert Armadillo column vector to Numpy 1D array.
Definition arma_npy.h:23
py::array_t< T > CubeToNpy(const arma::Cube< T > &data)
Convert Armadillo Cube to Numpy 3D array.
Definition arma_npy.h:76
pyarray< d > dpyarray
Definition arma_npy.h:11
py::array_t< T > MatToNpy(const arma::Mat< T > &data)
Convert Armadillo 2D array Numpy 2D array.
Definition arma_npy.h:49
arma::Mat< T > NpyToCol(pyarray< T > data)
Wrap Numpy array to 1D Armadillo column vector.
Definition arma_npy.h:127
arma::Mat< T > NpyToMat(pyarray< T > data)
BACK converters.
Definition arma_npy.h:110
py::array_t< T, py::array::f_style|py::array::forcecast > pyarray
Definition arma_npy.h:10
pyarray< c > cpyarray
Definition arma_npy.h:12
size_t us
We often use boolean values.
Definition lasp_types.h:29