Skip to content

Commit

Permalink
Fix extended_download function to allow download of multiple experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
killianrochet committed Dec 20, 2023
1 parent 9eb0ff5 commit 8267d52
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
32 changes: 28 additions & 4 deletions elab_bridge/server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
import pandas as pd


def extended_download(server_config_json, experiment_tags):
def extended_download(save_to, server_config_json, experiment_tags):
"""
Download an individual experiment.
Parameters
----------
save_to: str
Path where to save the retrieved experiment data
server_config_json: str
Path to the json file containing the api_url and the api_token
experiment_tags: list
List of tags of your experiments
Returns
-------
(list) List of the experiment downloaded
"""

api_client = get_elab_config(server_config_json)
experiment_api = elabapi_python.ExperimentsApi(api_client)

Expand All @@ -17,7 +34,14 @@ def extended_download(server_config_json, experiment_tags):
for experiment in experiments:
experiment_ids.append(experiment.id)

return experiment_ids
downloaded_experiments = []

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

return downloaded_experiments


def download_experiment(save_to, server_config_json, experiment_id, format='json', experiment_axis='columns'):
Expand Down Expand Up @@ -64,10 +88,10 @@ 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, index=False)
df.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, index=True)
df.to_csv(save_to, mode='a', index=True)
else:
raise ValueError(f'Unknown experiment axis: {experiment_axis}. Valid arguments are '
f'"columns" and "rows".')
Expand Down
15 changes: 5 additions & 10 deletions elab_bridge/tests/test_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,12 @@ def test_download_experiment(initialize_test_dir):
def test_extended_download(initialize_test_dir):
json_file = test_directory / 'testfiles_elab' / 'downloaded_experiment.json'

experiments_ids = extended_download(server_config_json=SERVER_CONFIG_YAML,
experiment = extended_download(save_to=json_file, server_config_json=SERVER_CONFIG_YAML,
experiment_tags=['BIDS'])
print(experiments_ids)

for experiment_id in experiments_ids:
experiment = download_experiment(save_to=json_file,
server_config_json=SERVER_CONFIG_YAML,
experiment_id=experiment_id,
format='json')
assert json_file.exists()
assert 'extra_fields' in experiment

assert json_file.exists()
for exp in experiment:
assert 'extra_fields' in exp

# cleanup
json_file.unlink()
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"external_modules": {
"auto_populate_fields": {
"link": "https://github.com/ctsit/auto_populate_fields",
"version": "v2.5.2"
"version": "v2.6.0"
}
},
"api_token": "REDCAP_API_TOKEN",
Expand Down

0 comments on commit 8267d52

Please sign in to comment.