Skip to content

Commit

Permalink
Merge pull request #151 from royshil/roy.pygrt
Browse files Browse the repository at this point in the history
PyGRT: Python bindings for GRT via SWIG
  • Loading branch information
nickgillian committed Jun 5, 2019
2 parents cc62906 + d712d91 commit ffa7690
Show file tree
Hide file tree
Showing 89 changed files with 3,664 additions and 68 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ env:
- CONFIG=Release
- CONFIG=Debug
before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew upgrade python ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install swig ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then pip3 install numpy ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -cs)-backports main restricted universe multiverse" ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get -qq update ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get -qq install g++-4.8 ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install cmake ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install -y cmake swig3.0 python3-dev python3-numpy; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" && "$CXX" = "g++" ]]; then sudo apt-get install -qq libstdc++-4.8-dev; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" && "$CXX" = "g++" ]]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
before_script:
Expand Down
4 changes: 2 additions & 2 deletions GRT/ClassificationModules/ANBC/ANBC_Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ Float ANBC_Model::predictUnnormed( const VectorFloat &x ){
return prediction;
}

inline Float ANBC_Model::gauss(const Float x,const Float mu,const Float sigma){
Float ANBC_Model::gauss(const Float x,const Float mu,const Float sigma){
return ( 1.0/(sigma*sqrt(TWO_PI)) ) * exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
}

inline Float ANBC_Model::unnormedGauss(const Float x,const Float mu,const Float sigma){
Float ANBC_Model::unnormedGauss(const Float x,const Float mu,const Float sigma){
return exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
}

Expand Down
4 changes: 2 additions & 2 deletions GRT/ClassificationModules/ANBC/ANBC_Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class GRT_API ANBC_Model{
bool train( const UINT classLabel, const MatrixDouble &trainingData, const VectorFloat &weightsVector );
Float predict( const VectorFloat &x );
Float predictUnnormed( const VectorFloat &x );
inline Float gauss(const Float x,const Float mu,const Float sigma);
inline Float unnormedGauss(const Float x,const Float mu,const Float sigma);
Float gauss(const Float x,const Float mu,const Float sigma);
Float unnormedGauss(const Float x,const Float mu,const Float sigma);
void recomputeThresholdValue(const Float gamma);

public:
Expand Down
2 changes: 1 addition & 1 deletion GRT/ClassificationModules/MinDist/MinDistModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class GRT_API MinDistModel
UINT getClassLabel() const;
UINT getNumFeatures() const;
UINT getNumClusters() const;
UINT getDistanceMode() const;
// UINT getDistanceMode() const;
Float getRejectionThreshold() const;
Float getGamma() const;
Float getTrainingMu() const;
Expand Down
2 changes: 1 addition & 1 deletion GRT/CoreModules/FeatureExtraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class GRT_API FeatureExtraction : public MLBase
template< typename T > FeatureExtraction *createNewFeatureExtractionModule() { return new T; } ///< Returns a pointer to a new instance of the template class, the caller is responsible for deleting the pointer

template< typename T >
class RegisterFeatureExtractionModule : FeatureExtraction {
class RegisterFeatureExtractionModule : public FeatureExtraction {
public:
RegisterFeatureExtractionModule(const std::string &newModuleId) {
getMap()->insert( std::pair< std::string, FeatureExtraction*(*)()>(newModuleId, &createNewFeatureExtractionModule< T > ) );
Expand Down
4 changes: 2 additions & 2 deletions GRT/CoreModules/MLBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ class GRT_API MLBase : public GRTBase, public Observer< TrainingResult >, public
@return returns the minimum change value
*/
Float getMinChange() const;
// Float getMinChange() const;

/**
Gets the current learningRate value, this is value used to update the weights at each step of a learning algorithm such as stochastic gradient descent.
Expand Down Expand Up @@ -538,7 +538,7 @@ class GRT_API MLBase : public GRTBase, public Observer< TrainingResult >, public
@return returns true if the order of the training dataset should be randomized, false otherwise
*/
bool getRandomiseTrainingOrder() const;
// bool getRandomiseTrainingOrder() const;

/**
Gets if the model for the derived class has been succesfully trained.
Expand Down
4 changes: 2 additions & 2 deletions GRT/DataStructures/VectorFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class VectorFloat : public Vector< Float >{
@param size: sets the size of the vector
*/
GRT_API VectorFloat( const size_type size );
GRT_API VectorFloat( const std::vector<Float>::size_type size );

/**
Constructor, sets the size of the vector and sets all elements to value
@param size: sets the size of the vector
@param value: the value that will be written to all elements in the vector
*/
GRT_API VectorFloat( const size_type size, const Float &value );
GRT_API VectorFloat( const std::vector<Float>::size_type size, const Float &value );

/**
Copy Constructor, copies the values from the rhs VectorFloat to this VectorFloat instance
Expand Down
2 changes: 1 addition & 1 deletion GRT/FeatureExtractionModules/FFT/FastFourierTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class GRT_API FastFourierTransform : public GRTBase{
VectorFloat phase;
VectorFloat power;
Float averagePower;
const static int MAX_FAST_BITS = 16;
const int MAX_FAST_BITS = 16;
Vector< Vector< int > > bitTable;
};

Expand Down
7 changes: 5 additions & 2 deletions GRT/PreProcessingModules/Derivative.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,15 @@ class GRT_API Derivative : public PreProcessing{
@param delta: the estimated sampling time between sensor samples, must be greater than zero
@return returns true if delta was set, false otherwise
*/
bool setDelta(const Float delta);
inline bool setDelta(const Float delta) {
this->delta = delta;
return true;
}

/**
Sets if the input data will be filtered using a moving average filter before the derivative is computed.
Updating this parameter will re-initialize this instance.
@param filterData: sets if the data should be filtered before computing the derivative
@return returns true if the filterData parameter was set, false otherwise
*/
Expand Down
2 changes: 1 addition & 1 deletion GRT/Util/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ Float Util::getMin(const VectorFloat &x){
return min;
}

unsigned int getMinIndex(const VectorFloat &x){
unsigned int Util::getMinIndex(const VectorFloat &x){
unsigned int minIndex = 0;
Float min = std::numeric_limits< Float >::max();
unsigned int N = (unsigned int)x.size();
Expand Down
8 changes: 8 additions & 0 deletions build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 2.8.7)

project(GRT C CXX)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake-modules")

# GRT library
set(GRT_LIB_NAME grt)

Expand All @@ -12,6 +14,7 @@ option(BUILD_EXAMPLES “build-examples” ON)
option(BUILD_TOOLS “build-tools” ON)
option(BUILD_SHARED_LIB "build-shared-lib" ON)
option(EXCLUDE_FROM_INSTALL "exclude-from-install" OFF)
option(BUILD_PYTHON_BINDING "build-python" ON)

#setup the default build flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ")
Expand Down Expand Up @@ -137,6 +140,11 @@ add_library(${GRT_LIB_NAME}
${GRT_HEADERS}
)

message(STATUS "Build Python bindings: ${BUILD_PYTHON_BINDING}")
if( BUILD_PYTHON_BINDING )
add_subdirectory(python)
endif()

#Add the options for what should be installed (when the user runs: sudo make install)
if(NOT EXCLUDE_FROM_INSTALL)
install(
Expand Down
62 changes: 62 additions & 0 deletions build/cmake-modules/FindNumpy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# - Find the NumPy libraries
# This module finds if NumPy is installed, and sets the following variables
# indicating where it is.
#
# TODO: Update to provide the libraries and paths for linking npymath lib.
#
# NUMPY_FOUND - was NumPy found
# NUMPY_VERSION - the version of NumPy found as a string
# NUMPY_VERSION_MAJOR - the major version number of NumPy
# NUMPY_VERSION_MINOR - the minor version number of NumPy
# NUMPY_VERSION_PATCH - the patch version number of NumPy
# NUMPY_VERSION_DECIMAL - e.g. version 1.6.1 is 10601
# NUMPY_INCLUDE_DIR - path to the NumPy include files

unset(NUMPY_VERSION)
unset(NUMPY_INCLUDE_DIR)

if(PYTHONINTERP_FOUND)
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import numpy as n; print(n.__version__); print(n.get_include());"
RESULT_VARIABLE __result
OUTPUT_VARIABLE __output
OUTPUT_STRIP_TRAILING_WHITESPACE)

if(__result MATCHES 0)
string(REGEX REPLACE ";" "\\\\;" __values ${__output})
string(REGEX REPLACE "\r?\n" ";" __values ${__values})
list(GET __values 0 NUMPY_VERSION)
list(GET __values 1 NUMPY_INCLUDE_DIR)

string(REGEX MATCH "^([0-9])+\\.([0-9])+\\.([0-9])+" __ver_check "${NUMPY_VERSION}")
if(NOT "${__ver_check}" STREQUAL "")
set(NUMPY_VERSION_MAJOR ${CMAKE_MATCH_1})
set(NUMPY_VERSION_MINOR ${CMAKE_MATCH_2})
set(NUMPY_VERSION_PATCH ${CMAKE_MATCH_3})
math(EXPR NUMPY_VERSION_DECIMAL
"(${NUMPY_VERSION_MAJOR} * 10000) + (${NUMPY_VERSION_MINOR} * 100) + ${NUMPY_VERSION_PATCH}")
string(REGEX REPLACE "\\\\" "/" NUMPY_INCLUDE_DIR ${NUMPY_INCLUDE_DIR})
else()
unset(NUMPY_VERSION)
unset(NUMPY_INCLUDE_DIR)
message(STATUS "Requested NumPy version and include path, but got instead:\n${__output}\n")
endif()
endif()
else()
message(STATUS "To find NumPy Python interpretator is required to be found.")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(NumPy REQUIRED_VARS NUMPY_INCLUDE_DIR NUMPY_VERSION
VERSION_VAR NUMPY_VERSION)

if(NUMPY_FOUND)
message(STATUS "NumPy ver. ${NUMPY_VERSION} found (include: ${NUMPY_INCLUDE_DIR})")
endif()

unset(__result)
unset(__output)
unset(__error_value)
unset(__values)
unset(__ver_check)
unset(__error_value)
47 changes: 47 additions & 0 deletions build/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

if (APPLE) # Assume homebrew
file(GLOB SWIG_EXECUTABLE /usr/local/Cellar/swig/*/bin/swig)
if (NOT EXISTS ${SWIG_EXECUTABLE})
message(FATAL_ERROR "Cannot find homebrew SWIG v3 in /usr/local/Cellar/swig/")
endif()
message(STATUS "Found swig executable: ${SWIG_EXECUTABLE}")
endif()
find_package(SWIG 3 REQUIRED)
INCLUDE(${SWIG_USE_FILE})

find_package(PythonLibs 3 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
message(STATUS "Found python: ${PYTHON_INCLUDE_DIRS} ${PYTHON_LIBRARIES}")

if (APPLE) # Assume homebrew
set(PYTHONINTERP_FOUND YES)
set(PYTHON_EXECUTABLE /usr/local/bin/python3)
if (NOT EXISTS ${PYTHON_EXECUTABLE})
message(FATAL_ERROR "Cannot find homebrew Python v3 in ${PYTHON_EXECUTABLE}")
endif()
message(STATUS "Found Python 3 executable: ${PYTHON_EXECUTABLE}")
else()
find_package(PythonInterp 3)
endif()
find_package(Numpy REQUIRED)
if (NOT IS_DIRECTORY ${NUMPY_INCLUDE_DIR})
message(FATAL_ERROR "Cannot find Numpy")
endif()
include_directories(${NUMPY_INCLUDE_DIR})


include_directories(${GRT_SRC_DIR})

add_compile_options(-Wno-deprecated)

set_property(SOURCE GRT.i PROPERTY CPLUSPLUS ON)
set_property(SOURCE GRT.i PROPERTY SWIG_FLAGS -Wall -verbose -py3)
swig_add_library(GRT LANGUAGE python SOURCES GRT.i)

swig_link_libraries(GRT ${PYTHON_LIBRARIES} ${GRT_LIB_NAME} )

# Copy python examples files
file(GLOB PYTHON_EXAMPLES
"${CMAKE_CURRENT_SOURCE_DIR}/*.py"
)
file(COPY ${PYTHON_EXAMPLES} DESTINATION ".")
Loading

0 comments on commit ffa7690

Please sign in to comment.