-
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
base: master
Are you sure you want to change the base?
Changes from 45 commits
2d8bdf0
d2e4d68
7f391a0
8e39090
123d640
92d9688
9a7198e
30ee058
c79c460
e1b399c
cd53432
600501a
c763793
7f1ae5c
5ff3b7b
e87dd3a
ac1ef6d
fabea67
d8d3914
4fc4a5c
b14be7e
cdd15e9
5e0682b
0fc9038
715fee4
8039667
a92631a
976e8ce
8744418
633524a
114d5c2
96d7980
d98add7
847a21d
a7ad952
0e54c10
73b8394
e485c06
d19b2d7
b9b2ac7
f2f6d70
834a0b5
3dacbd6
aa0d390
cc49716
52f1d78
52cc47e
d311cea
734cd08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # 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)." | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Prerequisites | ||
Tanmay2028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - 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, uvicorn. For more information about the installation, see [installation guide](../../tutorial/installation.md) | ||
| - 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). | ||
|
|
||
| !!! 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 | ||
| ``` | ||
|
|
||
| ### 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) | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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" } | ||
| 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 [explanation](../../how-tos/pynxtools/ontology-service.md)." | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Introduction | ||
|
|
||
| The ontology service in `pynxtools` provides a FastAPI-based app for querying the [NeXus ontology](https://github.com/nexusformat/NeXusOntology). This service enables users to retrieve ontological information—such as superclasses for NeXus application definitions—and makes datasets of different experimental techniques findable in NOMAD. | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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)**: 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. | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| By using these ontologies, the service maps NeXus application definitions to standardized experimental technique terms, improving data interoperability and enabling semantic search capabilities within NOMAD. | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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 [NeXus ontology](https://github.com/nexusformat/NeXusOntology) to users and connected systems. | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Core concepts | ||
|
|
||
| - **Ontology**: A formal representation of concepts (classes), relationships, and properties, typically serialized as [OWL](https://www.w3.org/OWL/) files. | ||
| - **Application definition**: A class or term describing an experimental technique in the NeXus (e.g., `NXmpes_arpes`). | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **Relation**: Connections between entities, such as superclass/subclass relationships. | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **Inferred ontology**: An ontology file that has been processed by a reasoner to infer additional relationships and properties between classes, beyond those explicitly defined. | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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 metainfo and schema integration. | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **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 ontology file is present and up-to-date. | ||
Tanmay2028 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **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
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from nomad.config.models.plugins import APIEntryPoint | ||
|
|
||
|
|
||
| class MyAPIEntryPoint(APIEntryPoint): | ||
| def load(self): | ||
| from pynxtools.nomad.apis.ontology_service import app | ||
|
|
||
| return app | ||
|
|
||
|
|
||
| ontology_service = MyAPIEntryPoint( | ||
| name="ontology_service", | ||
| description="A service to provide ontological information for a given NeXus class.", | ||
| prefix="/ontology_service", | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.