Skip to content

Commit

Permalink
Merge pull request #12 from VisLab/main
Browse files Browse the repository at this point in the history
Started work on wrappers for HED validation
  • Loading branch information
VisLab authored Apr 19, 2024
2 parents 728055e + 7778980 commit 93386bf
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 0 deletions.
File renamed without changes.
13 changes: 13 additions & 0 deletions hedmat/hed_wrappers/getHedSchema.m
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.
28 changes: 28 additions & 0 deletions hedmat/hed_wrappers/validateSidecar.m
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
29 changes: 29 additions & 0 deletions hedmat/hed_wrappers/validateString.m
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.
1 change: 1 addition & 0 deletions hedmat/web_services/runAllDemos.m
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
41 changes: 41 additions & 0 deletions tests/test_hed_wrappers/TestGetHedSchema.m
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
54 changes: 54 additions & 0 deletions tests/test_hed_wrappers/TestValidateSidecar.m
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
53 changes: 53 additions & 0 deletions tests/test_hed_wrappers/TestValidateString.m
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

0 comments on commit 93386bf

Please sign in to comment.