def enter_datarun(sql_config, run_config, aws_config=None, run_per_partition=False): """ Generate a datarun, including a dataset if necessary. sql_config: Object with all attributes necessary to initialize a Database. run_config: all attributes necessary to initialize a Datarun, including Dataset info if the dataset has not already been created. aws_config: all attributes necessary to connect to an S3 bucket. Returns: ID of the generated datarun """ # connect to the database db = Database(sql_config.dialect, sql_config.database, sql_config.username, sql_config.password, sql_config.host, sql_config.port, sql_config.query) # if the user has provided a dataset id, use that. Otherwise, create a new # dataset based on the arguments we were passed. if run_config.dataset_id is None: dataset = enter_dataset(db, run_config, aws_config=aws_config) else: dataset = db.get_dataset(run_config.dataset_id) method_parts = {} for m in run_config.methods: # enumerate all combinations of categorical variables for this method method = Method(METHODS_MAP[m]) method_parts[m] = method.get_hyperpartitions() print 'method', m, 'has', len(method_parts[m]), 'hyperpartitions' print # create hyperpartitions and datarun(s) run_ids = [] if not run_per_partition: print 'saving datarun...' datarun = create_datarun(db, dataset, run_config) print 'saving hyperpartions...' for method, parts in method_parts.items(): for part in parts: # if necessary, create a new datarun for each hyperpartition. # This setting is useful for debugging. if run_per_partition: datarun = create_datarun(db, dataset, run_config) run_ids.append(datarun.id) # create a new hyperpartition in the database db.create_hyperpartition(datarun_id=datarun.id, method=method, tunables=part.tunables, constants=part.constants, categoricals=part.categoricals, status=PartitionStatus.INCOMPLETE) print 'done!' print print '========== Summary ==========' print 'Dataset ID:', dataset.id print 'Training data:', dataset.train_path print 'Test data:', (dataset.test_path or '(None)') if run_per_partition: print 'Datarun IDs:', ', '.join(map(str, run_ids)) else: print 'Datarun ID:', datarun.id print 'Hyperpartition selection strategy:', datarun.selector print 'Parameter tuning strategy:', datarun.tuner print 'Budget: %d (%s)' % (datarun.budget, datarun.budget_type) print return run_ids or datarun.id
class ATM(object): LOOP_WAIT = 5 def __init__( self, # SQL Conf dialect='sqlite', database='atm.db', username=None, password=None, host=None, port=None, query=None, # AWS Conf access_key=None, secret_key=None, s3_bucket=None, s3_folder=None, # Log Conf models_dir='models', metrics_dir='metrics', verbose_metrics=False, ): self.db = Database(dialect, database, username, host, port, query) self.aws_access_key = access_key self.aws_secret_key = secret_key self.s3_bucket = s3_bucket self.s3_folder = s3_folder self.models_dir = models_dir self.metrics_dir = metrics_dir self.verbose_metrics = verbose_metrics def add_dataset(self, train_path, test_path=None, name=None, description=None, class_column=None): return self.db.create_dataset( train_path=train_path, test_path=test_path, name=name, description=description, class_column=class_column, aws_access_key=self.aws_access_key, aws_secret_key=self.aws_secret_key, ) def add_datarun(self, dataset_id, budget=100, budget_type='classifier', gridding=0, k_window=3, metric='f1', methods=['logreg', 'dt', 'knn'], r_minimum=2, run_per_partition=False, score_target='cv', priority=1, selector='uniform', tuner='uniform', deadline=None): dataruns = list() if deadline: deadline = datetime.strptime(deadline, TIME_FMT) budget_type = 'walltime' elif budget_type == 'walltime': deadline = datetime.now() + timedelta(minutes=budget) run_description = '___'.join([tuner, selector]) target = score_target + '_judgment_metric' method_parts = {} for method in methods: # enumerate all combinations of categorical variables for this method method_instance = Method(method) method_parts[method] = method_instance.get_hyperpartitions() LOGGER.info('method {} has {} hyperpartitions'.format( method, len(method_parts[method]))) if not run_per_partition: datarun = self.db.create_datarun( dataset_id=dataset_id, description=run_description, tuner=tuner, selector=selector, gridding=gridding, priority=priority, budget_type=budget_type, budget=budget, deadline=deadline, metric=metric, score_target=target, k_window=k_window, r_minimum=r_minimum ) dataruns.append(datarun) for method, parts in method_parts.items(): for part in parts: # if necessary, create a new datarun for each hyperpartition. # This setting is useful for debugging. if run_per_partition: datarun = self.db.create_datarun( dataset_id=dataset_id, description=run_description, tuner=tuner, selector=selector, gridding=gridding, priority=priority, budget_type=budget_type, budget=budget, deadline=deadline, metric=metric, score_target=target, k_window=k_window, r_minimum=r_minimum ) dataruns.append(datarun) # create a new hyperpartition in the database self.db.create_hyperpartition(datarun_id=datarun.id, method=method, tunables=part.tunables, constants=part.constants, categoricals=part.categoricals, status=PartitionStatus.INCOMPLETE) dataset = self.db.get_dataset(dataset_id) LOGGER.info('Dataruns created. Summary:') LOGGER.info('\tDataset ID: {}'.format(dataset.id)) LOGGER.info('\tTraining data: {}'.format(dataset.train_path)) LOGGER.info('\tTest data: {}'.format(dataset.test_path)) if run_per_partition: LOGGER.info('\tDatarun IDs: {}'.format( ', '.join(str(datarun.id) for datarun in dataruns))) else: LOGGER.info('\tDatarun ID: {}'.format(dataruns[0].id)) LOGGER.info('\tHyperpartition selection strategy: {}'.format(dataruns[0].selector)) LOGGER.info('\tParameter tuning strategy: {}'.format(dataruns[0].tuner)) LOGGER.info('\tBudget: {} ({})'.format(dataruns[0].budget, dataruns[0].budget_type)) return dataruns if run_per_partition else dataruns[0] def work(self, datarun_ids=None, save_files=True, choose_randomly=True, cloud_mode=False, total_time=None, wait=True, verbose=False): """ Check the ModelHub database for unfinished dataruns, and spawn workers to work on them as they are added. This process will continue to run until it exceeds total_time or is broken with ctrl-C. datarun_ids (optional): list of IDs of dataruns to compute on. If None, this will work on all unfinished dataruns in the database. choose_randomly: if True, work on all highest-priority dataruns in random order. If False, work on them in sequential order (by ID) cloud_mode: if True, save processed datasets to AWS. total_time (optional): if set to an integer, this worker will only work for total_time seconds. Otherwise, it will continue working until all dataruns are complete (or indefinitely). wait: if True, once all dataruns in the database are complete, keep spinning and wait for new runs to be added. If False, exit once all dataruns are complete. """ start_time = datetime.now() # main loop while True: # get all pending and running dataruns, or all pending/running dataruns # from the list we were given dataruns = self.db.get_dataruns(include_ids=datarun_ids, ignore_complete=True) if not dataruns: if wait: LOGGER.debug('No dataruns found. Sleeping %d seconds and trying again.', ATM.LOOP_WAIT) time.sleep(ATM.LOOP_WAIT) continue else: LOGGER.info('No dataruns found. Exiting.') break # either choose a run randomly between priority, or take the run with the lowest ID if choose_randomly: run = random.choice(dataruns) else: run = sorted(dataruns, key=attrgetter('id'))[0] # say we've started working on this datarun, if we haven't already self.db.mark_datarun_running(run.id) LOGGER.info('Computing on datarun %d' % run.id) # actual work happens here worker = Worker(self.db, run, save_files=save_files, cloud_mode=cloud_mode, aws_access_key=self.aws_access_key, aws_secret_key=self.aws_secret_key, s3_bucket=self.s3_bucket, s3_folder=self.s3_folder, models_dir=self.models_dir, metrics_dir=self.metrics_dir, verbose_metrics=self.verbose_metrics) try: if run.budget_type == 'classifier': pbar = tqdm( total=run.budget, ascii=True, initial=run.completed_classifiers, disable=not verbose ) while run.status != RunStatus.COMPLETE: worker.run_classifier() run = self.db.get_datarun(run.id) if verbose and run.completed_classifiers > pbar.last_print_n: pbar.update(run.completed_classifiers - pbar.last_print_n) pbar.close() elif run.budget_type == 'walltime': pbar = tqdm( disable=not verbose, ascii=True, initial=run.completed_classifiers, unit=' Classifiers' ) while run.status != RunStatus.COMPLETE: worker.run_classifier() run = self.db.get_datarun(run.id) # Refresh the datarun object. if verbose and run.completed_classifiers > pbar.last_print_n: pbar.update(run.completed_classifiers - pbar.last_print_n) pbar.close() except ClassifierError: # the exception has already been handled; just wait a sec so we # don't go out of control reporting errors LOGGER.error('Something went wrong. Sleeping %d seconds.', ATM.LOOP_WAIT) time.sleep(ATM.LOOP_WAIT) elapsed_time = (datetime.now() - start_time).total_seconds() if total_time is not None and elapsed_time >= total_time: LOGGER.info('Total run time for worker exceeded; exiting.') break def run(self, train_path, test_path=None, name=None, description=None, class_column='class', budget=100, budget_type='classifier', gridding=0, k_window=3, metric='f1', methods=['logreg', 'dt', 'knn'], r_minimum=2, run_per_partition=False, score_target='cv', selector='uniform', tuner='uniform', deadline=None, priority=1, save_files=True, choose_randomly=True, cloud_mode=False, total_time=None, wait=True, verbose=True): dataset = self.add_dataset(train_path, test_path, name, description, class_column) datarun = self.add_datarun( dataset.id, budget, budget_type, gridding, k_window, metric, methods, r_minimum, run_per_partition, score_target, priority, selector, tuner, deadline ) if run_per_partition: datarun_ids = [_datarun.id for _datarun in datarun] else: datarun_ids = [datarun.id] if verbose: print('Processing dataset {}'.format(train_path)) self.work( datarun_ids, save_files, choose_randomly, cloud_mode, total_time, False, verbose=verbose ) dataruns = self.db.get_dataruns( include_ids=datarun_ids, ignore_complete=False, ignore_pending=True ) if run_per_partition: return dataruns elif len(dataruns) == 1: return dataruns[0] def load_model(self, classifier_id): return self.db.get_classifier(classifier_id).load_model()
class ATM(object): """ Thiss class is code API instance that allows you to use ATM in your python code. """ LOOP_WAIT = 1 def __init__(self, **kwargs): if kwargs.get('log_config') is None: kwargs['log_config'] = os.path.join( PROJECT_ROOT, 'config/templates/log-script.yaml') self.sql_conf, self.run_conf, self.aws_conf, self.log_conf = load_config( **kwargs) self.db = Database(**vars(self.sql_conf)) initialize_logging(self.log_conf) def work(self, datarun_ids=None, save_files=False, choose_randomly=True, cloud_mode=False, total_time=None, wait=True): """ Check the ModelHub database for unfinished dataruns, and spawn workers to work on them as they are added. This process will continue to run until it exceeds total_time or is broken with ctrl-C. datarun_ids (optional): list of IDs of dataruns to compute on. If None, this will work on all unfinished dataruns in the database. choose_randomly: if True, work on all highest-priority dataruns in random order. If False, work on them in sequential order (by ID) cloud_mode: if True, save processed datasets to AWS. If this option is set, aws_config must be supplied. total_time (optional): if set to an integer, this worker will only work for total_time seconds. Otherwise, it will continue working until all dataruns are complete (or indefinitely). wait: if True, once all dataruns in the database are complete, keep spinning and wait for new runs to be added. If False, exit once all dataruns are complete. """ start_time = datetime.now() public_ip = get_public_ip() # main loop while True: # get all pending and running dataruns, or all pending/running dataruns # from the list we were given dataruns = self.db.get_dataruns(include_ids=datarun_ids, ignore_complete=True) if not dataruns: if wait: logger.warning( 'No dataruns found. Sleeping %d seconds and trying again.', ATM.LOOP_WAIT) time.sleep(ATM.LOOP_WAIT) continue else: logger.warning('No dataruns found. Exiting.') break max_priority = max([datarun.priority for datarun in dataruns]) priority_runs = [r for r in dataruns if r.priority == max_priority] # either choose a run randomly, or take the run with the lowest ID if choose_randomly: run = random.choice(priority_runs) else: run = sorted(dataruns, key=attrgetter('id'))[0] # say we've started working on this datarun, if we haven't already self.db.mark_datarun_running(run.id) logger.info('Computing on datarun %d' % run.id) # actual work happens here worker = Worker(self.db, run, save_files=save_files, cloud_mode=cloud_mode, aws_config=self.aws_conf, log_config=self.log_conf, public_ip=public_ip) try: worker.run_classifier() except ClassifierError: # the exception has already been handled; just wait a sec so we # don't go out of control reporting errors logger.warning('Something went wrong. Sleeping %d seconds.', ATM.LOOP_WAIT) time.sleep(ATM.LOOP_WAIT) elapsed_time = (datetime.now() - start_time).total_seconds() if total_time is not None and elapsed_time >= total_time: logger.warning('Total run time for worker exceeded; exiting.') break def create_dataset(self): """ Create a dataset and add it to the ModelHub database. """ # download data to the local filesystem to extract metadata train_local, test_local = download_data(self.run_conf.train_path, self.run_conf.test_path, self.aws_conf) # create the name of the dataset from the path to the data name = os.path.basename(train_local) name = name.replace("_train.csv", "").replace(".csv", "") # process the data into the form ATM needs and save it to disk meta = MetaData(self.run_conf.class_column, train_local, test_local) # enter dataset into database dataset = self.db.create_dataset( name=name, description=self.run_conf.data_description, train_path=self.run_conf.train_path, test_path=self.run_conf.test_path, class_column=self.run_conf.class_column, n_examples=meta.n_examples, k_classes=meta.k_classes, d_features=meta.d_features, majority=meta.majority, size_kb=old_div(meta.size, 1000)) return dataset def create_datarun(self, dataset): """ Given a config, creates a set of dataruns for the config and enters them into the database. Returns the ID of the created datarun. dataset: Dataset SQLAlchemy ORM object """ # describe the datarun by its tuner and selector run_description = '__'.join( [self.run_conf.tuner, self.run_conf.selector]) # set the deadline, if applicable deadline = self.run_conf.deadline if deadline: deadline = datetime.strptime(deadline, TIME_FMT) # this overrides the otherwise configured budget_type # TODO: why not walltime and classifiers budget simultaneously? self.run_conf.budget_type = 'walltime' elif self.run_conf.budget_type == 'walltime': deadline = datetime.now() + timedelta(minutes=self.run_conf.budget) target = self.run_conf.score_target + '_judgment_metric' datarun = self.db.create_datarun(dataset_id=dataset.id, description=run_description, tuner=self.run_conf.tuner, selector=self.run_conf.selector, gridding=self.run_conf.gridding, priority=self.run_conf.priority, budget_type=self.run_conf.budget_type, budget=self.run_conf.budget, deadline=deadline, metric=self.run_conf.metric, score_target=target, k_window=self.run_conf.k_window, r_minimum=self.run_conf.r_minimum) return datarun def enter_data(self, run_per_partition=False): """ Generate a datarun, including a dataset if necessary. Returns: ID of the generated datarun """ # connect to the database # if the user has provided a dataset id, use that. Otherwise, create a new # dataset based on the arguments we were passed. if self.run_conf.dataset_id is None: dataset = self.create_dataset() self.run_conf.dataset_id = dataset.id else: dataset = self.db.get_dataset(self.run_conf.dataset_id) method_parts = {} for m in self.run_conf.methods: # enumerate all combinations of categorical variables for this method method = Method(m) method_parts[m] = method.get_hyperpartitions() logger.info('method %s has %d hyperpartitions' % (m, len(method_parts[m]))) # create hyperpartitions and datarun(s) run_ids = [] if not run_per_partition: logger.debug('saving datarun...') datarun = self.create_datarun(dataset) logger.debug('saving hyperpartions...') for method, parts in list(method_parts.items()): for part in parts: # if necessary, create a new datarun for each hyperpartition. # This setting is useful for debugging. if run_per_partition: datarun = self.create_datarun(dataset) run_ids.append(datarun.id) # create a new hyperpartition in the database self.db.create_hyperpartition( datarun_id=datarun.id, method=method, tunables=part.tunables, constants=part.constants, categoricals=part.categoricals, status=PartitionStatus.INCOMPLETE) logger.info('Data entry complete. Summary:') logger.info('\tDataset ID: %d', dataset.id) logger.info('\tTraining data: %s', dataset.train_path) logger.info('\tTest data: %s', (dataset.test_path or 'None')) if run_per_partition: logger.info('\tDatarun IDs: %s', ', '.join(map(str, run_ids))) else: logger.info('\tDatarun ID: %d', datarun.id) logger.info('\tHyperpartition selection strategy: %s', datarun.selector) logger.info('\tParameter tuning strategy: %s', datarun.tuner) logger.info('\tBudget: %d (%s)', datarun.budget, datarun.budget_type) return run_ids or datarun.id
def enter_data(sql_config, run_config, aws_config=None, run_per_partition=False): """ Generate a datarun, including a dataset if necessary. sql_config: Object with all attributes necessary to initialize a Database. run_config: all attributes necessary to initialize a Datarun, including Dataset info if the dataset has not already been created. aws_config: all attributes necessary to connect to an S3 bucket. Returns: ID of the generated datarun """ # connect to the database db = Database(sql_config.dialect, sql_config.database, sql_config.username, sql_config.password, sql_config.host, sql_config.port, sql_config.query) # if the user has provided a dataset id, use that. Otherwise, create a new # dataset based on the arguments we were passed. if run_config.dataset_id is None: dataset = create_dataset(db, run_config, aws_config=aws_config) run_config.dataset_id = dataset.id else: dataset = db.get_dataset(run_config.dataset_id) method_parts = {} for m in run_config.methods: # enumerate all combinations of categorical variables for this method method = Method(m) method_parts[m] = method.get_hyperpartitions() logger.info('method %s has %d hyperpartitions' % (m, len(method_parts[m]))) # create hyperpartitions and datarun(s) run_ids = [] if not run_per_partition: logger.debug('saving datarun...') datarun = create_datarun(db, dataset, run_config) logger.debug('saving hyperpartions...') for method, parts in list(method_parts.items()): for part in parts: # if necessary, create a new datarun for each hyperpartition. # This setting is useful for debugging. if run_per_partition: datarun = create_datarun(db, dataset, run_config) run_ids.append(datarun.id) # create a new hyperpartition in the database db.create_hyperpartition(datarun_id=datarun.id, method=method, tunables=part.tunables, constants=part.constants, categoricals=part.categoricals, status=PartitionStatus.INCOMPLETE) logger.info('Data entry complete. Summary:') logger.info('\tDataset ID: %d' % dataset.id) logger.info('\tTraining data: %s' % dataset.train_path) logger.info('\tTest data: %s' % (dataset.test_path or 'None')) if run_per_partition: logger.info('\tDatarun IDs: %s' % ', '.join(map(str, run_ids))) else: logger.info('\tDatarun ID: %d' % datarun.id) logger.info('\tHyperpartition selection strategy: %s' % datarun.selector) logger.info('\tParameter tuning strategy: %s' % datarun.tuner) logger.info('\tBudget: %d (%s)' % (datarun.budget, datarun.budget_type)) return run_ids or datarun.id
class ATM(object): _LOOP_WAIT = 5 def __init__( self, # SQL Conf dialect='sqlite', database='atm.db', username=None, password=None, host=None, port=None, query=None, # AWS Conf access_key=None, secret_key=None, s3_bucket=None, s3_folder=None, # Log Conf models_dir='models', metrics_dir='metrics', verbose_metrics=False, ): self.db = Database(dialect, database, username, host, port, query) self.aws_access_key = access_key self.aws_secret_key = secret_key self.s3_bucket = s3_bucket self.s3_folder = s3_folder self.models_dir = models_dir self.metrics_dir = metrics_dir self.verbose_metrics = verbose_metrics def add_dataset(self, train_path, test_path=None, name=None, description=None, class_column=None): """Add a new dataset to the Database. Args: train_path (str): Path to the training CSV file. It can be a local filesystem path, absolute or relative, or an HTTP or HTTPS URL, or an S3 path in the format ``s3://{bucket_name}/{key}``. Required. test_path (str): Path to the testing CSV file. It can be a local filesystem path, absolute or relative, or an HTTP or HTTPS URL, or an S3 path in the format ``s3://{bucket_name}/{key}``. Optional. If not given, the training CSV will be split in two parts, train and test. name (str): Name given to this dataset. Optional. If not given, a hash will be generated from the training_path and used as the Dataset name. description (str): Human friendly description of the Dataset. Optional. class_column (str): Name of the column that will be used as the target variable. Optional. Defaults to ``'class'``. Returns: Dataset: The created dataset. """ return self.db.create_dataset( train_path=train_path, test_path=test_path, name=name, description=description, class_column=class_column, aws_access_key=self.aws_access_key, aws_secret_key=self.aws_secret_key, ) def add_datarun(self, dataset_id, budget=100, budget_type='classifier', gridding=0, k_window=3, metric='f1', methods=['logreg', 'dt', 'knn'], r_minimum=2, run_per_partition=False, score_target='cv', priority=1, selector='uniform', tuner='uniform', deadline=None): """Register one or more Dataruns to the Database. The methods hyperparameters will be analyzed and Hyperpartitions generated from them. If ``run_per_partition`` is ``True``, one Datarun will be created for each Hyperpartition. Otherwise, a single one will be created for all of them. Args: dataset_id (int): Id of the Dataset which this Datarun will belong to. budget (int): Budget amount. Optional. Defaults to ``100``. budget_type (str): Budget Type. Can be 'classifier' or 'walltime'. Optional. Defaults to ``'classifier'``. gridding (int): ``gridding`` setting for the Tuner. Optional. Defaults to ``0``. k_window (int): ``k`` setting for the Selector. Optional. Defaults to ``3``. metric (str): Metric to use for the tuning and selection. Optional. Defaults to ``'f1'``. methods (list): List of methods to try. Optional. Defaults to ``['logreg', 'dt', 'knn']``. r_minimum (int): ``r_minimum`` setting for the Tuner. Optional. Defaults to ``2``. run_per_partition (bool): whether to create a separated Datarun for each Hyperpartition or not. Optional. Defaults to ``False``. score_target (str): Which score to use for the tuning and selection process. It can be ``'cv'`` or ``'test'``. Optional. Defaults to ``'cv'``. priority (int): Priority of this Datarun. The higher the better. Optional. Defaults to ``1``. selector (str): Type of selector to use. Optional. Defaults to ``'uniform'``. tuner (str): Type of tuner to use. Optional. Defaults to ``'uniform'``. deadline (str): Time deadline. It must be a string representing a datetime in the format ``'%Y-%m-%d %H:%M'``. If given, ``budget_type`` will be set to ``'walltime'``. Returns: Datarun: The created Datarun or list of Dataruns. """ if deadline: deadline = datetime.strptime(deadline, TIME_FMT) budget_type = 'walltime' elif budget_type == 'walltime': deadline = datetime.now() + timedelta(minutes=budget) run_description = '___'.join([tuner, selector]) target = score_target + '_judgment_metric' method_parts = {} for method in methods: # enumerate all combinations of categorical variables for this method method_instance = Method(method) method_parts[method] = method_instance.get_hyperpartitions() LOGGER.info('method {} has {} hyperpartitions'.format( method, len(method_parts[method]))) dataruns = list() if not run_per_partition: datarun = self.db.create_datarun( dataset_id=dataset_id, description=run_description, tuner=tuner, selector=selector, gridding=gridding, priority=priority, budget_type=budget_type, budget=budget, deadline=deadline, metric=metric, score_target=target, k_window=k_window, r_minimum=r_minimum ) dataruns.append(datarun) for method, parts in method_parts.items(): for part in parts: # if necessary, create a new datarun for each hyperpartition. # This setting is useful for debugging. if run_per_partition: datarun = self.db.create_datarun( dataset_id=dataset_id, description=run_description, tuner=tuner, selector=selector, gridding=gridding, priority=priority, budget_type=budget_type, budget=budget, deadline=deadline, metric=metric, score_target=target, k_window=k_window, r_minimum=r_minimum ) dataruns.append(datarun) # create a new hyperpartition in the database self.db.create_hyperpartition(datarun_id=datarun.id, method=method, tunables=part.tunables, constants=part.constants, categoricals=part.categoricals, status=PartitionStatus.INCOMPLETE) dataset = self.db.get_dataset(dataset_id) LOGGER.info('Dataruns created. Summary:') LOGGER.info('\tDataset ID: {}'.format(dataset.id)) LOGGER.info('\tTraining data: {}'.format(dataset.train_path)) LOGGER.info('\tTest data: {}'.format(dataset.test_path)) if run_per_partition: LOGGER.info('\tDatarun IDs: {}'.format( ', '.join(str(datarun.id) for datarun in dataruns))) else: LOGGER.info('\tDatarun ID: {}'.format(dataruns[0].id)) LOGGER.info('\tHyperpartition selection strategy: {}'.format(dataruns[0].selector)) LOGGER.info('\tParameter tuning strategy: {}'.format(dataruns[0].tuner)) LOGGER.info('\tBudget: {} ({})'.format(dataruns[0].budget, dataruns[0].budget_type)) return dataruns if run_per_partition else dataruns[0] def work(self, datarun_ids=None, save_files=True, choose_randomly=True, cloud_mode=False, total_time=None, wait=True, verbose=False): """Get unfinished Dataruns from the database and work on them. Check the ModelHub Database for unfinished Dataruns, and work on them as they are added. This process will continue to run until it exceeds total_time or there are no more Dataruns to process or it is killed. Args: datarun_ids (list): list of IDs of Dataruns to work on. If ``None``, this will work on any unfinished Dataruns found in the database. Optional. Defaults to ``None``. save_files (bool): Whether to save the fitted classifiers and their metrics or not. Optional. Defaults to True. choose_randomly (bool): If ``True``, work on all the highest-priority dataruns in random order. Otherwise, work on them in sequential order (by ID). Optional. Defaults to ``True``. cloud_mode (bool): Save the models and metrics in AWS S3 instead of locally. This option works only if S3 configuration has been provided on initialization. Optional. Defaults to ``False``. total_time (int): Total time to run the work process, in seconds. If ``None``, continue to run until interrupted or there are no more Dataruns to process. Optional. Defaults to ``None``. wait (bool): If ``True``, wait for more Dataruns to be inserted into the Database once all have been processed. Otherwise, exit the worker loop when they run out. Optional. Defaults to ``False``. verbose (bool): Whether to be verbose about the process. Optional. Defaults to ``True``. """ start_time = datetime.now() # main loop while True: # get all pending and running dataruns, or all pending/running dataruns # from the list we were given dataruns = self.db.get_dataruns(include_ids=datarun_ids, ignore_complete=True) if not dataruns: if wait: LOGGER.debug('No dataruns found. Sleeping %d seconds and trying again.', self._LOOP_WAIT) time.sleep(self._LOOP_WAIT) continue else: LOGGER.info('No dataruns found. Exiting.') break # either choose a run randomly between priority, or take the run with the lowest ID if choose_randomly: run = random.choice(dataruns) else: run = sorted(dataruns, key=attrgetter('id'))[0] # say we've started working on this datarun, if we haven't already self.db.mark_datarun_running(run.id) LOGGER.info('Computing on datarun %d' % run.id) # actual work happens here worker = Worker(self.db, run, save_files=save_files, cloud_mode=cloud_mode, aws_access_key=self.aws_access_key, aws_secret_key=self.aws_secret_key, s3_bucket=self.s3_bucket, s3_folder=self.s3_folder, models_dir=self.models_dir, metrics_dir=self.metrics_dir, verbose_metrics=self.verbose_metrics) try: if run.budget_type == 'classifier': pbar = tqdm( total=run.budget, ascii=True, initial=run.completed_classifiers, disable=not verbose ) while run.status != RunStatus.COMPLETE: worker.run_classifier() run = self.db.get_datarun(run.id) if verbose and run.completed_classifiers > pbar.last_print_n: pbar.update(run.completed_classifiers - pbar.last_print_n) pbar.close() elif run.budget_type == 'walltime': pbar = tqdm( disable=not verbose, ascii=True, initial=run.completed_classifiers, unit=' Classifiers' ) while run.status != RunStatus.COMPLETE: worker.run_classifier() run = self.db.get_datarun(run.id) # Refresh the datarun object. if verbose and run.completed_classifiers > pbar.last_print_n: pbar.update(run.completed_classifiers - pbar.last_print_n) pbar.close() except ClassifierError: # the exception has already been handled; just wait a sec so we # don't go out of control reporting errors LOGGER.error('Something went wrong. Sleeping %d seconds.', self._LOOP_WAIT) time.sleep(self._LOOP_WAIT) elapsed_time = (datetime.now() - start_time).total_seconds() if total_time is not None and elapsed_time >= total_time: LOGGER.info('Total run time for worker exceeded; exiting.') break def run(self, train_path, test_path=None, name=None, description=None, class_column='class', budget=100, budget_type='classifier', gridding=0, k_window=3, metric='f1', methods=['logreg', 'dt', 'knn'], r_minimum=2, run_per_partition=False, score_target='cv', selector='uniform', tuner='uniform', deadline=None, priority=1, save_files=True, choose_randomly=True, cloud_mode=False, total_time=None, verbose=True): """Create a Dataset and a Datarun and then work on it. Args: train_path (str): Path to the training CSV file. It can be a local filesystem path, absolute or relative, or an HTTP or HTTPS URL, or an S3 path in the format ``s3://{bucket_name}/{key}``. Required. test_path (str): Path to the testing CSV file. It can be a local filesystem path, absolute or relative, or an HTTP or HTTPS URL, or an S3 path in the format ``s3://{bucket_name}/{key}``. Optional. If not given, the training CSV will be split in two parts, train and test. name (str): Name given to this dataset. Optional. If not given, a hash will be generated from the training_path and used as the Dataset name. description (str): Human friendly description of the Dataset. Optional. class_column (str): Name of the column that will be used as the target variable. Optional. Defaults to ``'class'``. budget (int): Budget amount. Optional. Defaults to ``100``. budget_type (str): Budget Type. Can be 'classifier' or 'walltime'. Optional. Defaults to ``'classifier'``. gridding (int): ``gridding`` setting for the Tuner. Optional. Defaults to ``0``. k_window (int): ``k`` setting for the Selector. Optional. Defaults to ``3``. metric (str): Metric to use for the tuning and selection. Optional. Defaults to ``'f1'``. methods (list): List of methods to try. Optional. Defaults to ``['logreg', 'dt', 'knn']``. r_minimum (int): ``r_minimum`` setting for the Tuner. Optional. Defaults to ``2``. run_per_partition (bool): whether to create a separated Datarun for each Hyperpartition or not. Optional. Defaults to ``False``. score_target (str): Which score to use for the tuning and selection process. It can be ``'cv'`` or ``'test'``. Optional. Defaults to ``'cv'``. priority (int): Priority of this Datarun. The higher the better. Optional. Defaults to ``1``. selector (str): Type of selector to use. Optional. Defaults to ``'uniform'``. tuner (str): Type of tuner to use. Optional. Defaults to ``'uniform'``. deadline (str): Time deadline. It must be a string representing a datetime in the format ``'%Y-%m-%d %H:%M'``. If given, ``budget_type`` will be set to ``'walltime'``. verbose (bool): Whether to be verbose about the process. Optional. Defaults to ``True``. Returns: Datarun: The created Datarun or list of Dataruns. """ dataset = self.add_dataset(train_path, test_path, name, description, class_column) datarun = self.add_datarun( dataset.id, budget, budget_type, gridding, k_window, metric, methods, r_minimum, run_per_partition, score_target, priority, selector, tuner, deadline ) if run_per_partition: datarun_ids = [_datarun.id for _datarun in datarun] else: datarun_ids = [datarun.id] if verbose: print('Processing dataset {}'.format(train_path)) self.work( datarun_ids, save_files, choose_randomly, cloud_mode, total_time, False, verbose=verbose ) dataruns = self.db.get_dataruns( include_ids=datarun_ids, ignore_complete=False, ignore_pending=True ) if run_per_partition: return dataruns elif len(dataruns) == 1: return dataruns[0] def load_model(self, classifier_id): """Load a Model from the Database. Args: classifier_id (int): Id of the Model to load. Returns: Model: The loaded model instance. """ return self.db.get_classifier(classifier_id).load_model()