Commit Graph

10020 Commits (6bf7ea23cfe732eeba5ec68ce86a1d32fe852ea6)

Author SHA1 Message Date
Duy-Nguyen Ta 6bf7ea23cf convert numpy input params to dtype float and order 'F' automatically
using numpy.astype(...). No copy if the params are already in the correct dtype and storage order.

For a function
    f(Matrix A, Matrix B),
simply wrapping it to pyx as
   f(A.astype(float, order='F', copy=False), B.astype(float, order='F', copy=False))
won't work.
It produces a strange side-effect that the content of A is overwritten by B and the two inputs are the same (data address) inside the function!

This is because Cython decreases the ref count for the temporary variable resulted from A.astype(...) before generates the wrap for B.astype(...).
Hence, the A.astype temp var is probably reused for B.astype, and they were pointing to the same data address.

For that reason, we have to go a longer route and wrap it as:
  A = A.astype(float, order='F', copy=False)
  B = B.astype(float, order='F', copy=False)
  f(A, B)

For future ref., here is a sample of the wrongly generated code that wraps the JacobianFactor constructor:
Jacobian(Key i1, Matrix A1, Key i2, Matrix A2, Vector b, noiseModel::Diagonal model)

Wrongly wrapped pyx code:
self.shared_CJacobianFactor_ = shared_ptr[CJacobianFactor](new CJacobianFactor(i1, <MatrixXd>(Map[MatrixXd](A1.astype(float, order='F',copy=False)), i2, <MatrixXd>(Map[MatrixXd](A2.astype(float, order='F', copy=False)), <VectorXd>(Map[VectorXd](b.astype(float, order='F', copy=False))), model.shared_CnoiseModel_Diagonal_))

The problematic Cython generated CPP code with a comment on the problematic line:
/////////////////////////////////////////
// WRONG VERSION
/////////////////////////////////////////
  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_A1), __pyx_n_s_astype); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_12);
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_INCREF(((PyObject *)(&PyFloat_Type)));
  __Pyx_GIVEREF(((PyObject *)(&PyFloat_Type)));
  PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)(&PyFloat_Type)));
  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_5);
  if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_order, __pyx_n_s_F) < 0) __PYX_ERR(0, 2107, __pyx_L1_error)
  if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_copy, Py_False) < 0) __PYX_ERR(0, 2107, __pyx_L1_error)
  __pyx_t_13 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_4, __pyx_t_5); if (unlikely(!__pyx_t_13)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_13);
  __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  if (!(likely(((__pyx_t_13) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_13, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 2107, __pyx_L1_error)
  try {
    __pyx_t_14 = eigency::Map<Eigen::MatrixXd> (((PyArrayObject *)__pyx_t_13));
  } catch(...) {
    __Pyx_CppExn2PyErr();
    __PYX_ERR(0, 2107, __pyx_L1_error)
  }
///////////////////////////////////////////////
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;  	//<------- Problematic line!!! Killing this will result in the correct result!
///////////////////////////////////////////////
  __pyx_t_13 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_A2), __pyx_n_s_astype); if (unlikely(!__pyx_t_13)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_13);
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_INCREF(((PyObject *)(&PyFloat_Type)));
  __Pyx_GIVEREF(((PyObject *)(&PyFloat_Type)));
  PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)(&PyFloat_Type)));
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_4);
  if (PyDict_SetItem(__pyx_t_4, __pyx_n_s_order, __pyx_n_s_F) < 0) __PYX_ERR(0, 2107, __pyx_L1_error)
  if (PyDict_SetItem(__pyx_t_4, __pyx_n_s_copy, Py_False) < 0) __PYX_ERR(0, 2107, __pyx_L1_error)
  __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_13, __pyx_t_5, __pyx_t_4); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_12);
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_12) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_12, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 2107, __pyx_L1_error)
  try {
    __pyx_t_15 = eigency::Map<Eigen::MatrixXd> (((PyArrayObject *)__pyx_t_12));
  } catch(...) {
    __Pyx_CppExn2PyErr();
    __PYX_ERR(0, 2107, __pyx_L1_error)
  }
  __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_b), __pyx_n_s_astype); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_12);
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_INCREF(((PyObject *)(&PyFloat_Type)));
  __Pyx_GIVEREF(((PyObject *)(&PyFloat_Type)));
  PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)(&PyFloat_Type)));
  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_5);
  if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_order, __pyx_n_s_F) < 0) __PYX_ERR(0, 2107, __pyx_L1_error)
  if (PyDict_SetItem(__pyx_t_5, __pyx_n_s_copy, Py_False) < 0) __PYX_ERR(0, 2107, __pyx_L1_error)
  __pyx_t_13 = __Pyx_PyObject_Call(__pyx_t_12, __pyx_t_4, __pyx_t_5); if (unlikely(!__pyx_t_13)) __PYX_ERR(0, 2107, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_13);
  __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  if (!(likely(((__pyx_t_13) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_13, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 2107, __pyx_L1_error)
  try {
    __pyx_t_16 = eigency::Map<Eigen::VectorXd> (((PyArrayObject *)__pyx_t_13));
  } catch(...) {
    __Pyx_CppExn2PyErr();
    __PYX_ERR(0, 2107, __pyx_L1_error)
  }
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
  try {
    __pyx_t_17 = new gtsam::JacobianFactor(__pyx_v_i1, ((Eigen::MatrixXd)__pyx_t_14), __pyx_v_i2, ((Eigen::MatrixXd)__pyx_t_15), ((Eigen::VectorXd)__pyx_t_16), __pyx_v_model->shared_CnoiseModel_Diagonal_);
  } catch(...) {
    __Pyx_CppExn2PyErr();
    __PYX_ERR(0, 2107, __pyx_L1_error)
  }
  __pyx_v_self->shared_CJacobianFactor_ = boost::shared_ptr<gtsam::JacobianFactor> (__pyx_t_17);
2017-03-15 13:47:11 -04:00
Duy-Nguyen Ta 0e278f81c6 remove Vectorize, simplify to just numpy.squeeze 2017-03-10 23:33:14 -05:00
Duy-Nguyen Ta 89bc31d703 fix comment 2017-03-10 23:28:26 -05:00
Duy-Nguyen Ta dc7792d350 unify/rename functions: matlab_code and cython_wrapper to generate_xxxxx_wrapper 2017-03-10 23:27:29 -05:00
Duy-Nguyen Ta b7efaf8c3f special ctor signature to be used with cyCreateFromShared
so that calling the default ctor by mistake on a class without the default ctor will respond nicely with an exception instead of a seg-fault
2017-03-08 15:22:16 -05:00
Duy-Nguyen Ta c9666a1b44 fix merge problem
Argument::isScalar() was moved to Qualified and should be checked via Argument::type
2017-03-08 15:15:37 -05:00
Duy-Nguyen Ta e2abfe256d remove requirements.txt
@dellaert: not needed for only 2 packages and an exception for eigency
2017-03-08 10:59:06 -05:00
Duy-Nguyen Ta 98e9ffdce4 Revert "[mEstimator] virtualize and implementing sqrtWeight instead of weight to speed up a bit"
This reverts commit 9187b47432.
2017-03-08 10:32:11 -05:00
Duy-Nguyen Ta a1c828c8bb Revert "split M-Estimators out from NoiseModel"
This reverts commit afb6c37630.
2017-03-08 10:31:53 -05:00
Duy-Nguyen Ta 456b4c5aed Revert "fix include"
This reverts commit cdaf928ecf.
2017-03-08 10:31:18 -05:00
Duy-Nguyen Ta 482d542da1 Revert "correct name for Welsch"
This reverts commit df8900a3d1.
2017-03-08 10:31:05 -05:00
Duy-Nguyen Ta c3b11af61e remove unfinished cython-wrap test prototype 2017-03-08 10:05:35 -05:00
Duy-Nguyen Ta d8e9271dd1 fix test 2017-03-08 10:03:27 -05:00
Duy-Nguyen Ta 68e0defa49 Merge branch 'develop' into feature/cython_wrapper 2017-03-08 09:51:15 -05:00
Chris Beall 2a7c64d4a8 Merged in fix/python_hide_library_rebased (pull request #288)
[python] Make python library hidden by renaming gtsampy.so to _gtsampy.so
2017-03-08 07:10:45 +00:00
Ellon Mendes 5482f1f5eb [python] Make python library hidden by renaming gtsampy.so to _gtsampy.so
This commit also fixes a naming problem of the python .so module
(_libgtsam_python.so -> _gtsampy.so)
2017-03-07 15:24:36 -08:00
Jing Dong 3095fb7e1f Merged in feature/ProductLieGroupJacobians (pull request #285)
Minor fixes of gtsam::traits

Approved-by: Chris Beall
2017-03-07 23:16:52 +00:00
Duy-Nguyen Ta 5a8bd5afda [cython] bypass a problem with no default constructor
Add this to support cyCreateFromShared, which needs to call the default Python constructor to construct the Python object before reassigning the internal shared ptr to the c++ object.
2017-03-06 01:18:19 -05:00
Duy-Nguyen Ta ed8f7c5f82 [cython] remove copy constructor requirement
Using make_shared[C](other) instead of shared_ptr[C](new C(other)) to leverage the implicit default constructor inside C++
2017-03-06 01:06:53 -05:00
Frank Dellaert 687ae3d251 Merged in fix/matlab_wrapper_scalar_references (pull request #287)
Fixed unwrapping of scalar references.
2017-01-20 02:14:36 +00:00
Simon Julier 6a109aca9b Throw an exception rather than call exit. 2017-01-20 01:58:59 +00:00
Simon Julier d1422ac921 Reverted change to make files to make the pull request clean. 2017-01-19 09:28:04 +00:00
Simon Julier d8d7c5618a Generate an error and exit if trying to wrap a non-const scalar reference. 2017-01-19 01:49:12 +00:00
Simon Julier ca9d175ad4 Merge branch 'develop' into fix/matlab_wrapper_scalar_references 2017-01-19 01:32:38 +00:00
Frank Dellaert b830b4473f Merged in feature/matlab_iostream_redirection (pull request #286)
Feature/matlab iostream redirection
2017-01-18 12:55:24 -08:00
Simon Julier f802099bfd Tidied up the text to make it a bit clearer / less ambiguous.y 2017-01-18 19:13:25 +00:00
Simon Julier 2a4aa76e7e Added instructions on the iostream and the wrapper. Also added instructions on how to enable the toolbox.y 2017-01-18 18:58:48 +00:00
Simon Julier f50c3c0d51 Use INSTALL_NAME_DIR to embed names in the dylibs and avoid linker errors.y 2017-01-17 16:53:28 +00:00
Simon Julier 21aa7a2e85 Fixed unrwapping of scalar references. 2017-01-17 10:12:00 +00:00
Duy-Nguyen Ta ff75d63876 Removing constness causes problems with some no-default-constructor classes. Add dummy default constructors or expose them to public for Cython wrapper only.
Maybe a Cython bug? Both object and pointer object appeared next to each other in the generated cpp file, e.g.
   JointMarginal p0;
   JointMarginal* p1;
With correct constness, only the pointer object shows up.

Maybe related: https://groups.google.com/forum/#!topic/cython-users/HB5yxgKQ6wc
2016-12-19 18:24:02 -05:00
Duy-Nguyen Ta 189ce33e1d Support exceptions so ipython/python can catch and doesn't crash. Trade constness with exception since Cython doesn't allow both.
See: http://stackoverflow.com/questions/26904268/cython-both-const-and-except-in-c-method-declaration
2016-12-19 17:53:14 -05:00
Duy-Nguyen Ta d9d97c4bc7 Forward declare not only classes but their inheritance
This is needed for wrapping to Cython another project based on gtsam. The current scheme requires information about all parent classes. See updated comments in gtsam.h.
2016-12-19 17:47:30 -05:00
Duy-Nguyen Ta b55f7b1fa4 remove unused argument 2016-12-19 17:30:29 -05:00
Duy-Nguyen Ta 05a76164d3 forward declaration of ForwardDeclaration 2016-12-16 19:26:40 -05:00
Duy-Nguyen Ta da8a8a3bb0 remove unused argument 2016-12-16 19:24:49 -05:00
Duy-Nguyen Ta 7c5db5e90f update README 2016-12-16 14:33:08 -05:00
Duy-Nguyen Ta 7abcdb1b45 reorganize script folders: more reasonable packaging 2016-12-16 14:17:15 -05:00
Duy-Nguyen Ta 126de1b8a4 revert usage info: interfacePath must be absolute. 2016-12-16 00:34:07 -05:00
Duy-Nguyen Ta a7c1c89c7b update short test version 2016-12-16 00:27:32 -05:00
Duy-Nguyen Ta c34349bb7c Update README, showing how to wrap other projects using gtsam 2016-12-16 00:26:52 -05:00
Duy-Nguyen Ta 70552e9f6d improve cmake Cython wrapper scripts to be usable in other projects 2016-12-16 00:26:03 -05:00
Duy-Nguyen Ta f154be176f Major update to generate proper Cython pxd header files which could be included in other projects/modules
All cdef (class, functions, variables) declarations are moved to pxd. Implementations of those cdefs and normal Python def are in pyx.
See: http://cython.readthedocs.io/en/latest/src/userguide/sharing_declarations.html#sharing-extension-types
2016-12-16 00:23:45 -05:00
Jing Dong c0c55c4a21 Merged develop into feature/ProductLieGroupJacobians 2016-12-08 15:21:56 -05:00
Jing Dong 5fa4abf99c fix optional jacobians 2016-12-08 15:19:18 -05:00
Duy-Nguyen Ta f3bf89b463 print for PreintegrationParams 2016-12-05 11:01:03 -05:00
Duy-Nguyen Ta 0cef864663 __cinit__ --> __init__ 2016-12-05 11:00:33 -05:00
Duy-Nguyen Ta 427d88ed5b test -> tests 2016-11-30 05:59:03 -05:00
Duy-Nguyen Ta 21fa3f07e9 basic experiments for testing the wrapper 2016-11-30 05:58:23 -05:00
Duy-Nguyen Ta 1e425536bb squeeze extra dims of numpy vectors so no need ravel. 2016-11-30 05:57:12 -05:00
Duy-Nguyen Ta 4439968f05 tabs to spaces 2016-11-30 05:56:07 -05:00