Skip to content

Commit

Permalink
Merge pull request #152 from BlueBrain/fix-endpoint
Browse files Browse the repository at this point in the history
do not have endpoint hard-coded for ontology stuff
  • Loading branch information
AurelienJaquier authored Jul 3, 2024
2 parents 8e16274 + a6937ee commit 69dc611
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
50 changes: 35 additions & 15 deletions bluepyemodel/access_point/forge_access_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,14 +895,16 @@ def filter_resources(self, resources, filter):
)


def ontology_forge_access_point(access_token=None, forge_path=None):
def ontology_forge_access_point(
access_token=None, forge_path=None, endpoint="https://bbp.epfl.ch/nexus/v1"
):
"""Returns an access point targeting the project containing the ontology for the
species and brain regions"""

access_point = NexusForgeAccessPoint(
project="datamodels",
organisation="neurosciencegraph",
endpoint="https://bbp.epfl.ch/nexus/v1",
endpoint=endpoint,
forge_path=forge_path,
access_token=access_token,
)
Expand Down Expand Up @@ -955,21 +957,29 @@ def raise_not_found_exception(base_text, label, access_point, filter, limit=30):
raise AccessPointException(base_text)


def check_resource(label, category, access_point=None, access_token=None, forge_path=None):
def check_resource(
label,
category,
access_point=None,
access_token=None,
forge_path=None,
endpoint="https://bbp.epfl.ch/nexus/v1",
):
"""Checks that resource is present on nexus and is part of the provided category
Arguments:
label (str): name of the resource to search for
category (str): can be "etype", "mtype" or "ttype"
access_point (str): ontology_forge_access_point(access_token)
forge_path (str): path to a .yml used as configuration by nexus-forge.
endpoint (str): nexus endpoint
"""
allowed_categories = ["etype", "mtype", "ttype"]
if category not in allowed_categories:
raise AccessPointException(f"Category is {category}, but should be in {allowed_categories}")

if access_point is None:
access_point = ontology_forge_access_point(access_token, forge_path)
access_point = ontology_forge_access_point(access_token, forge_path, endpoint)

resource = access_point.resolve(label, strategy="exact")
# raise Exception if resource was not found
Expand Down Expand Up @@ -1017,7 +1027,9 @@ def get_available_traces(species=None, brain_region=None, access_token=None, for
return resources


def get_brain_region(brain_region, access_token=None, forge_path=None):
def get_brain_region(
brain_region, access_token=None, forge_path=None, endpoint="https://bbp.epfl.ch/nexus/v1"
):
"""Returns the resource corresponding to the brain region
If the brain region name is not present in nexus,
Expand All @@ -1027,10 +1039,11 @@ def get_brain_region(brain_region, access_token=None, forge_path=None):
brain_region (str): name of the brain region to search for
access_token (str): nexus connection token
forge_path (str): path to a .yml used as configuration by nexus-forge.
endpoint (str): nexus endpoint
"""

filter = "brain_region"
access_point = ontology_forge_access_point(access_token, forge_path)
access_point = ontology_forge_access_point(access_token, forge_path, endpoint)

if brain_region in ["SSCX", "sscx"]:
brain_region = "somatosensory areas"
Expand Down Expand Up @@ -1062,20 +1075,23 @@ def get_brain_region(brain_region, access_token=None, forge_path=None):
return resource


def get_brain_region_dict(brain_region, access_token=None, forge_path=None):
def get_brain_region_dict(
brain_region, access_token=None, forge_path=None, endpoint="https://bbp.epfl.ch/nexus/v1"
):
"""Returns a dict with id and label of the resource corresponding to the brain region
Arguments:
brain_region (str): name of the brain region to search for
access_token (str): nexus connection token
forge_path (str): path to a .yml used as configuration by nexus-forge.
endpoint (str): nexus endpoint
Returns:
dict: the id and label of the nexus resource of the brain region
"""
br_resource = get_brain_region(brain_region, access_token, forge_path)
br_resource = get_brain_region(brain_region, access_token, forge_path, endpoint)

access_point = ontology_forge_access_point(access_token, forge_path)
access_point = ontology_forge_access_point(access_token, forge_path, endpoint)

# if no exception was raised, filter to get id and label and return them
brain_region_dict = access_point.forge.as_json(br_resource)
Expand All @@ -1085,25 +1101,29 @@ def get_brain_region_dict(brain_region, access_token=None, forge_path=None):
}


def get_brain_region_notation(brain_region, access_token=None, forge_path=None):
def get_brain_region_notation(
brain_region, access_token=None, forge_path=None, endpoint="https://bbp.epfl.ch/nexus/v1"
):
"""Get the ontology of the brain location."""
if brain_region is None:
return None

brain_region_resource = get_brain_region(
brain_region, access_token=access_token, forge_path=forge_path
brain_region, access_token=access_token, forge_path=forge_path, endpoint=endpoint
)

