Example #1
0
    def _save_new_trials_to_db_if_possible(
        self,
        experiment: Experiment,
        trials: List[BaseTrial],
        suppress_all_errors: bool = False,
    ) -> bool:
        """Saves new trials on given experiment if DB settings are set on this
        `WithDBSettingsBase` instance.

        Args:
            experiment: Experiment, on which to save new trials in DB.
            trials: Newly added trials to save.
            suppress_all_errors: Flag for `retry_on_exception` that makes
                the decorator suppress the thrown exception even if it
                occurred in all the retries (exception is still logged).

        Returns:
            bool: Whether the trials were saved.
        """
        if self.db_settings_set:
            start_time = time.time()
            _save_new_trials(experiment=experiment,
                             trials=trials,
                             encoder=self.db_settings.encoder)
            logger.debug(
                f"Saved trials {[trial.index for trial in trials]} in "
                f"{_round_floats_for_logging(time.time() - start_time)} seconds."
            )
            return True
        return False
Example #2
0
def save_new_trials(
    experiment: Experiment, trials: List[BaseTrial], db_settings: DBSettings
) -> None:
    """Save a set of new trials on an experiment in DB.

    NOTE: This function also saves data attached to experiment
    for these trials.

    Args:
        experiment: `Experiment` object.
        trials: List of trials (subclasses of `BaseTrial`: `Trial` or `BatchTrial`).
        db_settings: Defines behavior for loading/saving experiment to/from db.
    """
    init_engine_and_session_factory(creator=db_settings.creator, url=db_settings.url)
    start_time = time.time()
    _save_new_trials(experiment=experiment, trials=trials, encoder=db_settings.encoder)
    logger.debug(
        f"Saved trials {[trial.index for trial in trials]} in "
        f"{_round_floats_for_logging(time.time() - start_time)} seconds."
    )
Example #3
0
    def test_save_updated_trial(self):
        experiment, _ = self.init_experiment_and_generation_strategy(
            save_generation_strategy=False)

        exp = _load_experiment(
            experiment.name, decoder=self.with_db_settings.db_settings.decoder)
        trial = exp.new_trial()
        _save_new_trials(
            experiment=experiment,
            trials=[trial],
            encoder=self.with_db_settings.db_settings.encoder,
        )
        self.assertEqual(trial.status, TrialStatus.CANDIDATE)

        trial.mark_running(True)
        saved = self.with_db_settings._save_updated_trial_to_db_if_possible(
            exp, trial)
        self.assertTrue(saved)
        exp = _load_experiment(
            experiment.name, decoder=self.with_db_settings.db_settings.decoder)
        self.assertEqual(len(exp.trials), 1)
        self.assertEqual(exp.trials[0].status, TrialStatus.RUNNING)