示例#1
0
def test_deploy_ramp_event(session_scope_function):
    database_config = read_config(database_config_template())
    event_config_filename = ramp_config_template()
    event_config = read_config(event_config_filename)
    ramp_config = generate_ramp_config(event_config)
    deploy_ramp_event(database_config_template(), ramp_config_template())

    # simulate that we add users and sign-up for the event and that they
    # submitted the starting kit
    with session_scope(database_config['sqlalchemy']) as session:
        add_users(session)
        sign_up_team(session, ramp_config['event_name'], 'test_user')
        submit_starting_kits(session, ramp_config['event_name'], 'test_user',
                             ramp_config['ramp_kit_submissions_dir'])

    # run the dispatcher on the event which are in the dataset
    dispatcher = Dispatcher(config=database_config,
                            event_config=event_config,
                            worker=CondaEnvWorker,
                            n_workers=-1,
                            hunger_policy='exit')
    dispatcher.launch()

    # the iris kit contain a submission which should fail for a user
    with session_scope(database_config['sqlalchemy']) as session:
        submission = get_submissions(session,
                                     event_config['ramp']['event_name'],
                                     'training_error')
        assert len(submission) == 1
示例#2
0
def test_deploy_ramp_event_options(session_scope_function):
    database_config = read_config(database_config_template())
    ramp_config = generate_ramp_config(read_config(ramp_config_template()))
    deploy_ramp_event(database_config_template(), ramp_config_template())
    # deploy again by forcing the deployment
    deploy_ramp_event(database_config_template(),
                      ramp_config_template(),
                      force=True)
    # do not deploy the kit to trigger the error in the problem with we don't
    # force the deployment
    msg_err = 'The RAMP problem already exists in the database.'
    with pytest.raises(ValueError, match=msg_err):
        with session_scope(database_config['sqlalchemy']) as session:
            problem = get_problem(session, 'iris')
            problem.path_ramp_kit = problem.path_ramp_kit + '_xxx'
            session.commit()
            deploy_ramp_event(database_config_template(),
                              ramp_config_template(),
                              setup_ramp_repo=False,
                              force=False)

            problem = get_problem(session, 'iris')
            problem.path_ramp_kit = ramp_config['ramp_kit_dir']
            problem.path_ramp_data = problem.path_ramp_data + '_xxx'
            session.commit()
            deploy_ramp_event(database_config_template(),
                              ramp_config_template(),
                              setup_ramp_repo=False,
                              force=False)
示例#3
0
def session_scope_function(database_config, ramp_config, database_connection):
    try:
        deployment_dir = create_test_db(database_config, ramp_config)
        with session_scope(database_config['sqlalchemy']) as session:
            yield session
    finally:
        shutil.rmtree(deployment_dir, ignore_errors=True)
        db, _ = setup_db(database_config['sqlalchemy'])
        Model.metadata.drop_all(db)
示例#4
0
def session_scope_module():
    database_config = read_config(database_config_template())
    ramp_config = ramp_config_template()
    try:
        deployment_dir = create_toy_db(database_config, ramp_config)
        with session_scope(database_config['sqlalchemy']) as session:
            yield session
    finally:
        shutil.rmtree(deployment_dir, ignore_errors=True)
        db, _ = setup_db(database_config['sqlalchemy'])
        Model.metadata.drop_all(db)
示例#5
0
def session_scope_function():
    database_config = read_config(database_config_template())
    ramp_config = read_config(ramp_config_template())
    try:
        create_test_db(database_config, ramp_config)
        with session_scope(database_config['sqlalchemy']) as session:
            yield session
    finally:
        shutil.rmtree(ramp_config['ramp']['deployment_dir'],
                      ignore_errors=True)
        db, _ = setup_db(database_config['sqlalchemy'])
        Model.metadata.drop_all(db)
