Skip to content

Commit

Permalink
Merge pull request #608 from htm-community/serialization_rdse
Browse files Browse the repository at this point in the history
RDSE: serialization for py bindings
  • Loading branch information
breznak authored Sep 4, 2019
2 parents 53d494f + 4e29af9 commit 1946c86
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
41 changes: 41 additions & 0 deletions bindings/py/cpp_src/bindings/encoders/py_RDSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

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

#include <htm/encoders/RandomDistributedScalarEncoder.hpp>

namespace py = pybind11;

using namespace htm;
using namespace std;

namespace htm_ext
{
Expand Down Expand Up @@ -113,5 +115,44 @@ fields are filled in automatically.)");
self.encode(value, *sdr);
return sdr;
});


// Serialization
// loadFromString
py_RDSE.def("loadFromString", [](RDSE& self, const py::bytes& inString) {
std::stringstream inStream(inString.cast<std::string>());
self.load(inStream, JSON);
});

// writeToString
py_RDSE.def("writeToString", [](const RDSE& self) {
std::ostringstream os;
os.flags(ios::scientific);
os.precision(numeric_limits<double>::digits10 + 1);
self.save(os, JSON); // see serialization in bindings for SP, py_SpatialPooler.cpp for explanation
return py::bytes( os.str() );
});

// pickle
py_RDSE.def(py::pickle(
[](const RDSE& self) {
std::stringstream ss;
self.save(ss);
return py::bytes( ss.str() );
},
[](py::bytes &s) {
std::stringstream ss( s.cast<std::string>() );
std::unique_ptr<RDSE> self(new RDSE());
self->load(ss);
return self;
}));

py_RDSE.def("saveToFile",
[](RDSE &self, const std::string& filename) { self.saveToFile(filename, SerializableFormat::BINARY); });

py_RDSE.def("loadFromFile",
[](RDSE &self, const std::string& filename) { return self.loadFromFile(filename, SerializableFormat::BINARY); });


}
}
42 changes: 40 additions & 2 deletions bindings/py/tests/encoders/rdse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,44 @@ def testSeed(self):
B = R.encode( 987654 )
assert( A != B )

@unittest.skip(reason="Known issue: https://github.com/htm-community/htm.core/issues/160")

def testPickle(self):
assert(False) # TODO: Unimplemented
"""
The pickling is successfull if pickle serializes and de-serialize the
RDSE object.
Moreover, the de-serialized object shall give the same SDR than the
original encoder given the same scalar value to encode.
"""
rdse_params = RDSE_Parameters()
rdse_params.sparsity = 0.1
rdse_params.size = 100
rdse_params.resolution = 0.1
rdse_params.seed = 1997

rdse = RDSE(rdse_params)
filename = "RDSE_testPickle"

try:
with open(filename, "wb") as f:
pickle.dump(rdse, f)
except:
dump_success = False
else:
dump_success = True

assert(dump_success)

try:
with open(filename, "rb") as f:
rdse_loaded = pickle.load(f)
except:
read_success = False
else:
read_success = True

assert(read_success)
value_to_encode = 69003
SDR_original = rdse.encode(value_to_encode)
SDR_loaded = rdse_loaded.encode(value_to_encode)

assert(SDR_original == SDR_loaded)

0 comments on commit 1946c86

Please sign in to comment.