def test_serialization_deserialization(): basepath = 'shac' params = get_hyperparameter_list() h = hp.HyperParameterList(params) dataset = data.Dataset(h) # models clfs = [] # fit samples num_samples = 16 for i in range(3): samples = [h.sample() for _ in range(num_samples)] labels = [np.sum(sample) for sample in samples] x, y = samples, labels x, y = dataset.encode_dataset(x, y) model = xgb_utils.train_single_model(x, y) clfs.append(model) xgb_utils.save_classifiers(clfs, basepath) assert os.path.exists(os.path.join(basepath, 'classifiers', 'classifiers.pkl')) models = xgb_utils.restore_classifiers(basepath) assert len(models) == len(clfs) with pytest.raises(FileNotFoundError): models = xgb_utils.restore_classifiers('none')
def restore_data(self): """ Recover the serialized class objects by loading the dataset and the trained models from the default save directories. """ try: self.dataset = data.Dataset.load_from_directory() except FileNotFoundError: pass try: self.classifiers = xgb_utils.restore_classifiers(self.clf_dir) except FileNotFoundError: pass self.parameters = self.dataset.parameters self._dataset_index = len(self.dataset) if len(self.dataset) > 0: print("\nFound and restored dataset containing %d samples" % (len(self.dataset))) if len(self.classifiers) > 0: print("Found and restored %d classifiers" % (len(self.classifiers))) print()