Skip to content

Commit

Permalink
ENH Add CLI to delete predictions (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
maikia authored Oct 27, 2020
1 parent f9698eb commit 6373bf0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ramp-database/ramp_database/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,41 @@ def delete_event(config, config_event, dry_run, from_disk, force):
click.echo("Removed directory:\n{}".format(event_dir))


@main.command()
@click.option("--config", default='config.yml', show_default=True,
help='Configuration file YAML format containing the database '
'information')
@click.option("--config-event", required=True,
help='Path to configuration file YAML format '
'containing the database information, eg config.yml')
@click.option('--force', is_flag=True,
help='Flag to force a removal of the predictions from the disk, '
'when an event is not in the database.')
def delete_predictions(config, config_event, force):
"""Delete event predictions from the disk."""
internal_config = read_config(config)
ramp_config = generate_ramp_config(config_event, config)
event_name = ramp_config["event_name"]

with session_scope(internal_config['sqlalchemy']) as session:
db_event = event_module.get_event(session, event_name)

if not db_event and not force:
err_msg = ('{} event not found in the database. If you want '
'to force removing perdiction files from the disk use '
'the option "--force".'
.format(event_name))
raise click.ClickException(err_msg)

dir_to_remove = ramp_config["ramp_predictions_dir"]
if os.path.exists(dir_to_remove):
shutil.rmtree(dir_to_remove)
click.echo("Removed directory:\n{}".format(dir_to_remove))
else:
err_msg = ("Directory not found:\n{}".format(dir_to_remove))
raise click.ClickException(err_msg)


@main.command()
@click.option("--config", default='config.yml', show_default=True,
help='Configuration file YAML format containing the database '
Expand Down
65 changes: 65 additions & 0 deletions ramp-database/ramp_database/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,71 @@ def test_delete_event(make_toy_db, from_disk):
else not os.path.exists(event_path))


@pytest.mark.parametrize("force", [True, False])
@pytest.mark.parametrize("add_to_db", [True, False])
def test_delete_predictions(make_toy_db, database_connection, force,
add_to_db):
# check that delete event is removed from the database and optionally from
# the disk
runner = CliRunner()
ramp_config = read_config(ramp_config_template())
ramp_config['ramp']['event_name'] = 'iris_test2'
deployment_dir = os.path.commonpath([ramp_config['ramp']['kit_dir'],
ramp_config['ramp']['data_dir']])
event_config = os.path.join(
deployment_dir, 'events', ramp_config['ramp']['event_name'],
'config.yml'
)

if add_to_db:
# deploy a new event named `iris_test2`
runner.invoke(main_utils, ['init-event',
'--name', 'iris_test2',
'--deployment-dir', deployment_dir])

with open(event_config, 'w+') as f:
yaml.dump(ramp_config, f)
result = runner.invoke(main_utils, ['deploy-event',
'--config',
database_config_template(),
'--event-config',
event_config,
'--no-cloning'])

# add the directory for predictions
predictions_dir = ramp_config['ramp']['predictions_dir']

os.mkdir(predictions_dir)
assert os.path.exists(predictions_dir)

cmd = ['delete-predictions',
'--config', database_config_template(),
'--config-event', event_config]
if force:
cmd.append('--force')
result = runner.invoke(main, cmd)

if not add_to_db and not force:
assert result.exit_code == 1
assert 'use the option' in result.output
assert os.path.exists(predictions_dir)
else:
assert result.exit_code == 0, result.output
assert not os.path.exists(predictions_dir)

# clean up
if add_to_db:
# remove event from the db
cmd = ['delete-event',
'--config', database_config_template(),
'--config-event', event_config]
result = runner.invoke(main, cmd)

if os.path.exists(predictions_dir):
# remove the dir if not already done
shutil.rmtree(predictions_dir)


def test_sign_up_team(make_toy_db):
runner = CliRunner()
result = runner.invoke(main, ['sign-up-team',
Expand Down

0 comments on commit 6373bf0

Please sign in to comment.