示例#6
0
def session_scope_function():
    database_config = read_config(database_config_template())
    ramp_config = ramp_config_template()
    try:
        deployment_dir = create_test_db(database_config, ramp_config)
        with session_scope(database_config['sqlalchemy']) as session:
            add_users(session)
            add_problems(session)
            add_events(session)
            yield session
    finally:
        shutil.rmtree(deployment_dir, ignore_errors=True)
        db, _ = setup_db(database_config['sqlalchemy'])
        Model.metadata.drop_all(db)
示例#7
0
def client_session(database_connection):
    database_config = read_config(database_config_template())
    ramp_config = ramp_config_template()
    try:
        deployment_dir = create_toy_db(database_config, ramp_config)
        flask_config = generate_flask_config(database_config)
        app = create_app(flask_config)
        app.config['TESTING'] = True
        app.config['WTF_CSRF_ENABLED'] = False
        with session_scope(database_config['sqlalchemy']) as session:
            yield app.test_client(), session
    finally:
        shutil.rmtree(deployment_dir, ignore_errors=True)
        try:
            # In case of failure we should close the global flask engine
            from ramp_frontend import db as db_flask
            db_flask.session.close()
        except RuntimeError:
            pass
        db, _ = setup_db(database_config['sqlalchemy'])
        Model.metadata.drop_all(db)
示例#8
0
 def launch(self):
     """Launch the dispatcher."""
     logger.info('Starting the RAMP dispatcher')
     with session_scope(self._database_config) as session:
         logger.info('Open a session to the database')
         logger.info(
             'Reset unfinished trained submission from previous session')
         self._reset_submission_after_failure(
             session, self._ramp_config['event_name'])
         try:
             while not self._poison_pill:
                 self.fetch_from_db(session)
                 self.launch_workers(session)
                 self.collect_result(session)
                 self.update_database_results(session)
         finally:
             # reset the submissions to 'new' in case of error or unfinished
             # training
             self._reset_submission_after_failure(
                 session, self._ramp_config['event_name'])
         logger.info('Dispatcher killed by the poison pill')
示例#9
0
 def launch(self):
     """Launch the dispatcher."""
     logger.info('Starting the RAMP dispatcher')
     with session_scope(self._database_config) as session:
         logger.info('Open a session to the database')
         try:
             while not self._poison_pill:
                 self.fetch_from_db(session)
                 self.launch_workers(session)
                 self.collect_result(session)
                 self.update_database_results(session)
         finally:
             # reset the submissions to 'new' in case of error or unfinished
             # training
             submissions = get_submissions(session,
                                           self._ramp_config['event_name'],
                                           state=None)
             for submission_id, _, _ in submissions:
                 submission_state = get_submission_state(
                     session, submission_id)
                 if submission_state in ('training', 'send_to_training'):
                     set_submission_state(session, submission_id, 'new')
         logger.info('Dispatcher killed by the poison pill')
示例#10
0
def test_session_scope(database):
    database_config = read_config(database_config_template(),
                                  filter_section='sqlalchemy')
    with session_scope(database_config) as session:
        file_type = session.query(SubmissionFileType).all()
        assert len(file_type) > 0
