Skip to content
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

First pass create_ontology script #938

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hed/scripts/convert_and_update_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
48 changes: 48 additions & 0 deletions hed/scripts/create_ontology.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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)
# print(f"Creating ontology from {final_source}")

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())
24 changes: 24 additions & 0 deletions hed/scripts/script_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down