Skip to content

Commit

Permalink
Merge pull request #742 from htm-community/cleanup_exceptions
Browse files Browse the repository at this point in the history
Cleanup exceptions
  • Loading branch information
breznak authored Nov 9, 2019
2 parents 1aca435 + 17fdb7c commit 32ab02c
Show file tree
Hide file tree
Showing 25 changed files with 151 additions and 395 deletions.
15 changes: 8 additions & 7 deletions bindings/py/cpp_src/bindings/engine/py_Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ PyBind11 bindings for Engine classes
#include <pybind11/stl.h>

#include <htm/os/Timer.hpp>

#include <htm/ntypes/Array.hpp>
#include <htm/utils/Log.hpp>

#include <htm/engine/Link.hpp>
#include <htm/engine/Network.hpp>
#include <htm/engine/Region.hpp>
#include <htm/engine/Input.hpp>
#include <htm/engine/Spec.hpp>
#include <htm/types/Sdr.hpp>

#include <plugin/PyBindRegion.hpp>
#include <plugin/RegisteredRegionImplPy.hpp>

Expand Down Expand Up @@ -468,13 +469,13 @@ namespace htm_ext
, py::arg("srcOutput") = "", py::arg("destInput") = ""
, py::arg("propagationDelay") = 0);

py::enum_<LogLevel>(m, "LogLevel", py::arithmetic(), "An enumeration of logging levels.")
.value("None", LogLevel::LogLevel_None) // default
.value("Minimal", LogLevel::LogLevel_Minimal)
.value("Normal", LogLevel::LogLevel_Normal)
.value("Verbose", LogLevel::LogLevel_Verbose)
py::enum_<htm::LogLevel>(m, "LogLevel", "An enumeration of logging levels.")
.value("None", htm::LogLevel::LogLevel_None) // default
.value("Minimal", htm::LogLevel::LogLevel_Minimal)
.value("Normal", htm::LogLevel::LogLevel_Normal)
.value("Verbose", htm::LogLevel::LogLevel_Verbose)
.export_values();
py_Network.def("setLogLevel", &htm::Network::setLogLevel);
py_Network.def_static("setLogLevel", &htm::Network::setLogLevel, py::arg("level") = htm::LogLevel::LogLevel_None);


// plugin registration
Expand Down
8 changes: 5 additions & 3 deletions bindings/py/tests/regions/network_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ def testBuiltInRegions(self):
"""
This sets up a network with built-in regions.
"""

import htm
net = engine.Network()
net.setLogLevel(engine.Verbose) # Verbose shows data inputs and outputs while executing.
#net.setLogLevel(htm.bindings.engine_internal.LogLevel.Verbose) # Verbose shows data inputs and outputs while executing.

encoder = net.addRegion("encoder", "ScalarSensor", "{n: 6, w: 2}");
sp = net.addRegion("sp", "SPRegion", "{columnCount: 200}");
tm = net.addRegion("tm", "TMRegion", "");
Expand All @@ -316,6 +316,8 @@ def testBuiltInRegions(self):

tm_output = tm.getOutputArray("predictedActiveCells")
sdr = tm_output.getSDR()
print(sdr.sparse)
print(EXPECTED_RESULT3)
self.assertTrue(np.array_equal(sdr.sparse, EXPECTED_RESULT3))

