Merged in matt_broadway/gtsam/fix/python3cython (pull request #369)
Fix/python3cython Approved-by: Duy-Nguyen Ta <thduynguyen@gmail.com> Approved-by: Frank Dellaert <dellaert@cc.gatech.edu>release/4.3a0
						commit
						0e23f77212
					
				| 
						 | 
				
			
			@ -78,6 +78,7 @@ endif()
 | 
			
		|||
option(GTSAM_INSTALL_MATLAB_TOOLBOX      "Enable/Disable installation of matlab toolbox"  OFF)
 | 
			
		||||
option(GTSAM_INSTALL_CYTHON_TOOLBOX      "Enable/Disable installation of Cython toolbox"  OFF)
 | 
			
		||||
option(GTSAM_BUILD_WRAP                  "Enable/Disable building of matlab/cython wrap utility (necessary for matlab/cython interface)" ON)
 | 
			
		||||
set(GTSAM_PYTHON_VERSION "Default" CACHE STRING "The version of python to build the cython wrapper or python module for (or Default)")
 | 
			
		||||
 | 
			
		||||
# Check / set dependent variables for MATLAB wrapper
 | 
			
		||||
if((GTSAM_INSTALL_MATLAB_TOOLBOX OR GTSAM_INSTALL_CYTHON_TOOLBOX) AND NOT GTSAM_BUILD_WRAP)
 | 
			
		||||
| 
						 | 
				
			
			@ -554,6 +555,7 @@ endif()
 | 
			
		|||
 | 
			
		||||
message(STATUS "Cython toolbox flags                                      ")
 | 
			
		||||
print_config_flag(${GTSAM_INSTALL_CYTHON_TOOLBOX}      "Install Cython toolbox         ")
 | 
			
		||||
message(STATUS "  Python version                 : ${GTSAM_PYTHON_VERSION}")
 | 
			
		||||
print_config_flag(${GTSAM_BUILD_WRAP}                  "Build Wrap                     ")
 | 
			
		||||
message(STATUS "===============================================================")
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,10 +29,15 @@
 | 
			
		|||
 | 
			
		||||
# Use the Cython executable that lives next to the Python executable
 | 
			
		||||
# if it is a local installation.
 | 
			
		||||
find_package( PythonInterp )
 | 
			
		||||
if(GTSAM_PYTHON_VERSION STREQUAL "Default")
 | 
			
		||||
  find_package(PythonInterp)
 | 
			
		||||
else()
 | 
			
		||||
  find_package(PythonInterp ${GTSAM_PYTHON_VERSION} EXACT)
 | 
			
		||||
endif()
 | 
			
		||||
 | 
			
		||||
if ( PYTHONINTERP_FOUND )
 | 
			
		||||
  execute_process( COMMAND "${PYTHON_EXECUTABLE}" "-c"
 | 
			
		||||
      "import Cython; print Cython.__path__"
 | 
			
		||||
      "import Cython; print(Cython.__path__[0])"
 | 
			
		||||
      RESULT_VARIABLE RESULT
 | 
			
		||||
      OUTPUT_VARIABLE CYTHON_PATH
 | 
			
		||||
      OUTPUT_STRIP_TRAILING_WHITESPACE
 | 
			
		||||
| 
						 | 
				
			
			@ -51,7 +56,7 @@ endif ()
 | 
			
		|||
# RESULT=0 means ok
 | 
			
		||||
if ( NOT RESULT )
 | 
			
		||||
  execute_process( COMMAND "${PYTHON_EXECUTABLE}" "-c"
 | 
			
		||||
      "import Cython; print Cython.__version__"
 | 
			
		||||
      "import Cython; print(Cython.__version__)"
 | 
			
		||||
      RESULT_VARIABLE RESULT
 | 
			
		||||
      OUTPUT_VARIABLE CYTHON_VAR_OUTPUT
 | 
			
		||||
      ERROR_VARIABLE CYTHON_VAR_OUTPUT
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -40,9 +40,17 @@
 | 
			
		|||
 | 
			
		||||
# Finding NumPy involves calling the Python interpreter
 | 
			
		||||
if(NumPy_FIND_REQUIRED)
 | 
			
		||||
  if(GTSAM_PYTHON_VERSION STREQUAL "Default")
 | 
			
		||||
    find_package(PythonInterp REQUIRED)
 | 
			
		||||
  else()
 | 
			
		||||
      find_package(PythonInterp ${GTSAM_PYTHON_VERSION} EXACT REQUIRED)
 | 
			
		||||
  endif()
 | 
			
		||||
else()
 | 
			
		||||
  if(GTSAM_PYTHON_VERSION STREQUAL "Default")
 | 
			
		||||
    find_package(PythonInterp)
 | 
			
		||||
  else()
 | 
			
		||||
    find_package(PythonInterp ${GTSAM_PYTHON_VERSION} EXACT)
 | 
			
		||||
  endif()
 | 
			
		||||
endif()
 | 
			
		||||
 | 
			
		||||
if(NOT PYTHONINTERP_FOUND)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,8 +3,23 @@
 | 
			
		|||
# in the current environment are different from the cached!
 | 
			
		||||
unset(PYTHON_EXECUTABLE CACHE)
 | 
			
		||||
unset(CYTHON_EXECUTABLE CACHE)
 | 
			
		||||
unset(PYTHON_INCLUDE_DIR CACHE)
 | 
			
		||||
unset(PYTHON_MAJOR_VERSION CACHE)
 | 
			
		||||
 | 
			
		||||
if(GTSAM_PYTHON_VERSION STREQUAL "Default")
 | 
			
		||||
  find_package(PythonInterp REQUIRED)
 | 
			
		||||
  find_package(PythonLibs REQUIRED)
 | 
			
		||||
else()
 | 
			
		||||
  find_package(PythonInterp ${GTSAM_PYTHON_VERSION} EXACT REQUIRED)
 | 
			
		||||
  find_package(PythonLibs ${GTSAM_PYTHON_VERSION} EXACT REQUIRED)
 | 
			
		||||
endif()
 | 
			
		||||
find_package(Cython 0.25.2 REQUIRED)
 | 
			
		||||
 | 
			
		||||
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
 | 
			
		||||
    "from __future__ import print_function;import sys;print(sys.version[0], end='')"
 | 
			
		||||
    OUTPUT_VARIABLE PYTHON_MAJOR_VERSION
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# User-friendly Cython wrapping and installing function.
 | 
			
		||||
# Builds a Cython module from the provided interface_header.
 | 
			
		||||
# For example, for the interface header gtsam.h,
 | 
			
		||||
| 
						 | 
				
			
			@ -29,12 +44,12 @@ endfunction()
 | 
			
		|||
 | 
			
		||||
function(set_up_required_cython_packages)
 | 
			
		||||
  # Set up building of cython module
 | 
			
		||||
  find_package(PythonLibs 2.7 REQUIRED)
 | 
			
		||||
  include_directories(${PYTHON_INCLUDE_DIRS})
 | 
			
		||||
  find_package(NumPy REQUIRED)
 | 
			
		||||
  include_directories(${NUMPY_INCLUDE_DIRS})
 | 
			
		||||
endfunction()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Convert pyx to cpp by executing cython
 | 
			
		||||
# This is the first step to compile cython from the command line
 | 
			
		||||
# as described at: http://cython.readthedocs.io/en/latest/src/reference/compilation.html
 | 
			
		||||
| 
						 | 
				
			
			@ -52,7 +67,7 @@ function(pyx_to_cpp target pyx_file generated_cpp include_dirs)
 | 
			
		|||
  add_custom_command(
 | 
			
		||||
    OUTPUT ${generated_cpp}
 | 
			
		||||
    COMMAND
 | 
			
		||||
      ${CYTHON_EXECUTABLE} -X boundscheck=False -v --fast-fail --cplus ${includes_for_cython} ${pyx_file} -o ${generated_cpp}
 | 
			
		||||
    ${CYTHON_EXECUTABLE} -X boundscheck=False -v --fast-fail --cplus -${PYTHON_MAJOR_VERSION} ${includes_for_cython} ${pyx_file} -o ${generated_cpp}
 | 
			
		||||
    VERBATIM)
 | 
			
		||||
  add_custom_target(${target} ALL DEPENDS ${generated_cpp})
 | 
			
		||||
endfunction()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,7 +19,7 @@ if (GTSAM_INSTALL_CYTHON_TOOLBOX)
 | 
			
		|||
  # wrap gtsam_unstable
 | 
			
		||||
  if(GTSAM_BUILD_UNSTABLE)
 | 
			
		||||
    add_custom_target(gtsam_unstable_header DEPENDS "../gtsam_unstable/gtsam_unstable.h")
 | 
			
		||||
    set(GTSAM_UNSTABLE_IMPORT "from gtsam_unstable import *")
 | 
			
		||||
    set(GTSAM_UNSTABLE_IMPORT "from .gtsam_unstable import *")
 | 
			
		||||
    wrap_and_install_library_cython("../gtsam_unstable/gtsam_unstable.h" # interface_header
 | 
			
		||||
                                    "from gtsam.gtsam cimport *"                  # extra imports
 | 
			
		||||
                                    "${GTSAM_CYTHON_INSTALL_PATH}/gtsam" # install path
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,20 +2,22 @@ This is the Cython/Python wrapper around the GTSAM C++ library.
 | 
			
		|||
 | 
			
		||||
INSTALL
 | 
			
		||||
=======
 | 
			
		||||
- if you want to build the gtsam python library for a specific python version (eg 2.7), use the `-DGTSAM_PYTHON_VERSION=2.7` option when running `cmake` otherwise the default interpreter will be used.
 | 
			
		||||
    - If the interpreter is inside an environment (such as an anaconda environment or virtualenv environment) then the environment should be active while building gtsam.
 | 
			
		||||
- This wrapper needs Cython(>=0.25.2), backports_abc>=0.5, and numpy. These can be installed as follows:
 | 
			
		||||
 | 
			
		||||
```bash
 | 
			
		||||
 pip install -r <gtsam_folder>/cython/requirements.txt
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
- For compatiblity with gtsam's Eigen version, it contains its own cloned version of [Eigency](https://github.com/wouterboomsma/eigency.git),
 | 
			
		||||
- For compatibility with gtsam's Eigen version, it contains its own cloned version of [Eigency](https://github.com/wouterboomsma/eigency.git),
 | 
			
		||||
named **gtsam_eigency**, to interface between C++'s Eigen and Python's numpy.
 | 
			
		||||
 | 
			
		||||
- Build and install gtsam using cmake with GTSAM_INSTALL_CYTHON_TOOLBOX enabled.
 | 
			
		||||
The wrapped module will be installed to GTSAM_CYTHON_INSTALL_PATH, which is
 | 
			
		||||
by default: <your CMAKE_INSTALL_PREFIX>/cython
 | 
			
		||||
- Build and install gtsam using cmake with `GTSAM_INSTALL_CYTHON_TOOLBOX` enabled.
 | 
			
		||||
The wrapped module will be installed to `GTSAM_CYTHON_INSTALL_PATH`, which is
 | 
			
		||||
by default: `<your CMAKE_INSTALL_PREFIX>/cython`
 | 
			
		||||
 | 
			
		||||
- Modify your PYTHONPATH to include the GTSAM_CYTHON_INSTALL_PATH:
 | 
			
		||||
- Modify your `PYTHONPATH` to include the `GTSAM_CYTHON_INSTALL_PATH`:
 | 
			
		||||
```bash
 | 
			
		||||
export PYTHONPATH=$PYTHONPATH:<GTSAM_CYTHON_INSTALL_PATH>
 | 
			
		||||
```
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,2 +1,2 @@
 | 
			
		|||
from gtsam import *
 | 
			
		||||
from .gtsam import *
 | 
			
		||||
${GTSAM_UNSTABLE_IMPORT}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -394,14 +394,18 @@ void Module::emit_cython_pxd(FileWriter& pxdFile) const {
 | 
			
		|||
 | 
			
		||||
/* ************************************************************************* */
 | 
			
		||||
void Module::emit_cython_pyx(FileWriter& pyxFile) const {
 | 
			
		||||
  // directives...
 | 
			
		||||
  // allow str to automatically coerce to std::string and back (for python3)
 | 
			
		||||
  pyxFile.oss << "# cython: c_string_type=str, c_string_encoding=ascii\n\n";
 | 
			
		||||
 | 
			
		||||
  // headers...
 | 
			
		||||
  string pxdHeader = name;
 | 
			
		||||
  pyxFile.oss << "cimport numpy as np\n"
 | 
			
		||||
                 "import numpy as npp\n"
 | 
			
		||||
                 "cimport " << pxdHeader << "\n"
 | 
			
		||||
                 "from "<< pxdHeader << " cimport shared_ptr\n"
 | 
			
		||||
                 "from "<< pxdHeader << " cimport dynamic_pointer_cast\n"
 | 
			
		||||
                 "from "<< pxdHeader << " cimport make_shared\n";
 | 
			
		||||
                 "from ."<< pxdHeader << " cimport shared_ptr\n"
 | 
			
		||||
                 "from ."<< pxdHeader << " cimport dynamic_pointer_cast\n"
 | 
			
		||||
                 "from ."<< pxdHeader << " cimport make_shared\n";
 | 
			
		||||
 | 
			
		||||
  pyxFile.oss << "# C helper function that copies all arguments into a positional list.\n"
 | 
			
		||||
                 "cdef list process_args(list keywords, tuple args, dict kwargs):\n"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,11 @@
 | 
			
		|||
# cython: c_string_type=str, c_string_encoding=ascii
 | 
			
		||||
 | 
			
		||||
cimport numpy as np
 | 
			
		||||
import numpy as npp
 | 
			
		||||
cimport geometry
 | 
			
		||||
from geometry cimport shared_ptr
 | 
			
		||||
from geometry cimport dynamic_pointer_cast
 | 
			
		||||
from geometry cimport make_shared
 | 
			
		||||
from .geometry cimport shared_ptr
 | 
			
		||||
from .geometry cimport dynamic_pointer_cast
 | 
			
		||||
from .geometry cimport make_shared
 | 
			
		||||
# C helper function that copies all arguments into a positional list.
 | 
			
		||||
cdef list process_args(list keywords, tuple args, dict kwargs):
 | 
			
		||||
   cdef str keyword
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue