def test_experiment_with():
    """
    DEPRECATED INTERFACE

    This syntax uses the record_experiment function directly instead of hiding it.
    """

    with experiment_testing_context():
        delete_experiment_with_id('test_exp')
        with record_experiment(identifier = 'test_exp', print_to_console=True) as exp_rec:
            experiment_test_function()
            assert_experiment_record_is_correct(exp_rec, show_figures=False)
Example #2
0
def test_experiment_with():
    """
    DEPRECATED INTERFACE

    This syntax uses the record_experiment function directly instead of hiding it.
    """

    with experiment_testing_context():
        delete_experiment_with_id('test_exp')
        with record_experiment(identifier='test_exp',
                               print_to_console=True) as exp_rec:
            experiment_test_function()
            assert_experiment_record_is_correct(exp_rec, show_figures=False)
Example #3
0
    def run(self,
            print_to_console=True,
            show_figs=None,
            test_mode=None,
            keep_record=None,
            raise_exceptions=True,
            display_results=True,
            **experiment_record_kwargs):
        """
        Run the experiment, and return the ExperimentRecord that is generated.

        :param print_to_console: Print to console (as well as logging to file)
        :param show_figs: Show figures (as well as saving to file)
        :param test_mode: Run in "test_mode".  This sets the global "test_mode" flag when running the experiment.  This
            flag can be used to, for example, shorten a training session to verify that the code runs.  Can be:
            True: Run in test mode
            False: Don't run in test mode:
            None: Keep the current state of the global "is_test_mode()" flag.
        :param keep_record: Keep the folder that results are saved into.
            True: Results are saved into a folder
            False: Results folder is deleted at the end.
            None: If "test_mode" is true, then delete results at end, otherwise save them.
        :param raise_exceptions: True to raise any exception that occurs when running the experiment.  False to catch it,
            print the error, and move on.
        :param experiment_record_kwargs: Passed to the "record_experiment" context.
        :return: The ExperimentRecord object, if keep_record is true, otherwise None
        """
        if test_mode is None:
            test_mode = is_test_mode()
        if keep_record is None:
            keep_record = keep_record_by_default if keep_record_by_default is not None else not test_mode

        old_test_mode = is_test_mode()
        set_test_mode(test_mode)
        ARTEMIS_LOGGER.info(
            '{border} {mode} Experiment: {name} {border}'.format(
                border='=' * 10,
                mode="Testing" if test_mode else "Running",
                name=self.name))
        EIF = ExpInfoFields
        date = datetime.now()
        with record_experiment(name=self.name,
                               print_to_console=print_to_console,
                               show_figs=show_figs,
                               use_temp_dir=not keep_record,
                               date=date,
                               **experiment_record_kwargs) as exp_rec:
            start_time = time.time()
            try:

                exp_rec.info.set_field(ExpInfoFields.NAME, self.name)
                exp_rec.info.set_field(ExpInfoFields.ID, exp_rec.get_id())
                exp_rec.info.set_field(ExpInfoFields.DIR, exp_rec.get_dir())
                exp_rec.info.set_field(EIF.ARGS, self.get_args().items())
                root_function = self.get_root_function()
                exp_rec.info.set_field(EIF.FUNCTION, root_function.__name__)
                exp_rec.info.set_field(EIF.TIMESTAMP, str(date))
                module = inspect.getmodule(root_function)
                exp_rec.info.set_field(EIF.MODULE, module.__name__)
                exp_rec.info.set_field(
                    EIF.FILE, module.__file__
                    if hasattr(module, '__file__') else '<unknown>')
                exp_rec.info.set_field(EIF.STATUS, ExpStatusOptions.STARTED)
                exp_rec.info.set_field(EIF.USER, getuser())
                exp_rec.info.set_field(
                    EIF.MAC, ':'.join(("%012X" % getnode())[i:i + 2]
                                      for i in range(0, 12, 2)))
                results = self.function()
                exp_rec.info.set_field(EIF.STATUS, ExpStatusOptions.FINISHED)
            except KeyboardInterrupt:
                exp_rec.info.set_field(EIF.STATUS, ExpStatusOptions.STOPPED)
                exp_rec.write_error_trace(print_too=False)
                raise
            except Exception:
                exp_rec.info.set_field(EIF.STATUS, ExpStatusOptions.ERROR)
                exp_rec.write_error_trace(print_too=not raise_exceptions)
                if raise_exceptions:
                    raise
                else:
                    return exp_rec
            finally:
                exp_rec.info.set_field(EIF.RUNTIME, time.time() - start_time)
                fig_locs = exp_rec.get_figure_locs(include_directory=False)
                exp_rec.info.set_field(EIF.N_FIGS, len(fig_locs))
                exp_rec.info.set_field(EIF.FIGS, fig_locs)

        exp_rec.save_result(results)
        for n in self._notes:
            exp_rec.info.add_note(n)
        if display_results and self.display_function is not None:
            self.display_function(results)
        ARTEMIS_LOGGER.info(
            '{border} Done {mode} Experiment: {name} {border}'.format(
                border='=' * 10,
                mode="Testing" if test_mode else "Running",
                name=self.name))
        set_test_mode(old_test_mode)
        return exp_rec
Example #4
0
def start_experiment(*args, **kwargs):
    global _CURRENT_EXPERIMENT_CONTEXT
    _CURRENT_EXPERIMENT_CONTEXT = record_experiment(*args, **kwargs)
    return _CURRENT_EXPERIMENT_CONTEXT.__enter__()