-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from VisLab/main
Started work on wrappers for HED validation
- Loading branch information
Showing
15 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |