allensdk.api.api module

class allensdk.api.api.Api(api_base_url_string=None)[source]

Bases: object

cleanup_truncated_file(file_path)[source]

Helper for removing files.

Parameters:
file_pathstring

Absolute path including the file name to remove.

construct_well_known_file_download_url(well_known_file_id)[source]

Join data api endpoint and id.

Parameters:
well_known_file_idinteger or string representing an integer

well known file id

Returns:
string

the well-known-file download url for the current api api server

See also

retrieve_file_over_http

Can be used to retrieve the file from the url.

default_api_url = 'http://api.brain-map.org'
do_query(url_builder_fn, json_traversal_fn, *args, **kwargs)[source]

Bundle an query url construction function with a corresponding response json traversal function.

Parameters:
url_builder_fnfunction

A function that takes parameters and returns an rma url.

json_traversal_fnfunction

A function that takes a json-parsed python data structure and returns data from it.

postboolean, optional kwarg

True does an HTTP POST, False (default) does a GET

argsarguments

Arguments to be passed to the url builder function.

kwargskeyword arguments

Keyword arguments to be passed to the rma builder function.

Returns:
any type

The data extracted from the json response.

Examples

A simple Api subclass example.

do_rma_query(rma_builder_fn, json_traversal_fn, *args, **kwargs)[source]

Bundle an RMA query url construction function with a corresponding response json traversal function.

..note:: Deprecated in AllenSDK 0.9.2

do_rma_query will be removed in AllenSDK 1.0, it is replaced by do_query because the latter is more general.

Parameters:
rma_builder_fnfunction

A function that takes parameters and returns an rma url.

json_traversal_fnfunction

A function that takes a json-parsed python data structure and returns data from it.

argsarguments

Arguments to be passed to the rma builder function.

kwargskeyword arguments

Keyword arguments to be passed to the rma builder function.

Returns:
any type

The data extracted from the json response.

Examples

A simple Api subclass example.

download_url = 'http://download.alleninstitute.org'
json_msg_query(url, dataframe=False)[source]
Common case where the url is fully constructed

and the response data is stored in the ‘msg’ field.

Parameters:
urlstring

Where to get the data in json form

dataframeboolean

True converts to a pandas dataframe, False (default) doesn’t

Returns:
dict or DataFrame

returned data; type depends on dataframe option

load_api_schema()[source]

Download the RMA schema from the current RMA endpoint

Returns:
dict

the parsed json schema message

Notes

This information and other Allen Brain Atlas Data Portal Data Model documentation is also available as a Class Hierarchy and Class List.

read_data(parsed_json)[source]

Return the message data from the parsed query.

Parameters:
parsed_jsondict

A python structure corresponding to the JSON data returned from the API.

Notes

See API Response Formats - Response Envelope for additional documentation.

retrieve_file_over_http(url, file_path, zipped=False)[source]

Get a file from the data api and save it.

Parameters:
urlstring

Url[R099781a1d33c-1]_ from which to get the file.

file_pathstring

Absolute path including the file name to save.

zippedbool, optional

If true, assume that the response is a zipped directory and attempt to extract contained files into the directory containing file_path. Default is False.

See also

construct_well_known_file_download_url

Can be used to construct the url.

References

[1]

Allen Brain Atlas Data Portal: Downloading a WellKnownFile.

retrieve_parsed_json_over_http(url, post=False)[source]

Get the document and put it in a Python data structure

Parameters:
urlstring

Full API query url.

postboolean

True does an HTTP POST, False (default) encodes the URL and does a GET

Returns:
dict

Result document as parsed by the JSON library.

retrieve_xml_over_http(url)[source]

Get the document and put it in a Python data structure

Parameters:
urlstring

Full API query url.

Returns:
string

Unparsed xml string.

set_api_urls(api_base_url_string)[source]

Set the internal RMA and well known file download endpoint urls based on a api server endpoint.

Parameters:
api_base_url_stringstring

url of the api to point to

set_default_working_directory(working_directory)[source]

Set the working directory where files will be saved.

Parameters:
working_directorystring

the absolute path string of the working directory.

allensdk.api.api.stream_file_over_http(url, file_path, timeout=(9.05, 31.1))[source]

Supply an http get request and stream the response to a file.

Parameters:
urlstr

Send the request to this url

file_pathstr

Stream the response to this path

timeoutfloat or tuple of float, optional

Specify a timeout for the request. If a tuple, specify seperate connect and read timeouts.

allensdk.api.api.stream_zip_directory_over_http(url, directory, members=None, timeout=(9.05, 31.1))[source]

Supply an http get request and stream the response to a file.

Parameters:
urlstr

Send the request to this url

directorystr

Extract the response to this directory

memberslist of str, optional

Extract only these files

timeoutfloat or tuple of float, optional

Specify a timeout for the request. If a tuple, specify seperate connect and read timeouts.