Skip to content

Commit

Permalink
removing Dimensions.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
dkeeney committed Mar 20, 2019
2 parents c5cf3a8 + 5bd94e4 commit 91408da
Show file tree
Hide file tree
Showing 90 changed files with 4,686 additions and 1,853 deletions.
4 changes: 3 additions & 1 deletion API_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The API is specified in the [API Docs](http://nupic.docs.numenta.org/prerelease/
We try to remain compatible where possible, to make it easy for the users and programmers to switch/use
our implementation. And for developers to be easily able to navigate within the (known) codebase.
Despite of this, sometimes changes need to happen - be it for optimization, removal/replacement of some
features or implemenation detail, etc.
features or implementation detail, etc.


## API breaking changes in this repo
Expand Down Expand Up @@ -60,3 +60,5 @@ are ignored. PR #271
* Removed `void SpatialPooler::stripUnlearnedColumns()` as unused and not useful (did not effectively remove any columns). PR #286

* Changed SDRClassifier::compute() signature to take parameter `ClassifierResult& result`, instead of a raw pointer. PR #301

* Rewrote ScalarEncoder API, all code using it needs to be rewritten. PR #314
82 changes: 65 additions & 17 deletions bindings/py/cpp_src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,24 @@ message(STATUS "Configuring Python interface")
#
set(src_py_algorithms_files
bindings/algorithms/algorithm_module.cpp
bindings/algorithms/py_SDR.cpp
bindings/algorithms/py_SDR_Metrics.cpp
bindings/algorithms/py_Algorithms.cpp
bindings/algorithms/py_Cells4.cpp
bindings/algorithms/py_HTM.cpp
bindings/algorithms/py_SDRClassifier.cpp
bindings/algorithms/py_SpatialPooler.cpp
)

)

set(src_py_sdr_files
bindings/sdr/sdr_module.cpp
bindings/sdr/py_SDR.cpp
bindings/sdr/py_SDR_Metrics.cpp
)

set(src_py_encoders_files
bindings/encoders/encoders_module.cpp
bindings/encoders/py_ScalarEncoder.cpp
)

