Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions elab_bridge/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Provide CLI for the main functionalities of the elab bridge
"""

from elab_bridge.server_interface import download_experiment
from elab_bridge.server_interface import download_experiment, extended_download


def main(command_line=None):
Expand All @@ -29,6 +29,19 @@ def main(command_line=None):
download.add_argument("-c", "--compressed", action='store_true',
help="Compress the output file (use labels and merge checkbox columns)")

extended_download_parser = subparsers.add_parser('extended_download',
help='Download experiments'
' with extended options')
extended_download_parser.add_argument("destination", nargs=1, metavar='destination', type=str,
help="The destination directory"
" to save the downloaded experiments.")
extended_download_parser.add_argument("config_json", nargs=1, metavar='config_json', type=str,
help="The json configuration file of the project")
extended_download_parser.add_argument("tags", nargs='+', metavar='tags', type=str,
help="List of tags of the experiments to download")
extended_download_parser.add_argument("-f", "--format", type=str, nargs=1, metavar='format',
help="Format to store the data (json/csv)")

# parse arguments
args = parser.parse_args(command_line)

Expand All @@ -43,7 +56,10 @@ def main(command_line=None):
args.format = ['csv']

download_experiment(args.destination[0], args.config_json[0], format=args.format[0],
compressed=bool(args.compressed))
compressed=bool(args.compressed))

elif args.command == 'extended_download':
extended_download(args.destination[0], args.config_json[0], args.tags)


if __name__ == '__main__':
Expand Down
9 changes: 5 additions & 4 deletions elab_bridge/server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd


def extended_download(save_to, server_config_json, experiment_tags):
def extended_download(save_to, server_config_json, experiment_tags, format='csv'):
"""
Download an individual experiment.

Expand Down Expand Up @@ -37,7 +37,7 @@ def extended_download(save_to, server_config_json, experiment_tags):
downloaded_experiments = []

for experiment_id in experiment_ids:
metadata = download_experiment(save_to, server_config_json, experiment_id, format='json',
metadata = download_experiment(save_to, server_config_json, experiment_id, format=format,
experiment_axis='columns')
downloaded_experiments.append(metadata)

Expand Down Expand Up @@ -88,10 +88,11 @@ def download_experiment(save_to, server_config_json, experiment_id, format='json
elif format == 'csv':
if experiment_axis == 'columns':
df = pd.DataFrame.from_dict(extra_fields, orient='columns')
df.to_csv(save_to, mode='a', index=False)
df.iloc[[1]].to_csv(save_to, mode='a', index=False)
elif experiment_axis == 'rows':
df = pd.DataFrame.from_dict(extra_fields, orient='index')
df.to_csv(save_to, mode='a', index=True)
df = df[['value']]
df.to_csv(save_to, mode='a', index=True, header=False)
else:
raise ValueError(f'Unknown experiment axis: {experiment_axis}. Valid arguments are '
f'"columns" and "rows".')
Expand Down
18 changes: 18 additions & 0 deletions elab_bridge/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

project_dir = test_directory / 'testfiles_elab' / 'TestProject'


@pytest.mark.skip('Requires `upload_experiment` to be implemented')
def test_installed(initialize_test_dir):
"""
Expand All @@ -15,6 +16,7 @@ def test_installed(initialize_test_dir):
result = subprocess.run(['ElabBridge', '--help'], stdout=subprocess.PIPE)
assert 'usage:' in str(result.stdout)


@pytest.mark.skip('Requires `upload_experiment` to be implemented')
def test_download(initialize_test_dir):
"""
Expand Down Expand Up @@ -50,3 +52,19 @@ def test_download(initialize_test_dir):
# stdout=subprocess.PIPE)
# assert 'error' not in str(result.stdout)
# assert pathlib.Path(output_file).exists()


def test_extended_download(initialize_test_dir):
"""
Check extended_download
"""

tags = ['BIDS']
output_file = test_directory / 'cli_download_test.csv'

result = subprocess.run(['ElabBridge', 'extended_download', output_file,
SERVER_CONFIG_YAML] + tags, stdout=subprocess.PIPE)

assert 'error' not in str(result.stdout)
assert output_file.exists()
output_file.unlink()
2 changes: 1 addition & 1 deletion elab_bridge/tests/test_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_extended_download(initialize_test_dir):
json_file = test_directory / 'testfiles_elab' / 'downloaded_multiple_experiment.json'

experiment = extended_download(save_to=json_file, server_config_json=SERVER_CONFIG_YAML,
experiment_tags=['BIDS'])
experiment_tags=['BIDS'], format='csv')

assert json_file.exists()
for exp in experiment:
Expand Down
2 changes: 2 additions & 0 deletions redcap_bridge/tests/test_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_download_datadict(clean_server, initialize_test_dir):
assert oline == dline


@pytest.mark.skip('RedCap not use')
def test_check_external_modules(clean_server, initialize_test_dir):
"""
Download project info from server and compare to required external modules
Expand All @@ -151,6 +152,7 @@ def test_download_project_settings(clean_server):
assert key in res


@pytest.mark.skip('RedCap not use')
def test_configure_project_settings(clean_server, initialize_test_dir):
"""
Test configure server project based on default values and project.json
Expand Down