oneibl.webclient

Functions

dataset_record_to_url

Extracts a list of files urls from a list of dataset queries.

file_record_to_url

Translate a Json dictionary to an usable http url for downlading files.

globus_path_from_dataset

Returns local one file path from a dset record or a list of dsets records from REST :param dset: dset dictionary or list of dictionaries from ALyx rest endpoint :param repository: (optional) repository name of the file record (if None, will take the first filerecord with an URL)

http_download_file

param full_link_to_file

http link to the file.

http_download_file_list

Downloads a list of files from the flat Iron from a list of links.

one_path_from_dataset

Returns local one file path from a dset record or a list of dsets records from REST :param dset: dset dictionary or list of dictionaries from ALyx rest endpoint :param one_cache: the one cache directory

sdsc_globus_path_from_dataset

param dset

dset dictionary or list of dictionaries from ALyx rest endpoint

sdsc_path_from_dataset

Returns sdsc file path from a dset record or a list of dsets records from REST :param dset: dset dictionary or list of dictionaries from ALyx rest endpoint :param root_path: (optional) the prefix path such as one download directory or sdsc root

Classes

AlyxClient

Class that implements simple GET/POST wrappers for the Alyx REST API http://alyx.readthedocs.io/en/latest/api.html

UniqueSingletons

sdsc_globus_path_from_dataset(dset)[source]
Parameters

dset – dset dictionary or list of dictionaries from ALyx rest endpoint

Returns SDSC globus file path from a dset record or a list of dsets records from REST

globus_path_from_dataset(dset, repository=None, uuid=False)[source]

Returns local one file path from a dset record or a list of dsets records from REST :param dset: dset dictionary or list of dictionaries from ALyx rest endpoint :param repository: (optional) repository name of the file record (if None, will take

the first filerecord with an URL)

one_path_from_dataset(dset, one_cache)[source]

Returns local one file path from a dset record or a list of dsets records from REST :param dset: dset dictionary or list of dictionaries from ALyx rest endpoint :param one_cache: the one cache directory

sdsc_path_from_dataset(dset, root_path=PurePosixPath('/mnt/ibl'))[source]

Returns sdsc file path from a dset record or a list of dsets records from REST :param dset: dset dictionary or list of dictionaries from ALyx rest endpoint :param root_path: (optional) the prefix path such as one download directory or sdsc root

http_download_file_list(links_to_file_list, **kwargs)[source]

Downloads a list of files from the flat Iron from a list of links. Same options behaviour as http_download_file

Parameters

links_to_file_list (list) – list of http links to files.

Returns

(list) a list of the local full path of the downloaded files.

http_download_file(full_link_to_file, chunks=None, *, clobber=False, username='', password='', cache_dir='', return_md5=False, headers=None)[source]
Parameters
  • full_link_to_file (str) – http link to the file.

  • clobber (bool) – [False] If True, force overwrite the existing file.

  • username (str) – [‘’] authentication for password protected file server.

  • password (str) – [‘’] authentication for password protected file server.

  • cache_dir (str) – [‘’] directory in which files are cached; defaults to user’s Download directory.

Param

headers: [{}] additional headers to add to the request (auth tokens etc..)

Returns

(str) a list of the local full path of the downloaded files.

file_record_to_url(file_records, urls=[])[source]

Translate a Json dictionary to an usable http url for downlading files.

Parameters
  • file_records (dict) – json containing a ‘data_url’ field

  • urls (list) – a list of strings containing previous data_urls on which new urls will be appended

Returns

urls: (list) a list of strings representing full data urls

dataset_record_to_url(dataset_record)[source]

Extracts a list of files urls from a list of dataset queries.

Parameters

dataset_record (list) – dataset Json from a rest request.

Returns

(list) a list of strings representing files urls corresponding to the datasets records

class UniqueSingletons[source]

Bases: type

class AlyxClient(*args, **kwargs)[source]

Bases: object

Class that implements simple GET/POST wrappers for the Alyx REST API http://alyx.readthedocs.io/en/latest/api.html

authenticate(username='', password='', base_url='')[source]

Gets a security token from the Alyx REST API to create requests headers. Credentials are in the params_secret_template.py file

Parameters
  • username (str) – Alyx database user

  • password (str) – Alyx database password

  • base_url (str) – Alyx server address, including port and protocol

delete(rest_query)[source]

Sends a DELETE request to the Alyx server. Will raise an exception on any status_code other than 200, 201.

Parameters

rest_query (str) – examples: ‘/weighings/c617562d-c107-432e-a8ee-682c17f9e698’ ‘https://test.alyx.internationalbrainlab.org/weighings/c617562d-c107-432e-a8ee-682c17f9e698’.

Returns

(dict/list) json interpreted dictionary from response

download_file(url, **kwargs)[source]

