How to use the chemprop.parsing.add_train_args function in chemprop

To help you get started, we’ve selected a few chemprop examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wengong-jin / chemprop / hyperparameter_optimization.py View on Github external
# Report best result
    results = [result for result in results if not np.isnan(result['mean_score'])]
    best_result = min(results, key=lambda result: (1 if args.minimize_score else -1) * result['mean_score'])
    logger.info('best')
    logger.info(best_result['hyperparams'])
    logger.info(f'num params: {best_result["num_params"]:,}')
    logger.info(f'{best_result["mean_score"]} +/- {best_result["std_score"]} {args.metric}')

    # Save best hyperparameter settings as JSON config file
    with open(args.config_save_path, 'w') as f:
        json.dump(best_result['hyperparams'], f, indent=4, sort_keys=True)


if __name__ == '__main__':
    parser = ArgumentParser()
    add_train_args(parser)
    parser.add_argument('--num_iters', type=int, default=20,
                        help='Number of hyperparameter choices to try')
    parser.add_argument('--config_save_path', type=str, required=True,
                        help='Path to .json file where best hyperparameter settings will be written')
    parser.add_argument('--log_path', type=str,
                        help='(Optional) Path to .log file where all results of the hyperparameter optimization will be written')
    args = parser.parse_args()
    modify_train_args(args)

    grid_search(args)
github wengong-jin / chemprop / web.py View on Github external
if request.method == 'GET':
        return render_train()

    # Get arguments
    data_name, epochs, checkpoint_name = \
        request.form['dataName'], int(request.form['epochs']), request.form['checkpointName']
    gpu = request.form.get('gpu')
    data_path = os.path.join(app.config['DATA_FOLDER'], data_name)
    dataset_type = request.form.get('datasetType', 'regression')

    if not checkpoint_name.endswith('.pt'):
        checkpoint_name += '.pt'

    # Create and modify args
    parser = ArgumentParser()
    add_train_args(parser)
    args = parser.parse_args()

    args.data_path = data_path
    args.dataset_type = dataset_type
    args.epochs = epochs

    # Check if regression/classification selection matches data
    data = get_data(path=data_path)
    targets = data.targets()
    unique_targets = set(np.unique(targets))

    if dataset_type == 'classification' and len(unique_targets - {0, 1}) > 0:
        errors.append('Selected classification dataset but not all labels are 0 or 1. Select regression instead.')

        return render_train(warnings=warnings, errors=errors)
github wengong-jin / chemprop / model_comparison.py View on Github external
logger.addHandler(ch)

    if save_path is not None:
        save_dir = os.path.dirname(save_path)
        if save_dir != '':
            os.makedirs(save_dir, exist_ok=True)
        fh = logging.FileHandler(save_path)
        fh.setLevel(logging.DEBUG)
        logger.addHandler(fh)
    
    return logger


if __name__ == '__main__':
    parser = ArgumentParser()
    add_train_args(parser)
    parser.add_argument('--log_name', type=str, default='gs.log',
                        help='Name of file where model comparison results will be saved')
    parser.add_argument('--experiments', type=str, nargs='*', default=['all'],
                        help='Which experiments to run')
    parser.add_argument('--datasets', type=str, nargs='+', default=list(DATASETS.keys()), choices=list(DATASETS.keys()),
                        help='Which datasets to perform a grid search on')
    args = parser.parse_args()

    log_path = os.path.join(args.save_dir, args.log_name)

    logger = create_logger(name='model_comparison', save_path=log_path)

    if 'all' in args.experiments or 'base' in args.experiments:
        logger.info('base')
        experiment_args = deepcopy(args)
        experiment_args.save_dir = os.path.join(experiment_args.save_dir, 'base')
github wengong-jin / chemprop / end_to_end.py View on Github external
tvf.write(line.strip() + '\n')
        vf.readline()  # skip header from validation file
        for line in vf:
            tvf.write(line.strip() + '\n')


if __name__ == '__main__':
    # Note: Just put junk for --data_path, it's required but it'll be overwritten
    # Also need to specify:
    # --dataset_type "classification" or "regression"
    # --results_dir for hyperparam results
    # --test_path for test csv file
    # --preds_path where predictions will be saved
    # --compound_names if necessary - might need to hard code so only during test
    parser = ArgumentParser()
    add_train_args(parser)
    add_hyper_opt_args(parser)
    add_predict_args(parser)
    parser.add_argument('--train_path', type=str, required=True,
                        help='Path to CSV file containing training data in chronological order')
    parser.add_argument('--val_path', type=str, required=True,
                        help='Path to CSV file containing val data in chronological order')
    parser.add_argument('--train_save', type=str, required=True,
                        help='Path to CSV file for new train data')
    parser.add_argument('--val_save', type=str, required=True,
                        help='Path to CSV file for new val data')
    parser.add_argument('--val_frac', type=float, default=0.2,
                        help='frac of data to use for validation')
    parser.add_argument('--train_val_save', type=str, required=True,
                        help='Path to CSV file for combined train and val data')
    args = parser.parse_args()