Beispiel #1
0
def _warn_of_updated_ballet(latest: Optional[str]):
    if latest is not None:
        current = ballet.__version__
        msg = dedent(
            f'''
            A new version of ballet is available: v{latest}
            - you currently have ballet v{current}
            - if you don't update ballet, you won't receive project template updates
            - update ballet and then try again:

                $ python -m pip install --upgrade ballet
            '''  # noqa E501
        ).strip()
        logger.warning(msg)
Beispiel #2
0
def nonnegative(call: Call, name: Optional[str] = None):
    """Warn if the function's return value is negative and set it to 0"""
    result = call()
    with suppress(TypeError):
        if result < 0:
            result = 0.0
            # Format a nice log message
            if name is None:
                try:
                    pieces = call._func.__name__.split('_')[1:]
                    name = ''.join(map(str.capitalize, pieces))
                except RuntimeError:
                    name = 'Result'
            logger.warning(f'{name} should be non-negative.')
    return result
Beispiel #3
0
    def fit(self, X, y, tune=True, **fit_kwargs):
        if tune:
            # do some tuning
            if btb is not None and self.tunables is not None:

                scorer = None

                def score(estimator):
                    scores = cross_val_score(estimator,
                                             X,
                                             y,
                                             scoring=scorer,
                                             cv=self.tuning_cv,
                                             fit_params=fit_kwargs)
                    return np.mean(scores)

                logger.info('Tuning model using BTB GP tuner...')
                tuner = btb.tuning.gp.GP(self.tunables)
                estimator = self._get_parent_instance()
                original_score = score(estimator)
                # TODO: this leads to an error because default value of
                # max_depth for RF is `None`
                # params = funcy.project(
                #     estimator.get_params(), [t[0] for t in self.tunables])
                # tuner.add(params, original_score)
                for i in range(self.tuning_iter):
                    params = tuner.propose()
                    estimator.set_params(**params)
                    score_ = score(estimator)
                    logger.debug('Iteration {}, params {}, score {}'.format(
                        i, params, score_))
                    tuner.add(params, score_)

                best_params = tuner._best_hyperparams
                best_score = tuner._best_score
                self.set_params(**best_params)
                logger.info(
                    'Tuning complete. '
                    'Cross val score changed from {:0.3f} to {:0.3f}.'.format(
                        original_score, best_score))
            else:
                logger.warning('Tuning requested, but either btb not '
                               'installed or tunable HyperParameters not '
                               'specified.')

        return super().fit(X, y, **fit_kwargs)
Beispiel #4
0
def render_project_template(project_template_path: Optional[Pathy] = None,
                            create_github_repo: bool = False,
                            github_token: Optional[str] = None,
                            **cc_kwargs) -> str:
    """Generate a ballet project according to the project template

    If creating the GitHub repo is requested and the process fails for any
    reason, quickstart will complete successfully and users are instructed
    to read the corresponding section of the Maintainer's Guide to continue
    manually.

    Args:
        project_template_path: path to specific project template
        create_github_repo: whether to act to create the desired repo on
            GitHub after rendering the project. The repo will be owned by
            either the user or an org that the user has relevant permissions
            for, depending on what is entered during the quickstart prompts.
            If True, then a valid github token must also be provided.
        github_token: valid github token with appropriate permissions
        **cc_kwargs: options for the cookiecutter template
    """
    if project_template_path is None:
        project_template_path = PROJECT_TEMPLATE_PATH

    project_path = cookiecutter(project_template_path, **cc_kwargs)

    if create_github_repo:
        if github_token is None:
            raise ValueError('Need to provide github token')
        g = Github(github_token)

        # need to get params from new project config
        project = Project.from_path(project_path)
        owner = project.config.get('github.github_owner')
        name = project.config.get('project.project_slug')

        # create repo on github
        try:
            github_repo = ballet.util.git.create_github_repo(g, owner, name)
            logger.info(f'Created repo on GitHub at {github_repo.html_url}')
        except GithubException:
            logger.exception('Failed to create GitHub repo for this project')
            logger.warning(
                'Failed to create GitHub repo for this project...\n'
                'did you specify the intended repo owner, and do you have'
                ' permissions to create a repo under that owner?\n'
                'Try manually creating the repo: https://ballet.github.io/ballet/maintainer_guide.html#manual-repository-creation'  # noqa E501
            )
            return project_path

        # now push to remote
        # we don't need to set up the remote, as it has already been setup in
        # post_gen_hook.py
        local_repo = project.repo
        remote_name = project.config.get('github.remote')
        branches = [DEFAULT_BRANCH]
        try:
            push_branches_to_remote(local_repo, remote_name, branches)
        except BalletError:
            logger.exception('Failed to push branches to GitHub repo')
            logger.warning(
                'Failed to push branches to GitHub repo...\n'
                'Try manually pushing the branches: https://ballet.github.io/ballet/maintainer_guide.html#manual-repository-creation'  # noqa E501
            )
            return project_path

    return project_path