def do_io(self): _, temp_filename = mkstemp() remove(temp_filename) self.model_serial.save(temp_filename, include_training_data=False) with self.assertRaises(IOError): self.model_serial.save(temp_filename, overwrite=False) names = ("_data_attributes", "_trained_attributes", "_descriptive_attributes") attrs = ( self.model_serial._data_attributes, self.model_serial._trained_attributes, self.model_serial._descriptive_attributes ) for name, item in zip(names, attrs): _ = [] + list(item) _.append("metadata") setattr(self.model_serial, name, _) with self.assertRaises(ValueError): self.model_serial.save(temp_filename, overwrite=True) setattr(self.model_serial, name, _[:-1]) self.model_serial.save(temp_filename, include_training_data=True, overwrite=True) self.model_parallel.reset() self.model_parallel.load(temp_filename, verify_training_data=True) # Check that the trained attributes in both model are equal. for _attribute in self.model_serial._trained_attributes: # And nearly as we expected. self.assertTrue(np.allclose( getattr(self.model_serial, _attribute), getattr(self.model_parallel, _attribute) )) self.assertTrue(np.allclose( self.test_data_set[_attribute[1:]], getattr(self.model_serial, _attribute))) #rtol=0.5, atol=1e-8)) # Check that the data attributes in both model are equal. for _attribute in self.model_serial._data_attributes: self.assertTrue( utils.short_hash(getattr(self.model_serial, _attribute)), utils.short_hash(getattr(self.model_parallel, _attribute)) ) # Alter the hash and expect failure kwds = { "encoding": "latin1" } if sys.version_info[0] >= 3 else {} with open(temp_filename, "rb") as fp: contents = pickle.load(fp, **kwds) contents["training_set_hash"] = "" with open(temp_filename, "wb") as fp: pickle.dump(contents, fp, -1) with self.assertRaises(ValueError): self.model_serial.load(temp_filename, verify_training_data=True) if path.exists(temp_filename): remove(temp_filename)
def do_io(self): _, temp_filename = mkstemp() remove(temp_filename) self.model_serial.save(temp_filename, include_training_data=False) with self.assertRaises(IOError): self.model_serial.save(temp_filename, overwrite=False) names = ("_data_attributes", "_trained_attributes", "_descriptive_attributes") attrs = (self.model_serial._data_attributes, self.model_serial._trained_attributes, self.model_serial._descriptive_attributes) for name, item in zip(names, attrs): _ = [] + list(item) _.append("metadata") setattr(self.model_serial, name, _) with self.assertRaises(ValueError): self.model_serial.save(temp_filename, overwrite=True) setattr(self.model_serial, name, _[:-1]) self.model_serial.save(temp_filename, include_training_data=True, overwrite=True) self.model_parallel.reset() self.model_parallel.load(temp_filename, verify_training_data=True) # Check that the trained attributes in both model are equal. for _attribute in self.model_serial._trained_attributes: # And nearly as we expected. self.assertTrue( np.allclose(getattr(self.model_serial, _attribute), getattr(self.model_parallel, _attribute))) self.assertTrue( np.allclose(self.test_data_set[_attribute[1:]], getattr(self.model_serial, _attribute))) #rtol=0.5, atol=1e-8)) # Check that the data attributes in both model are equal. for _attribute in self.model_serial._data_attributes: self.assertTrue( utils.short_hash(getattr(self.model_serial, _attribute)), utils.short_hash(getattr(self.model_parallel, _attribute))) # Alter the hash and expect failure kwds = {"encoding": "latin1"} if sys.version_info[0] >= 3 else {} with open(temp_filename, "rb") as fp: contents = pickle.load(fp, **kwds) contents["training_set_hash"] = "" with open(temp_filename, "wb") as fp: pickle.dump(contents, fp, -1) with self.assertRaises(ValueError): self.model_serial.load(temp_filename, verify_training_data=True) if path.exists(temp_filename): remove(temp_filename)
def test_hashing(self): # So that it does not change with time and break old hashes. self.assertEqual("7fc56270e79d5ed678fe0d61f8370cf623e75af33a3ea00cfc", utils.short_hash("ABCDE"))
def test_different(self): self.assertNotEqual(utils.short_hash(True), utils.short_hash(False))