def test_as_pickle(self): # given obj = {"a": [b"xyz", 34], "b": 1246} # when file = File.as_pickle(obj) # then self.assertEqual(file.extension, "pkl") self.assertEqual(file.content, pickle.dumps(obj))
def _log_study(run, study: optuna.Study): try: if type(study._storage) is optuna.storages._in_memory.InMemoryStorage: """pickle and log the study object to the 'study/study.pkl' path""" run['study/study_name'] = study.study_name run['study/storage_type'] = 'InMemoryStorage' run['study/study'] = File.as_pickle(study) pass else: run['study/study_name'] = study.study_name if isinstance(study._storage, optuna.storages.RedisStorage): run['study/storage_type'] = "RedisStorage" run['study/storage_url'] = study._storage._url elif isinstance(study._storage, optuna.storages._CachedStorage): run['study/storage_type'] = "RDBStorage" # apparently CachedStorage typically wraps RDBStorage run['study/storage_url'] = study._storage._backend.url elif isinstance(study._storage, optuna.storages.RDBStorage): run['study/storage_type'] = "RDBStorage" run['study/storage_url'] = study._storage.url else: run['study/storage_type'] = "unknown storage type" run['study/storage_url'] = "unknown storage url" except AttributeError: pass