diff --git a/elab_bridge/server_interface.py b/elab_bridge/server_interface.py index 21a5c6cc..9ccc6e74 100644 --- a/elab_bridge/server_interface.py +++ b/elab_bridge/server_interface.py @@ -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".') diff --git a/elab_bridge/tests/test_server_interface.py b/elab_bridge/tests/test_server_interface.py index 258815c1..e5adadd2 100644 --- a/elab_bridge/tests/test_server_interface.py +++ b/elab_bridge/tests/test_server_interface.py @@ -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() diff --git a/redcap_bridge/tests/testfiles_redcap/TestProject/project.json b/redcap_bridge/tests/testfiles_redcap/TestProject/project.json index 5f5b9373..4dc099d7 100644 --- a/redcap_bridge/tests/testfiles_redcap/TestProject/project.json +++ b/redcap_bridge/tests/testfiles_redcap/TestProject/project.json @@ -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",