Control object for interruptible, checkpoint-able evaluation
| 701 | |
| 702 | |
| 703 | class Ctrl: |
| 704 | """Control object for interruptible, checkpoint-able evaluation""" |
| 705 | |
| 706 | info = logger.info |
| 707 | warn = logger.warning |
| 708 | error = logger.error |
| 709 | debug = logger.debug |
| 710 | |
| 711 | def __init__(self, trials, current_trial=None): |
| 712 | # -- attachments should be used like |
| 713 | # attachments[key] |
| 714 | # attachments[key] = value |
| 715 | # where key and value are strings. Client code should not |
| 716 | # expect any dictionary-like behaviour beyond that (no update) |
| 717 | if trials is None: |
| 718 | self.trials = Trials() |
| 719 | else: |
| 720 | self.trials = trials |
| 721 | self.current_trial = current_trial |
| 722 | |
| 723 | def checkpoint(self, r=None): |
| 724 | assert self.current_trial in self.trials._trials |
| 725 | if r is not None: |
| 726 | self.current_trial["result"] = r |
| 727 | |
| 728 | @property |
| 729 | def attachments(self): |
| 730 | """ |
| 731 | Support syntax for load: self.attachments[name] |
| 732 | Support syntax for store: self.attachments[name] = value |
| 733 | """ |
| 734 | return self.trials.trial_attachments(trial=self.current_trial) |
| 735 | |
| 736 | def inject_results(self, specs, results, miscs, new_tids=None): |
| 737 | """Inject new results into self.trials |
| 738 | |
| 739 | Returns ??? XXX |
| 740 | |
| 741 | new_tids can be None, in which case new tids will be generated |
| 742 | automatically |
| 743 | |
| 744 | """ |
| 745 | trial = self.current_trial |
| 746 | assert trial is not None |
| 747 | num_news = len(specs) |
| 748 | assert len(specs) == len(results) == len(miscs) |
| 749 | if new_tids is None: |
| 750 | new_tids = self.trials.new_trial_ids(num_news) |
| 751 | new_trials = self.trials.source_trial_docs( |
| 752 | tids=new_tids, specs=specs, results=results, miscs=miscs, sources=[trial] |
| 753 | ) |
| 754 | for t in new_trials: |
| 755 | t["state"] = JOB_STATE_DONE |
| 756 | return self.trials.insert_trial_docs(new_trials) |
| 757 | |
| 758 | |
| 759 | class Domain: |