Skip to main content

Python API for the EBRAINS Knowledge Graph

Project description

fairgraph: a Python API for the EBRAINS Knowledge Graph.

Authors: Andrew P. Davison, Onur Ates, Nico Feld, Yann Zerlaut, Glynis Mattheisen, Peyman Najafi

Copyright CNRS 2019-2024

fairgraph is a Python library for working with metadata in the EBRAINS Knowledge Graph, with a particular focus on data reuse, although it is also useful in metadata registration/curation. The API is not stable, and is subject to change.

Installation

To get the latest release:

pip install fairgraph

To get the development version:

git clone https://github.com/HumanBrainProject/fairgraph.git
pip install -U ./fairgraph

Knowledge Graph versions

This version of fairgraph supports version 3 of the EBRAINS Knowledge Graph (KG).

Basic setup

The basic idea of the library is to represent metadata nodes from the Knowledge Graph as Python objects. Communication with the Knowledge Graph service is through a client object, for which an access token associated with an EBRAINS account is needed.

If you are working in a Collaboratory Jupyter notebook, the client will find your token automatically.

If working outside the Collaboratory, we recommend you obtain a token from whichever authentication endpoint is available to you, and save it as an environment variable so the client can find it, e.g. at a shell prompt:

export KG_AUTH_TOKEN=eyJhbGci...nPq

You can then create the client object:

>>> from fairgraph import KGClient

>>> client = KGClient(host="core.kg.ebrains.eu")

You can also pass the token explicitly to the client:

>>> client = KGClient(token)

Retrieving metadata from the Knowledge Graph

The Knowledge Graph uses openMINDS schemas. Each openMINDS schema corresponds to a Python class, which are grouped into modules following the openMINDS structure. For example:

>>> from fairgraph.openminds.core import DatasetVersion
>>> from fairgraph.openminds.controlled_terms import Technique

The following openMINDS modules are currently available: core, controlled_terms, sands, computation, chemicals, specimen_prep, ephys, publications, stimulation. Using these classes, it is possible to list all metadata matching a particular criterion, e.g.

>>> patch_techniques = Technique.list(client, name="patch clamp")
>>> print([technique.name for technique in patch_techniques])
['cell attached patch clamp', 'multiple whole cell patch clamp', 'patch clamp', 'patch clamp technique', 'whole cell patch clamp']
>>> whole_cell_patch = patch_techniques[4]
>>> datasets = DatasetVersion.list(client, techniques=whole_cell_patch, scope="in progress")

For research products that are versioned, such as datasets, models, and software, certain attributes may be inherited from the parent (e.g. a DatasetVersion generally inherits its name from a Dataset). In this case, we have a convenience method to retrieve the parent's name:

>>> print(datasets[0].get_name(client, scope="in progress"))
'Cholinergic interneurons in the striatum - Single cell patch clamp recordings'

If you know the unique identifier of an object, you can retrieve it directly:

>>> dataset = DatasetVersion.from_id("17196b79-04db-4ea4-bb69-d20aab6f1d62", client, scope="in progress")

Links between metadata in the Knowledge Graph are not followed automatically, to avoid unnecessary network traffic, but can be followed with the resolve() method:

>>> license = dataset.license.resolve(client, scope="in progress")
>>> authors = [author.resolve(client, scope="in progress") for author in dataset.authors]

The associated metadata is accessible as attributes of the Python objects, e.g.:

>>> print(dataset.version_innovation)
This is the first version of this research product.

To print out all the metadata for a given object, use the show() method:

>>> print(license.show())
id          https://kg.ebrains.eu/api/instances/6ebce971-7f99-4fbc-9621-eeae47a70d85
name        Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
legal_code  https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
alias       CC BY-NC-SA 4.0
webpages    ['https://creativecommons.org/licenses/by-nc-sa/4.0', 'https://spdx.org/licenses/CC-BY-NC-SA-4.0.html']

You can also access any associated data:

>>> dataset.download(client, local_directory=dataset.alias)

Storing and editing metadata

For those users who have the necessary permissions to store and edit metadata in the Knowledge Graph, fairgraph objects can be created or edited in Python, and then saved back to the Knowledge Graph, e.g.:

from datetime import datetime
from fairgraph.openminds.core import Person, Organization, Affiliation

mgm = Organization(name="Metro-Goldwyn-Mayer", alias="MGM")
mgm.save(client, space="myspace")

affiliation = Affiliation(organization=mgm, start_date=datetime(1942, 1, 1))
author = Person(family_name="Laurel", given_name="Stan", affiliations=affiliation)
author.save(client, space="myspace")

Getting help

In case of questions about fairgraph, please contact us via https://ebrains.eu/support/. If you find a bug or would like to suggest an enhancement or new feature, please open a ticket in the issue tracker.

Acknowledgements

EU Logo

This open source software code was developed in part or in whole in the Human Brain Project, funded from the European Union's Horizon 2020 Framework Programme for Research and Innovation under Specific Grant Agreements No. 720270, No. 785907 and No. 945539 (Human Brain Project SGA1, SGA2 and SGA3) and in the EBRAINS research infrastructure, funded from the European Union's Horizon Europe funding programme under grant agreement No. 101147319 (EBRAINS-2.0).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fairgraph-0.12.2.tar.gz (155.7 kB view details)

Uploaded Source

Built Distribution

fairgraph-0.12.2-py3-none-any.whl (401.1 kB view details)

Uploaded Python 3

File details

Details for the file fairgraph-0.12.2.tar.gz.

File metadata

  • Download URL: fairgraph-0.12.2.tar.gz
  • Upload date:
  • Size: 155.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for fairgraph-0.12.2.tar.gz
Algorithm Hash digest
SHA256 e6e25b4a8b801d035f1ee9324bfdea606c79ae9976337c8562fe674012a900ec
MD5 52f4554a2b4820b6bcd143051b4e440b
BLAKE2b-256 891dc8a7dafd576dfa2d58b19a842f40cc7a5c84118bba6bc9c433826d0b5f99

See more details on using hashes here.

File details

Details for the file fairgraph-0.12.2-py3-none-any.whl.

File metadata

  • Download URL: fairgraph-0.12.2-py3-none-any.whl
  • Upload date:
  • Size: 401.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for fairgraph-0.12.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0802dc7e8b4616c1bb33d719ce64d8f783e65b3d36e6ad3e24fe684e6f7f1db4
MD5 6665eeda2c8af77d7aba15c8790c6674
BLAKE2b-256 1851cd61406d9c06d4ec66bdaaf84dd7149873e40e9289adb35ebcde8dc3c747

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page