-
Notifications
You must be signed in to change notification settings - Fork 10
Ontology Service Integration #680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tanmay2028
wants to merge
49
commits into
master
Choose a base branch
from
ontology-service
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
2d8bdf0
Register all superclasses in a list: results.eln.methods. This will h…
Tanmay2028 d2e4d68
Register superclasses
Tanmay2028 7f391a0
Searching classes with iri
Tanmay2028 8e39090
Search superclasses with search_one functionality
Tanmay2028 123d640
test
Tanmay2028 92d9688
Superclasses server to store superclasses
Tanmay2028 9a7198e
Renamed superclasses_server.py as ontoloy_service.py
Tanmay2028 30ee058
Minor changes
Tanmay2028 c79c460
Uses pynxtools nexus definitions
Tanmay2028 e1b399c
Update .gitignore to exclude .owl files
Tanmay2028 cd53432
Added scripts to generate owl file locally using definitions submodule
Tanmay2028 600501a
Resolved conflicts from rebase
Tanmay2028 c763793
Generates owl file w.r.t latest commit hash of definitions submodule
Tanmay2028 7f1ae5c
Update NeXusOntology as submodule and refactor ontology_service
Tanmay2028 5ff3b7b
Remove obselete code block from schema.py
Tanmay2028 e87dd3a
Include NeXusOntology scripts in package and exclude from ruff
Tanmay2028 ac1ef6d
Update NeXusOntology submodule to 99b7516
Tanmay2028 fabea67
Update paths in nxdl.py, .owl and git files
Tanmay2028 d8d3914
Update NeXusOntology submodule and format schema.py
Tanmay2028 4fc4a5c
Removed logging from ontology_service.py
Tanmay2028 b14be7e
Use reasoner and load superclasses from saved inferred ontology
Tanmay2028 cdd15e9
Update NeXusOntology submodule branch name to oscars-project
Tanmay2028 5e0682b
Resolved comments
Tanmay2028 0fc9038
Rebased ontology-service onto master
Tanmay2028 715fee4
Update definitions to that of master and add dependencies
Tanmay2028 8039667
Fixed linting error with ontology_service.py
Tanmay2028 a92631a
linting
Tanmay2028 976e8ce
Update pyproject.toml
Tanmay2028 8744418
Add uvicorn to dependencies
Tanmay2028 633524a
Update submodule and test endpoints for ontology service
Tanmay2028 114d5c2
Added ontology service as APIEntryPoint
Tanmay2028 96d7980
Update definitions and ruff format
Tanmay2028 d98add7
Keep only inferred ontology
Tanmay2028 847a21d
fix rebase in schema.py
a7ad952
remove unneeded import, ignore NeXusOntology in cspell
0e54c10
Add PaNET terms widget
Tanmay2028 73b8394
Add PaNET terms widget
Tanmay2028 e485c06
Add ontology service docs and tests
Tanmay2028 d19b2d7
Fix pytest for ontology service
Tanmay2028 b9b2ac7
Update ontology service how to doc
Tanmay2028 f2f6d70
Update submodule and deactivate caching schema
Tanmay2028 834a0b5
Fix NeXusOntology submodule
Tanmay2028 3dacbd6
Fix pre-commit
Tanmay2028 aa0d390
Update docs
Tanmay2028 cc49716
Update docs with suggested changes
Tanmay2028 52f1d78
Implement suggested changes in docs
Tanmay2028 52cc47e
Import ESRFET and PaNET from nomad.yaml
Tanmay2028 d311cea
select ontology file path based on environment
734cd08
ship inferred ontology
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| [submodule "src/pynxtools/definitions"] | ||
| path = src/pynxtools/definitions | ||
| url = https://github.com/FAIRmat-NFDI/nexus_definitions.git | ||
|
|
||
| [submodule "src/pynxtools/NeXusOntology"] | ||
| path = src/pynxtools/NeXusOntology | ||
lukaspie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| url = https://github.com/FAIRmat-NFDI/NeXusOntology.git | ||
| branch = oscars-project | ||
| fetchRecurseSubmodules = false | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # Using the ontology service in pynxtools | ||
|
|
||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| !!! info "This is a how-to guide for using the ontology service. If you want to learn more about how `ontology service` works in `pynxtools`, please visit the [explanation](../../learn/pynxtools/ontology-service.md) page." | ||
|
|
||
| ## Prerequisites | ||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - If you plan to work on `pynxtools` and the ontology service locally, follow the [development guide](../../tutorial/contributing.md). Ensure the NeXusOntology and definitions submodules [are initialized](../../tutorial/contributing.md#development-installation). | ||
| - Python 3.10+ | ||
| - You will first need to install pynxtools and its dependencies (see [pyproject.toml](https://github.com/FAIRmat-NFDI/pynxtools/blob/master/pyproject.toml)), including owlready2, pygit2, fastapi. For more information about the installation, see [installation guide](../../tutorial/installation.md) | ||
|
|
||
| !!! note "NeXusOntology and definitions submodules" | ||
| === "Using pynxtools (standard installation)" | ||
| If you are simply using `pynxtools` as a package, the NeXusOntology and definitions submodules are included automatically during installation. Follow the [installation guide](../../tutorial/installation.md) to get started. | ||
|
|
||
| === "Developing the ontology service" | ||
| If you are contributing to or developing the ontology service, you need to manually initialize the submodules. See the [development guide](../../tutorial/contributing.md#development-installation) for instructions on setting up the development environment. | ||
|
|
||
| ## Getting started | ||
|
|
||
| ### Importing and using the service | ||
|
|
||
| The main entry point is the FastAPI app defined in [`pynxtools.nomad.apis.ontology_service`](https://github.com/FAIRmat-NFDI/pynxtools/blob/ontology-service/src/pynxtools/nomad/apis/ontology_service.py). You can import and use the service as follows: | ||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```python | ||
| from pynxtools.nomad.apis.ontology_service import app, load_ontology, fetch_superclasses | ||
| ``` | ||
|
|
||
| ## Configuring ontology imports | ||
|
|
||
| The ontologies used by the ontology service in `pynxtools` are configurable via the [`nomad.yaml`](https://nomad-lab.eu/prod/v1/docs/howto/develop/setup.html#nomadyaml) configuration file. This allows you to control exactly which ontologies are loaded and reasoned over. | ||
|
|
||
| To specify which ontologies to import, add their URLs under the `plugins.options.pynxtools.nomad.apis:ontology_service.imports` section in your `nomad.yaml`: | ||
|
|
||
| ```yaml | ||
| plugins: | ||
| options: | ||
| pynxtools.nomad.apis:ontology_service: | ||
| imports: | ||
| - "https://raw.githubusercontent.com/pan-ontologies/esrf-ontologies/refs/heads/oscars-deliverable-2/ontologies/esrfet/ESRFET.owl" | ||
| - "http://purl.org/pan-science/PaNET/PaNET.owl" | ||
| ``` | ||
|
|
||
| By default, the [ESRFET](https://github.com/pan-ontologies/esrf-ontologies) and [PaNET](https://bioportal.bioontology.org/ontologies/PANET) ontology are imported, like in the example. | ||
|
|
||
| **Important:** | ||
| Even if an ontology (e.g., [ESRFET](https://github.com/pan-ontologies/esrf-ontologies)) references another ontology (e.g., [PaNET](https://bioportal.bioontology.org/ontologies/PANET)) via `owl:imports`, you must still list both URLs explicitly in the imports. The ontology service only loads the ontologies specified in the configuration and does not automatically follow `owl:imports` statements. | ||
|
|
||
| For details on using `pynxtools` as a NOMAD plugin, refer to the [development guide](../pynxtools/../../tutorial/contributing.md#developing-pynxtools-as-a-nomad-plugin). | ||
|
|
||
| ### Minimal working example | ||
|
|
||
| You can extract superclasses for a NeXus class using the ontology service's HTTP API endpoint. Here's an example using Python's `requests` library: | ||
|
|
||
| ```python | ||
| import requests | ||
|
|
||
| # Replace with the actual running service URL | ||
| base_url = "http://localhost:8000/nomad-oasis/" | ||
| class_name = "NXmpes_arpes" | ||
| response = requests.get(f"{base_url}/superclasses/{class_name}") | ||
| if response.status_code == 200: | ||
| superclasses = response.json().get("superclasses", []) | ||
| print(superclasses) | ||
| else: | ||
| print(f"Error: {response.status_code} - {response.text}") | ||
| ``` | ||
|
|
||
| This endpoint returns a JSON object with the list of superclasses for the given NeXus class name, as used internally in [pynxtools](https://github.com/FAIRmat-NFDI/pynxtools/blob/ontology-service/src/pynxtools/nomad/schema.py). | ||
|
|
||
| ## How it works in NOMAD | ||
|
|
||
| When you upload a NeXus file to NOMAD, the ontology service is automatically triggered during data processing. Here's what happens: | ||
|
|
||
| 1. **Triggering**: During normalization, NOMAD reads the `definition__field` from NeXus entry (e.g., `NXmpes_arpes`). | ||
|
|
||
| { width="800" } | ||
| { width="800" } | ||
|
|
||
| 2. **Querying**: The service loads the ontology (or generates it if not already present) and retrieves all superclasses for that application definition. | ||
|
|
||
| 3. **Storing results**: The retrieved superclasses are stored in `results.eln.methods` in the NOMAD archive. | ||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { width="800" } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Understanding the ontology service in pynxtools | ||
|
|
||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| !!! info "This is a learn guide for using the ontology service. If you want to learn more about how to use `ontology service` in `pynxtools`, please visit the [how-to guide](../../how-tos/pynxtools/ontology-service.md)." | ||
|
|
||
| ## Introduction | ||
|
|
||
| The ontology service in `pynxtools` provides a [FastAPI](https://fastapi.tiangolo.com/)-based app for querying the [NeXusOntology](https://github.com/nexusformat/NeXusOntology). This service enables users to retrieve semantic contextualization—such as superclasses for NeXus application definitions—and makes datasets of different experimental techniques findable in NOMAD. | ||
|
|
||
| The NeXus ontology integrates two key experimental technique ontologies: | ||
lukaspie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - **[PaN Experimental Technique Ontology (PaNET)](https://bioportal.bioontology.org/ontologies/PANET)**: Photon and Neutron Experimental Techniques ontology (PaNET) provides a standardized vocabulary for describing experimental techniques used at photon and neutron research facilities, enabling consistent categorization and discovery of scientific data across different institutions. | ||
|
|
||
| - **[ESRF Experimental Technique Ontology (ESRFET)](https://github.com/pan-ontologies/esrf-ontologies)**: An ontology developed by the European Synchrotron Radiation Facility (ESRF) to classify and describe experimental techniques specific to synchrotron radiation science. It complements PaNET by providing more granular terms for synchrotron-based methods. | ||
|
|
||
| By using these ontologies, the service maps terms that are defined in NeXus application definitions to standardized terms for experimental techniques, improving data interoperability and enabling semantic search capabilities within NOMAD. | ||
|
|
||
| ## How it fits in pynxtools | ||
|
|
||
| The `ontology service` only operates when `pynxtools` is run within `NOMAD`. It is registered as an [`APIEntryPoint`](https://nomad-lab.eu/prod/v1/docs/reference/config.html#apientrypoint), which allows NOMAD to automatically discover and mount the FastAPI routes at startup. The service exposes the [NeXusOntology](https://github.com/nexusformat/NeXusOntology) to users and connected systems. | ||
|
|
||
| ## Core concepts | ||
|
|
||
| - **Ontology**: A formal representation of concepts (classes), relationships, and properties, typically serialized as [OWL](https://www.w3.org/OWL/) files. | ||
| - **Name of an application definition**: A symbol describing an experimental technique in NeXus (e.g., `NXmpes_arpes`). | ||
| - **Relation**: A connection between entities, such as a superclass/subclass relationship. | ||
| - **Inferred ontology**: An ontology file that has been processed by a reasoner to infer additional relationships and properties between classes, beyond those explicitly defined in the original ontology. | ||
|
|
||
| Ontologies are represented as OWL files (e.g., `NeXusOntology_full_<commit>_inferred.owl`) and loaded using `owlready2`. | ||
|
|
||
| ## Architecture & design | ||
|
|
||
| - **Main modules**: | ||
| - `pynxtools.nomad.apis.ontology_service`: FastAPI app, core logic for loading and querying ontologies. | ||
| - `pynxtools.NeXusOntology.script.generate_ontology`: Generates ontology files from NeXus definitions. | ||
| - `pynxtools.nomad.schema`: Initializes [NOMAD Metainfo](https://nomad-lab.eu/prod/v1/docs/explanation/data.html#schema) and schema integration.. | ||
| - **Key classes/functions**: | ||
| - `load_ontology()`: Loads the inferred ontology file. | ||
| - `fetch_superclasses(ontology, class_name)`: Retrieves superclasses for a given NeXus class. | ||
| - `ensure_ontology_file()`: Ensures an ontology file is present and up-to-date. | ||
| - **Data flow**: | ||
| - On startup, the service verifies whether the inferred ontology file exists; if absent, it runs the reasoner and generates the inferred ontology file. | ||
| - Inferred ontology is loaded via `owlready2`. | ||
| - API endpoints query this inferred ontology for relationships and metadata. | ||
|
|
||
| ## Extensibility points | ||
|
|
||
| - Extend FastAPI routes in [`ontology_service.py`](https://github.com/FAIRmat-NFDI/pynxtools/blob/ontology-service/src/pynxtools/nomad/apis/ontology_service.py) for new queries. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```python | ||
| import requests | ||
|
|
||
| # Replace with the actual running service URL | ||
| base_url = "http://localhost:8000/nomad-oasis/" | ||
| class_name = "NXmpes_arpes" | ||
| response = requests.get(f"{base_url}/superclasses/{class_name}") | ||
| if response.status_code == 200: | ||
| superclasses = response.json().get("superclasses", []) | ||
| print(superclasses) | ||
| else: | ||
| print(f"Error: {response.status_code} - {response.text}") | ||
| ``` | ||
|
|
||
| ## Glossary | ||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - **NeXus**: A common data format for neutron, X-ray, and muon science. | ||
| - **OWL**: Web Ontology Language, used for representing ontologies. | ||
| - **Ontology**: Structured representation of concepts and relationships. | ||
| - **Superclass**: A parent class in the ontology hierarchy. | ||
| - **Reasoner**: Tool for inferring new relationships in an ontology. | ||
| - **NOMAD**: The FAIRmat NOMAD project for materials data. | ||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.