Skip to content

Commit

Permalink
Merge pull request #28 from VisLab/main
Browse files Browse the repository at this point in the history
Changed Python module import strategy
  • Loading branch information
VisLab authored Jul 28, 2024
2 parents ef0f01d + 0f5a34f commit dc14c15
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
9 changes: 4 additions & 5 deletions hedmat/hedtools/HedTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@

sidecar = generateSidecar(obj, eventsIn, valueColumns, skipColumns);

annotations = getHedAnnotations(obj, eventsIn, sidecar, ...
removeTypesOn, includeContext, replaceDefs);
annotations = getHedAnnotations(obj, eventsIn, sidecar, varargin);

factors = searchHed(obj, annotations, queries);

issues = validateEvents(obj, eventsIn, sidecar, checkWarnings);
issues = validateEvents(obj, eventsIn, sidecar, varargin);

issues = validateSidecar(obj, sidecar, checkWarnings);
issues = validateSidecar(obj, sidecar, varargin);

issues = validateTags(obj, hedtags, checkWarnings);
issues = validateTags(obj, hedtags, varargin);

resetHedVersion(obj, hedVersion)
end
Expand Down
70 changes: 37 additions & 33 deletions hedmat/hedtools/HedToolsPython.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
% Concrete class using direct calls to Python for HedTools interface.

properties
hmod
HedVersion
HedSchema
end
Expand All @@ -14,7 +15,7 @@
% version - string or char array or cellstr
% representing a valid HED version.
%

obj.hmod = py.importlib.import_module('hed');
obj.resetHedVersion(version)
end

Expand Down Expand Up @@ -78,7 +79,7 @@
obj.HedSchema, p.Results.removeTypesOn, ...
p.Results.includeContext, p.Results.replaceDefs);
strs = ...
py.hed.tools.analysis.annotation_util.to_strlist(hedObjs);
obj.hmod.tools.analysis.annotation_util.to_strlist(hedObjs);
cStrs = cell(strs);
% Convert each string object in the cell array to a char array
annotations = cellfun(@char, cStrs(:), 'UniformOutput', false);
Expand All @@ -96,9 +97,9 @@
%

