INCF数字图谱基础设施的Python API
项目描述
本软件包提供了对国际神经信息学协调设施(INCF)(INCF)提供的数字脑图谱网络服务的Python API访问。为了在使用时具有实用性和功能性,它需要运行时的有效互联网连接。INCF数字图谱基础设施(incf.dai)中的核心网络服务的详细信息可以从规范中获取。
为了交互式地探索本软件包的功能,强烈建议使用增强型交互式Python解释器,如IPython或bpython。
发现和访问中心节点
简而言之,INCF DAI由一个所谓的中心节点网络组成,提供由管理该中心节点的小组决定共享的各种服务。要发现可用的中心节点,有一个实用函数
>>> from incf.dai.utils import list_hub_names >>> sorted(list_hub_names()) ['aba', 'central', 'emap', 'ucsd', 'whs']
这提供了一个当前已知中心节点的名称列表。(注意:目前该列表由软件包提供,因为管理注册的INCF中心节点仍然是一个快速移动的目标。)
知道可用的中心节点名称后,可以通过调用
>>> from incf.dai.utils import get_hub_by_name >>> whs = get_hub_by_name('whs') >>> whs # doctest: +ELLIPSIS <incf.dai.hub.HubProxy object at ...>
如果您调用未知中心节点(或输入了错误)您将得到一个KeyError
>>> foo = get_hub_by_name('foo') Traceback (most recent call last): KeyError
对于自省,该中心节点的服务控制器URL是可用的
>>> whs.base_url 'http://incf-dev-local.crbs.ucsd.edu/whs/atlas?service=WPS'
所有中心节点都应提供两种方法
>>> whs.GetCapabilities # doctest: +ELLIPSIS <bound method HubProxy.GetCapabilities of ...>
>>> whs.DescribeProcess # doctest: +ELLIPSIS <bound method HubProxy.DescribeProcess of ...>
调用这些方法中的任何一种都会返回一个自定义响应对象
>>> response = whs.GetCapabilities() >>> response # doctest: +ELLIPSIS <incf.dai.response.Response object at 0x...>
>>> response = whs.DescribeProcess() >>> response # doctest: +ELLIPSIS <incf.dai.response.Response object at 0x...>
通常,无需调用上述两种方法,因为它们返回的主要信息已经可以通过
>>> sorted(whs.capabilities) [u'DescribeSRS', u'GetStructureNamesByPOI', u'ListSRSs', u'ListTransformations', u'TransformPOI']
和所有列出方法都可在中心节点代理上立即使用
>>> sorted(whs.__dict__.keys()) # doctest: +NORMALIZE_WHITESPACE ['DescribeSRS', 'GetStructureNamesByPOI', 'ListSRSs', 'ListTransformations', 'TransformPOI', 'base_url', 'capabilities', 'process_descriptions', 'proxy']
如下所示
>>> response = whs.ListSRSs() >>> sorted(response.keys()) # doctest: +NORMALIZE_WHITESPACE [u'service', u'serviceInstance', u'version', u'wps_Process', u'wps_ProcessOutputs', u'wps_Status', u'xml_lang', u'xmlns_ogc', u'xmlns_ows', u'xmlns_wps', u'xmlns_xlink', u'xmlns_xsi', u'xsi_schemaLocation']
>>> response['wps_ProcessOutputs']['wps_Output']['wps_Data']['wps_ComplexData']['ListSRSResponse']['SRSCollection']['SRSList'] # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE {SRS:[{Area:{structureName:u'Whole Brain'}, Author:{authorCode:u'WHS', ...
为了方便起见,响应对象也支持类似属性的数据访问,如下所示
>>> response.wps_Status # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE {creationTime:...
注意命名空间被保留为键和属性名称的前缀。
省略必需的参数会引发一个ApplicationError
>>> response = whs.DescribeSRS() # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE Traceback (most recent call last): ... ApplicationError: Code: NoApplicableCode Text: Unexpected exception occured URL: http://incf-dev-local.crbs.ucsd.edu/whs/atlas?service=WPS&version=1.0.0&request=Execute&Identifier=DescribeSRS&ResponseForm=xml
而正确调用方法会给出合适的响应(希望如此)
>>> response = whs.GetStructureNamesByPOI(format=None, srsName="Mouse_paxinos_1.0", x='1', y='4.3', z='1.78') # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE >>> response.keys() [u'xmlns_xlink', u'wps_Process', u'xml_lang', u'wps_Status', u'wps_ProcessOutputs', u'service', u'xmlns_xsi', u'xmlns_ows', u'xsi_schemaLocation', u'version', u'xmlns_ogc', u'xmlns_wps', u'serviceInstance'] >>> response.wps_ProcessOutputs.wps_Output.wps_Data.wps_ComplexData.StructureTermsResponse.StructureTerms.StructureTerm.Code.data u'Cx'
这里的format=None解决了问题http://code.google.com/p/incf-dai/issues/detail?id=14
响应的详细情况
从服务调用返回的自定义响应对象提供了各种有用的信息,如HTTP响应头
>>> sorted(response.headers.keys()) # doctest: +NORMALIZE_WHITESPACE ['connection', 'content-location', 'content-type', 'date', 'status', 'transfer-encoding']
为了方便起见,提供了一个到内容类型的快捷方式
>>> response.content_type 'text/xml;charset=UTF-8'
返回响应页面的源代码可用作
>>> response.content # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE '<?xml version="1.0" encoding="UTF-8"?>\n...
打印时可能更易读(对于本文档测试,避免调用print response,但在交互式会话中应正常工作)
>>> str(response) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE '<?xml version="1.0" ?><wps:ExecuteResponse service="WPS" ...
如果内容类型是XML,将有一个‘data’属性包含解析后的响应
>>> type(response.data) <class 'incf.dai.utils.DataNode'>
它就像嵌套字典一样操作
>>> response.data.keys() # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE [u'xmlns_xlink', u'wps_Process', u'xml_lang', u'wps_Status', u'wps_ProcessOutputs', u'service', u'xmlns_xsi', u'xmlns_ows', u'xsi_schemaLocation', u'version', u'xmlns_ogc', u'xmlns_wps', u'serviceInstance']
>>> wps_Data = response.data['wps_ProcessOutputs']['wps_Output']['wps_Data'] >>> wps_Data['wps_ComplexData']['StructureTermsResponse']['StructureTerms'] # doctest: +NORMALIZE_WHITESPACE {StructureTerm:{Code:{codeSpace:u'Mouse_paxinos_1.0', isDefault:u'true', data:u'Cx'}, Description:u'Term - Cx derived from WHS hub based on the supplied POI.', Name:''}, hubCode:u'WHS'}
但也支持类似属性的操作(只要键不包含使它们不适合作为属性名称的字符 - 在这种情况下,只有订阅访问是可行的)
>>> response.data.xmlns_xsi u'http://www.w3.org/2001/XMLSchema-instance'
为了方便起见,可以绕过data属性,因为data内容被“提升”到响应对象本身
>>> response.data.keys() == response.keys() True
和
>>> response.xmlns_xsi u'http://www.w3.org/2001/XMLSchema-instance'
深入了解响应数据需要了解响应模式,该模式可在http://incf.org/WaxML或通过自省response.data属性的内容中获得。
提高性能
如上所述,通过调用get_hub_by_name访问中心节点会触发在中心节点代理初始化时调用DescribeProcess,以推断中心节点的功能并动态创建使这些功能 readily available on the center node proxy 的方法。根据上下文和预期使用,您可能想避免这种开销,而更愿意使用“裸”中心节点代理,如下所示
>>> whs_minimal = get_hub_by_name('whs', minimal=True)
这避免了在初始化时调用 DescribeProcess,但作为交换,你只剩下了需要传递所有构造正确WPS请求所需参数的通用调用方法
>>> response = whs_minimal(version='1.0.0', request='Execute', identifier='ListSRSs') >>> sorted(response.keys()) # doctest: +NORMALIZE_WHITESPACE [u'service', u'serviceInstance', u'version', u'wps_Process', u'wps_ProcessOutputs', u'wps_Status', u'xml_lang', u'xmlns_ogc', u'xmlns_ows', u'xmlns_wps', u'xmlns_xlink', u'xmlns_xsi', u'xsi_schemaLocation']
一旦你知道需要从自己的代码中调用什么,你可能更喜欢这种方法。
日志记录
默认情况下,所有服务调用都以 INFO 级别记录到当前工作目录中的自定义日志文件(incf.dai.log),包括时间戳、包名、日志级别和被调用的URL。
访问未在INCF注册的Hubs
要连接到未在INCF中央注册的Hub(例如,一个正在开发中的本地Hub),可以显式实例化代理,如(设置 offline=True 避免调用URL)
>>> from incf.dai.hub import HubProxy >>> myhub = HubProxy('http://some.url?service=WPS', offline=True) >>> myhub # doctest: +ELLIPSIS <incf.dai.hub.HubProxy object at ...>
至少应该始终可用的是要调用的通用调用方法,如下所示
>>> capabilities = myhub(version=None, request='GetCapabilities') Requested URL: http://some.url?service=WPS&request=GetCapabilities&ResponseForm=xml positional arguments: ('GET',)
应该始终可用。这里的第一个参数是用来覆盖版本指定的,对于 GetCapabilities 调用不支持。第二个是要在Hub上调用的请求的名称。为了方便,GetCapabilities 也作为Hub代理本身的方法提供。
>>> myhub.GetCapabilities() # doctest: +ELLIPSIS Requested URL: http://some.url?service=WPS&request=GetCapabilities&ResponseForm=xml positional arguments: ('GET',) <incf.dai.response.Response object at ...>
更改日志
0.7 (2011-09-15)
更新通用调用和响应处理,以遵循将DAI的真正服务更改为基于学位服务的更改。[raphael]
0.6 (2010-09-26)
- 初始版本
版本号(0.6)指的是此版本的INCF.DAI服务规范 - 这仍然是一个非常在进度中的工作
项目详情
incf.dai-0.7.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6c1f0c8d5c01ecb8fdae123254a44ddde8d54c04973edcdd4637e7748100d97a |
|
MD5 | 767f78ecddd7a5e96e1d7c380f1881cd |
|
BLAKE2b-256 | d7a635a6557a1ef4f574c33a3ffb22ac247f3094b4b1bbdcd7904c82afc89162 |