Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable pickle tests #821

Merged
merged 24 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fd4ad3b
test: enable py TM test
breznak Jun 2, 2020
ec3c3c0
ScalarEncoder: py add pickle serialization
breznak Jun 2, 2020
fbccfa4
SDR: enable py test pickle
breznak Jun 2, 2020
762ab12
ScalarEncoder: fix de/serialization.
breznak Jun 2, 2020
891693e
ScalarEncoder: revert param checks
breznak Jun 2, 2020
fe076fc
Merge branch 'master' into enable_tests
breznak Jun 3, 2020
9c77607
Merge branch 'master_community' into enable_tests
breznak Jun 4, 2020
1b41667
Merge remote-tracking branch 'community/enable_tests' into enable_tests
breznak Jun 4, 2020
b0c63e6
bump gtest to latest release 1.10.0
breznak Jun 4, 2020
ca06206
Merge branch 'master' into enable_tests
breznak Jun 4, 2020
6e6fa1c
Merge branch 'master' into enable_tests
breznak Jul 9, 2020
27752d6
Merge branch 'master_community' into enable_tests
breznak Sep 2, 2020
047341d
Merge remote-tracking branch 'community/enable_tests' into enable_tests
breznak Sep 2, 2020
a577384
revert gtest to version 1.8.1
breznak Sep 3, 2020
fbe6ce5
WIP Serialization bug precesion float to string
breznak Sep 3, 2020
9b78a70
Merge branch 'master_community' into enable_tests
breznak Sep 8, 2020
c3e0fbf
Merge branch 'master' into enable_tests
breznak Feb 18, 2021
fb2d27a
Merge branch 'master_community' into enable_tests
breznak Feb 18, 2021
f776bfe
Merge branch 'master_community' into enable_tests
breznak Feb 20, 2021
daf28b2
gtest: switch to official google/gtest repo
breznak Feb 20, 2021
46231e9
Merge remote-tracking branch 'community/enable_tests' into enable_tests
breznak Feb 20, 2021
8310c0c
Merge branch 'master' into enable_tests
ctrl-z-9000-times May 5, 2021
ed4ac1e
Revert changes to src/htm/types/Serializable.hpp
ctrl-z-9000-times May 5, 2021
8be24fb
Fix ScalarEncoder serialization
ctrl-z-9000-times May 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ distribution packages as listed and rename them as indicated. Copy these to
| :--------------------- | :----------------- |
| libyaml.zip (*node1) | https://github.com/yaml/libyaml/archive/master.zip |
| boost.tar.gz (*note3) | https://dl.bintray.com/boostorg/release/1.72.0/source/boost_1_72_0.tar.gz |
| googletest.tar.gz | https://github.com/google/googletest/archive/release-1.8.1.tar.gz |
| eigen.tar.bz2 | https://gitlab.com/libeigen/eigen/-/archive/3.3.7/eigen-3.3.7.tar.bz2 |
| googletest.tar.gz | https://github.com/abseil/googletest/archive/release-1.8.1.tar.gz |
| mnist.zip (*note4) | https://github.com/wichtounet/mnist/archive/master.zip |
| pybind11.tar.gz | https://github.com/pybind/pybind11/archive/v2.4.2.tar.gz |
| cereal.tar.gz | https://github.com/USCiLab/cereal/archive/v1.2.2.tar.gz |
Expand Down
37 changes: 37 additions & 0 deletions bindings/py/cpp_src/bindings/encoders/py_ScalarEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,42 @@ fields are filled in automatically.)");
self.encode( value, *output );
return output; },
R"()");


// pickle
py_ScalarEnc.def(py::pickle(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added pickle for ScalarEncoder

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing pickle and adding some unit tests!

[](const ScalarEncoder& enc) // __getstate__
{
std::stringstream ss;
enc.save(ss);

/* The values in stringstream are binary so pickle will get confused
* trying to treat it as utf8 if you just return ss.str().
* So we must treat it as py::bytes. Some characters could be null values.
*/
return py::bytes( ss.str() );
},

[](py::bytes &s) // __setstate__
{
/* pybind11 will pass in the bytes array without conversion.
* so we should be able to just create a string to initalize the stringstream.
*/
std::stringstream ss( s.cast<std::string>() );
std::unique_ptr<ScalarEncoder> enc(new ScalarEncoder());
enc->load(ss);

/*
* The __setstate__ part of the py::pickle() is actually a py::init() with some options.
* So the return value can be the object returned by value, by pointer,
* or by container (meaning a unique_ptr). SP has a problem with the copy constructor
* and pointers have problems knowing who the owner is so lets use unique_ptr.
* See: https://pybind11.readthedocs.io/en/stable/advanced/classes.html#custom-constructors
*/
return enc;
}));



}
}
1 change: 0 additions & 1 deletion bindings/py/tests/algorithms/temporal_memory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def testNupicTemporalMemoryPickling(self):
"Simple NuPIC TemporalMemory pickle/unpickle failed.")


@pytest.mark.skip(reason="Fails with rapidjson internal assertion -- indicates a bad serialization")
def testNupicTemporalMemorySavingToString(self):
"""Test writing to and reading from TemporalMemory."""
inputs = SDR( 100 ).randomize( .05 )
Expand Down
25 changes: 23 additions & 2 deletions bindings/py/tests/encoders/scalar_encoder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,27 @@ def testStatistics(self):
assert( mtr.activationFrequency.max() < 1.75 * .10 )
assert( mtr.overlap.min() > .85 )