示例#11
0
def deploy_ramp_event(config, event_config, setup_ramp_repo=True, force=False):
    """Deploy a RAMP event using a configuration file.

    This utility is in charge of creating the kit and data repository for a
    given RAMP event. It will also setup the database.

    Parameters
    ----------
    config : str
        The path to the YAML file containing the database information.
    event_config : str
        The path to the YAML file containing the RAMP infomation.
    setup_ramp_repo : bool, default is True
        Whether or not to setup the RAMP kit and data repositories.
    force : bool, default is False
        Whether or not to potentially overwrite the repositories, problem and
        event in the database.
    """
    database_config = read_config(config, filter_section='sqlalchemy')
    ramp_config = generate_ramp_config(event_config, config)

    with session_scope(database_config) as session:
        setup_files_extension_type(session)
        if setup_ramp_repo:
            setup_ramp_kit_ramp_data(ramp_config, ramp_config['problem_name'],
                                     force)
        else:
            # we do not clone the repository but we need to convert the
            # notebook to html
            current_directory = os.getcwd()
            problem_kit_path = ramp_config['ramp_kit_dir']
            os.chdir(problem_kit_path)
            subprocess.check_output([
                "jupyter", "nbconvert", "--to", "html",
                "{}_starting_kit.ipynb".format(ramp_config['problem_name'])
            ])
            # delete this line since it trigger in the front-end
            # (try to open execute "custom.css".)
            _delete_line_from_file(
                "{}_starting_kit.html".format(ramp_config['problem_name']),
                '<link rel="stylesheet" href="custom.css">\n')
            os.chdir(current_directory)
        # check if the repository exists
        problem = get_problem(session, ramp_config['problem_name'])
        if problem is None:
            add_problem(session, ramp_config['problem_name'],
                        ramp_config['ramp_kit_dir'],
                        ramp_config['ramp_data_dir'])
        else:
            if ((ramp_config['ramp_kit_dir'] != problem.path_ramp_kit
                 or ramp_config['ramp_data_dir'] != problem.path_ramp_data)
                    and not force):
                raise ValueError(
                    'The RAMP problem already exists in the database. The path'
                    ' to the kit or to the data is different. You need to set'
                    ' "force=True" if you want to overwrite these parameters.')
            if setup_ramp_repo:
                setup_ramp_kit_ramp_data(ramp_config,
                                         ramp_config['problem_name'], force)
            add_problem(session, ramp_config['problem_name'],
                        ramp_config['ramp_kit_dir'],
                        ramp_config['ramp_data_dir'], force)

        if not os.path.exists(ramp_config['ramp_submissions_dir']):
            os.makedirs(ramp_config['ramp_submissions_dir'])
        add_event(session, ramp_config['problem_name'],
                  ramp_config['event_name'], ramp_config['event_title'],
                  ramp_config['sandbox_name'],
                  ramp_config['ramp_submissions_dir'],
                  ramp_config['event_is_public'], force)
示例#12
0
def deploy_ramp_event(config, event_config, setup_ramp_repo=True, force=False):
    """Deploy a RAMP event using a configuration file.

    This utility is in charge of creating the kit and data repository for a
    given RAMP event. It will also setup the database.

    Parameters
    ----------
    config : str
        The path to the YAML file containing the database information.
    event_config : str
        The path to the YAML file containing the RAMP infomation.
    setup_ramp_repo : bool, default is True
        Whether or not to setup the RAMP kit and data repositories.
    force : bool, default is False
        Whether or not to potentially overwrite the repositories, problem and
        event in the database.
    """
    database_config = read_config(config, filter_section='sqlalchemy')
    event_config = read_config(event_config)
    ramp_config = generate_ramp_config(event_config)

    with session_scope(database_config) as session:
        setup_files_extension_type(session)
        if setup_ramp_repo:
            setup_ramp_kits_ramp_data(
                event_config, ramp_config['event'], force
            )
        # check if the repository exists
        problem = get_problem(session, ramp_config['event'])
        if problem is None:
            add_problem(session, ramp_config['event'],
                        ramp_config['ramp_kits_dir'],
                        ramp_config['ramp_data_dir'])
        else:
            if ((ramp_config['ramp_kits_dir'] != problem.path_ramp_kits or
                 ramp_config['ramp_data_dir'] != problem.path_ramp_data) and
                    not force):
                raise ValueError(
                    'The RAMP problem already exists in the database. The path'
                    'to the kit or to the data is different. You need to set'
                    '"force=True" if you want to overwrite these parameters.'
                )
            if setup_ramp_repo:
                setup_ramp_kits_ramp_data(
                    event_config, ramp_config['event'], force
                )
            add_problem(session, ramp_config['event'],
                        ramp_config['ramp_kits_dir'],
                        ramp_config['ramp_data_dir'],
                        force)

        if not os.path.exists(ramp_config['ramp_submissions_dir']):
            os.makedirs(ramp_config['ramp_submissions_dir'])
        add_event(session, ramp_config['event'],
                  ramp_config['event_name'],
                  ramp_config['event_title'],
                  ramp_config['sandbox_name'],
                  ramp_config['ramp_submissions_dir'],
                  ramp_config['event_is_public'],
                  force)