From 7778980383ca931db45098da139f9bf030fc7f20 Mon Sep 17 00:00:00 2001 From: Kay Robbins <1189050+VisLab@users.noreply.github.com> Date: Fri, 19 Apr 2024 15:05:30 -0500 Subject: [PATCH] Started work on wrappers for HED validation --- .../getFactors.m | 0 hedmat/hed_wrappers/getHedSchema.m | 13 +++++ .../runBidsValidation.m | 0 .../validateHedInBids.m | 0 hedmat/hed_wrappers/validateSidecar.m | 28 ++++++++++ hedmat/hed_wrappers/validateString.m | 29 ++++++++++ .../remodelExample1.m | 0 .../runRemodel.m | 0 .../runRemodelBackup.m | 0 .../runRemodelExamples.m | 0 .../runRemodelRestore.m | 0 hedmat/web_services/runAllDemos.m | 1 + tests/test_hed_wrappers/TestGetHedSchema.m | 41 ++++++++++++++ tests/test_hed_wrappers/TestValidateSidecar.m | 54 +++++++++++++++++++ tests/test_hed_wrappers/TestValidateString.m | 53 ++++++++++++++++++ 15 files changed, 219 insertions(+) rename hedmat/{hedtools_wrappers => hed_wrappers}/getFactors.m (100%) create mode 100644 hedmat/hed_wrappers/getHedSchema.m rename hedmat/{hedtools_wrappers => hed_wrappers}/runBidsValidation.m (100%) rename hedmat/{hedtools_wrappers => hed_wrappers}/validateHedInBids.m (100%) create mode 100644 hedmat/hed_wrappers/validateSidecar.m create mode 100644 hedmat/hed_wrappers/validateString.m rename hedmat/{hedtools_wrappers => remodeling}/remodelExample1.m (100%) rename hedmat/{hedtools_wrappers => remodeling}/runRemodel.m (100%) rename hedmat/{hedtools_wrappers => remodeling}/runRemodelBackup.m (100%) rename hedmat/{hedtools_wrappers => remodeling}/runRemodelExamples.m (100%) rename hedmat/{hedtools_wrappers => remodeling}/runRemodelRestore.m (100%) create mode 100644 tests/test_hed_wrappers/TestGetHedSchema.m create mode 100644 tests/test_hed_wrappers/TestValidateSidecar.m create mode 100644 tests/test_hed_wrappers/TestValidateString.m diff --git a/hedmat/hedtools_wrappers/getFactors.m b/hedmat/hed_wrappers/getFactors.m similarity index 100% rename from hedmat/hedtools_wrappers/getFactors.m rename to hedmat/hed_wrappers/getFactors.m diff --git a/hedmat/hed_wrappers/getHedSchema.m b/hedmat/hed_wrappers/getHedSchema.m new file mode 100644 index 0000000..85d7993 --- /dev/null +++ b/hedmat/hed_wrappers/getHedSchema.m @@ -0,0 +1,13 @@ +function hedSchema = getHedSchema(hedVersion) +% Return a HedSchema or HedSchemaGroup object based on hedVersion +% +% Parameters: +% hedVersion - a single string or a cell array of strings representing +% the HED schema version. +% +% Returns: +% hedSchema - A hedSchema object +% + py.importlib.import_module('hed'); + + hedSchema = py.hed.schema.load_schema_version(hedVersion); \ No newline at end of file diff --git a/hedmat/hedtools_wrappers/runBidsValidation.m b/hedmat/hed_wrappers/runBidsValidation.m similarity index 100% rename from hedmat/hedtools_wrappers/runBidsValidation.m rename to hedmat/hed_wrappers/runBidsValidation.m diff --git a/hedmat/hedtools_wrappers/validateHedInBids.m b/hedmat/hed_wrappers/validateHedInBids.m similarity index 100% rename from hedmat/hedtools_wrappers/validateHedInBids.m rename to hedmat/hed_wrappers/validateHedInBids.m diff --git a/hedmat/hed_wrappers/validateSidecar.m b/hedmat/hed_wrappers/validateSidecar.m new file mode 100644 index 0000000..9bc8b81 --- /dev/null +++ b/hedmat/hed_wrappers/validateSidecar.m @@ -0,0 +1,28 @@ +function issueString = validateSidecar(sidecar, hedSchema, checkForWarnings) +% Validate a sidecar containing HED tags. +% +% Parameters: +% sidecar - JSON string or a Sidecar object +% hedSchema - A HED schema object or HedVersion +% checkForWarnings - Boolean indicating checking for warnings +% +% Returns: +% issueString - A string with the validation issues suitable for +% printing (has newlines). +% + hedModule = py.importlib.import_module('hed'); + if ~py.isinstance(hedSchema, hedModule.HedSchema) && ... + ~py.isinstance(hedSchema, hedModule.HedSchemaGroup) + hedSchema = getHedSchema(hedSchema); + end + if py.isinstance(sidecar, hedModule.Sidecar) + sidecarObj = sidecar; + end + errorHandler = py.hed.errors.error_reporter.ErrorHandler(... + check_for_warnings=checkForWarnings); + issues = sidecarObj.validate(hedSchema, error_handler=errorHandler); + if isempty(issues) + issueString = ''; + else + issueString = string(py.hed.get_printable_issue_string(issues)); + end diff --git a/hedmat/hed_wrappers/validateString.m b/hedmat/hed_wrappers/validateString.m new file mode 100644 index 0000000..3c37e04 --- /dev/null +++ b/hedmat/hed_wrappers/validateString.m @@ -0,0 +1,29 @@ +function issueString = validateString(hedtags, hedSchema, ... + checkForWarnings, hedDefinitions) +% Validate a string containing HED tags. +% +% Parameters: +% hedString - A MATLAB string or character array. +% hedSchema - A HED schema object or HedVersion +% checkForWarnings - Boolean indicating checking for warnings +% hedDefinitions - A structure with HED definitions. +% +% Returns: +% issueString - A string with the validation issues suitable for +% printing (has newlines). +% ToDo: Make hedDefinitions optional. +% + hedModule = py.importlib.import_module('hed'); + if ~py.isinstance(hedSchema, hedModule.HedSchema) + hedSchema = getHedSchema(hedSchema); + end + hedString = hedModule.HedString(hedtags, hedSchema); + errorHandler = py.hed.errors.error_reporter.ErrorHandler(... + check_for_warnings=checkForWarnings); + validator = hedModule.validator.hed_validator.HedValidator(hedSchema, hedDefinitions); + issues = validator.validate(hedString, false, error_handler=errorHandler); + if isempty(issues) + issueString = ''; + else + issueString = string(py.hed.get_printable_issue_string(issues)); + end diff --git a/hedmat/hedtools_wrappers/remodelExample1.m b/hedmat/remodeling/remodelExample1.m similarity index 100% rename from hedmat/hedtools_wrappers/remodelExample1.m rename to hedmat/remodeling/remodelExample1.m diff --git a/hedmat/hedtools_wrappers/runRemodel.m b/hedmat/remodeling/runRemodel.m similarity index 100% rename from hedmat/hedtools_wrappers/runRemodel.m rename to hedmat/remodeling/runRemodel.m diff --git a/hedmat/hedtools_wrappers/runRemodelBackup.m b/hedmat/remodeling/runRemodelBackup.m similarity index 100% rename from hedmat/hedtools_wrappers/runRemodelBackup.m rename to hedmat/remodeling/runRemodelBackup.m diff --git a/hedmat/hedtools_wrappers/runRemodelExamples.m b/hedmat/remodeling/runRemodelExamples.m similarity index 100% rename from hedmat/hedtools_wrappers/runRemodelExamples.m rename to hedmat/remodeling/runRemodelExamples.m diff --git a/hedmat/hedtools_wrappers/runRemodelRestore.m b/hedmat/remodeling/runRemodelRestore.m similarity index 100% rename from hedmat/hedtools_wrappers/runRemodelRestore.m rename to hedmat/remodeling/runRemodelRestore.m diff --git a/hedmat/web_services/runAllDemos.m b/hedmat/web_services/runAllDemos.m index 2ac0ec2..c1284a9 100644 --- a/hedmat/web_services/runAllDemos.m +++ b/hedmat/web_services/runAllDemos.m @@ -1,6 +1,7 @@ host = 'https://hedtools.org/hed'; %host = 'https://hedtools.org/hed_dev'; host = 'http://127.0.0.1:5000'; +host = 'http://192.168.0.25/hed_dev'; errorMap = containers.Map('KeyType', 'char', 'ValueType', 'any'); diff --git a/tests/test_hed_wrappers/TestGetHedSchema.m b/tests/test_hed_wrappers/TestGetHedSchema.m new file mode 100644 index 0000000..222707c --- /dev/null +++ b/tests/test_hed_wrappers/TestGetHedSchema.m @@ -0,0 +1,41 @@ +classdef TestGetHedSchema < matlab.unittest.TestCase + + properties + hedModule + end + + methods (TestClassSetup) + function importPythonModules(testCase) + testCase.hedModule = py.importlib.import_module('hed'); + end + end + + methods (Test) + + function testSimpleVersion(testCase) + % Test single version + hedVersion = '8.2.0'; + hedSchema = getHedSchema(hedVersion); + assertTrue(testCase, py.isinstance(hedSchema, ... + testCase.hedModule.schema.HedSchema), ... + 'The object is not an instance of HedSchema.'); + version = char(hedSchema.version); + testCase.verifyEqual(version, '8.2.0', ... + 'Created schema has incorrect version.'); + end + + function testMultipleVersions(testCase) + % Test complex version with prefix and partnered library + hedVersion = py.list({'ts:8.2.0', 'score_1.1.0'}); + hedSchema = getHedSchema(hedVersion); + assertTrue(testCase, py.isinstance(hedSchema, ... + testCase.hedModule.schema.HedSchemaGroup), ... + 'The object is not an instance of HedSchemaGroup.'); + versions = cell(hedSchema.get_schema_versions()); + testCase.verifyEqual(char(versions{1}), 'ts:8.2.0', ... + 'Created schema has incorrect version.'); + testCase.verifyEqual(char(versions{2}), 'score_1.1.0', ... + 'Created schema has incorrect version.'); + end + end +end \ No newline at end of file diff --git a/tests/test_hed_wrappers/TestValidateSidecar.m b/tests/test_hed_wrappers/TestValidateSidecar.m new file mode 100644 index 0000000..f906457 --- /dev/null +++ b/tests/test_hed_wrappers/TestValidateSidecar.m @@ -0,0 +1,54 @@ +classdef TestValidateSidecar < matlab.unittest.TestCase + + properties + hedModule + hedSchema + goodSidecar + end + + methods (TestClassSetup) + function importPythonModules(testCase) + testCase.hedModule = py.importlib.import_module('hed'); + testCase.hedSchema = getHedSchema('8.2.0'); + end + end + + methods (Test) + + function testBasicValid(testCase) + % Test a simple string + % issues = validateString('Red, Blue', testCase.hedSchema, ... + % true, struct()); + % testCase.verifyEqual(strlength(issues), 0, ... + % 'Valid HED string has issues.'); + % % Test with extension and check for warnings is true + % issues = validateString('Red, Blue/Apple', ... + % testCase.hedSchema, true, struct()); + % testCase.verifyGreaterThan(strlength(issues), 0, ... + % 'Valid HED string with ext has warning.'); + % + % % Test with extension and check for warnings is false + % issues = validateString('Red, Blue/Apple', ... + % testCase.hedSchema, false, struct()); + % testCase.verifyEqual(strlength(issues), 0, ... + % 'Valid HED string with ext has no errors.'); + end + + function testBasicInvalid(testCase) + % Test a simple string + % issues = validateString('Red, Yikes', testCase.hedSchema, ... + % true, struct()); + % testCase.verifyGreaterThan(strlength(issues), 0, ... + % 'Invalid HED string has no issues.'); + % % Test with extension and check for warnings is true + % issues = validateString('Red, Blue/Apple, Yikes', ... + % testCase.hedSchema, false, struct()); + % testCase.verifyGreaterThan(strlength(issues), 0, ... + % 'Invalid HED string hs no issues.'); + end + + % Todo: test with and without schema + % Todo: test with definitions + + end +end \ No newline at end of file diff --git a/tests/test_hed_wrappers/TestValidateString.m b/tests/test_hed_wrappers/TestValidateString.m new file mode 100644 index 0000000..4297538 --- /dev/null +++ b/tests/test_hed_wrappers/TestValidateString.m @@ -0,0 +1,53 @@ +classdef TestValidateString < matlab.unittest.TestCase + + properties + hedModule + hedSchema + end + + methods (TestClassSetup) + function importPythonModules(testCase) + testCase.hedModule = py.importlib.import_module('hed'); + testCase.hedSchema = getHedSchema('8.2.0'); + end + end + + methods (Test) + + function testBasicValid(testCase) + % Test a simple string + issues = validateString('Red, Blue', testCase.hedSchema, ... + true, struct()); + testCase.verifyEqual(strlength(issues), 0, ... + 'Valid HED string has issues.'); + % Test with extension and check for warnings is true + issues = validateString('Red, Blue/Apple', ... + testCase.hedSchema, true, struct()); + testCase.verifyGreaterThan(strlength(issues), 0, ... + 'Valid HED string with ext has warning.'); + + % Test with extension and check for warnings is false + issues = validateString('Red, Blue/Apple', ... + testCase.hedSchema, false, struct()); + testCase.verifyEqual(strlength(issues), 0, ... + 'Valid HED string with ext has no errors.'); + end + + function testBasicInvalid(testCase) + % Test a simple string + issues = validateString('Red, Yikes', testCase.hedSchema, ... + true, struct()); + testCase.verifyGreaterThan(strlength(issues), 0, ... + 'Invalid HED string has no issues.'); + % Test with extension and check for warnings is true + issues = validateString('Red, Blue/Apple, Yikes', ... + testCase.hedSchema, false, struct()); + testCase.verifyGreaterThan(strlength(issues), 0, ... + 'Invalid HED string hs no issues.'); + end + + % Todo: test with and without schema + % Todo: test with definitions + + end +end \ No newline at end of file