results = ...
cell(py.hed.models.query_service.get_query_handlers(...
cell(obj.hmod.models.query_service.get_query_handlers(...
queries, py.None));
issueString = char(py.hed.get_printable_issue_string(...
issueString = char(obj.hmod.get_printable_issue_string(...
results{3}));
if ~isempty(issueString)
throw(MException( ...
Expand All @@ -108,12 +109,13 @@
queries = results{1};
queryNames = results{2};
hed_objs = obj.getHedFromAnnotations(annotations, obj.HedSchema);
df_factors = py.hed.models.query_service.search_hed_objs(...
df_factors = obj.hmod.models.query_service.search_hed_objs(...
hed_objs, queries, queryNames);
factors = double(df_factors.to_numpy());
end

function issues = validateEvents(obj, events, sidecar, varargin)

% Validate HED in events or other tabular-type input.
%
% Parameters:
Expand All @@ -133,22 +135,22 @@
parse(p, varargin{:});
checkWarnings = p.Results.checkWarnings;
issues = '';
ehandler = py.hed.errors.error_reporter.ErrorHandler(...
ehandler = obj.hmod.errors.error_reporter.ErrorHandler(...
check_for_warnings=checkWarnings);
if ~isempty(sidecar) && ~isequal(sidecar, py.None)
sidecar = HedTools.formatSidecar(sidecar);
sidecarObj = py.hed.tools.strs_to_sidecar(sidecar);
sidecarObj = obj.hmod.tools.strs_to_sidecar(sidecar);
issues = sidecarObj.validate(obj.HedSchema, error_handler=ehandler);
hasErrors = py.hed.errors.error_reporter.check_for_any_errors(issues);
issues = char(py.hed.get_printable_issue_string(issues));
hasErrors = obj.hmod.errors.error_reporter.check_for_any_errors(issues);
issues = char(obj.hmod.get_printable_issue_string(issues));
if hasErrors
return;
end
end
eventsObj = HedToolsPython.getTabularObj(events, sidecarObj);
issuesEvents = eventsObj.validate(obj.HedSchema, error_handler=ehandler);
issues = [issues, ...
char(py.hed.get_printable_issue_string(issuesEvents))];
char(obj.hmod.get_printable_issue_string(issuesEvents))];

end

Expand All @@ -169,15 +171,14 @@
p = inputParser;
p.addParameter('checkWarnings', false, @(x) islogical(x))
parse(p, varargin{:});
ehandler = py.hed.errors.error_reporter.ErrorHandler(...
ehandler = obj.hmod.errors.error_reporter.ErrorHandler(...
check_for_warnings=p.Results.checkWarnings);
sidecarObj = HedToolsPython.getSidecarObj(sidecar);
issues = sidecarObj.validate(obj.HedSchema, error_handler=ehandler);
if isempty(issues)
issues = '';
else
issues = ...
char(py.hed.get_printable_issue_string(issues));
issues = char(obj.hmod.get_printable_issue_string(issues));
end
end

Expand Down Expand Up @@ -207,18 +208,17 @@
p.addParameter('checkWarnings', false, @(x) islogical(x))
parse(p, varargin{:});

hedStringObj = py.hed.HedString(hedTags, obj.HedSchema);
ehandler = py.hed.errors.error_reporter.ErrorHandler(...
hedStringObj = obj.hmod.models.hed_string.HedString(hedTags, obj.HedSchema);
ehandler = obj.hmod.errors.error_reporter.ErrorHandler(...
check_for_warnings=p.Results.checkWarnings);
validator = ...
py.hed.validator.hed_validator.HedValidator(obj.HedSchema);
obj.hmod.validator.hed_validator.HedValidator(obj.HedSchema);
issues = validator.validate(hedStringObj, false, ...
error_handler=ehandler);
if isempty(issues)
issues = '';
else
issues = ...
char(py.hed.get_printable_issue_string(issues));
issues = char(obj.hmod.get_printable_issue_string(issues));
end
end

Expand All @@ -245,8 +245,10 @@
end

methods (Static)

function hedStringObjs = getHedFromAnnotations(annotations, schema)
% Cell array of char or string convert to py.list of HedString
mmod = py.importlib.import_module('hed.models');
hedStringObjs = cell(1, length(annotations));

for k=1:length(annotations)
Expand All @@ -255,7 +257,7 @@
hedStringObjs{k} = py.None;
else
hedStringObjs{k} = ...
py.hed.HedString(char(annotations{k}), schema);
mmod.hed_string.HedString(char(annotations{k}), schema);
end
end
hedStringObjs = py.list(hedStringObjs);
Expand All @@ -278,14 +280,14 @@
% Note this is used as the basis for HED queries or for assembled HED.
% To manipulate directly in MATLAB -- convert to a cell array of char
% using string(cell(hedObjs))

eventManager = py.hed.tools.EventManager(tabular, schema);
umod = py.importlib.import_module('hed.tools.analysis');
eventManager = umod.event_manager.EventManager(tabular, schema);
if removeTypesOn
removeTypes = {'Condition-variable', 'Task'};
else
removeTypes = {};
end
tagManager = py.hed.tools.HedTagManager(eventManager, ...
tagManager = umod.hed_tag_manager.HedTagManager(eventManager, ...
py.list(removeTypes));
hedStringObjs = ...
tagManager.get_hed_objs(includeContext, replaceDefs);
Expand All @@ -301,13 +303,13 @@
% Returns:
% hedSchemaObj - A hedSchema object
%

smod = py.importlib.import_module('hed.schema');
if ischar(schema)
hedSchemaObj = py.hed.load_schema_version(schema);
hedSchemaObj = smod.load_schema_version(schema);
elseif iscell(schema)
hedSchemaObj = py.hed.load_schema_version(py.list(schema));
elseif py.isinstance(schema, obj.hmod.HedSchema) || ...
py.isinstance(schema, obj.hmod.HedSchemaGroup)
hedSchemaObj = smod.load_schema_version(py.list(schema));
elseif py.isinstance(schema, obj.smod.HedSchema) || ...
py.isinstance(schema, obj.smod.HedSchemaGroup)
hedSchemaObj = schema;
else
hedSchemaObj = py.None;
Expand Down Expand Up @@ -354,7 +356,7 @@
% Returns:
% sidecar_obj - a HEDTools Sidecar object.
%
hmod = py.importlib.import_module('hed');
amod = py.importlib.import_module('hed');
umod = py.importlib.import_module('hed.tools.analysis.annotation_util');

if ischar(sidecar)
Expand All @@ -366,7 +368,7 @@
elseif isempty(sidecar) || ...
(isa(sidecar, 'py.NoneType') && sidecar == py.None)
sidecarObj = py.None;
elseif py.isinstance(sidecar, hmod.Sidecar)
elseif py.isinstance(sidecar, amod.Sidecar)
sidecarObj = sidecar;
else
throw(MException('HedToolsPythonGetSidecarObj:BadInputFormat', ...
Expand All @@ -384,7 +386,7 @@
% Returns:
% tabularObj - HEDTools TabularInput object representing tabular data.
%
hmod = py.importlib.import_module('hed');
amod = py.importlib.import_module('hed');
umod = py.importlib.import_module('hed.tools.analysis.annotation_util');

sidecarObj = HedToolsPython.getSidecarObj(sidecar);
Expand All @@ -393,7 +395,7 @@
tabularObj = umod.str_to_tabular(events, sidecarObj);
elseif ischar(events) || isstring(events)
tabularObj = umod.str_to_tabular(events, sidecarObj);
elseif py.isinstance(events, hmod.TabularInput)
elseif py.isinstance(events, amod.TabularInput)
tabularObj = events;
else
throw(MException('HedToolsPytonGetTabularInput:Invalid input'))
Expand All @@ -413,16 +415,18 @@
% Throws: HedFileError if valueColumns and skipColumns
% overlap.
%

amod = py.importlib.import_module('hed');
try
valueList = py.list(valueColumns);
skipList = py.list(skipColumns);
tabularSum = py.hed.tools.analysis.tabular_summary.TabularSummary(valueList, skipList);
tabularSum = amod.tools.analysis.tabular_summary.TabularSummary(valueList, skipList);
catch ME
throw(MException('HedToolsPythonGetTabularSummary:ColumnNameOverlap', ...
'valueColumns and skipColumns can not overlap'))
end
end

end


end
38 changes: 19 additions & 19 deletions tests/test_hed_tools/TestHedToolsPython.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

methods (TestClassSetup)
function setUp(testCase)
testCase.hmod = py.importlib.import_module('hed');
testCase.hed = HedToolsPython('8.3.0');
testCase.hmod = py.importlib.import_module('hed');
[curDir, ~, ~] = fileparts(mfilename("fullpath"));
dataPath = fullfile(curDir, filesep, '..', filesep, '..', ...
filesep, 'data', filesep);
Expand Down Expand Up @@ -306,7 +306,7 @@ function testSidecarValid(testCase)
% Valid sidecar obj should not have errors or warnings
sidecarObj = HedToolsPython.getSidecarObj(sidecarChar);
testCase.verifyTrue(py.isinstance(sidecarObj, ...
testCase.hmod.Sidecar))
testCase.hmod.models.sidecar.Sidecar))
issueString = testCase.hed.validateSidecar( ...
sidecarObj, 'checkWarnings', false);
testCase.verifyEqual(strlength(issueString), 0);
Expand Down Expand Up @@ -357,7 +357,7 @@ function testSidecarInvalid(testCase)
% Invalid sidecar obj should have errors
sidecarObj = HedToolsPython.getSidecarObj(sidecarChar);
testCase.verifyTrue(py.isinstance(sidecarObj, ...
testCase.hmod.Sidecar))
testCase.hmod.models.sidecar.Sidecar))
issueString = testCase.hed.validateSidecar( ...
sidecarObj, 'checkWarnings', false);
testCase.verifyGreaterThan(strlength(issueString), 0);
Expand Down Expand Up @@ -412,7 +412,7 @@ function testGetHedQueryHandler(testCase)
query1 = 'Sensory-event';
qHandler = HedToolsPython.getHedQueryHandler(query1);
testCase.verifyTrue(py.isinstance(qHandler, ...
testCase.hmod.QueryHandler));
testCase.hmod.models.query_handler.QueryHandler));
end

function testGetHedStringObjs(testCase)
Expand Down Expand Up @@ -490,7 +490,7 @@ function testGetHedSchemaObjs(testCase)
% Valid char events should not have errors or warnings
schema = HedToolsPython.getHedSchemaObj('8.2.0');
testCase.verifyTrue(py.isinstance(schema, ...
testCase.hmod.HedSchema));
testCase.hmod.schema.hed_schema.HedSchema));
version = char(schema.version);
testCase.verifyEqual(version, '8.2.0');
end
Expand All @@ -500,7 +500,7 @@ function testGetHedSchemaObjsMultiple(testCase)
version = {'ts:8.2.0', 'score_1.1.0'};
schema = HedToolsPython.getHedSchemaObj(version);
assertTrue(testCase, py.isinstance(schema, ...
testCase.hmod.HedSchemaGroup));
testCase.hmod.schema.hed_schema_group.HedSchemaGroup));
versions = cell(schema.get_schema_versions());
testCase.verifyEqual(char(versions{1}), 'ts:8.2.0');
testCase.verifyEqual(char(versions{2}), 'score_1.1.0');
Expand All @@ -514,21 +514,21 @@ function testGetHedSchemaObjsNone(testCase)

function testGetSidecarObjSidecarIn(testCase)
% Test with incoming sidecar
sidecar = testCase.hmod.Sidecar(testCase.goodSidecarPath);
testCase.assertTrue(...
py.isinstance(sidecar, testCase.hmod.Sidecar));
sidecar = testCase.hmod.models.sidecar.Sidecar(testCase.goodSidecarPath);
testCase.assertTrue(py.isinstance(sidecar, ...
testCase.hmod.models.sidecar.Sidecar));
sidecarObj = HedToolsPython.getSidecarObj(sidecar);
testCase.assertTrue(...
py.isinstance(sidecarObj, testCase.hmod.Sidecar));
testCase.assertTrue(py.isinstance(sidecarObj, ...
testCase.hmod.models.sidecar.Sidecar));
end

function testGetSidecarObjCharIn(testCase)
% Test with incoming sidecar string
jsonChar = fileread(testCase.goodSidecarPath);
testCase.verifyTrue(ischar(jsonChar));
sidecarObj = HedToolsPython.getSidecarObj(jsonChar);
testCase.verifyTrue(...
py.isinstance(sidecarObj, testCase.hmod.Sidecar));
testCase.verifyTrue(py.isinstance(sidecarObj, ...
testCase.hmod.models.sidecar.Sidecar));
end

function testGetSidecarObjStringIn(testCase)
Expand All @@ -537,7 +537,7 @@ function testGetSidecarObjStringIn(testCase)
testCase.verifyTrue(isstring(jsonString));
sidecarObj = testCase.hed.getSidecarObj(jsonString);
testCase.verifyTrue(py.isinstance(sidecarObj, ...
testCase.hmod.Sidecar));
testCase.hmod.models.sidecar.Sidecar));
end

