Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minimize copies in eigen to numpy Conversion #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions rednose/helpers/ekf_sym_pyx.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ cdef extern from "rednose/helpers/ekf_sym.h" namespace "EKFS":

@cython.wraparound(False)
@cython.boundscheck(False)
cdef np.ndarray[np.float64_t, ndim=2, mode="c"] matrix_to_numpy(MatrixXdr arr):
cdef np.ndarray[np.float64_t, ndim=2, mode="c"] matrix_to_numpy(MatrixXdr &arr):
cdef double[:,:] mem_view = <double[:arr.rows(),:arr.cols()]>arr.data()
return np.copy(np.asarray(mem_view, dtype=np.double, order="C"))

@cython.wraparound(False)
@cython.boundscheck(False)
cdef np.ndarray[np.float64_t, ndim=1, mode="c"] vector_to_numpy(VectorXd arr):
cdef np.ndarray[np.float64_t, ndim=1, mode="c"] vector_to_numpy(VectorXd &arr):
cdef double[:] mem_view = <double[:arr.rows()]>arr.data()
return np.copy(np.asarray(mem_view, dtype=np.double, order="C"))

Expand Down Expand Up @@ -120,11 +120,13 @@ cdef class EKF_sym_pyx:
)

def state(self):
cdef np.ndarray res = vector_to_numpy(self.ekf.state())
cdef VectorXd state = self.ekf.state()
cdef np.ndarray res = vector_to_numpy(state)
return res

def covs(self):
return matrix_to_numpy(self.ekf.covs())
cdef MatrixXdr covs = self.ekf.covs()
return matrix_to_numpy(covs)

def set_filter_time(self, double t):
self.ekf.set_filter_time(t)
Expand Down Expand Up @@ -167,14 +169,15 @@ cdef class EKF_sym_pyx:
return None

cdef VectorXd tmpvec
cdef Estimate* est = &res.value()
return (
vector_to_numpy(res.value().xk1),
vector_to_numpy(res.value().xk),
matrix_to_numpy(res.value().Pk1),
matrix_to_numpy(res.value().Pk),
res.value().t,
res.value().kind,
[vector_to_numpy(tmpvec) for tmpvec in res.value().y],
vector_to_numpy(est.xk1),
vector_to_numpy(est.xk),
matrix_to_numpy(est.Pk1),
matrix_to_numpy(est.Pk),
est.t,
est.kind,
[vector_to_numpy(tmpvec) for tmpvec in est.y],
z, # TODO: take return values?
extra_args,
)
Expand Down