def show(self, *args): """ :param user_range: A range specifying the record """ parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', help='A selection of experiment records to show. ') parser.add_argument('-l', '--last', action='store_true', help='Just select the last record from the list of experiments.') parser.add_argument('-r', '--results', default=False, action = "store_true", help="Only show records with results.") parser.add_argument('-o', '--original', default=False, action = "store_true", help="Use the original default show function: show_record") parser.add_argument('-p', '--process', default=False, action = "store_true", help="Launch show in new process") args = parser.parse_args(args) user_range = args.user_range if args.results: user_range += '@result' if args.last: user_range += '@last' records = select_experiment_records(user_range, self.exp_record_dict, flat=True) func = show_record if args.original else None if args.process: Process(target=partial(show_multiple_records, records, func)).start() else: show_multiple_records(records, func) _warn_with_prompt(use_prompt=False)
def errortrace(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) with IndentPrint("Error Traces:", show_line=True, show_end=True): for record in records: with IndentPrint(record.get_id(), show_line=True): print(record.get_error_trace()) _warn_with_prompt(use_prompt=False)
def side_by_side(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) print side_by_side([get_record_full_string(rec) for rec in records], max_linewidth=128) _warn_with_prompt(use_prompt=not self.close_after)
def show(self, *args): """ :param user_range: A range specifying the record """ parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', help='A selection of experiment records to show. ') parser.add_argument('-l', '--last', action='store_true', help='Just select the last record from the list of experiments.') parser.add_argument('-r', '--results', default=False, action = "store_true", help="Only show records with results.") parser.add_argument('-o', '--original', default=False, action = "store_true", help="Use the original default show function: show_record") parser.add_argument('-p', '--process', default=False, action = "store_true", help="Launch show in new process") args = parser.parse_args(args) user_range = args.user_range if args.results: user_range += '@result' if args.last: user_range += '@last' records = select_experiment_records(user_range, self.exp_record_dict, flat=True) func = show_record if args.original else None if args.process: Process(target=partial(show_multiple_records, records, func)).start() else: show_multiple_records(records, func) _warn_with_prompt(use_prompt=False)
def compare(self, *args): parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', help='A selection of experiment records to compare. Examples: "3" or "3-5", or "3,4,5"') parser.add_argument('-l', '--last', default=False, action = "store_true", help="Use this flag if you want to select Experiments instead of Experiment Records, and just show the last completed.") parser.add_argument('-r', '--results', default=False, action = "store_true", help="Only compare records with results.") parser.add_argument('-o', '--original', default=False, action = "store_true", help="Use original compare funcion") args = parser.parse_args(args) user_range = args.user_range if not args.last else args.user_range + '@result@last' records = select_experiment_records(user_range, self.exp_record_dict, flat=True) if args.results: records = [rec for rec in records if rec.has_result()] if len(records)==0: raise RecordSelectionError('No records were selected with "{}"'.format(args.user_range)) if args.original: func = compare_experiment_records else: compare_funcs = [rec.get_experiment().compare for rec in records] assert all_equal(compare_funcs), "Your records have different comparison functions - {} - so you can't compare them".format(set(compare_funcs)) func = compare_funcs[0] # The following could be used to launch comparisons in a new process. We don't do this now because # comparison function often use matplotlib, and matplotlib's Tkinter backend hangs when trying to create # a new figure in a new thread. # thread = Process(target = partial(func, records)) # thread.start() # thread.join() func(records) _warn_with_prompt(use_prompt=False)
def kill(self, *args): parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', help='A selection of experiments whose records to pull. Examples: "3" or "3-5", or "3,4,5"') parser.add_argument('-s', '--skip', action='store_true', default=True, help='Skip the check that all selected records are currently running (just filter running ones)') args = parser.parse_args(args) records = select_experiment_records(args.user_range, self.exp_record_dict, flat=True) if not args.skip and (not all(record.get_status() is ExpStatusOptions.STARTED for record in records)): raise RecordSelectionError('Not all records you selected to kill were running: \n- {}'.format('\n- '.join('{}: {}'.format(rec.get_id(), rec.get_status()) for rec in records))) elif args.skip: records = [rec for rec in records if rec.get_status() is ExpStatusOptions.STARTED] if len(records)==0: raise RecordSelectionError('Selection "{}" selected no active processes to kill'.format(args.user_range)) print('{} Running Experiments will be killed.'.format(len(records))) with IndentPrint(): print(ExperimentRecordBrowser.get_record_table(records, )) response = input('Type "yes" to continue. >').strip().lower() if response == 'yes': for rec in records: rec.kill() print('Experiments killed.') return ExperimentBrowser.REFRESH else: _warn_with_prompt('Experiments were not killed.', use_prompt=False)
def argtable(self, *args): parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', nargs = '?', default='all', help='A selection of experiment records to run. Examples: "3" or "3-5", or "3,4,5"') args = parser.parse_args(args) records = select_experiment_records(args.user_range, self.exp_record_dict, flat=True) print_experiment_record_argtable(records) _warn_with_prompt(use_prompt=False)
def kill(self, *args): parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', help='A selection of experiments whose records to pull. Examples: "3" or "3-5", or "3,4,5"') parser.add_argument('-s', '--skip', action='store_true', default=True, help='Skip the check that all selected records are currently running (just filter running ones)') args = parser.parse_args(args) records = select_experiment_records(args.user_range, self.exp_record_dict, flat=True) if not args.skip and (not all(record.get_status() is ExpStatusOptions.STARTED for record in records)): raise RecordSelectionError('Not all records you selected to kill were running: \n- {}'.format('\n- '.join('{}: {}'.format(rec.get_id(), rec.get_status()) for rec in records))) elif args.skip: records = [rec for rec in records if rec.get_status() is ExpStatusOptions.STARTED] if len(records)==0: raise RecordSelectionError('Selection "{}" selected no active processes to kill'.format(args.user_range)) print('{} Running Experiments will be killed.'.format(len(records))) with IndentPrint(): print(ExperimentRecordBrowser.get_record_table(records, )) response = input('Type "yes" to continue. >').strip().lower() if response == 'yes': for rec in records: rec.kill() print('Experiments killed.') return ExperimentBrowser.REFRESH else: _warn_with_prompt('Experiments were not killed.', use_prompt=False)
def errortrace(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) with IndentPrint("Error Traces:", show_line=True, show_end=True): for record in records: with IndentPrint(record.get_id(), show_line=True): print(record.get_error_trace()) _warn_with_prompt(use_prompt=False)
def compare(self, *args): parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', help='A selection of experiment records to compare. Examples: "3" or "3-5", or "3,4,5"') parser.add_argument('-l', '--last', default=False, action = "store_true", help="Use this flag if you want to select Experiments instead of Experiment Records, and just show the last completed.") parser.add_argument('-r', '--results', default=False, action = "store_true", help="Only compare records with results.") parser.add_argument('-o', '--original', default=False, action = "store_true", help="Use original compare funcion") args = parser.parse_args(args) user_range = args.user_range if not args.last else args.user_range + '@result@last' records = select_experiment_records(user_range, self.exp_record_dict, flat=True) if args.results: records = [rec for rec in records if rec.has_result()] if len(records)==0: raise RecordSelectionError('No records were selected with "{}"'.format(args.user_range)) if args.original: func = compare_experiment_records else: compare_funcs = [rec.get_experiment().compare for rec in records] assert all_equal(compare_funcs), "Your records have different comparison functions - {} - so you can't compare them".format(set(compare_funcs)) func = compare_funcs[0] # The following could be used to launch comparisons in a new process. We don't do this now because # comparison function often use matplotlib, and matplotlib's Tkinter backend hangs when trying to create # a new figure in a new thread. # thread = Process(target = partial(func, records)) # thread.start() # thread.join() func(records) _warn_with_prompt(use_prompt=False)
def argtable(self, *args): parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', nargs = '?', default='all', help='A selection of experiment records to run. Examples: "3" or "3-5", or "3,4,5"') args = parser.parse_args(args) records = select_experiment_records(args.user_range, self.exp_record_dict, flat=True) print_experiment_record_argtable(records) _warn_with_prompt(use_prompt=False)
def archive(self, *args): parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', help='A selection of experiments to run. Examples: "3" or "3-5", or "3,4,5"') args = parser.parse_args(args) records = select_experiment_records(args.user_range, self.exp_record_dict, flat=True) for record in records: archive_record(record) print('{} Experiment Records were archived.'.format(len(records)))
def selectrec(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) with IndentPrint(): print ExperimentRecordBrowser.get_record_table(records) _warn_with_prompt( 'Record Selection "{}" includes {} out of {} records.'.format( user_range, len(records), sum(len(recs) for recs in self.exp_record_dict.values())), use_prompt=not self.close_after)
def delete(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) print('{} out of {} Records will be deleted.'.format(len(records), sum(len(recs) for recs in self.exp_record_dict.values()))) with IndentPrint(): print(ExperimentRecordBrowser.get_record_table(records, )) response = input('Type "yes" to continue. >').strip().lower() if response == 'yes': clear_experiment_records([record.get_id() for record in records]) print('Records deleted.') return ExperimentBrowser.REFRESH else: _warn_with_prompt('Records were not deleted.', use_prompt=False)
def delete(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) print('{} out of {} Records will be deleted.'.format(len(records), sum(len(recs) for recs in self.exp_record_dict.values()))) with IndentPrint(): print(ExperimentRecordBrowser.get_record_table(records, )) response = input('Type "yes" to continue. >').strip().lower() if response == 'yes': clear_experiment_records([record.get_id() for record in records]) print('Records deleted.') return ExperimentBrowser.REFRESH else: _warn_with_prompt('Records were not deleted.', use_prompt=False)
def display(self, user_range='all', skip_if_single=True): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) if len(records) == 1 and skip_if_single: display_experiment_record(records[0]) else: with IndentPrint("Results:", show_line=True, show_end=True): for record in records: with IndentPrint(record.get_id(), show_line=True, show_end=True): display_experiment_record(record) _warn_with_prompt(use_prompt=not self.close_after)
def delete(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) print '{} out of {} Records will be deleted.'.format( len(records), sum(len(recs) for recs in self.exp_record_dict.values())) with IndentPrint(): print ExperimentRecordBrowser.get_record_table(records, ) response = raw_input('Type "yes" to continue. >') if response.lower() == 'yes': clear_experiment_records([record.get_id() for record in records]) print 'Records deleted.' else: _warn_with_prompt('Records were not deleted.', use_prompt=not self.close_after)
def _filter_record_dict(self, all_experiments): # Apply filters and display Table: if self._filter is not None: try: all_experiments = OrderedDict((exp_name, all_experiments[exp_name]) for exp_name in select_experiments(self._filter, all_experiments)) except RecordSelectionError as err: old_filter = self._filter self._filter = None raise RecordSelectionError("Failed to apply experiment filter: '{}' because {}. Removing filter.".format(old_filter, err)) if self._filterrec is not None: try: all_experiments = select_experiment_records(self._filterrec, all_experiments, load_records=False, flat=False) except RecordSelectionError as err: old_filterrec = self._filterrec self._filterrec = None raise RecordSelectionError("Failed to apply record filter: '{}' because {}. Removing filter.".format(old_filterrec, err)) return all_experiments
def _filter_record_dict(self, all_experiments): # Apply filters and display Table: if self._filter is not None: try: all_experiments = OrderedDict((exp_name, all_experiments[exp_name]) for exp_name in select_experiments(self._filter, all_experiments)) except RecordSelectionError as err: old_filter = self._filter self._filter = None raise RecordSelectionError("Failed to apply experiment filter: '{}' because {}. Removing filter.".format(old_filter, err)) if self._filterrec is not None: try: all_experiments = select_experiment_records(self._filterrec, all_experiments, load_records=False, flat=False) except RecordSelectionError as err: old_filterrec = self._filterrec self._filterrec = None raise RecordSelectionError("Failed to apply record filter: '{}' because {}. Removing filter.".format(old_filterrec, err)) return all_experiments
def show(self, user_range, parallel_arg=None): """ :param user_range: A range specifying the record :param parallel_arg: -p to print logs side-by-side, and -s to print them in sequence. """ records = select_experiment_records(user_range, self.exp_record_dict, flat=True) parallel_text = {'-p': True, '-s': False, None: None}[parallel_arg] has_matplotlib_figures = show_experiment_records( records, parallel_text=parallel_text, truncate_logs=None) if has_matplotlib_figures: from matplotlib import pyplot as plt print '\n\n... Close all figures to return to experiment browser ...' plt.ioff() plt.show() else: _warn_with_prompt(use_prompt=not self.close_after)
def show(self, *args): """ :param user_range: A range specifying the record """ parser = argparse.ArgumentParser() parser.add_argument('user_range', action='store', help='A selection of experiment records to show. ') parser.add_argument( '-l', '--last', action='store_true', help='Just select the last record from the list of experiments.') parser.add_argument('-r', '--results', default=False, action="store_true", help="Only show records with results.") parser.add_argument( '-o', '--original', default=False, action="store_true", help="Use the original default show function: show_record") args = parser.parse_args(args) user_range = args.user_range if args.results: user_range += '>result' if args.last: user_range += '>last' try: records = select_experiment_records(user_range, self.exp_record_dict, flat=True) except RecordSelectionError as err: print('FAILED!: {}'.format(str(err))) func = show_record if args.original else None show_multiple_records(records, func) _warn_with_prompt(use_prompt=not self.close_after)
def compare(self, *args): parser = argparse.ArgumentParser() parser.add_argument( 'user_range', action='store', help= 'A selection of experiment records to compare. Examples: "3" or "3-5", or "3,4,5"' ) parser.add_argument( '-l', '--last', default=False, action="store_true", help= "Use this flag if you want to select Experiments instead of Experiment Records, and just show the last completed." ) parser.add_argument('-r', '--results', default=False, action="store_true", help="Only compare records with results.") args = parser.parse_args(args) user_range = args.user_range if not args.last else args.user_range + '>finished>last' records = select_experiment_records(user_range, self.exp_record_dict, flat=True) if args.results: records = [rec for rec in records if rec.has_result()] compare_funcs = [rec.get_experiment().compare for rec in records] assert all_equal( compare_funcs ), "Your records have different comparison functions - {} - so you can't compare them".format( set(compare_funcs)) func = compare_funcs[0] func(records) _warn_with_prompt(use_prompt=not self.close_after)
def launch(self, command=None): func_dict = { 'run': self.run, 'test': self.test, 'show': self.show, 'call': self.call, 'selectexp': self.selectexp, 'selectrec': self.selectrec, 'view': self.view, 'h': self.help, 'filter': self.filter, 'filterrec': self.filterrec, 'displayformat': self.displayformat, 'explist': self.explist, 'sidebyside': self.side_by_side, 'argtable': self.argtable, 'compare': self.compare, 'delete': self.delete, 'errortrace': self.errortrace, 'q': self.quit, 'records': self.records, 'pull': self.pull, 'clearcache': clear_ui_cache, } while True: all_experiments = self.reload_record_dict() # Display Table: self.exp_record_dict = all_experiments if self._filter is None else \ OrderedDict((exp_name, all_experiments[exp_name]) for exp_name in select_experiments(self._filter, all_experiments)) if self._filterrec is not None: self.exp_record_dict = select_experiment_records( self._filterrec, self.exp_record_dict, load_records=False, flat=False) print(self.get_experiment_list_str(self.exp_record_dict)) if self._filter is not None or self._filterrec is not None: print( '[Filtered out {}/{} experiments with "{}" and {}/{} records with "{}"]' .format(len(self.exp_record_dict), len(all_experiments), self._filter, sum(len(v) for v in self.exp_record_dict.values()), sum(len(v) for v in all_experiments.values()), self._filterrec)) # Get command from user if command is None: user_input = input( 'Enter command or experiment # to run (h for help) >> ' ).strip() else: user_input = command command = None # Respond to user input with IndentPrint(): try: split = user_input.split(' ') if len(split) == 0: continue cmd = split[0] args = split[1:] if cmd == '': continue elif cmd in func_dict: out = func_dict[cmd](*args) elif interpret_numbers(cmd) is not None: if not any(x in args for x in ('-s', '-e', '-p')): args = args + ['-e'] out = self.run(cmd, *args) elif cmd == 'r': # Refresh continue else: edit_distances = [ levenshtein_distance(cmd, other_cmd) for other_cmd in func_dict.keys() ] min_distance = min(edit_distances) closest = func_dict.keys()[edit_distances.index( min_distance)] suggestion = ' Did you mean "{}"?. '.format( closest) if min_distance <= 2 else '' if self.close_after: raise Exception( 'Unrecognised command: "{}"'.format(cmd)) else: response = input( 'Unrecognised command: "{}". {}Type "h" for help or Enter to continue. >' .format(cmd, suggestion, closest)) if response.strip().lower() == 'h': self.help() out = None if out is self.QUIT or self.close_after: break except Exception as name: if self.catch_errors: res = input( '%s: %s\nEnter "e" to view the stacktrace, or anything else to continue.' % (name.__class__.__name__, name.message)) if res.strip().lower() == 'e': raise else: raise
def side_by_side(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) print(side_by_side([get_record_full_string(rec) for rec in records], max_linewidth=128)) _warn_with_prompt(use_prompt=False)
def argtable(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) print_experiment_record_argtable(records) _warn_with_prompt(use_prompt=not self.close_after)
def selectrec(self, user_range): records = select_experiment_records(user_range, self.exp_record_dict, flat=True) with IndentPrint(): print(ExperimentRecordBrowser.get_record_table(records)) _warn_with_prompt('Record Selection "{}" includes {} out of {} records.'.format(user_range, len(records), sum(len(recs) for recs in self.exp_record_dict.values())), use_prompt=not self.close_after)