def test_save_model_with_read_only_cache(self): # If one or both cache elements are read-only, no saving. expected_mean_vec = numpy.array([1, 2, 3]) expected_rotation = numpy.eye(3) itq = ItqFunctor() itq.mean_vec = expected_mean_vec itq.rotation = expected_rotation # read-only mean-vec cache itq.mean_vec_cache_elem = DataMemoryElement(readonly=True) itq.rotation_cache_elem = DataMemoryElement(readonly=False) itq.save_model() self.assertEqual(itq.mean_vec_cache_elem.get_bytes(), six.b('')) self.assertEqual(itq.rotation_cache_elem.get_bytes(), six.b('')) # read-only rotation cache itq.mean_vec_cache_elem = DataMemoryElement(readonly=False) itq.rotation_cache_elem = DataMemoryElement(readonly=True) itq.save_model() self.assertEqual(itq.mean_vec_cache_elem.get_bytes(), six.b('')) self.assertEqual(itq.rotation_cache_elem.get_bytes(), six.b('')) # Both read-only itq.mean_vec_cache_elem = DataMemoryElement(readonly=True) itq.rotation_cache_elem = DataMemoryElement(readonly=True) itq.save_model() self.assertEqual(itq.mean_vec_cache_elem.get_bytes(), six.b('')) self.assertEqual(itq.rotation_cache_elem.get_bytes(), six.b(''))
def test_save_model_with_writable_caches(self): # If one or both cache elements are read-only, no saving. expected_mean_vec = numpy.array([1, 2, 3]) expected_rotation = numpy.eye(3) expected_mean_vec_bytes = six.BytesIO() # noinspection PyTypeChecker numpy.save(expected_mean_vec_bytes, expected_mean_vec) expected_mean_vec_bytes = expected_mean_vec_bytes.getvalue() expected_rotation_bytes = six.BytesIO() # noinspection PyTypeChecker numpy.save(expected_rotation_bytes, expected_rotation) expected_rotation_bytes = expected_rotation_bytes.getvalue() itq = ItqFunctor() itq.mean_vec = expected_mean_vec itq.rotation = expected_rotation itq.mean_vec_cache_elem = DataMemoryElement(readonly=False) itq.rotation_cache_elem = DataMemoryElement(readonly=False) itq.save_model() self.assertEqual(itq.mean_vec_cache_elem.get_bytes(), expected_mean_vec_bytes) self.assertEqual(itq.rotation_cache_elem.get_bytes(), expected_rotation_bytes)
def test_save_model_with_writable_caches(self): # If one or both cache elements are read-only, no saving. expected_mean_vec = numpy.array([1, 2, 3]) expected_rotation = numpy.eye(3) expected_mean_vec_bytes = BytesIO() # noinspection PyTypeChecker numpy.save(expected_mean_vec_bytes, expected_mean_vec) expected_mean_vec_bytes = expected_mean_vec_bytes.getvalue() expected_rotation_bytes = BytesIO() # noinspection PyTypeChecker numpy.save(expected_rotation_bytes, expected_rotation) expected_rotation_bytes = expected_rotation_bytes.getvalue() itq = ItqFunctor() itq.mean_vec = expected_mean_vec itq.rotation = expected_rotation itq.mean_vec_cache_elem = DataMemoryElement(readonly=False) itq.rotation_cache_elem = DataMemoryElement(readonly=False) itq.save_model() self.assertEqual(itq.mean_vec_cache_elem.get_bytes(), expected_mean_vec_bytes) self.assertEqual(itq.rotation_cache_elem.get_bytes(), expected_rotation_bytes)
def test_get_config_with_cache_elements(self): itq = ItqFunctor(bit_length=5, itq_iterations=6, normalize=7, random_seed=8) itq.mean_vec_cache_elem = DataMemoryElement('cached vec bytes') itq.rotation_cache_elem = DataMemoryElement('cached rot bytes') c = itq.get_config() NT.assert_equal(c['bit_length'], 5) NT.assert_equal(c['itq_iterations'], 6) NT.assert_equal(c['normalize'], 7) NT.assert_equal(c['random_seed'], 8) NT.assert_equal(c['mean_vec_cache']['type'], "DataMemoryElement") NT.assert_equal(c['mean_vec_cache']['DataMemoryElement']['bytes'], 'cached vec bytes') NT.assert_equal(c['rotation_cache']['DataMemoryElement']['bytes'], 'cached rot bytes')
def test_get_config_with_cache_elements(self): itq = ItqFunctor(bit_length=5, itq_iterations=6, normalize=7, random_seed=8) itq.mean_vec_cache_elem = DataMemoryElement('cached vec bytes') itq.rotation_cache_elem = DataMemoryElement('cached rot bytes') c = itq.get_config() self.assertEqual(c['bit_length'], 5) self.assertEqual(c['itq_iterations'], 6) self.assertEqual(c['normalize'], 7) self.assertEqual(c['random_seed'], 8) self.assertEqual(c['mean_vec_cache']['type'], "DataMemoryElement") self.assertEqual(c['mean_vec_cache']['DataMemoryElement']['bytes'], 'cached vec bytes') self.assertEqual(c['rotation_cache']['DataMemoryElement']['bytes'], 'cached rot bytes')
def test_get_config_with_cache_elements(self): itq = ItqFunctor(bit_length=5, itq_iterations=6, normalize=7, random_seed=8) itq.mean_vec_cache_elem = DataMemoryElement(b'cached vec bytes') itq.rotation_cache_elem = DataMemoryElement(b'cached rot bytes') c = itq.get_config() self.assertEqual(c['bit_length'], 5) self.assertEqual(c['itq_iterations'], 6) self.assertEqual(c['normalize'], 7) self.assertEqual(c['random_seed'], 8) self.assertEqual(c['mean_vec_cache']['type'], "DataMemoryElement") # Check using string encodings of set bytes (JSON compliant). self.assertEqual(c['mean_vec_cache']['DataMemoryElement']['bytes'], 'cached vec bytes') self.assertEqual(c['rotation_cache']['DataMemoryElement']['bytes'], 'cached rot bytes')