def to_dict(self, flatten=False): """Convert to a dictionary (optionally flatten to single level) Parameters ---------- flatten : bool Should the return dictionary be single level (i.e. should `params` or other dictionary variables be serialized). Returns ------- dict Dictionary representing the run - if flattened then params are returned as a JSON format sting. """ def convert_nonserializable(obj): if isinstance(obj, numpy.int64): return int(obj) raise TypeError('Unknown type:', type(obj)) if flatten: out_dict = { 'run_name': self.run_name, 'run_dir': self.run_dir, 'params': json.dumps(self.params, default=convert_nonserializable), 'status': constants.Status(self.status), 'campaign': self.campaign, 'sampler': self.sample, 'app': self.app, 'iteration': self.iteration, } else: out_dict = { 'run_name': self.run_name, 'run_dir': self.run_dir, 'params': self.params, 'status': constants.Status(self.status), 'campaign': self.campaign, 'sampler': self.sample, 'app': self.app, 'iteration': self.iteration, } return out_dict
def get_run_status(self, run_name, campaign=None, sampler=None): """ Return the status (enum) for the run with name 'run_name' (and, optionally, filtering for campaign and sampler by id) Parameters ---------- run_name: str Name of the run campaign: int ID of the desired Campaign sampler: int ID of the desired Sampler Returns ------- status: enum(Status) Status of the run. """ filter_options = {'run_name': run_name} if campaign: filter_options['campaign'] = campaign if sampler: filter_options['sampler'] = sampler selected = self.session.query(RunTable).filter_by(**filter_options) if selected.count() != 1: logging.critical('Multiple runs selected - using the first') selected = selected.first() return constants.Status(selected.status)
def _run_to_dict(run_row): """ Convert the provided row from 'runs' table into a dictionary Parameters ---------- run_row: RunTable Information on a particular run in the database. Returns ------- dict: Contains run information (keys = run_name, params, status, sample, campaign and app) """ run_info = { 'run_name': run_row.run_name, 'ensemble_name': run_row.ensemble_name, 'params': json.loads(run_row.params), 'status': constants.Status(run_row.status), 'sampler': run_row.sampler, 'campaign': run_row.campaign, 'app': run_row.app, 'result': run_row.result, 'run_dir': run_row.run_dir } return run_info
def _load_campaign(self, src, name): with open(src, "r") as infile: input_info = json.load(infile) if 'campaign' not in input_info: message = f"Campaign information missing from state file {src}." logger.critical(message) raise RuntimeError(message) if name is not None and input_info['campaign']['name'] != name: message = f'No campaign called {name} found - unable to continue.' logger.critical(message) raise RuntimeError(message) # TODO: Add version check self._campaign_info = input_info['campaign'] self._app = input_info.get('app', {}) self._runs = input_info.get('runs', {}) self._sample = input_info.get('sample', {}) self._collation_csv = input_info.get('collation_csv', {}) self._next_run = input_info['next_run'] self._next_ensemble = input_info['next_ensemble'] self._app['params'] = ParamsSpecification.deserialize( self._app['params']) # Convert run statuses to enums for run_id in self._runs: self._runs[run_id]['status'] = constants.Status( self._runs[run_id]['status'])
def get_run_status(self, run_name, campaign=None, sampler=None): """ Return the status (enum) for the run with name 'run_name' (and, optionally, filtering for campaign and sampler by id) Parameters ---------- run_name: str Name of the run campaign: int ID of the desired Campaign sampler: int ID of the desired Sampler Returns ------- status: enum(Status) Status of the run. """ if campaign is not None: logger.warning("Only 1 campaign is possible in JSON db") if sampler is not None: logger.warning("Only 1 sampler is possible in JSON db") return constants.Status(self._runs[run_name]['status'])