Beispiel #1
0
    def testErrorChecks(self):
        params1 = RDSE_Parameters()
        params1.size     = 100
        params1.sparsity = .10
        params1.radius   = 10
        R1 = RDSE( params1 )
        A = SDR([10, 10])
        R1.encode( 33, A )

        # Test wrong input dimensions
        B = SDR( 1 )
        with self.assertRaises(RuntimeError):
            R1.encode( 3, B )

        # Test invalid parameters, size == 0
        params1.size = 0
        with self.assertRaises(RuntimeError):
            RDSE( params1 )
        params1.size = 100

        # Test invalid parameters, activeBits == 0
        params1.activeBits = 0
        params1.sparsity = 0.00001 # Rounds to zero!
        with self.assertRaises(RuntimeError):
            RDSE( params1 )

        # Test missing activeBits
        params2 = RDSE_Parameters()
        params2.size     = 100
        params2.radius   = 10
        with self.assertRaises(RuntimeError):
            RDSE( params2 )
        # Test missing resolution/radius
        params3 = RDSE_Parameters()
        params3.size       = 100
        params3.activeBits = 10
        with self.assertRaises(RuntimeError):
            RDSE( params3 )

        # Test too many parameters: activeBits & sparsity
        params4 = RDSE_Parameters()
        params4.size       = 100
        params4.sparsity   = .6
        params4.activeBits = 10
        params4.radius     = 4
        with self.assertRaises(RuntimeError):
            RDSE( params4 )
        # Test too many parameters: resolution & radius
        params5 = RDSE_Parameters()
        params5.size       = 100
        params5.activeBits = 10
        params5.radius     = 4
        params5.resolution = 4
        with self.assertRaises(RuntimeError):
            RDSE( params5 )
Beispiel #2
0
    def testRadiusResolution(self):
        """ Check that these arguments are equivalent. """
        # radius -> resolution
        P = RDSE_Parameters()
        P.size       = 2000
        P.activeBits = 100
        P.radius     = 12
        R = RDSE( P )
        self.assertAlmostEqual( R.parameters.resolution, 12./100, places=5)

        # resolution -> radius
        P = RDSE_Parameters()
        P.size       = 2000
        P.activeBits = 100
        P.resolution = 12
        R = RDSE( P )
        self.assertAlmostEqual( R.parameters.radius, 12*100, places=5)

        # Moving 1 resolution moves 1 bit (usually)
        P = RDSE_Parameters()
        P.size       = 2000
        P.activeBits = 100
        P.resolution = 3.33
        P.seed       = 42
        R = RDSE( P )
        sdrs = []
        for i in range(100):
            X = i * ( R.parameters.resolution )
            sdrs.append( R.encode( X ) )
            print("X", X, sdrs[-1])
        moved_one = 0
        moved_one_samples = 0
        for A, B in zip(sdrs, sdrs[1:]):
            delta = A.getSum() - A.getOverlap( B )
            if A.getSum() == B.getSum():
                assert( delta < 2 )
                moved_one += delta
                moved_one_samples += 1
        assert( moved_one >= .9 * moved_one_samples )
Beispiel #3
0
    def testPickle(self):
        """
        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)
Beispiel #4
0
    def testJSONSerialization(self):
        """
        This test is to insure that Python can access the C++ serialization functions.
        Serialization is tested more completely in C++ unit tests. Just checking 
        that Python can access it.
        """
        rdse_params = RDSE_Parameters()
        rdse_params.sparsity = 0.1
        rdse_params.size = 1000
        rdse_params.resolution = 0.1
        rdse_params.seed = 1997

        rdse = RDSE(rdse_params)
        filename = 'RDSE_testPickle'
        rdse.saveToFile(filename, 'JSON')

        rdse_loaded = RDSE()
        rdse_loaded.loadFromFile(filename, 'JSON')

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

        assert (SDR_original == SDR_loaded)