Commit Graph

68 Commits (74a33ff222ba30114b3e0fb4289ecbb751d1b519)

Author SHA1 Message Date
dellaert 74a33ff222 Re-structured argument overloading to call a common function 2017-08-06 11:07:13 -07:00
Duy-Nguyen Ta 52f54d07bd remove blank lines 2017-03-15 22:45:48 -04: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 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 68e0defa49 Merge branch 'develop' into feature/cython_wrapper 2017-03-08 09:51:15 -05:00
Simon Julier 6a109aca9b Throw an exception rather than call exit. 2017-01-20 01:58:59 +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 21aa7a2e85 Fixed unrwapping of scalar references. 2017-01-17 10:12:00 +00: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 e407a42160 Merge branch 'develop' into feature/cython_wrapper
# Conflicts:
#	wrap/Module.cpp
2016-11-25 03:43:36 -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 fbcb9041f2 big refactoring, support method/static method overloading 2016-11-20 09:24:43 -05:00
Duy-Nguyen Ta acf3c9d259 proper overloading constructors 2016-11-16 17:51:03 -05:00
Duy-Nguyen Ta 435870ebf0 remove extra Xd of Eigen types in Argument and ReturnType 2016-09-14 07:44:38 -04:00
Duy-Nguyen Ta 1b04c6713b handle "This". Wrap all geometry 2016-09-11 18:14:19 -04:00
Duy-Nguyen Ta b91a7d368d fix Vector/Matrix and Map[Vector/Matrix] ambiguity.
That also fixes the problem of function templated on Matrix/Vector
2016-09-11 08:24:45 -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 1e84da1cfa pyx: add constructors and fixing inheritance 2016-09-09 15:52:44 -04:00
Duy-Nguyen Ta ecde851d8c [inprogress] cython wrapper 2016-09-08 13:33:32 -04:00
Ryan Estep d1cdafa3f5 Removed the boost::regex include (not used) from the matlab wrapper & removed any linking to boost::regex 2016-08-29 13:03:46 +12:00
Yao Chen d1ea1015a9 Replaced BOOSE_FOREACH with for in wrap folder. Tested the changed code locally: successful. 2016-05-20 21:41:18 -04:00
lvzhaoyang bcfcf8be8e fix gtsam wrapper for priorFactorVector 2014-12-12 11:34:34 -05:00
dellaert 6d916c6b75 Semi-private name/namespaces 2014-11-30 20:20:13 +01:00
dellaert b12255285b More clear than operator overload 2014-11-30 00:13:29 +01:00
dellaert 8d9e108acc Check Vector by checking size 2014-11-29 21:43:48 +01:00
dellaert 370f2c6763 Isolated argument check 2014-11-29 21:11:13 +01:00
dellaert 0eaabd700b Refactored emit call 2014-11-29 20:53:38 +01:00
dellaert 482dbd9226 Made TemplateSubstitution into an operator, and added stream operator 2014-11-13 19:34:25 +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 7d4f5a4820 Make explicit whether wrapper or proxy is written to... 2014-11-12 20:51:47 +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 5987f78be3 Methods to check whether arguments are scalar 2014-05-25 15:21:13 -04:00
dellaert 9d9614d185 Split up into two calls 2014-05-25 14:59:20 -04:00
dellaert 52cd200718 ReturnValue now emits, eliminated some copy/paste. Also removed unused verbose field/argument in ReturnValue 2014-05-25 14:53:32 -04:00
dellaert 406419317f eliminated more copy/paste mess between Method and StaticMethod 2014-05-25 14:35:07 -04:00
dellaert 4403d47865 Slight refactor 2014-05-25 13:12:48 -04:00
dellaert 80b7d91264 emit prototype method (to eliminate horrible copy/paste mess someone thought was good programming style) 2014-05-25 13:01:36 -04:00
Chris Beall 4297d24c96 changed tabs to spaces for consistent indentation in all of GTSAM 2012-10-02 14:40:07 +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 3c27daae18 wrap_mods_inheritance branch: in progress with inheritance in matlab wrapper 2012-07-08 12:27:39 +00:00
Andrew Melim ead88ae35a Changes to wrap requested by frank. Wrap now allows for multiple includes. Changes to wrap tests 2012-07-02 18:18:11 +00:00
Andrew Melim 6d776812d3 new wrap! :) 2012-06-26 18:52:27 +00:00
Frank Dellaert d13db7a997 Fixed wrap problem where a double could not be passed to a method taking a size_t. Also, if a method takes a bool, you now *have* to pass a logical in matlab (i.e., true or false) 2012-06-13 12:03:33 +00:00
Richard Roberts 90578e2532 Merge remote-tracking branch 'svn/trunk' into windows
Conflicts:
	gtsam.h
	wrap/CMakeLists.txt
2012-06-05 13:11:34 +00:00
Alex Cunningham a07e4a7368 Fixed handling of char in wrap, updated README 2012-06-04 19:45:32 +00:00
Alex Cunningham 73b4f35e02 Fixed matlab issue with unsigned char types 2012-06-04 19:23:25 +00:00