用于人脑项目知识图的python api
fairgraph的Python项目详细描述
fairgraph:一个用于人脑项目知识图的python api。
作者:Andrew Davison和Onur Ates,CNRS
CNRS 2019版权所有
fairgraph是一个用于处理元数据的实验性python库 在HBP知识图中,特别关注数据重用, 尽管它在元数据注册/管理中也很有用。 api不稳定,可能会发生更改。
安装
git clone https://github.com/HumanBrainProject/pyxus.git pyxus_src
pip install -r pyxus_src/pyxus/requirements.txt
pip install pyxus_src/pyxus
git clone https://github.com/HumanBrainProject/fairgraph.git
pip install -U ./fairgraph
基本设置
库的基本思想是将知识图中的数据实例表示为Python对象。 与知识图服务的通信是通过客户端对象进行的, 需要与hbp标识帐户关联的访问令牌。
如果您使用的是协作式Jupyter笔记本:
from jupyter_collab_storage import oauth_token_handler
token = oauth_token_handler.get_token()
如果在协作区之外工作,我们建议您从https://nexus-iam.humanbrainproject.org/v0/oauth2/authorize获取令牌 并将其另存为环境变量,例如在shell提示下:
export HBP_token=eyJhbGci...nPq
然后在Python中
token = os.environ['HBP_token']
一旦您有了令牌:
from fairgraph import KGClient
client = KGClient(token)
从知识图中检索元数据
知识图中可用的不同元数据/数据类型被分组到模块中,
目前有commons
、core
、brainsimulation
、electrophysiology
和minds
。
例如:
from fairgraph.commons import BrainRegion
from fairgraph.electrophysiology import PatchedCell
使用这些类,可以列出与特定条件匹配的所有元数据,例如
cells_in_ca1 = PatchedCell.list(client, brain_region=BrainRegion("hippocampus CA1"))
如果知道对象的唯一标识符,可以直接检索:
cell_of_interest = PatchedCell.from_uuid("153ec151-b1ae-417b-96b5-4ce9950a3c56", client)
知识图中元数据之间的链接不会自动跟踪,
为了避免不必要的网络流量,但是可以使用resolve()
方法:
example_cell = cells_in_ca1[3]
experiment = example_cell.experiments.resolve(client)
trace = experiment.traces.resolve(client)
关联的元数据可以作为python对象的属性访问,例如:
print(example_cell.cell_type)
print(example_cell.reversal_potential_cl)
print(trace.time_step)
print(trace.data_unit)
您还可以访问任何相关数据:
import requests
import numpy as np
download_url = trace.data_location['downloadURL']
data = np.genfromtxt(BytesIO(requests.get(download_url).content))
高级查询
虽然某些过滤器和查询是内置的(例如上面的按大脑区域过滤)。 使用Nexus query API
from fairgraph.base import KGQuery
from fairgraph.minds import Dataset
query = {
"path": "minds:specimen_group / minds:subjects / minds:samples / minds:methods / schema:name",
"op": "in",
"value": ["Electrophysiology recording",
"Voltage clamp recording",
"Single electrode recording",
"functional magnetic resonance imaging"]
}
context = {
"schema": "http://schema.org/",
"minds": "https://schema.hbp.eu/minds/"
}
activity_datasets = KGQuery(Dataset, query, context).resolve(client)
for dataset in activity_datasets:
print("* " + dataset.name)
存储和编辑元数据
对于那些拥有在知识图中存储和编辑元数据的必要权限的用户, *fairgraph对象可以在python中创建或编辑,然后保存回知识图,例如:
from fairgraph.core import Person, Organization
from fairgraph.commons import Address
mgm = Organization("Metro-Goldwyn-Mayer")
mgm.save(client)
author = Person("Laurel", "Stan", "laurel@example.com", affiliation=mgm)
author.save(client)
mgm.address = Address(locality='Hollywood', country='United States')
mgm.save(client)
得到帮助
如果对fairgraph有疑问,请发电子邮件给support@humanbrainproject.eu。 如果你发现了一个bug或者想推荐一个增强或者新特性, 请在issue tracker开一张票
致谢
该开源软件代码部分或全部是在人脑项目中开发的,该项目由欧盟的地平线2020研究与创新框架计划根据第720270号和第785907号特定赠款协议(人脑项目SGA1和SGA2)资助。