Commit Graph

64 Commits (f558ccbb2de2d25c93705b96d482b07e0c7a19d2)

Author SHA1 Message Date
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 c5a0f1a839 Undo unrelated changes 2017-08-06 17:42:20 -07:00
dellaert d752c9e249 Re-wrote constructor overloading logic which saves a lot of overhead 2017-08-06 14:58:23 -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 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 c13b964777 standardize names for classes with inner namespace 2016-11-24 19:22:44 -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 acf3c9d259 proper overloading constructors 2016-11-16 17:51:03 -05:00
Duy-Nguyen Ta f5691804ed Use class name as ctor for static construction calls
Longer, but more conventional
2016-09-19 12:39:04 -04:00
Duy-Nguyen Ta a294c2ab11 simplify python constructor call 2016-09-16 11:43:25 -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 450a652bcf pxd hack: always add copy constructor by default 2016-09-12 18:47:04 -04:00
Duy-Nguyen Ta 53dbe25c50 Cython pxd: putting template instantiations at the correct place right after a template class 2016-09-12 18:36:45 -04:00
Duy-Nguyen Ta 6044bffd8a handle Key by adding noninstantiating normal typedef rule. Fix copy constructor in template classes: using This. 2016-09-12 18:17:47 -04:00
Duy-Nguyen Ta 1b04c6713b handle "This". Wrap all geometry 2016-09-11 18:14:19 -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 2496de85a9 check if default constructor exists. Autogenerate copy constructor by default 2016-09-10 19:44:53 -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 d65d87072b use __Create__ as name for "constructors" instead of the object name
so we call : Class.__Create__(...) to create a python object, instead of Class.Class(...) which seems duplicated and complicated if Class is long, e.g. "mEstimator_noiseModel_GemanMcClure"
2016-09-09 16:39:12 -04:00
Duy-Nguyen Ta 1e84da1cfa pyx: add constructors and fixing inheritance 2016-09-09 15:52:44 -04:00
Duy-Nguyen Ta 2d3d6d99f9 standardize function name to emit_cython_[pxd/pyx]. Remove first level namespace from Cython object names.
Examples: gtsam_Point3 --> Point3, gtsam_noiseModel_Base --> noiseModel_Base
2016-09-09 12:01:51 -04:00
Duy-Nguyen Ta ecde851d8c [inprogress] cython wrapper 2016-09-08 13:33:32 -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 6c24fc2aca Python prototype 2014-11-14 17:47:25 +01:00
Richard Roberts 686051c032 Convert DOS line endings to UNIX 2014-01-09 16:39:27 -05:00
Duy-Nguyen Ta 3a62daf985 bug fix: remove a redundantly generated double quote when verbose is on 2013-03-15 23:24:30 +00:00
Chris Beall 4297d24c96 changed tabs to spaces for consistent indentation in all of GTSAM 2012-10-02 14:40:07 +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
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 8a8b27005f wrap_mods_inheritance branch - Can now pass derived classes in as base class arguments (i.e. pass gtsamPose2 where gtsamValues.insert expects gtsamValue) 2012-07-09 00:02:43 +00:00
Richard Roberts 3c27daae18 wrap_mods_inheritance branch: in progress with inheritance in matlab wrapper 2012-07-08 12:27:39 +00:00
Richard Roberts 8ab18498ad Add to collector through matlab function to allow returning objects from other wrap modules 2012-07-05 14:04:57 +00:00
Richard Roberts b5937ce35d Modified wrap to generate a single cpp wrapper file containing all wrapped functions, and one .m file per class and static method. 2012-07-05 14:04:36 +00:00
Andrew Melim e40ce8b44d Fixed class issue calling destructor 2012-07-03 01:28:41 +00:00
Richard Roberts b163d26d5e Fixed invalid iterator bug during clear all in Matlab wrapper 2012-07-02 22:33:36 +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 f0c8c023a0 Added small verbosity changes, verbose now defaults to false in wrap.cpp 2012-06-29 18:38:54 +00:00
Andrew Melim fb00f4b834 fixes for two word args 2012-06-27 21:50:45 +00:00
Andrew Melim ea0c85ef06 Crash for some objects is now fixed 2012-06-27 16:22:12 +00:00
Andrew Melim 6d776812d3 new wrap! :) 2012-06-26 18:52:27 +00:00
Alex Cunningham 3942f28a68 Wrap now only writes new files when there is a change, and install only updates files if necessary 2012-01-15 21:42:44 +00:00
Alex Cunningham 66a9d635b3 Unified file writing to use a custom class to allow for smart checking 2012-01-15 21:42:41 +00:00
Frank Dellaert 9868355b40 Changed & to && to avoid MATLAB warnings 2012-01-15 04:17:21 +00:00