def ls(self, table=None, verbose=False): """ Queries the database for a list of 'users' and/or 'dataset-types' and/or 'subjects' fields :param table: the table (s) to query among: 'dataset-types','users' and 'subjects'; if empty or None assumes all tables :type table: str, list :param verbose: [False] prints the list in the current window :type verbose: bool :return: list of names to query, list of full raw output in json serialized format :rtype: list, list """ tlist = ('dataset-types', 'users', 'subjects') field = ('name', 'username', 'nickname') if not table: table = tlist table = [table] if isinstance(table, str) else table full_out = [] list_out = [] for ind, tab in enumerate(tlist): if tab in table: full_out.append(self._alyxClient.get('/' + tab)) list_out.append([f[field[ind]] for f in full_out[-1]]) if verbose: pprint(list_out) if len(table) == 1: return list_out[0], full_out[0] else: return list_out, full_out
def _ls(self, table=None, verbose=False): """ Queries the database for a list of 'users' and/or 'dataset-types' and/or 'subjects' fields :param table: the table (s) to query among: 'dataset-types','users' and 'subjects'; if empty or None assumes all tables :type table: str :param verbose: [False] prints the list in the current window :type verbose: bool :return: list of names to query, list of full raw output in json serialized format :rtype: list, list """ assert (isinstance(table, str)) table_field_names = { 'dataset-types': 'name', 'datasets': 'name', 'users': 'username', 'subjects': 'nickname', 'labs': 'name' } if not table or table not in list(set(_ENDPOINTS.keys())): raise KeyError("The attribute/endpoint: " + table + " doesn't exist \n" + "possible values are " + str(set(_ENDPOINTS.values()))) field_name = table_field_names[_ENDPOINTS[table]] full_out = self.alyx.get('/' + _ENDPOINTS[table]) list_out = [f[field_name] for f in full_out] if verbose: pprint(list_out) return list_out, full_out
def rest(self, url=None, action=None, id=None, data=None, **kwargs): """ 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_ict) alyx.client.rest('subjects', 'delete', id='nickname') :param url: endpoint name :param action: 'list', 'create', 'read', 'update', 'partial_update', 'delete' :param id: lookup string for actions 'read', 'update', 'partial_update', and 'delete' :param data: data dictionary for actions 'update', 'partial_update' and 'create' :param ``**kwargs``: filter as per the REST documentation :return: list of queried dicts ('list') or dict (other actions) """ # if endpoint is None, list available endpoints if not url: pprint([ k for k in self._rest_schemes.keys() if not k.startswith('_') and k ]) return # remove beginning slash if any if url.startswith('/'): url = url[1:] # and split to the next slash or question mark endpoint = re.findall("^/*[^?/]*", url)[0].replace('/', '') # make sure the queryied endpoint exists, if not throw an informative error if endpoint not in self._rest_schemes.keys(): av = [ k for k in self._rest_schemes.keys() if not k.startswith('_') and k ] raise ValueError('REST endpoint "' + endpoint + '" does not exist. Available ' + 'endpoints are \n ' + '\n '.join(av)) endpoint_scheme = self._rest_schemes[endpoint] # on a filter request, override the default action parameter if '?' in url: action = 'list' # if action is None, list available actions for the required endpoint if not action: pprint(list(endpoint_scheme.keys())) return # make sure the the desired action exists, if not throw an informative error if action not in endpoint_scheme.keys(): raise ValueError('Action "' + action + '" for REST endpoint "' + endpoint + '" does ' + 'not exist. Available actions are: ' + '\n ' + '\n '.join(endpoint_scheme.keys())) # the actions below require an id in the URL, warn and help the user if action in ['read', 'update', 'partial_update', 'delete'] and not id: logger_.warning('REST action "' + action + '" requires an ID in the URL: ' + endpoint_scheme[action]['url']) return # the actions below require a data dictionary, warn and help the user with fields list if action in ['create', 'update', 'partial_update'] and not data: pprint(endpoint_scheme[action]['fields']) for act in endpoint_scheme[action]['fields']: print("'" + act['name'] + "': ...,") logger_.warning('REST action "' + action + '" requires a data dict with above keys') return if action == 'list': # list doesn't require id nor assert (endpoint_scheme[action]['action'] == 'get') # add to url data if it is a string if id: url = url + id # otherwise, look for a dictionary of filter terms elif kwargs: for k in kwargs.keys(): if isinstance(kwargs[k], str): query = kwargs[k] elif isinstance(kwargs[k], list): query = ','.join(kwargs[k]) else: continue url = url + f"?&{k}=" + query return self.get('/' + url) if action == 'read': assert (endpoint_scheme[action]['action'] == 'get') return self.get('/' + endpoint + '/' + id.split('/')[-1]) elif action == 'create': assert (endpoint_scheme[action]['action'] == 'post') return self.post('/' + endpoint, data=data) elif action == 'delete': assert (endpoint_scheme[action]['action'] == 'delete') return self.delete('/' + endpoint + '/' + id.split('/')[-1]) elif action == 'partial_update': assert (endpoint_scheme[action]['action'] == 'patch') return self.patch('/' + endpoint + '/' + id.split('/')[-1], data=data) elif action == 'update': assert (endpoint_scheme[action]['action'] == 'put') return self.put('/' + endpoint + '/' + id.split('/')[-1], data=data)
from ibllib.misc import pprint from oneibl.one import ONE one = ONE(base_url='https://test.alyx.internationalbrainlab.org') # list all water administrations wa = one.alyx.rest('water-administrations', 'list') # to list administrations for one subject, it is better to use the subjects endpoint sub_info = one.alyx.rest('subjects', 'read', 'ZM_346') pprint(sub_info['water_administrations']) # this is how to programmatically create a water administration wa_ = { 'subject': 'ZM_346', 'date_time': '2018-11-25T12:34', 'water_administered': 25, 'water_type': 'Water 10% Sucrose', 'user': '******', 'session': 'f4b13ba2-1308-4673-820e-0e0a3e0f2d73', 'adlib': True } # do not use the example on anything else than alyx-dev if one.alyx._base_url != 'https://dev.alyx.internationalbrainlab.org': rep = one.alyx.rest('water-administrations', 'create', wa_)
## Init from oneibl.one import ONE from ibllib.misc import pprint one = ONE(base_url='https://test.alyx.internationalbrainlab.org', username='******', password='******') ## Find an experiment eid = one.search(users='olivier', date_range=['2018-08-24', '2018-08-24']) pprint(eid) one.search_terms() ## List dataset types for a session eid = 'cf264653-2deb-44cb-aa84-89b82507028a' one.list(eid) ## List #1 one.list() ## Load #1 dataset_types = [ 'clusters.templateWaveforms', 'clusters.probes', 'clusters.depths' ] eid = 'cf264653-2deb-44cb-aa84-89b82507028a' wf, pr, d = one.load(eid, dataset_types=dataset_types) ## Load #2 my_data = one.load(eid, dataset_types=dataset_types, dclass_output=True) from ibllib.misc import pprint
def rest(self, url=None, action=None, data=None, **kwargs): """ alyx_client.rest() alyx_client.rest("sessions") lab_info = alyx_client.rest('labs', 'read', 'mainenlab') OR lab_info = alyx_client.rest('labs', 'read', 'https://test.alyx.internationalbrainlab.org/labs/mainenlab') :param url: :param action: :param data: :return: """ # if endpoint is None, list available endpoints if not url: pprint([ k for k in self._rest_schemes.keys() if not k.startswith('_') and k ]) return # remove beginning slash if any if url.startswith('/'): url = url[1:] # and split to the next slash or question mark endpoint = re.findall("^/*[^?/]*", url)[0].replace('/', '') # make sure the queryied endpoint exists, if not throw an informative error if endpoint not in self._rest_schemes.keys(): av = [ k for k in self._rest_schemes.keys() if not k.startswith('_') and k ] raise ValueError('REST endpoint "' + endpoint + '" does not exist. Available ' + 'endpoints are \n ' + '\n '.join(av)) endpoint_scheme = self._rest_schemes[endpoint] # on a filter request, override the default action parameter if '?' in url: action = 'list' # if action is None, list available actions for the required endpoint if not action: pprint(list(endpoint_scheme.keys())) return # make sure the the desired action exists, if not throw an informative error if action not in endpoint_scheme.keys(): raise ValueError('Action "' + action + '" for REST endpoint "' + endpoint + '" does ' + 'not exist. Available actions are: ' + '\n ' + '\n '.join(endpoint_scheme.keys())) # if there is no data (except for list), show the user a list of fields if action != 'list' and not data: pprint(endpoint_scheme[action]['fields']) for act in endpoint_scheme[action]['fields']: print("'" + act['name'] + "': ...,") return if action == 'list': assert (endpoint_scheme[action]['action'] == 'get') # add to url data if it is a string if data: url = url + data # otherwise, look for a dictionary of filter terms elif kwargs: for k in kwargs.keys(): if isinstance(kwargs[k], str): query = kwargs[k] elif isinstance(kwargs[k], list): query = ','.join(kwargs[k]) else: continue url = url + f"?&{k}=" + query return self.get('/' + url) if action == 'read': assert (endpoint_scheme[action]['action'] == 'get') return self.get('/' + endpoint + '/' + data.split('/')[-1]) elif action == 'create': assert (endpoint_scheme[action]['action'] == 'post') return self.post('/' + endpoint, data) elif action == 'delete': assert (endpoint_scheme[action]['action'] == 'delete') return self.delete('/' + endpoint + '/' + data.split('/')[-1]) elif action == 'partial_update': assert (endpoint_scheme[action]['action'] == 'patch') return self.patch('/' + url, data) elif action == 'update': assert (endpoint_scheme[action]['action'] == 'put') return self.put('/' + url, data)
''' Print water administration values from behavior data downloaded via ONE. ''' # Author: Olivier Winter from ibllib.misc import pprint from oneibl.one import ONE one = ONE(base_url='https://dev.alyx.internationalbrainlab.org') # -- Get saved water administration -- # List all water administrations wa = one.alyx.rest('water-administrations', 'list') # To list administrations for one subject, it is better to use the subjects endpoint subject_details = one.alyx.rest('subjects', 'read', 'ZM_346') pprint(subject_details['water_administrations'] [0:2]) # Print the first 2 water admin.
## More Info about a session d = one.session_data_info(eid) print(d) ## Load #1 dataset_types = [ 'clusters.templateWaveforms', 'clusters.probes', 'clusters.depths' ] eid = '86e27228-8708-48d8-96ed-9aa61ab951db' wf, pr, d = one.load(eid, dataset_types=dataset_types) ## Load #2 my_data = one.load(eid, dataset_types=dataset_types, dclass_output=True) from ibllib.misc import pprint pprint(my_data.local_path) pprint(my_data.dataset_type) ## Load everything eid, ses_info = one.search(subject='flowers') my_data = one.load(eid[0]) pprint(my_data.dataset_type) ## Load eid = '86e27228-8708-48d8-96ed-9aa61ab951db' dataset_types = [ 'clusters.probes', 'thisDataset.IveJustMadeUp', 'clusters.depths' ] t, empty, cl = one.load(eid, dataset_types=dataset_types) ## List #1
''' Print water administration values from behavior data downloaded via ONE. ''' # Author: Olivier Winter from ibllib.misc import pprint from oneibl.one import ONE one = ONE(base_url='https://test.alyx.internationalbrainlab.org') # -- Get saved water administration -- # List all water administrations wa = one.alyx.rest('water-administrations', 'list') # To list administrations for one subject, it is better to use the subjects endpoint subject_details = one.alyx.rest('subjects', 'read', 'ZM_346') pprint(subject_details['water_administrations'])