Skip to content

Conversation

@sanbrock
Copy link
Contributor

@sanbrock sanbrock commented Dec 9, 2025

No description provided.

# This script generates the NeXus ontology in OWL format.
python -m script.generate_ontology
python -m script.generate_ontology full
python -m script.generate_ontology full testdata
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my understanding, what is this testdata ontology?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testdata includes a few individuals showing how NeXus Ontology can be turned into a knowledge graph by adding experiment data. The example includes dataset000 with the Field NXiv_temp/ENTRY/defnition and a few more.

import owlready2
from .NeXusOntology import NeXusOntology
import pygit2
import os
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We made some changes in #7 for the ontology generation that differ from what is used here? Would it be possible to use the version that is used there or bring in the changed from there in addition?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example is that we have an additional argument store_commit_filename which allows us to store the commit has within the filename of the ontology. We are using this to cache the ontology for a specific state of the definitions.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the full diff between the two branches (with + indicating the version from @Tanmay2028):

diff --git a/script/generate_ontology.py b/script/generate_ontology.py
index 49974a7..d154407 100644
--- a/script/generate_ontology.py
+++ b/script/generate_ontology.py
@@ -4,8 +4,8 @@ import pygit2
 import os
 import sys
 
-def main(full=False, testdata=False, nexus_def_path=None, def_commit=None):
-    print(f"Debug: Generating ontology with full={full} and testdata={testdata}")
+def main(full=False, nexus_def_path=None, def_commit=None, store_commit_filename = False):
+    print(f"Debug: Generating ontology with full={full}")
     local_dir = os.path.abspath(os.path.dirname(__file__))
     os.environ['NEXUS_DEF_PATH'] = nexus_def_path
 
@@ -13,23 +13,25 @@ def main(full=False, testdata=False, nexus_def_path=None, def_commit=None):
     web_page_base_prefix = 'https://manual.nexusformat.org/'
 
     detailed_iri = 'http://purl.org/nexusformat/v2.0/definitions/' + def_commit + '/'
-    base_iri = 'http://purl.org/nexusformat/definitions'
-    onto = owlready2.get_ontology(base_iri)
+    base_iri = 'http://purl.org/nexusformat/definitions/'
+    onto = owlready2.get_ontology(base_iri + "NeXusOntology")
+
+    esrfet_iri = "https://raw.githubusercontent.com/pan-ontologies/esrf-ontologies/refs/heads/oscars-deliverable-2/ontologies/esrfet/ESRFET.owl"
+    onto.imported_ontologies.append(owlready2.get_ontology(esrfet_iri))
 
     nexus_ontology = NeXusOntology(onto, base_iri, web_page_base_prefix, def_commit, full)
     nexus_ontology.gen_classes()
     nexus_ontology.gen_children()
     if full:
-        if testdata:
-            nexus_ontology.gen_datasets()
-            fullsuffix = '_full_testdata'
-        else:
-            fullsuffix = '_full'
+        nexus_ontology.gen_datasets()
+        fullsuffix = '_full'
     else:
         fullsuffix = ''
     
     # Include the commit hash in the output file name
-    output_file_name = f"NeXusOntology{fullsuffix}_{def_commit}.owl"
+
+    def_commit_text = f"_{def_commit}" if store_commit_filename else "" 
+    output_file_name = f"NeXusOntology{fullsuffix}{def_commit_text}.owl"
     output_path = os.path.join(local_dir, f"..{os.sep}ontology{os.sep}{output_file_name}")
     onto.save(file=output_path, format="rdfxml")
 
@@ -38,23 +40,19 @@ if __name__ == "__main__":
     local_dir = os.path.abspath(os.path.dirname(__file__))
     one_up = os.path.join(local_dir, "..", "definitions")
     two_up = os.path.join(local_dir, "..", "..", "definitions")
-    if os.path.isdir(one_up):
-        nexus_def_path = one_up
-    elif os.path.isdir(two_up):
+    if os.path.isdir(two_up):
         nexus_def_path = two_up
+    elif os.path.isdir(one_up):
+        nexus_def_path = one_up
     else:
         raise FileNotFoundError("definitions folder not found one or two directories up from script location.")
-    commit_arg=1
-    full = len(sys.argv) > 1 and sys.argv[1] == 'full'
-    if full:
-        commit_arg = 2
-    testdata = full and len(sys.argv) > 2 and sys.argv[2] == 'testdata'
-    if testdata:
-        commit_arg = 3
+    full = True if "full" in sys.argv else False
+    store_commit_filename = True if "store_commit_filename" in sys.argv else False
     repo = pygit2.Repository(nexus_def_path)
     # Check for provided commit hash argument
-    if len(sys.argv) > commit_arg:
-        commit_hash = sys.argv[commit_arg]
+    extra_args = [arg for arg in sys.argv[1:] if arg not in ("full", "store_commit_filename")]
+    if extra_args:
+        commit_hash = extra_args[-1]
         try:
             # Use the provided commit hash directly
             commit = repo.revparse_single(commit_hash)
@@ -68,4 +66,5 @@ if __name__ == "__main__":
     else:
         # Use the current HEAD commit if no version is specified
         def_commit = str(repo.head.target)[:7]
-    main(full=full, testdata=testdata, nexus_def_path=nexus_def_path, def_commit=def_commit)
\ No newline at end of file
+
+    main(full=full, nexus_def_path=nexus_def_path, def_commit=def_commit, store_commit_filename=store_commit_filename)
\ No newline at end of file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script in its current state is already generating the ontology with the commit hash in the filename.
On the other hand, in the official repo, it was requested to store the ontology without a version in the filename, so when a specific commit is checked out, one shall get the corresponding ontology always with the same name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command line option for "store_commit_filename", actually, can be brought over, so it is not needed to manually remove the commit hash if was not needed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was my idea, that basically you bring the changes from Tanmay's branch over.

Copy link

@Tanmay2028 Tanmay2028 Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.

The script on the oscars-project branch adds the commit hash to the generated file name only if the corresponding argument is set to true in the function call or explicitly specified in generate_ontology.sh.

This allows us to generate ontologies using specific commit hash and not include it in the filename, fulfilling the request from official repo.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sanbrock @Tanmay2028 I added now the command line option for store_commit_filename from Tanmay's branch here. I did not bring the changes related to ESRFET over. I think this PR here is good to go then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants