def test_get_partial_chain(): def f(a, b): return a+b g = partial(f, b=3) h = partial(g, a=2) if sys.version_info < (3, 0): # Python 2.X assert [f, g, h] == get_partial_chain(h) else: # Python 3.X base, part = get_partial_chain(h) assert base is f assert part.keywords == {'a': 2, 'b': 3}
def test_get_partial_chain(): def f(a, b): return a + b g = partial(f, b=3) h = partial(g, a=2) assert [f, g, h] == get_partial_chain(h)
def get_root_function(self): return get_partial_chain(self.function)[0]
def run_and_record(function, experiment_id, print_to_console=True, show_figs=None, test_mode=None, keep_record=None, raise_exceptions=True, notes=(), prefix=None, **experiment_record_kwargs): """ Run an experiment function. Save the console output, return values, and any matplotlib figures generated to a new experiment folder in ~/.artemis/experiments :param function: A function which takes no args. :param experiment_id: The name under which you'd like to save this run of this experiment. :param print_to_console: Show the print output in the console (as well as saving it) :param show_figs: :param test_mode: :param keep_record: :param raise_exceptions: :param notes: :param experiment_record_kwargs: :return: The ExperimentRecord object """ if test_mode is None: test_mode = is_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=experiment_id)) EIF = ExpInfoFields date = datetime.now() with record_experiment(name=experiment_id, print_to_console=print_to_console, show_figs=show_figs, use_temp_dir=not keep_record, date=date, prefix=prefix, **experiment_record_kwargs) as exp_rec: start_time = time.time() try: exp_rec.info.set_field(ExpInfoFields.NAME, experiment_id) exp_rec.info.set_field(ExpInfoFields.ID, exp_rec.get_id()) exp_rec.info.set_field(ExpInfoFields.DIR, exp_rec.get_dir()) root_function = get_partial_chain(function)[0] args, undefined_args = get_defined_and_undefined_args(function) assert len( undefined_args ) == 0, "Required arguments {} are still undefined!".format( undefined_args) try: exp_rec.info.set_field(EIF.ARGS, get_serialized_args(args)) except PicklingError as err: ARTEMIS_LOGGER.error( 'Could not pickle arguments for experiment: {}. Artemis demands that arguments be piclable. If they are not, just make a new function.' ) raise exp_rec.info.set_field(EIF.FUNCTION, root_function.__name__) exp_rec.info.set_field(EIF.TIMESTAMP, 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))) exp_rec.info.set_field(EIF.PID, os.getpid()) exp_rec.info.set_field(EIF.ARTEMIS_VERSION, ARTEMIS_VERSION) if inspect.isgeneratorfunction(root_function): for result in function(): exp_rec.save_result(result) yield exp_rec else: result = function() exp_rec.save_result(result) 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: yield exp_rec return 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) for n in notes: exp_rec.info.add_note(n) ARTEMIS_LOGGER.info( '{border} Done {mode} Experiment: {name} {border}'.format( border='=' * 10, mode="Testing" if test_mode else "Running", name=experiment_id)) set_test_mode(old_test_mode) yield exp_rec
def run_and_record(function, experiment_id, print_to_console=True, show_figs=None, test_mode=None, keep_record=None, raise_exceptions=True, notes = (), prefix=None, **experiment_record_kwargs): """ Run an experiment function. Save the console output, return values, and any matplotlib figures generated to a new experiment folder in ~/.artemis/experiments :param function: A function which takes no args. :param experiment_id: The name under which you'd like to save this run of this experiment. :param print_to_console: Show the print output in the console (as well as saving it) :param show_figs: :param test_mode: :param keep_record: :param raise_exceptions: :param notes: :param experiment_record_kwargs: :return: The ExperimentRecord object """ if test_mode is None: test_mode = is_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=experiment_id)) EIF = ExpInfoFields date = datetime.now() with record_experiment(name=experiment_id, print_to_console=print_to_console, show_figs=show_figs, use_temp_dir=not keep_record, date=date, prefix=prefix, **experiment_record_kwargs) as exp_rec: start_time = time.time() try: exp_rec.info.set_field(ExpInfoFields.NAME, experiment_id) exp_rec.info.set_field(ExpInfoFields.ID, exp_rec.get_id()) exp_rec.info.set_field(ExpInfoFields.DIR, exp_rec.get_dir()) root_function = get_partial_chain(function)[0] args, undefined_args = get_defined_and_undefined_args(function) assert len(undefined_args)==0, "Required arguments {} are still undefined!".format(undefined_args) try: exp_rec.info.set_field(EIF.ARGS, get_serialized_args(args)) except PicklingError as err: ARTEMIS_LOGGER.error('Could not pickle arguments for experiment: {}. Artemis demands that arguments be piclable. If they are not, just make a new function.') raise exp_rec.info.set_field(EIF.FUNCTION, root_function.__name__) exp_rec.info.set_field(EIF.TIMESTAMP, 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))) exp_rec.info.set_field(EIF.PID, os.getpid()) exp_rec.info.set_field(EIF.ARTEMIS_VERSION, ARTEMIS_VERSION) if inspect.isgeneratorfunction(root_function): for result in function(): exp_rec.save_result(result) yield exp_rec else: result = function() exp_rec.save_result(result) 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: yield exp_rec return 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) for n in notes: exp_rec.info.add_note(n) ARTEMIS_LOGGER.info('{border} Done {mode} Experiment: {name} {border}'.format(border='=' * 10, mode="Testing" if test_mode else "Running", name=experiment_id)) set_test_mode(old_test_mode) yield exp_rec