Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First version of the multiplate export / download of experiments. #92

Merged
merged 6 commits into from
Dec 20, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix extended_download function to allow download of multiple experiment
killianrochet committed Dec 20, 2023
commit 8267d5275d3cb3e65f861d5c84b3e140039f9503
32 changes: 28 additions & 4 deletions elab_bridge/server_interface.py
Original file line number Diff line number Diff line change
@@ -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)

@@ -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'):
@@ -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".')
15 changes: 5 additions & 10 deletions elab_bridge/tests/test_server_interface.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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",