def testExecuteCommand1(self):
Expand Down
4 changes: 0 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ set(types_files
set(utils_files
htm/utils/GroupBy.hpp
htm/utils/Log.hpp
htm/utils/LoggingException.cpp
htm/utils/LoggingException.hpp
htm/utils/LogItem.cpp
htm/utils/LogItem.hpp
htm/utils/MovingAverage.cpp
htm/utils/MovingAverage.hpp
htm/utils/Random.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/htm/engine/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void Input::uninitialize() {

namespace htm {
std::ostream &operator<<(std::ostream &f, const Input &d) {
f << "Input: " << d.getRegion()->getName() << "." << d.getName() << " " << d.getData();
f << "Input: " << d.getRegion()->getName() << "." << d.getName() << " " << d.getData();
return f;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/htm/engine/Link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <htm/ntypes/BasicType.hpp>
#include <htm/utils/Log.hpp>

// By calling LogItem::setLogLevel(LogLevel_Verbose)
// By calling Network::setLogLevel(LogLevel_Verbose)
// you can enable the NTA_DEBUG macros below.

namespace htm {
Expand Down Expand Up @@ -187,7 +187,7 @@ void Link::compute() {
const Array &src = propagationDelay_ ? propagationDelayBuffer_.front() : src_->getData();
Array &dest = dest_->getData();

NTA_DEBUG << "Link::compute: " << getMoniker() << "; copying to dest input"
NTA_DEBUG << "compute Link: copying " << getMoniker()
<< "; delay=" << propagationDelay_ << "; size=" << src.getCount()
<< " type=" << BasicType::getName(src.getType())
<< " --> " << BasicType::getName(dest.getType()) << std::endl;
Expand Down
7 changes: 5 additions & 2 deletions src/htm/engine/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ namespace htm {

class RegisteredRegionImpl;

thread_local LogLevel NTA_LOG_LEVEL;


Network::Network() {
commonInit();
}

// move constructor
Network::Network(Network && n) {
Network::Network(Network &&n) noexcept {
regions_ = std::move(n.regions_);
minEnabledPhase_ = n.minEnabledPhase_;
maxEnabledPhase_ = n.maxEnabledPhase_;
phaseInfo_ = std::move(n.phaseInfo_);
callbacks_ = n.callbacks_;
iteration_ = n.iteration_;
}
}

Network::Network(const std::string& filename) {
commonInit();
Expand Down
14 changes: 8 additions & 6 deletions src/htm/engine/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Link;
/**
* Cannot copy or assign a Network object. But can be moved.
*/
Network(Network&&); // move is allowed
Network(Network &&) noexcept; // move is allowed
Network(const Network&) = delete;
void operator=(const Network&) = delete;

Expand Down Expand Up @@ -375,12 +375,14 @@ class Link;
*/
void resetProfiling();

/**
* Set one of the debug levels: LogLevel_None = 0, LogLevel_Minimal, LogLevel_Normal, LogLevel_Verbose
/**
* Set one of the debug levels: LogLevel_None = 0, LogLevel_Minimal, LogLevel_Normal, LogLevel_Verbose
*/
void setLogLevel(LogLevel level) {
LogItem::setLogLevel(level);
}
static LogLevel setLogLevel(LogLevel level) {
LogLevel prev = NTA_LOG_LEVEL;
NTA_LOG_LEVEL = level;
return prev;
}


/**
Expand Down
2 changes: 1 addition & 1 deletion src/htm/engine/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void Output::removeLink(const std::shared_ptr<Link>& link) {

namespace htm {
std::ostream &operator<<(std::ostream &f, const Output &d) {
f << "Output:" << d.getRegion()->getName() << "." << d.getName() << " " << d.getData();
f << "Output: " << d.getRegion()->getName() << "." << d.getName() << " " << d.getData();
return f;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/htm/engine/RegionImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ std::shared_ptr<Input> RegionImpl::getInput(const std::string &name) const {
}

std::shared_ptr<Output> RegionImpl::getOutput(const std::string &name) const {
return region_->getOutput(name);
auto out = region_->getOutput(name);
NTA_CHECK(out != nullptr) << "Requested output not found: " << name;
return out;
}
Dimensions RegionImpl::getInputDimensions(const std::string &name) const {
return region_->getInputDimensions(name);
Expand Down
2 changes: 1 addition & 1 deletion src/htm/regions/SPRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void SPRegion::compute() {
// Call SpatialPooler compute
sp_->compute(inputBuffer.getSDR(), args_.learningMode, outputBuffer.getSDR());


// trace facility
NTA_DEBUG << "compute " << *getOutput("bottomUpOut") << "\n";

}
Expand Down
3 changes: 3 additions & 0 deletions src/htm/regions/ScalarSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ void ScalarSensor::compute()
{
SDR &output = getOutput("encoded")->getData().getSDR();
encoder_->encode((Real64)sensedValue_, output);

// trace facility
NTA_DEBUG << "compute " << getOutput("encoded") << std::endl;
}

ScalarSensor::~ScalarSensor() {}
Expand Down
30 changes: 14 additions & 16 deletions src/htm/regions/TMRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ void TMRegion::initialize() {
args_.sequencePos = 0;
}


void TMRegion::compute() {

NTA_ASSERT(tm_) << "TM not initialized";
Expand Down Expand Up @@ -209,6 +210,7 @@ void TMRegion::compute() {
Array &externalPredictiveInputsWinners = getInput("externalPredictiveInputsWinners")->getData();
SDR& externalPredictiveInputsWinnerCells = (args_.externalPredictiveInputs) ? (externalPredictiveInputsWinners.getSDR()) : nullSDR;

// Trace facility
NTA_DEBUG << "compute " << *in << std::endl;

// Perform Bottom up compute()
Expand All @@ -230,36 +232,32 @@ void TMRegion::compute() {
//
std::shared_ptr<Output> out;
out = getOutput("bottomUpOut");
if (out && (out->hasOutgoingLinks() || LogItem::isDebug())) {
//call Network::setLogLevel(LogLevel::LogLevel_Verbose);
// to output the NTA_DEBUG statements below
SDR& sdr = out->getData().getSDR();
tm_->getActiveCells(sdr); //active cells
if (args_.orColumnOutputs) { //output as columns
sdr = tm_->cellsToColumns(sdr);
}
NTA_DEBUG << "bottomUpOut " << *out << std::endl;
}
NTA_DEBUG << "compute "<< *out << std::endl;

out = getOutput("activeCells");
if (out && (out->hasOutgoingLinks() || LogItem::isDebug())) {
tm_->getActiveCells(out->getData().getSDR());
NTA_DEBUG << "active " << *out << std::endl;
}
NTA_DEBUG << "compute "<< *out << std::endl;

out = getOutput("predictedActiveCells");
if (out && (out->hasOutgoingLinks() || LogItem::isDebug())) {
tm_->activateDendrites();
tm_->getWinnerCells(out->getData().getSDR());
NTA_DEBUG << "winners " << *out << std::endl;
}
NTA_DEBUG << "compute "<< *out << std::endl;

out = getOutput("anomaly");
if (out && (out->hasOutgoingLinks() || LogItem::isDebug())) {
Real32* buffer = reinterpret_cast<Real32*>(out->getData().getBuffer());
buffer[0] = tm_->anomaly;
NTA_DEBUG << "anomaly " << *out << std::endl;
}
buffer[0] = tm_->anomaly; //only the first field is valid
NTA_DEBUG << "compute "<< *out << std::endl;

out = getOutput("predictiveCells");
if (out && (out->hasOutgoingLinks() || LogItem::isDebug())) {
out->getData().getSDR() = tm_->getPredictiveCells();
NTA_DEBUG << "predictive " << *out << std::endl;
}
NTA_DEBUG << "compute " << *out << std::endl;
}


Expand Down
8 changes: 8 additions & 0 deletions src/htm/regions/TestNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ void TestNode::compute() {
Array &inputArray = bottomUpIn_->getData();
Real64* inputBuffer = (Real64*)inputArray.getBuffer();
size_t count = inputArray.getCount();

// trace facility
NTA_DEBUG << "compute " << bottomUpIn_ << std::endl;


// See TestNode.hpp for description of the computation

Expand All @@ -198,6 +202,10 @@ void TestNode::compute() {
}
}

// trace facility
NTA_DEBUG << "compute " << bottomUpOut_ << "\n";


iter_++;
}

Expand Down
3 changes: 2 additions & 1 deletion src/htm/regions/VectorFileEffector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void VectorFileEffector::initialize() {
}

void VectorFileEffector::compute() {
NTA_DEBUG << "VectorFileEffector compute() input: " << *region_->getInput("dataIn") << "\n";
// trace facility
NTA_DEBUG << "compute " << *region_->getInput("dataIn") << "\n";
dataIn_ = region_->getInput("dataIn")->getData();
// It's not necessarily an error to have no inputs. In this case we just
// return
Expand Down
12 changes: 9 additions & 3 deletions src/htm/regions/VectorFileSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,25 @@ void VectorFileSensor::compute() {
Real *categoryOut = reinterpret_cast<Real *>(categoryOut_.getBuffer());
vectorFile_.getRawVector((htm::UInt)curVector_, categoryOut, offset, 1);
offset++;
NTA_DEBUG << "VectorFileSensor compute() CategoryOut= " << *region_->getOutput("categoryOut") << "\n";

// trace facility
NTA_DEBUG << "compute " << region_->getOutput("categoryOut") << std::endl;
}

if (hasResetOut_) {
resetOut_ = region_->getOutput("resetOut")->getData();
Real *resetOut = reinterpret_cast<Real *>(resetOut_.getBuffer());
vectorFile_.getRawVector((htm::UInt)curVector_, resetOut, offset, 1);
offset++;
NTA_DEBUG << "VectorFileSensor compute() reset= " << *region_->getOutput("reset") << "\n";

// trace facility
NTA_DEBUG << "compute " << *region_->getOutput("reset") << std::endl;
}

vectorFile_.getScaledVector((htm::UInt)curVector_, out, offset, count);
NTA_DEBUG << "VectorFileSensor compute() dataOut= " << *region_->getOutput("dataOut") << "\n";

// trace facility
NTA_DEBUG << "compute " << *region_->getOutput("dataOut") << std::endl;
iterations_++;
}

Expand Down
Loading

0 comments on commit 32ab02c

Please sign in to comment.