set(src_py_engine_files
plugin/PyBindRegion.cpp
plugin/PyBindRegion.hpp
Expand All @@ -75,12 +84,14 @@ set(src_py_test_files

#set up file tabs in Visual Studio
source_group("algorithms" FILES ${src_py_algorithms_files})
source_group("engine" FILES ${src_py_engine_files})
source_group("math" FILES ${src_py_math_files})
source_group("test" FILES ${src_py_test_files})
source_group("sdr" FILES ${src_py_sdr_files})
source_group("encoders" FILES ${src_py_encoders_files})
source_group("engine" FILES ${src_py_engine_files})
source_group("math" FILES ${src_py_math_files})
source_group("test" FILES ${src_py_test_files})
############################################################
#
# Build the three shared binding libraries for the Python Interface
# Build the shared binding libraries for the Python Interface
#
############################################################

Expand All @@ -106,18 +117,52 @@ set(pybind11_INCLUDE_DIRS ${pybind11_SOURCE_DIR}/include)
set(algorithms_shared_lib algorithms)
pybind11_add_module(${algorithms_shared_lib} ${src_py_algorithms_files} )
target_link_libraries(${algorithms_shared_lib} PRIVATE
${core_library}
${COMMON_OS_LIBS}
)
${core_library}
${COMMON_OS_LIBS}
)
target_compile_options(${algorithms_shared_lib} PUBLIC ${INTERNAL_CXX_FLAGS})
target_compile_definitions(${algorithms_shared_lib} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
target_include_directories(${algorithms_shared_lib} PRIVATE
${PYTHON_INCLUDE_DIRS}
${pybind11_INCLUDE_DIRS}
${CORE_LIB_INCLUDES}
${EXTERNAL_INCLUDES}
${PROJECT_SOURCE_DIR}
)
${PYTHON_INCLUDE_DIRS}
${pybind11_INCLUDE_DIRS}
${CORE_LIB_INCLUDES}
${EXTERNAL_INCLUDES}
${PROJECT_SOURCE_DIR}
)


set(sdr_shared_lib sdr)
pybind11_add_module(${sdr_shared_lib} ${src_py_sdr_files} )
target_link_libraries(${sdr_shared_lib} PRIVATE
${core_library}
${COMMON_OS_LIBS}
)
target_compile_options(${sdr_shared_lib} PUBLIC ${INTERNAL_CXX_FLAGS})
target_compile_definitions(${sdr_shared_lib} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
target_include_directories(${sdr_shared_lib} PRIVATE
${PYTHON_INCLUDE_DIRS}
${pybind11_INCLUDE_DIRS}
${CORE_LIB_INCLUDES}
${EXTERNAL_INCLUDES}
${PROJECT_SOURCE_DIR}
)


set(encoders_shared_lib encoders)
pybind11_add_module(${encoders_shared_lib} ${src_py_encoders_files} )
target_link_libraries(${encoders_shared_lib} PRIVATE
${core_library}
${COMMON_OS_LIBS}
)
target_compile_options(${encoders_shared_lib} PUBLIC ${INTERNAL_CXX_FLAGS})
target_compile_definitions(${encoders_shared_lib} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
target_include_directories(${encoders_shared_lib} PRIVATE
${PYTHON_INCLUDE_DIRS}
${pybind11_INCLUDE_DIRS}
${CORE_LIB_INCLUDES}
${EXTERNAL_INCLUDES}
${PROJECT_SOURCE_DIR}
)


set(engine_shared_lib engine_internal)
Expand All @@ -136,6 +181,7 @@ target_include_directories(${engine_shared_lib} PRIVATE
${PROJECT_SOURCE_DIR}
)


set(math_shared_lib math)
pybind11_add_module(${math_shared_lib} ${src_py_math_files})
target_link_libraries(${math_shared_lib} PRIVATE
Expand Down Expand Up @@ -190,6 +236,8 @@ target_include_directories(${math_shared_lib} PRIVATE
#
###################################################################
install(TARGETS
sdr
encoders
algorithms
engine_internal
math
Expand Down
4 changes: 0 additions & 4 deletions bindings/py/cpp_src/bindings/algorithms/algorithm_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ namespace py = pybind11;

namespace nupic_ext
{
void init_SDR(py::module&);
void init_SDR_Metrics(py::module&);
void init_algorithms(py::module&);
void init_Cells4(py::module&);
void init_HTM(py::module&);
Expand All @@ -48,8 +46,6 @@ using namespace nupic_ext;
PYBIND11_MODULE(algorithms, m) {
m.doc() = "nupic.core.algorithms plugin"; // optional module docstring

init_SDR(m);
init_SDR_Metrics(m);
init_algorithms(m);
init_HTM(m);
init_Cells4(m);
Expand Down
1 change: 1 addition & 0 deletions bindings/py/cpp_src/bindings/algorithms/py_HTM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace nupic_ext

namespace py = pybind11;
using namespace nupic;
using nupic::sdr::SDR;
using nupic::algorithms::connections::Permanence;

void init_HTM(py::module& m)
Expand Down
41 changes: 41 additions & 0 deletions bindings/py/cpp_src/bindings/encoders/encoders_module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ---------------------------------------------------------------------
* Numenta Platform for Intelligent Computing (NuPIC)
* Copyright (C) 2018, chhenning
* 2019, David McDougall
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with this program. If not, see http://www.gnu.org/licenses.
*
* http://numenta.org/licenses/
* --------------------------------------------------------------------- */

/** @file
* Encoders bindings Module file for pybind11
*/

#include <bindings/suppress_register.hpp> //must be before pybind11.h
#include <pybind11/pybind11.h>

namespace py = pybind11;

namespace nupic_ext
{
void init_ScalarEncoder(py::module&);
}

using namespace nupic_ext;

PYBIND11_MODULE(encoders, m) {
m.doc() = "nupic.bindings.encoders"; // optional module docstring

init_ScalarEncoder(m);
}
118 changes: 118 additions & 0 deletions bindings/py/cpp_src/bindings/encoders/py_ScalarEncoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* ----------------------------------------------------------------------
* Numenta Platform for Intelligent Computing (NuPIC)
* Copyright (C) 2019, David McDougall
* The following terms and conditions apply:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with this program. If not, see http://www.gnu.org/licenses.
*
* http://numenta.org/licenses/
* ---------------------------------------------------------------------- */

#include <bindings/suppress_register.hpp> //include before pybind11.h
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
namespace py = pybind11;

#include <nupic/encoders/ScalarEncoder.hpp>
#include <nupic/types/Sdr.hpp>

namespace nupic_ext
{
using namespace nupic::encoders;
using nupic::sdr::SDR;

void init_ScalarEncoder(py::module& m)
{
py::class_<ScalarEncoderParameters> py_ScalarEncParams(m, "ScalarEncoderParameters",
R"(
The following three (3) members define the total number of bits in the output:
size,
radius,
resolution.
These are mutually exclusive and only one of them should be non-zero when
constructing the encoder.)");

py_ScalarEncParams.def(py::init<>(), R"()");

py_ScalarEncParams.def_readwrite("minimum", &ScalarEncoderParameters::minimum,
R"(This defines the range of the input signal. These endpoints are inclusive.)");

py_ScalarEncParams.def_readwrite("maximum", &ScalarEncoderParameters::maximum,
R"(This defines the range of the input signal. These endpoints are inclusive.)");

py_ScalarEncParams.def_readwrite("clipInput", &ScalarEncoderParameters::clipInput,
R"(This determines whether to allow input values outside the
range [minimum, maximum].
If true, the input will be clipped into the range [minimum, maximum].
If false, inputs outside of the range will raise an error.)");

py_ScalarEncParams.def_readwrite("periodic", &ScalarEncoderParameters::periodic,
R"(This controls what happens near the edges of the input range.
If true, then the minimum & maximum input values are adjacent and the first and
last bits of the output SDR are also adjacent. The contiguous block of 1's
wraps around the end back to the begining.
If false, then minimum & maximum input values are the endpoints of the input
range, are not adjacent, and activity does not wrap around.)");

py_ScalarEncParams.def_readwrite("activeBits", &ScalarEncoderParameters::activeBits,
R"(This is the number of true bits in the encoded output SDR. The output
encodings will have a contiguous block of this many 1's.)");

py_ScalarEncParams.def_readwrite("sparsity", &ScalarEncoderParameters::sparsity,
R"(This is an alternative way to specify the the number of active bits.
Sparsity requires that the size to also be specified.
Specify only one of: activeBits or sparsity.)");

py_ScalarEncParams.def_readwrite("size", &ScalarEncoderParameters::size,
R"(This is the total number of bits in the encoded output SDR.)");

py_ScalarEncParams.def_readwrite("radius", &ScalarEncoderParameters::radius,
R"(Two inputs separated by more than the radius have non-overlapping
representations. Two inputs separated by less than the radius will in general
overlap in at least some of their bits. You can think of this as the radius of
the input.)");

py_ScalarEncParams.def_readwrite("resolution", &ScalarEncoderParameters::resolution,
R"(Two inputs separated by greater than, or equal to the resolution are guaranteed
to have different representations.)");


py::class_<ScalarEncoder> py_ScalarEnc(m, "ScalarEncoder",
R"(Encodes a real number as a contiguous block of 1's.
The ScalarEncoder encodes a numeric (floating point) value into an array of
bits. The output is 0's except for a contiguous block of 1's. The location of
this contiguous block varies continuously with the input value.
TODO, Example Usage & unit test for it.)");

py_ScalarEnc.def(py::init<ScalarEncoderParameters&>(), R"()");
py_ScalarEnc.def_property_readonly("parameters",
[](const ScalarEncoder &self) { return self.parameters; },
R"(Contains the parameter structure which this encoder uses internally. All
fields are filled in automatically.)");

py_ScalarEnc.def("encode", &ScalarEncoder::encode, R"()");

py_ScalarEnc.def("encode", [](ScalarEncoder &self, nupic::Real64 value) {
auto output = new SDR( self.dimensions );
self.encode( value, *output );
return output; },
R"()");
}
}
Loading

0 comments on commit 91408da

Please sign in to comment.