def test_hyperparams_repository_should_create_new_trial(tmpdir): hyperparams_json_repository = HyperparamsJSONRepository(tmpdir) hyperparams = HyperparameterSamples(HYPERPARAMS) hyperparams_json_repository.create_new_trial(hyperparams) trial_hash = hyperparams_json_repository._get_trial_hash( hyperparams.to_flat_dict()) file_name = 'NEW_' + trial_hash + '.json' path = os.path.join(tmpdir, file_name) with open(path) as f: trial_json = json.load(f) assert trial_json['hyperparams'] == hyperparams.to_flat_dict() assert trial_json['score'] is None
def test_hyperparams_repository_should_save_success_trial(tmpdir): hyperparams_json_repository = HyperparamsJSONRepository(tmpdir) hyperparams = HyperparameterSamples(HYPERPARAMS) hyperparams_json_repository._save_successful_trial_json( hyperparams, FIRST_SCORE_FOR_TRIAL) trial_hash = hyperparams_json_repository._get_trial_hash( hyperparams.to_flat_dict()) file_name = str(float(FIRST_SCORE_FOR_TRIAL)).replace( '.', ',') + '_' + trial_hash + '.json' path = os.path.join(tmpdir, file_name) with open(path) as f: trial_json = json.load(f) assert trial_json['hyperparams'] == hyperparams.to_flat_dict() assert trial_json['score'] == FIRST_SCORE_FOR_TRIAL
def test_hyperparams_repository_should_save_failed_trial(tmpdir): hyperparams_json_repository = HyperparamsJSONRepository(tmpdir) hyperparams = HyperparameterSamples(HYPERPARAMS) hyperparams_json_repository._save_failed_trial_json( hyperparams, Exception('trial failed')) trial_hash = hyperparams_json_repository._get_trial_hash( hyperparams.to_flat_dict()) file_name = 'FAILED_' + trial_hash + '.json' path = os.path.join(tmpdir, file_name) with open(path) as f: trial_json = json.load(f) assert trial_json['hyperparams'] == hyperparams.to_flat_dict() assert 'exception' in trial_json.keys() assert trial_json['score'] is None
class Trial: def __init__(self, hyperparams: HyperparameterSamples, score: float, status: TRIAL_STATUS): self.hyperparams = HyperparameterSamples(hyperparams) self.score = score self.status = status def to_json(self) -> dict: return { 'hyperparams': self.hyperparams.to_flat_dict(), 'score': self.score, 'status': self.status } @staticmethod def from_json(trial_json) -> 'Trial': return Trial( hyperparams=trial_json['hyperparams'], score=trial_json['score'], status=trial_json['status'] ) def __str__(self): return self.__repr__() def __repr__(self): s = "Trial.from_json({})".format(str(self.to_json())) return s
def test_hyperparams_to_flat(): dict_values = { 'hp': 1, 'stepa': { 'hp': 2, 'stepb': { 'hp': 3 } } } r = HyperparameterSamples(**dict_values) r = r.to_flat_dict() expected_dict_values = { 'hp': 1, 'stepa__hp': 2, 'stepa__stepb__hp': 3 } assert r == expected_dict_values