!pip install nexusforge==0.7.0
!pip install allensdk
!pip install neurom[plotly]==3.0.1
!pip install --upgrade nest-asyncio==1.5.4
!pip install --upgrade jinja2
The Nexus sandbox application can be used to get a token:
Step 1: From the web page, click on the login button in the top right corner and follow the instructions on screen.
Step 2: Once logged in, click on the button on the top right that displays your GitHub username. From the dropdown select Copy token
option. This will copy the token to your clipboard.
Once a token is obtained, proceed to paste it as the value of the TOKEN
variable below.
Important: A Nexus token is valid for 8 hours, if your working session is open for more than 8 hours, you may need to refresh the value of the token and reintialize the forge client in the 'Configure a forge client to store, manage and access datasets' section below.
import getpass
TOKEN = getpass.getpass()
import uuid
import base64
import requests
import json
from pathlib import Path
from kgforge.core import KnowledgeGraphForge
from kgforge.specializations.mappings import DictionaryMapping
from allensdk.api.queries.cell_types_api import CellTypesApi
from allensdk.core.cell_types_cache import CellTypesCache
r = requests.get('https://raw.githubusercontent.com/BlueBrain/nexus/d9f6cb83a27149c29bc604f3c34ea00c9ad64e67/docs/src/main/paradox/docs/getting-started/notebooks/rdfmodel/jsonldcontext.json')
dirpath = './rdfmodel'
Path(dirpath).mkdir(parents=True, exist_ok=True)
with open(f'{dirpath}/jsonldcontext.json', 'w') as outfile:
json.dump(r.json(), outfile)
ORG = "github-users"
PROJECT = "" # Provide here the automatically created project name created when you logged into the Nexus sandbox instance.
forge = KnowledgeGraphForge("https://raw.githubusercontent.com/BlueBrain/nexus/d9f6cb83a27149c29bc604f3c34ea00c9ad64e67/docs/src/main/paradox/docs/getting-started/notebooks/forge.yml",
bucket=f"{ORG}/{PROJECT}",
endpoint="https://sandbox.bluebrainnexus.io/v1",
token=TOKEN)
We will be downloading mouse neuron morphology data from the Allen Cell Types Database. The AllenSDK can be used for data download.
ALLEN_DIR = "allen_cell_types_database"
ctc = CellTypesCache(manifest_file=f"{ALLEN_DIR}/manifest.json")
MAX_CELLS = 1
SPECIES = CellTypesApi.MOUSE
nm_allen_identifiers = [cell["id"] for cell in ctc.get_cells(species=[SPECIES], require_reconstruction = True)][:MAX_CELLS]
print(f"Selected a mouse neuron with identifier: {nm_allen_identifiers}")
with open(f"{ALLEN_DIR}/cells.json") as f:
allen_cell_types_metadata = json.load(f)
nm_allen_metadata = [neuron for neuron in allen_cell_types_metadata if neuron["specimen__id"] in nm_allen_identifiers]
print(f"Metadata of the neuron {nm_allen_identifiers}:")
nm_allen_metadata
We will be downloading one mouse neuron morphology from the Allen Cell Types Database using the AllenSDK.
for identifier in nm_allen_identifiers:
ctc.get_reconstruction(identifier)
We will be downloading one mouse neuron electrophysiology from the Allen Cell Types Database using the AllenSDK.
for identifier in nm_allen_identifiers:
ctc.get_ephys_data(identifier)
allen_nm_mapping = DictionaryMapping.load("https://raw.githubusercontent.com/BlueBrain/nexus/d9f6cb83a27149c29bc604f3c34ea00c9ad64e67/docs/src/main/paradox/docs/getting-started/notebooks/mappings/allen_morphology_dataset.hjson")
nm_allen_resources = forge.map(nm_allen_metadata, allen_nm_mapping, na='')
allen_ephys_mapping = DictionaryMapping.load("https://raw.githubusercontent.com/BlueBrain/nexus/d9f6cb83a27149c29bc604f3c34ea00c9ad64e67/docs/src/main/paradox/docs/getting-started/notebooks/mappings/allen_ephys_dataset.hjson")
nephys_allen_resources = forge.map(nm_allen_metadata, allen_ephys_mapping, na='')
If the registration fails, try refreshing the access token and reinitializing the forge client in the 'Configure a forge client to store, manage and access datasets' section.
nm_allen_resources.id = forge.format("identifier", "neuronmorphologies", str(uuid.uuid4()))
forge.register(nm_allen_resources)
nephys_allen_resources.id = forge.format("identifier", "traces", str(uuid.uuid4()))
forge.register(nephys_allen_resources)
_type = "NeuronMorphology"
filters = {"type": _type}
number_of_results = 10 # You can limit the number of results, pass `None` to fetch all the results
data = forge.search(filters, limit=number_of_results)
print(f"{str(len(data))} dataset(s) of type {_type} found")
property_to_display = ["id","name","subject","brainLocation.brainRegion.id","brainLocation.brainRegion.label","brainLocation.layer.id","brainLocation.layer.label", "contribution","brainLocation.layer.id","brainLocation.layer.label","distribution.name","distribution.contentUrl","distribution.encodingFormat"]
reshaped_data = forge.reshape(data, keep=property_to_display)
forge.as_dataframe(reshaped_data)
dirpath = "./downloaded/"
forge.download(data, "distribution.contentUrl", dirpath, overwrite=True)
ls ./downloaded/
from neurom import load_morphology
from neurom.view.plotly_impl import plot_morph3d
import IPython
neuron = load_morphology(f"{dirpath}/{data[0].distribution.name}")
plot_morph3d(neuron, inline=False)
IPython.display.HTML(filename='./morphology-3D.html')
Tagging a dataset is equivalent to git tag
. It allows to version a dataset.
forge.tag(data, value="releaseV112")
# The version argument can be specified to retrieve the dataset at a given tag.
tagged_data = forge.retrieve(id=data[0].id, version="releaseV112")
forge.as_dataframe(tagged_data)
data[0].description="Neuron Morphology from Allen"
forge.update(data[0])
non_tagged_data = forge.retrieve(id=data[0].id)
forge.as_dataframe(non_tagged_data)