Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/hed-standard/hed-python
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
VisLab committed Jun 5, 2024
2 parents 72447e0 + 193696a commit 0024ff0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
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
2 changes: 1 addition & 1 deletion tests/schema/test_ontology_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_update_dataframes_from_schema(self):
try:
updated_dataframes = update_dataframes_from_schema(schema_dataframes_new, schema)
except HedFileError as e:
self.assertEqual(len(e.issues), 111)
self.assertEqual(len(e.issues), 115)
breakHere = 3


Expand Down
22 changes: 11 additions & 11 deletions tests/scripts/test_script_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def test_regular_extension(self):
def test_tsv_extension(self):
"""Test that .tsv extensions are handled differently."""
# Assuming the function correctly handles paths with directories
self.assertEqual(add_extension("path/to/filename", ".tsv"), "path/to/hedtsv/filename")
self.assertEqual(add_extension(os.path.normpath("path/to/filename"), ".tsv"), os.path.normpath("path/to/hedtsv/filename"))
# Testing with a basename only
self.assertEqual(add_extension("filename", ".tsv"), "hedtsv/filename")
self.assertEqual(add_extension("filename", ".tsv"), os.path.normpath("hedtsv/filename"))

def test_empty_extension(self):
"""Test adding an empty extension."""
Expand All @@ -33,7 +33,7 @@ class TestSortBaseSchemas(unittest.TestCase):
def test_mixed_file_types(self):
filenames = [
"test_schema.mediawiki",
"hedtsv/test_schema/test_schema_Tag.tsv",
os.path.normpath("hedtsv/test_schema/test_schema_Tag.tsv"),
"other_schema.xml"
]
expected = {
Expand All @@ -45,9 +45,9 @@ def test_mixed_file_types(self):

def test_tsv_in_correct_subfolder(self):
filenames = [
"hedtsv/test_schema/test_schema_Tag.tsv",
"hedtsv/test_schema/test_schema_Tag.tsv",
"hedtsv/wrong_folder/wrong_name_Tag.tsv" # Should be ignored
os.path.normpath("hedtsv/test_schema/test_schema_Tag.tsv"),
os.path.normpath("hedtsv/test_schema/test_schema_Tag.tsv"),
os.path.normpath("hedtsv/wrong_folder/wrong_name_Tag.tsv") # Should be ignored
]
expected = {
"test_schema": {".tsv"}
Expand All @@ -57,20 +57,20 @@ def test_tsv_in_correct_subfolder(self):

def test_tsv_in_correct_subfolder2(self):
filenames = [
"prerelease/hedtsv/test_schema/test_schema_Tag.tsv",
"prerelease/hedtsv/test_schema/test_schema_Tag.tsv",
"prerelease/hedtsv/wrong_folder/wrong_name_Tag.tsv" # Should be ignored
os.path.normpath("prerelease/hedtsv/test_schema/test_schema_Tag.tsv"),
os.path.normpath("prerelease/hedtsv/test_schema/test_schema_Tag.tsv"),
os.path.normpath("prerelease/hedtsv/wrong_folder/wrong_name_Tag.tsv") # Should be ignored
]
expected = {
"prerelease/test_schema": {".tsv"}
os.path.normpath("prerelease/test_schema"): {".tsv"}
}
result = sort_base_schemas(filenames)
self.assertEqual(dict(result), expected)

def test_ignored_files(self):
filenames = [
"test_schema.mediawiki",
"not_hedtsv/test_schema/test_schema_Tag.tsv" # Should be ignored
os.path.normpath("not_hedtsv/test_schema/test_schema_Tag.tsv") # Should be ignored
]
expected = {
"test_schema": {".mediawiki"}
Expand Down

0 comments on commit 0024ff0

Please sign in to comment.