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)
def test_check_problem(session_scope_function): ramp_configs = { 'iris': read_config(ramp_config_iris()), 'boston_housing': read_config(ramp_config_boston_housing()) } for problem_name, ramp_config in ramp_configs.items(): internal_ramp_config = generate_ramp_config(ramp_config) setup_ramp_kit_ramp_data(internal_ramp_config, problem_name) add_problem(session_scope_function, problem_name, internal_ramp_config['ramp_kit_dir'], internal_ramp_config['ramp_data_dir']) problem_name = 'iris' problem = get_problem(session_scope_function, problem_name) assert problem.name == problem_name assert isinstance(problem, Problem) problem = get_problem(session_scope_function, None) assert len(problem) == 2 assert isinstance(problem, list) # Without forcing, we cannot write the same problem twice internal_ramp_config = generate_ramp_config(ramp_configs[problem_name]) err_msg = 'Attempting to overwrite a problem and delete all linked events' with pytest.raises(ValueError, match=err_msg): add_problem( session_scope_function, problem_name, internal_ramp_config['ramp_kit_dir'], internal_ramp_config['ramp_data_dir'], force=False ) # Force add the problem add_problem( session_scope_function, problem_name, internal_ramp_config['ramp_kit_dir'], internal_ramp_config['ramp_data_dir'], force=True ) problem = get_problem(session_scope_function, problem_name) assert problem.name == problem_name assert isinstance(problem, Problem) delete_problem(session_scope_function, problem_name) problem = get_problem(session_scope_function, problem_name) assert problem is None problem = get_problem(session_scope_function, None) assert len(problem) == 1 assert isinstance(problem, list)
def problem(problem_name): """Landing page for a single RAMP problem. Parameters ---------- problem_name : str The name of a problem. """ current_problem = get_problem(db.session, problem_name) user = (flask_login.current_user if flask_login.current_user.is_authenticated else None) admin = user.access_level == 'admin' if user is not None else False if current_problem: if app.config['TRACK_USER_INTERACTION']: if flask_login.current_user.is_authenticated: add_user_interaction(db.session, interaction='looking at problem', user=flask_login.current_user, problem=current_problem) else: add_user_interaction(db.session, interaction='looking at problem', problem=current_problem) description_f_name = os.path.join( current_problem.path_ramp_kit, '{}_starting_kit.html'.format(current_problem.name)) with codecs.open(description_f_name, 'r', 'utf-8') as description_file: description = description_file.read() return render_template('problem.html', problem=current_problem, description=description, admin=admin) else: return redirect_to_user( u'Problem {} does not exist'.format(problem_name), is_error=True)
def test_problem_model_backref(session_scope_module, backref, expected_type): problem = get_problem(session_scope_module, 'iris') backref_attr = getattr(problem, backref) assert isinstance(backref_attr, list) # only check if the list is not empty if backref_attr: assert isinstance(backref_attr[0], expected_type)
def test_problem_model(session_scope_module): problem = get_problem(session_scope_module, 'iris') assert (repr(problem) == "Problem({})\nWorkflow(Classifier)\n\tWorkflow(Classifier): " "WorkflowElement(classifier)".format(encode_string('iris'))) # check that we can access the problem module and that we have one of the # expected function there. assert hasattr(problem.module, 'get_train_data') assert problem.title == 'Iris classification' assert issubclass(problem.Predictions, BasePrediction) X_train, y_train = problem.get_train_data() assert X_train.shape == (120, 4) assert y_train.shape == (120, ) X_test, y_test = problem.get_test_data() assert X_test.shape == (30, 4) assert y_test.shape == (30, ) gt_train = problem.ground_truths_train() assert hasattr(gt_train, 'label_names') assert gt_train.y_pred.shape == (120, 3) gt_test = problem.ground_truths_test() assert hasattr(gt_test, 'label_names') assert gt_test.y_pred.shape == (30, 3) gt_valid = problem.ground_truths_valid([0, 1, 2]) assert hasattr(gt_valid, 'label_names') assert gt_valid.y_pred.shape == (3, 3) assert isinstance(problem.workflow_object, Classifier)
def problem(problem_name): """Landing page for a single RAMP problem. Parameters ---------- problem_name : str The name of a problem. """ current_problem = get_problem(db.session, problem_name) if current_problem: if flask_login.current_user.is_authenticated: add_user_interaction(db.session, interaction='looking at problem', user=flask_login.current_user, problem=current_problem) else: add_user_interaction(db.session, interaction='looking at problem', problem=current_problem) description_f_name = os.path.join( current_problem.path_ramp_kits, current_problem.name, '{}_starting_kit.html'.format(current_problem.name)) with codecs.open(description_f_name, 'r', 'utf-8') as description_file: description = description_file.read() return render_template('problem.html', problem=current_problem, description=description) else: return redirect_to_user( u'Problem {} does not exist'.format(problem_name), is_error=True)
def test_add_problems(session_scope_function): add_problems(session_scope_function) problems = get_problem(session_scope_function, None) for problem in problems: assert problem.name in ('iris', 'boston_housing') # trying to add twice the same problem will raise a git error since the # repositories already exist. msg_err = 'The RAMP kit repository was previously cloned.' with pytest.raises(ValueError, match=msg_err): add_problems(session_scope_function)
def problems(): """Landing page showing all the RAMP problems.""" user = (flask_login.current_user if flask_login.current_user.is_authenticated else None) add_user_interaction(db.session, interaction='looking at problems', user=user) # problems = Problem.query.order_by(Problem.id.desc()) return render_template('problems.html', problems=get_problem(db.session, None))
def test_check_problem(session_scope_function): config = read_config(ramp_config_template()) problem_names = ['iris', 'boston_housing'] for problem_name in problem_names: setup_ramp_kits_ramp_data(config, problem_name) ramp_config = generate_ramp_config(config) add_problem(session_scope_function, problem_name, ramp_config['ramp_kits_dir'], ramp_config['ramp_data_dir']) problem = get_problem(session_scope_function, problem_names[0]) assert problem.name == problem_names[0] assert isinstance(problem, Problem) problem = get_problem(session_scope_function, None) assert len(problem) == 2 assert isinstance(problem, list) # Without forcing, we cannot write the same problem twice err_msg = 'Attempting to overwrite a problem and delete all linked events' with pytest.raises(ValueError, match=err_msg): add_problem(session_scope_function, problem_names[0], ramp_config['ramp_kits_dir'], ramp_config['ramp_data_dir'], force=False) # Force add the problem add_problem(session_scope_function, problem_names[0], ramp_config['ramp_kits_dir'], ramp_config['ramp_data_dir'], force=True) problem = get_problem(session_scope_function, problem_names[0]) assert problem.name == problem_names[0] assert isinstance(problem, Problem) delete_problem(session_scope_function, problem_names[0]) problem = get_problem(session_scope_function, problem_names[0]) assert problem is None problem = get_problem(session_scope_function, None) assert len(problem) == 1 assert isinstance(problem, list)
def problems(): """Landing page showing all the RAMP problems.""" user = (flask_login.current_user if flask_login.current_user.is_authenticated else None) admin = user.access_level == 'admin' if user is not None else False if app.config['TRACK_USER_INTERACTION']: add_user_interaction(db.session, interaction='looking at problems', user=user) # problems = Problem.query.order_by(Problem.id.desc()) return render_template('problems.html', problems=get_problem(db.session, None), admin=admin)
def problem(problem_name): """Landing page for a single RAMP problem. Parameters ---------- problem_name : str The name of a problem. """ current_problem = get_problem(db.session, problem_name) user = (flask_login.current_user if flask_login.current_user.is_authenticated else None) admin = user.access_level == 'admin' if user is not None else False if current_problem: if app.config['TRACK_USER_INTERACTION']: if flask_login.current_user.is_authenticated: add_user_interaction(db.session, interaction='looking at problem', user=flask_login.current_user, problem=current_problem) else: add_user_interaction(db.session, interaction='looking at problem', problem=current_problem) description_f_name = os.path.join( current_problem.path_ramp_kit, '{}_starting_kit.html'.format(current_problem.name)) # check which event ramp-kit archive is the latest archive_dir = os.path.join(current_problem.path_ramp_kit, "events_archived") latest_event_zip = max( [f for f in os.scandir(archive_dir) if f.name.endswith(".zip")], key=lambda x: x.stat().st_mtime) latest_event = os.path.splitext(latest_event_zip.name)[0] return render_template('problem.html', problem=current_problem, admin=admin, notebook_filename=description_f_name, latest_event=latest_event) else: return redirect_to_user( 'Problem {} does not exist'.format(problem_name), is_error=True)
def problems(): """Landing page showing all the RAMP problems.""" user = (flask_login.current_user if flask_login.current_user.is_authenticated else None) admin = user.access_level == 'admin' if user is not None else False if app.config['TRACK_USER_INTERACTION']: add_user_interaction( db.session, interaction='looking at problems', user=user ) problems = get_problem(db.session, None) for problem in problems: for event in problem.events: # check the state of the event now = datetime.datetime.now() start = event.opening_timestamp start_collab = event.public_opening_timestamp end = event.closing_timestamp if now < start or now >= end: event.state = 'close' elif now >= start and now < start_collab: event.state = 'competitive' elif now >= start and now >= start_collab and now < end: event.state = 'collab' if user: signed = get_event_team_by_name( db.session, event.name, flask_login.current_user.name) if not signed: event.state_user = '******' elif signed.approved: event.state_user = '******' elif signed: event.state_user = '******' else: event.state_user = '******' # problems = Problem.query.order_by(Problem.id.desc()) return render_template('problems.html', problems=problems, admin=admin)
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)
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)
def notebook(problem_name): current_problem = get_problem(db.session, problem_name) return send_from_directory( current_problem.path_ramp_kit, '{}_starting_kit.html'.format(current_problem.name))