@pytest.mark.skip(reason="Known issue: https://github.com/htm-community/htm.core/issues/160")
def testPickle(self):
assert(False) # TODO: Unimplemented
p = ScalarEncoderParameters()
p.size = 100
p.activeBits = 10
p.minimum = 0
p.maximum = 20
p.clipInput = True

enc = ScalarEncoder( p )
import pickle

picklestr = pickle.dumps(enc)
enc2 = pickle.loads(picklestr)
#assert enc.parameters == enc2.parameters

assert enc.size == enc2.size

out = SDR( enc.parameters.size )
out2 = SDR( enc2.parameters.size )
enc.encode(10, out)
enc2.encode(10, out2)
assert out == out2
#TODO add enc == enc2

1 change: 0 additions & 1 deletion bindings/py/tests/sdr/SDR_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ def testStr(self):
B.dense = B.dense
assert(str(B) == "SDR( 100, 100, 1 ) 0, 9999")

@pytest.mark.skip(reason="Known issue: https://github.com/htm-community/htm.core/issues/160")
def testPickle(self):
for sparsity in (0, .3, 1):
A = SDR((103,))
Expand Down
2 changes: 1 addition & 1 deletion external/gtest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
if(EXISTS "${REPOSITORY_DIR}/build/ThirdParty/share/googletest.tar.gz")
set(URL "${REPOSITORY_DIR}/build/ThirdParty/share/googletest.tar.gz")
else()
set(URL https://github.com/abseil/googletest/archive/release-1.10.0.tar.gz)
set(URL https://github.com/google/googletest/archive/release-1.10.0.tar.gz)
endif()

#
Expand Down
14 changes: 8 additions & 6 deletions src/htm/encoders/ScalarEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void ScalarEncoder::initialize(const ScalarEncoderParameters &parameters)
NTA_CHECK( num_active_args != 0u )
<< "Missing argument, need one of: 'activeBits' or 'sparsity'.";
NTA_CHECK( num_active_args == 1u )
<< "Too many arguments, choose only one of: 'activeBits' or 'sparsity'.";
<< "Specified both: 'activeBits' and 'sparsity'. Specify only one of them.";

UInt num_size_args = 0;
if( parameters.size > 0u) { num_size_args++; }
Expand All @@ -51,7 +51,7 @@ void ScalarEncoder::initialize(const ScalarEncoderParameters &parameters)
NTA_CHECK( num_size_args != 0u )
<< "Missing argument, need one of: 'size', 'radius', 'resolution', 'category'.";
NTA_CHECK( num_size_args == 1u )
<< "Too many arguments, choose only one of: 'size', 'radius', 'resolution', 'category'.";
<< "Too many arguments specified: 'size', 'radius', 'resolution', 'category'. Choose only one of them.";

if( parameters.periodic ) {
NTA_CHECK( not parameters.clipInput )
Expand Down Expand Up @@ -79,9 +79,9 @@ void ScalarEncoder::initialize(const ScalarEncoderParameters &parameters)
if( args_.sparsity > 0.0f ) {
NTA_CHECK( parameters.sparsity >= 0.0f );
NTA_CHECK( parameters.sparsity <= 1.0f );
NTA_CHECK( args_.size > 0u )
<< "Argument 'sparsity' requires that the 'size' also be given.";
args_.activeBits = (UInt) round( args_.size * args_.sparsity );
NTA_CHECK( args_.size > 0u ) << "Argument 'sparsity' requires that the 'size' also be given.";
args_.activeBits = static_cast<UInt>(round( args_.size * args_.sparsity ));
NTA_CHECK(args_.activeBits > 0) << "sparsity and size must be given so that sparsity * size > 0!";
}

// Determine resolution & size.
Expand Down Expand Up @@ -126,9 +126,11 @@ void ScalarEncoder::initialize(const ScalarEncoderParameters &parameters)

// Determine radius. Always calculate this even if it was given, to correct for rounding error.
args_.radius = args_.activeBits * args_.resolution;
NTA_CHECK(args_.radius > 0);

// Determine sparsity. Always calculate this even if it was given, to correct for rounding error.
args_.sparsity = (Real) args_.activeBits / args_.size;
args_.sparsity = args_.activeBits / static_cast<Real>(args_.size);
NTA_CHECK(args_.sparsity > 0);


// Initialize parent class.
Expand Down
2 changes: 2 additions & 0 deletions src/htm/encoders/ScalarEncoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ namespace htm {
ar(cereal::make_nvp("size", args_.size));
ar(cereal::make_nvp("radius", args_.radius));
ar(cereal::make_nvp("resolution", args_.resolution));

BaseEncoder<Real64>::initialize({ args_.size });
}

~ScalarEncoder() override {};
Expand Down
2 changes: 1 addition & 1 deletion src/test/unit/encoders/ScalarEncoderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ TEST(ScalarEncoder, Serialization) {
std::stringstream buf;
enc1->save( buf, JSON );

//std::cerr << "SERIALIZED:" << std::endl << buf.str() << std::endl;
std::cerr << "SERIALIZED:" << std::endl << buf.str() << std::endl;
buf.seekg(0);

ScalarEncoder enc2;
Expand Down