Commit Graph

65 Commits (7cfc5c352254b5bbc710f3e1d5b1f9bf19cfd84f)

Author SHA1 Message Date
Duy-Nguyen Ta 1cdc228d6a remove trailing spaces 2019-02-11 10:58:34 -05:00
dellaert 2374347e69 Make internal, overloaded static methods cdefs to avoid call code/overhead 2017-08-06 13:25:54 -07:00
dellaert 74a33ff222 Re-structured argument overloading to call a common function 2017-08-06 11:07:13 -07:00
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 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 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
Duy-Nguyen Ta 4439968f05 tabs to spaces 2016-11-30 05:56:07 -05:00
Duy-Nguyen Ta 5958b2397c resolve overloads via type checking, simplify Values's insert and update, more friendly Matrix and Vector utils
Keyword arguments are not needed anymore
2016-11-29 11:58:22 -05:00
Duy-Nguyen Ta 74f80fea4f [refactor] more understandable function names
Clearing confusions between pxd and pyx classes and objects!
2016-11-22 12:13:33 -05:00
Duy-Nguyen Ta 52a85f23f8 fix bugs on returned values 2016-11-21 17:14:30 -05:00
Duy-Nguyen Ta fbcb9041f2 big refactoring, support method/static method overloading 2016-11-20 09:24:43 -05:00
Duy-Nguyen Ta 1b04c6713b handle "This". Wrap all geometry 2016-09-11 18:14:19 -04:00
Duy-Nguyen Ta fabfac65f4 unify and simplify function call in Method and StaticMethod 2016-09-11 17:14:06 -04:00
Duy-Nguyen Ta 63a5d1e15a wrap pair. Improve return. 2016-09-11 16:40:09 -04:00
Duy-Nguyen Ta 948e6262db first version ever compiled.
... Not without some changes:
- add traits<size_t> in Key.h
- add these to JacobianFactor:
    explicit JacobianFactor(const Eigen::Map<Vector>& b_in);
    Vector py_getb() { return getb(); }
    Matrix py_getA() { return getA(); }
---------
... Remaining corner cases:
 ☐ Eigency: Map[] to Block
 ☐ Eigency: ambiguous call: A(const T&) A(const Vector& v) and Eigency A(Map[Vector]& v)
 ☐ Fix return properly
   ☐ handle pair
 ☐ Fix method template of Vector/Matrix: template argument is [Vector] while arugment is Map[Vector]
 ☐ Constructor: generate default constructor? (hack: if it's serializable?)
 ☐ Constructor: ambiguous construct from Vector/Matrix
 ☐ Key and size_t: traits<size_t> doesn't exist
 ☐ [Nice to have] Auto delete duplicate methods in derived class
2016-09-10 19:50:12 -04:00
Duy-Nguyen Ta 10f510119a pyx class methods with arguments/return type casting 2016-09-09 18:37:48 -04:00
Duy-Nguyen Ta 56c0d2a65e pyx wrapper for static methods 2016-09-09 16:39:47 -04:00
Duy-Nguyen Ta 2c52928d57 pxd staticmethod 2016-09-09 07:09:39 -04:00
dellaert 72fe66d468 Removed headers 2016-05-22 14:22:36 -07:00
dellaert 14ef786dfe Removing empty in favor of boost::optional 2014-11-30 10:38:24 +01:00
dellaert b0fa5ce684 Cut out unused arguments 2014-11-29 19:34:46 +01:00
dellaert 6c0439f6ea Method and StaticMethod now inherit from MethodBase - much better 2014-11-29 15:31:29 +01:00
dellaert 6c24fc2aca Python prototype 2014-11-14 17:47:25 +01:00
dellaert 7a4748d3dc Simplified method/function hierarchy drastically, and renamed bottom addOverload to initializeOrCheck to reflect what it does. Also, gratuitous re-ordering of addOverload arguments. 2014-11-14 16:44:08 +01:00
dellaert 67bc951ac2 Fixed proxy files and calls for static methods 2014-11-13 23:21:05 +01:00
dellaert e07da1c82d Added matlabName, and made data members private 2014-11-13 22:43:29 +01:00
dellaert 8a05136ca0 Fixed proxy wrapper name 2014-11-13 22:15:36 +01:00
dellaert fe481dc775 typedef to cope with abundance of strings 2014-11-13 18:58:44 +01:00
dellaert b451e97f6f New TemplateSubstitution object simplifies a lot 2014-11-13 17:28:05 +01:00
dellaert a5e0adb7e6 Made methods and global functions derive from Function 2014-11-13 12:52:41 +01:00
dellaert 0a23529032 Everything compiles 2014-11-12 14:37:08 +01:00
dellaert 77935bd631 Massive edit: new Qualified type groups namespaces with name, eliminates a lot of clutter. 2014-11-12 02:49:23 +01:00
dellaert 406419317f eliminated more copy/paste mess between Method and StaticMethod 2014-05-25 14:35:07 -04:00
dellaert 95b85e8494 Now using ArgumentList::emit_prototype everywhere, for non copy/paste code 2014-05-25 13:22:10 -04:00
dellaert 82d6bae4b9 Standard BORG formatting 2014-05-25 12:43:19 -04:00
Richard Roberts 547323cc79 Fixed a bunch of compiler warnings 2013-04-05 21:34:04 +00:00
Chris Beall 4297d24c96 changed tabs to spaces for consistent indentation in all of GTSAM 2012-10-02 14:40:07 +00:00
Andrew Melim 84a806bf39 Final fixes made for wrap comments 2012-08-29 20:27:10 +00:00
Andrew Melim d0a1e662a7 Wrap comments are now EVEN better 2012-08-28 21:44:45 +00:00
Andrew Melim a6545b4e01 Fixed wrap tests and changed comments 2012-08-27 17:54:25 +00:00
Andrew Melim c3e615b2df Added doxygen info to wrap commenting system 2012-08-27 15:30:47 +00:00
Andrew Melim 6da48bc580 Even more comment improvements for wrap 2012-08-27 15:19:55 +00:00
Richard Roberts 0b08923c41 Generated code formatting fix 2012-07-23 21:27:38 +00:00
Alex Cunningham 656f573c0a Removed from wrap the use of "using namespace xxx" statements - wasn't fully supported before, and now we have real namespace support 2012-07-23 18:24:43 +00:00
Alex Cunningham 79c9bc99ff Some additional cleanup in wrap 2012-07-23 18:24:39 +00:00
Alex Cunningham b7c2177f0b Generating code for global functions now works 2012-07-23 18:24:35 +00:00
Richard Roberts 8dbffd4629 Wrap generates Matlab namespaces, so now 'import gtsam.*' allows class names like Values, NonlinearFactor, to be used. Without import, syntax is gtsam.Values, etc. 2012-07-18 15:47:06 +00:00
Richard Roberts ce12f3d255 Code cleanup and comments 2012-07-12 22:28:28 +00:00
Richard Roberts e915e666b5 Can return abstract base classes from functions in matlab wrapper, i.e. Values::at 2012-07-09 20:19:37 +00:00
Richard Roberts f774a380ec Implemented method overloading in matlab wrapper, made static functions static in matlab classes 2012-07-05 14:05:00 +00:00