return brain_region_resource.notation


def get_nexus_brain_region(brain_region, access_token=None, forge_path=None):
def get_nexus_brain_region(
brain_region, access_token=None, forge_path=None, endpoint="https://bbp.epfl.ch/nexus/v1"
):
"""Get the ontology of the brain location."""
if brain_region is None:
return None

brain_region_from_nexus = get_brain_region_dict(
brain_region, access_token=access_token, forge_path=forge_path
brain_region, access_token=access_token, forge_path=forge_path, endpoint=endpoint
)

return {
Expand All @@ -1112,8 +1132,8 @@ def get_nexus_brain_region(brain_region, access_token=None, forge_path=None):
}


def get_all_species(access_token=None, forge_path=None):
access_point = ontology_forge_access_point(access_token, forge_path)
def get_all_species(access_token=None, forge_path=None, endpoint="https://bbp.epfl.ch/nexus/v1"):
access_point = ontology_forge_access_point(access_token, forge_path, endpoint)

resources = access_point.forge.search({"subClassOf": "nsg:Species"}, limit=100)

Expand Down
11 changes: 8 additions & 3 deletions bluepyemodel/access_point/nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def __init__(
self.emodel_metadata.brain_region,
self.access_point.access_token,
self.forge_ontology_path,
self.access_point.endpoint,
)

self.pipeline_settings = self.get_pipeline_settings(strict=False)
Expand All @@ -145,7 +146,7 @@ def download_directory(self):
def check_mettypes(self):
"""Check that etype, mtype and ttype are present on nexus"""
ontology_access_point = ontology_forge_access_point(
self.access_point.access_token, self.forge_ontology_path
self.access_point.access_token, self.forge_ontology_path, self.access_point.endpoint
)

logger.info("Checking if etype %s is present on nexus...", self.emodel_metadata.etype)
Expand All @@ -155,6 +156,7 @@ def check_mettypes(self):
access_point=ontology_access_point,
access_token=self.access_point.access_token,
forge_path=self.forge_ontology_path,
endpoint=self.access_point.endpoint,
)
logger.info("Etype checked")

Expand All @@ -169,6 +171,7 @@ def check_mettypes(self):
access_point=ontology_access_point,
access_token=self.access_point.access_token,
forge_path=self.forge_ontology_path,
endpoint=self.access_point.endpoint,
)
logger.info("Mtype checked")
else:
Expand All @@ -185,6 +188,7 @@ def check_mettypes(self):
access_point=ontology_access_point,
access_token=self.access_point.access_token,
forge_path=self.forge_ontology_path,
endpoint=self.access_point.endpoint,
)
logger.info("Ttype checked")
else:
Expand Down Expand Up @@ -228,6 +232,7 @@ def build_ontology_based_metadata(self):
self.emodel_metadata.brain_region,
self.access_point.access_token,
self.forge_ontology_path,
self.access_point.endpoint,
)

def get_nexus_subject(self, species):
Expand Down Expand Up @@ -573,7 +578,7 @@ def store_model_configuration(self, configuration, path=None):

# set id to mechanisms by filtering with brain region, temperature, species, ljp correction
ontology_access_point = ontology_forge_access_point(
self.access_point.access_token, self.forge_ontology_path
self.access_point.access_token, self.forge_ontology_path, self.access_point.endpoint
)
for mechanism in configuration.mechanisms:
if mechanism.name in NEURON_BUILTIN_MECHANISMS:
Expand Down Expand Up @@ -891,7 +896,7 @@ def download_mechanisms(self, mechanisms, any_downloaded=False):

mechanisms_directory = self.get_mechanisms_directory()
ontology_access_point = ontology_forge_access_point(
self.access_point.access_token, self.forge_ontology_path
self.access_point.access_token, self.forge_ontology_path, self.access_point.endpoint
)

for mechanism in mechanisms:
Expand Down
6 changes: 4 additions & 2 deletions tests/unit_tests/test_nexus_access_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,17 @@ def test_check_mettypes(mock_nexus_access_point, mock_available_etypes, mock_ava

mock_ontology_forge.assert_called_once_with(
mock_nexus_access_point.access_point.access_token,
mock_nexus_access_point.forge_ontology_path
mock_nexus_access_point.forge_ontology_path,
mock_nexus_access_point.access_point.endpoint,
)

mock_check_resource.assert_any_call(
"cAC",
"etype",
access_point=mock_ontology_forge.return_value,
access_token=mock_nexus_access_point.access_point.access_token,
forge_path=mock_nexus_access_point.forge_ontology_path
forge_path=mock_nexus_access_point.forge_ontology_path,
endpoint=mock_nexus_access_point.access_point.endpoint,
)


Expand Down

0 comments on commit 69dc611

Please sign in to comment.