Commit Graph

95 Commits (98ed4d78502fdc746a68024a0e0af25b0202e326)

Author SHA1 Message Date
Frank Dellaert 98ed4d7850 Only create typedef to SharedXXX where really needed. 2019-03-19 12:09:00 -04:00
Frank Dellaert edb94a6e93 Better error message. 2019-01-20 16:52:44 -05:00
Duy-Nguyen Ta d802255eb3 support pythonic print for gtsam objects. 2018-10-17 05:40:57 -04:00
Frank Dellaert 4188a739ec Fixed overloaded methods/constructors 2017-12-02 18:43:18 -08:00
Duy-Nguyen Ta d23b5e4cfd only catch AssertionError exceptions when handling overloads, so that other C++ exceptions can be raised correctly 2017-08-25 11:18:16 -07:00
dellaert 3bbea0f301 Make pxd a bit nicer to read 2017-08-06 17:26:12 -07:00
dellaert 905aac29f8 Only define as many return values as needed 2017-08-06 17:15:47 -07:00
dellaert 81c15d950a Removed kwargs overhead for overloaded methods 2017-08-06 16:53:04 -07:00
dellaert 74a33ff222 Re-structured argument overloading to call a common function 2017-08-06 11:07:13 -07:00
Duy-Nguyen Ta c0dd740d12 correct indentation for __str__ 2017-07-27 22:32:27 -04:00
Duy-Nguyen Ta b1071b08a0 redirect stdcout to a stringstream so Python __str__() can properly return a string after calling the c++ print function
This is to avoid a printing issue in some Python's IDE when __str__() is called to update objects' values in Debug mode.
2017-07-25 16:32:26 -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 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 c13b964777 standardize names for classes with inner namespace 2016-11-24 19:22:44 -05:00
Duy-Nguyen Ta 338c73669e support global functions (no overload) 2016-11-22 17:09:35 -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 dc185a6d30 support python print for classes with print_ function 2016-11-16 17:37:33 -05:00
Duy-Nguyen Ta 814abcb67c print_() insteads of _print() (easier to type) 2016-09-19 12:37:03 -04:00
Duy-Nguyen Ta 547606e6c2 correct cython wrapper module name in pyx import 2016-09-14 07:45:26 -04:00
Duy-Nguyen Ta 2433cbd8e8 Remove copy constructor assumption. Manually add copy constructors. Remove dependency on default constructor (some like Optimizers and Marginals don't have the default constructor). Remove cyCreateFromValue. Ignore variable name when checking overload similarity. 2016-09-13 17:11:23 -04: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 40da298f68 emit methods to pxd, change the way template methods are handled
pxd allows template methods, whereas the current scheme instantiates/expands all template methods and add them to the same methods_ container. The new scheme treats them all separately: nontemplated methods in methods_, template methods in templateMethods_, and template methods after instantiation in expandedTemplateMethods_.
2016-09-09 07:26:11 -04:00
dellaert 72fe66d468 Removed headers 2016-05-22 14:22:36 -07:00
dellaert ba51b02cf0 Moving to optionals fixed template dreturn argument!
Merge branch 'qualified' into grammar_wrongtest

Conflicts:
	wrap/Class.cpp
	wrap/Function.h
	wrap/Qualified.h
	wrap/ReturnType.h
	wrap/tests/testWrap.cpp
2014-12-01 09:48:56 +01: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 be00e1c348 Allow Vector and Matrix in list of template values 2014-11-29 13:44:49 +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 e07da1c82d Added matlabName, and made data members private 2014-11-13 22:43:29 +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 341ad9f288 gtsam.h with templated Values::at now compiles ! 2014-11-13 01:26:06 +01:00
dellaert 5ca71a2eb9 Fixed exception bug 2014-11-12 23:54:37 +01:00
dellaert 1ea0225030 Big refactor because methods now private member of Class 2014-11-12 23:23:07 +01:00
dellaert ad9f3b334c test addOverload 2014-11-12 22:06:53 +01:00
dellaert 7d4f5a4820 Make explicit whether wrapper or proxy is written to... 2014-11-12 20:51:47 +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 9a102e8c59 Handle special case of single overload with all numeric arguments 2014-05-25 15:21:49 -04:00