Downloads a file on the Alyx server from a filerecord REST field URL :param url: full url of the file :param kwargs: webclient.http_download_file parameters :return: local path of downloaded file

get(rest_query)[source]

Sends a GET request to the Alyx server. Will raise an exception on any status_code other than 200, 201. For the dictionary contents and list of endpoints, refer to: https://alyx.internationalbrainlab.org/docs

Parameters

rest_query (str) – example: ‘/sessions?user=Hamish’.

Returns

(dict/list) json interpreted dictionary from response

patch(rest_query, data=None, files=None)[source]

Sends a PATCH request to the Alyx server. For the dictionary contents, refer to: https://alyx.internationalbrainlab.org/docs

Parameters
  • rest_query (str) – (required)the endpoint as full or relative URL

  • data (None, dict or str) – json encoded string or dictionary (cf.requests)

  • files – dictionary / tuple (cf.requests)

Returns

response object

post(rest_query, data=None, files=None)[source]

Sends a POST request to the Alyx server. For the dictionary contents, refer to: https://alyx.internationalbrainlab.org/docs

Parameters
  • rest_query (str) – (required)the endpoint as full or relative URL

  • data (None, dict or str) – dictionary or json encoded string

  • files – dictionary / tuple (cf.requests)

Returns

response object

put(rest_query, data=None, files=None)[source]

Sends a PUT request to the Alyx server. For the dictionary contents, refer to: https://alyx.internationalbrainlab.org/docs

Parameters
  • rest_query (str) – (required)the endpoint as full or relative URL

  • data (None, dict or str) – dictionary or json encoded string

  • files – dictionary / tuple (cf.requests)

Returns

response object

rest(url=None, action=None, id=None, data=None, files=None, **kwargs)[source]

alyx_client.rest(): lists endpoints alyx_client.rest(endpoint): lists actions for endpoint alyx_client.rest(endpoint, action): lists fields and URL

Example with a rest endpoint with all actions

>>> alyx.client.rest('subjects', 'list')
    alyx.client.rest('subjects', 'list', field_filter1='filterval')
    alyx.client.rest('subjects', 'create', data=sub_dict)
    alyx.client.rest('subjects', 'read', id='nickname')
    alyx.client.rest('subjects', 'update', id='nickname', data=sub_dict)
    alyx.client.rest('subjects', 'partial_update', id='nickname', data=sub_dict)
    alyx.client.rest('subjects', 'delete', id='nickname')
    alyx.client.rest('notes', 'create', data=nd, files={'image': open(image_file, 'rb')})
Parameters
  • url – endpoint name

  • action – ‘list’, ‘create’, ‘read’, ‘update’, ‘partial_update’, ‘delete’

  • id – lookup string for actions ‘read’, ‘update’, ‘partial_update’, and ‘delete’

  • data – data dictionary for actions ‘update’, ‘partial_update’ and ‘create’

  • files – if file upload

  • **kwargs – filter as per the Alyx REST documentation cf. https://alyx.internationalbrainlab.org/docs/

Returns

list of queried dicts (‘list’) or dict (other actions)

json_field_write(endpoint: str = None, uuid: str = None, field_name: str = None, data: dict = None) → dict[source]

json_field_write [summary] Write data to WILL NOT CHECK IF DATA EXISTS NOTE: Destructive write!

Parameters
  • endpoint (str, optional) – Valid alyx endpoint, defaults to None

  • uuid (str, optional) – uuid or lookup name for endpoint

  • field_name (str, optional) – Valid json field name, defaults to None

  • data (dict, optional) – data to write to json field, defaults to None

Returns

Written data dict

Return type

dict

json_field_update(endpoint: str = None, uuid: str = None, field_name: str = 'json', data: dict = None) → dict[source]

Non destructive update of json field of endpoint for object Will update the field_name of the object with pk = uuid of given endpoint If data has keys with the same name of existing keys it will squash the old values (uses the dict.update() method)

Example: one.alyx.json_field_update(“sessions”, “eid_str”, “extended_qc”, {“key”: value})

Parameters
  • endpoint (str) – endpoint to hit

  • uuid (str) – uuid or lookup name of object

  • field_name (str) – name of the json field

  • data (dict) – dictionary with fields to be updated

Returns

new patched json field contents

Return type

dict

json_field_remove_key(endpoint: str = None, uuid: str = None, field_name: str = 'json', key: str = None) → dict[source]

Will remove inputted key from json field dict and reupload it to Alyx. Needs endpoint, uuid and json field name

Parameters
  • endpoint (str, optional) – endpoint to hit, defaults to None

  • uuid (str, optional) – uuid or lookup name for endpoint

  • field_name (str, optional) – json field name of object, defaults to None

  • key (str, optional) – key name of dictionary inside object, defaults to None

Returns

returns new content of json field

Return type

dict

json_field_delete(endpoint: str = None, uuid: str = None, field_name: str = None) → None[source]