function testGetSidecarObjStructIn(testCase)
Expand All @@ -546,7 +546,7 @@ function testGetSidecarObjStructIn(testCase)
testCase.verifyTrue(isstruct(jsonStruct));
sidecarObj = HedToolsPython.getSidecarObj(jsonStruct);
testCase.assertTrue(py.isinstance(sidecarObj, ...
testCase.hmod.Sidecar));
testCase.hmod.models.sidecar.Sidecar));
end

function testGetSidecarObjEmptyInputs(testCase)
Expand All @@ -561,7 +561,7 @@ function testGetTabularInputSimple(testCase)
sidecar = fileread(testCase.goodSidecarPath);
tabularObj = HedToolsPython.getTabularObj(events, sidecar);
testCase.assertTrue(py.isinstance(tabularObj, ...
testCase.hmod.TabularInput));
testCase.hmod.models.tabular_input.TabularInput));
end

function testGetTabularInputNoSidecar(testCase)
Expand All @@ -570,7 +570,7 @@ function testGetTabularInputNoSidecar(testCase)
sidecar = py.None;
tabularObj = HedToolsPython.getTabularObj(events, sidecar);
testCase.assertTrue(py.isinstance(tabularObj, ...
testCase.hmod.TabularInput));
testCase.hmod.models.tabular_input.TabularInput));
end

function testGetTabularSummary(testCase)
Expand All @@ -580,10 +580,10 @@ function testGetTabularSummary(testCase)
tabularSum = HedToolsPython.getTabularSummary(...
valueColumns, skipColumns);
testCase.assertTrue(py.isinstance(tabularSum, ...
testCase.hmod.tools.TabularSummary));
testCase.hmod.tools.analysis.tabular_summary.TabularSummary));
tabularSum1 = HedToolsPython.getTabularSummary({}, {});
testCase.assertTrue(py.isinstance(tabularSum1, ...
testCase.hmod.tools.TabularSummary));
testCase.hmod.tools.analysis.tabular_summary.TabularSummary));
end

function testGetTabularSummaryInvalid(testCase)
Expand Down

0 comments on commit dc14c15

Please sign in to comment.