Skip to content

Commit

Permalink
Merge pull request #92 from killianrochet/eLabFTW/MultipleDownload
Browse files Browse the repository at this point in the history
First version of the multiplate export / download of experiments.
  • Loading branch information
killianrochet authored Dec 20, 2023
2 parents 7099729 + 9dca82f commit 6308281
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
44 changes: 42 additions & 2 deletions elab_bridge/server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,46 @@
import pandas as pd


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)

response = experiment_api.read_experiments_with_http_info(tags=experiment_tags)

experiments = response[0]

experiment_ids = []

for experiment in experiments:
experiment_ids.append(experiment.id)

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'):
"""
Download an individual experiment.
Expand Down Expand Up @@ -48,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
16 changes: 15 additions & 1 deletion elab_bridge/tests/test_server_interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from diglab_utils.test_utils import (test_directory, initialize_test_dir)
from elab_bridge.server_interface import (download_experiment, upload_template, upload_experiment,
delete_template, delete_experiment)
delete_template, delete_experiment, extended_download)

SERVER_CONFIG_YAML = (test_directory / 'testfiles_elab' / 'TestProject' / 'project.json').resolve()

Expand Down Expand Up @@ -52,3 +52,17 @@ def test_download_experiment(initialize_test_dir):
# cleanup
delete_experiment(server_config_json=SERVER_CONFIG_YAML, experiment_id=experiment_id)
json_file.unlink()


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'])

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

# cleanup
json_file.unlink()

0 comments on commit 6308281

Please sign in to comment.