From 287183c65e10060921ce170bfca98bec74de669e Mon Sep 17 00:00:00 2001 From: IanCa Date: Tue, 4 Jun 2024 18:26:20 -0500 Subject: [PATCH 1/3] First pass create_ontology script --- hed/scripts/convert_and_update_schema.py | 2 +- hed/scripts/create_ontology.py | 47 ++++++++++++++++++++++++ hed/scripts/script_util.py | 24 ++++++++++++ pyproject.toml | 1 + 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 hed/scripts/create_ontology.py diff --git a/hed/scripts/convert_and_update_schema.py b/hed/scripts/convert_and_update_schema.py index 4117985d..1883c499 100644 --- a/hed/scripts/convert_and_update_schema.py +++ b/hed/scripts/convert_and_update_schema.py @@ -72,7 +72,7 @@ def convert_and_update(filenames, set_ids): def main(): parser = argparse.ArgumentParser(description='Update other schema formats based on the changed one.') parser.add_argument('filenames', nargs='*', help='List of files to process') - parser.add_argument('--set-ids', action='store_true', help='Set IDs for each file') + parser.add_argument('--set-ids', action='store_true', help='Add missing hed ids') args = parser.parse_args() diff --git a/hed/scripts/create_ontology.py b/hed/scripts/create_ontology.py new file mode 100644 index 00000000..0725a691 --- /dev/null +++ b/hed/scripts/create_ontology.py @@ -0,0 +1,47 @@ +from hed.schema import load_schema_version +from hed.schema.schema_io.df2schema import load_dataframes +from hed.schema.schema_io.ontology_util import convert_df_to_omn +from hed.scripts.script_util import get_prerelease_path, get_schema_filename +import argparse +import os + + +def create_ontology(repo_path, schema_name, schema_version, dest): + final_source = get_prerelease_path(repo_path, schema_name, schema_version) + + dataframes = load_dataframes(final_source) + _, omn_dict = convert_df_to_omn(dataframes) + + base = get_schema_filename(schema_name, schema_version) + output_dest = os.path.join(dest, base) + os.makedirs(output_dest, exist_ok=True) + for suffix, omn_text in omn_dict.items(): + filename = os.path.join(output_dest, f"{base}_{suffix}.omn") + with open(filename, mode='w', encoding='utf-8') as opened_file: + opened_file.writelines(omn_text) + + return 0 + + +def main(): + parser = argparse.ArgumentParser(description='Convert a specified schema in the prerelease folder to an ontology.') + parser.add_argument('repo_path', help='The location of the hed-schemas directory') + parser.add_argument('schema_name', help='The name of the schema to convert("standard" for standard schema)') + parser.add_argument('schema_version', help='The location of the hed-schemas directory') + parser.add_argument('--dest', default=os.path.join("src", "ontology"), help='The base location to save to') + + args = parser.parse_args() + + repo_path = args.repo_path + schema_name = args.schema_name + schema_version = args.schema_version + dest = args.dest + + # Trigger a local cache hit (this ensures trying to load withStandard schemas will work properly) + _ = load_schema_version("8.2.0") + + return create_ontology(repo_path, schema_name, schema_version, dest) + + +if __name__ == "__main__": + exit(main()) diff --git a/hed/scripts/script_util.py b/hed/scripts/script_util.py index 441ec736..06be5af1 100644 --- a/hed/scripts/script_util.py +++ b/hed/scripts/script_util.py @@ -159,3 +159,27 @@ def validate_all_schemas(schema_files): all_issues += single_schema_issues return all_issues + + + +def get_schema_filename(schema_name, schema_version): + schema_name = schema_name.lower() + if schema_name == "standard" or schema_name == "": + return f"HED{schema_version}" + else: + return f"HED_{schema_name}_{schema_version}" + + +def get_prerelease_path(repo_path, schema_name, schema_version): + """Returns the location of the given pre-release schema in the repo""" + schema_name = schema_name.lower() + if schema_name == "" or schema_name == "standard": + base_path = "standard_schema" + else: + base_path = os.path.join("library_schemas", schema_name) + + base_path = os.path.join(repo_path, base_path, "prerelease") + + schema_filename = get_schema_filename(schema_name, schema_version) + + return os.path.join(base_path, "hedtsv", schema_filename) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 7634e7e5..b7e341ae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,6 +57,7 @@ run_remodel_backup = "hed.tools.remodeling.cli.run_remodel_backup:main" run_remodel_restore = "hed.tools.remodeling.cli.run_remodel_restore:main" hed_validate_schemas = "hed.scripts.validate_schemas:main" hed_update_schemas = "hed.scripts.convert_and_update_schema:main" +hed_create_ontology = "hed.scripts.create_ontology:main" [tool.versioneer] VCS = "git" From 9789e3814ef1ff372da0d151170dde21bc93d2a5 Mon Sep 17 00:00:00 2001 From: IanCa Date: Tue, 4 Jun 2024 18:36:44 -0500 Subject: [PATCH 2/3] Add debug print --- hed/scripts/create_ontology.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hed/scripts/create_ontology.py b/hed/scripts/create_ontology.py index 0725a691..2b4aa993 100644 --- a/hed/scripts/create_ontology.py +++ b/hed/scripts/create_ontology.py @@ -8,6 +8,7 @@ def create_ontology(repo_path, schema_name, schema_version, dest): final_source = get_prerelease_path(repo_path, schema_name, schema_version) + print(f"Creating ontology from {final_source}") dataframes = load_dataframes(final_source) _, omn_dict = convert_df_to_omn(dataframes) From c23524a20960f5222b2ceac63e01cc0ec220b201 Mon Sep 17 00:00:00 2001 From: IanCa Date: Tue, 4 Jun 2024 18:53:55 -0500 Subject: [PATCH 3/3] remove debug print --- hed/scripts/create_ontology.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hed/scripts/create_ontology.py b/hed/scripts/create_ontology.py index 2b4aa993..24878d33 100644 --- a/hed/scripts/create_ontology.py +++ b/hed/scripts/create_ontology.py @@ -8,7 +8,7 @@ def create_ontology(repo_path, schema_name, schema_version, dest): final_source = get_prerelease_path(repo_path, schema_name, schema_version) - print(f"Creating ontology from {final_source}") + # print(f"Creating ontology from {final_source}") dataframes = load_dataframes(final_source) _, omn_dict = convert_df_to_omn(dataframes)