Skip to content

Commit

Permalink
Merge pull request #29 from VisLab/main
Browse files Browse the repository at this point in the history
Modified the imports for second level modules
  • Loading branch information
VisLab authored Aug 1, 2024
2 parents dc14c15 + a01076a commit 3cd1db4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
53 changes: 27 additions & 26 deletions hedmat/hedtools/HedToolsPython.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

properties
hmod
amod
vmod
HedVersion
HedSchema
end
Expand All @@ -16,12 +18,14 @@
% representing a valid HED version.
%
obj.hmod = py.importlib.import_module('hed');
obj.amod = py.importlib.import_module('hed.tools.analysis');
obj.vmod = py.importlib.import_module('hed.validator');
obj.resetHedVersion(version)
end

function sidecar = generateSidecar(obj, eventsIn, valueColumns, ...
skipColumns)
% Return a sidecar string based on an events data.
% Return a sidecar string based on an events data.py.hed
%
% Parameters:
% eventsIn - char, string or rectified struct.
Expand Down Expand Up @@ -78,8 +82,7 @@
hedObjs = HedToolsPython.getHedStringObjs(eventsTab, ...
obj.HedSchema, p.Results.removeTypesOn, ...
p.Results.includeContext, p.Results.replaceDefs);
strs = ...
obj.hmod.tools.analysis.annotation_util.to_strlist(hedObjs);
strs = obj.amod.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 Down Expand Up @@ -139,7 +142,7 @@
check_for_warnings=checkWarnings);
if ~isempty(sidecar) && ~isequal(sidecar, py.None)
sidecar = HedTools.formatSidecar(sidecar);
sidecarObj = obj.hmod.tools.strs_to_sidecar(sidecar);
sidecarObj = obj.amod.annotation_util.strs_to_sidecar(sidecar);
issues = sidecarObj.validate(obj.HedSchema, error_handler=ehandler);
hasErrors = obj.hmod.errors.error_reporter.check_for_any_errors(issues);
issues = char(obj.hmod.get_printable_issue_string(issues));
Expand Down Expand Up @@ -211,10 +214,8 @@
hedStringObj = obj.hmod.models.hed_string.HedString(hedTags, obj.HedSchema);
ehandler = obj.hmod.errors.error_reporter.ErrorHandler(...
check_for_warnings=p.Results.checkWarnings);
validator = ...
obj.hmod.validator.hed_validator.HedValidator(obj.HedSchema);
issues = validator.validate(hedStringObj, false, ...
error_handler=ehandler);
val = obj.vmod.hed_validator.HedValidator(obj.HedSchema);
issues = val.validate(hedStringObj, false, error_handler=ehandler);
if isempty(issues)
issues = '';
else
Expand Down Expand Up @@ -280,14 +281,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))
umod = py.importlib.import_module('hed.tools.analysis');
eventManager = umod.event_manager.EventManager(tabular, schema);
amod = py.importlib.import_module('hed.tools.analysis');
eventManager = amod.event_manager.EventManager(tabular, schema);
if removeTypesOn
removeTypes = {'Condition-variable', 'Task'};
else
removeTypes = {};
end
tagManager = umod.hed_tag_manager.HedTagManager(eventManager, ...
tagManager = amod.hed_tag_manager.HedTagManager(eventManager, ...
py.list(removeTypes));
hedStringObjs = ...
tagManager.get_hed_objs(includeContext, replaceDefs);
Expand All @@ -308,8 +309,8 @@
hedSchemaObj = smod.load_schema_version(schema);
elseif iscell(schema)
hedSchemaObj = smod.load_schema_version(py.list(schema));
elseif py.isinstance(schema, obj.smod.HedSchema) || ...
py.isinstance(schema, obj.smod.HedSchemaGroup)
elseif py.isinstance(schema, smod.HedSchema) || ...
py.isinstance(schema, smod.HedSchemaGroup)
hedSchemaObj = schema;
else
hedSchemaObj = py.None;
Expand Down Expand Up @@ -356,19 +357,19 @@
% Returns:
% sidecar_obj - a HEDTools Sidecar object.
%
amod = py.importlib.import_module('hed');
umod = py.importlib.import_module('hed.tools.analysis.annotation_util');
hmod = py.importlib.import_module('hed');
amod = py.importlib.import_module('hed.tools.analysis');

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

sidecarObj = HedToolsPython.getSidecarObj(sidecar);
if isstruct(events)
events = events2string(events);
tabularObj = umod.str_to_tabular(events, sidecarObj);
tabularObj = amod.annotation_util.str_to_tabular(events, sidecarObj);
elseif ischar(events) || isstring(events)
tabularObj = umod.str_to_tabular(events, sidecarObj);
elseif py.isinstance(events, amod.TabularInput)
tabularObj = amod.annotation_util.str_to_tabular(events, sidecarObj);
elseif py.isinstance(events, hmod.TabularInput)
tabularObj = events;
else
throw(MException('HedToolsPytonGetTabularInput:Invalid input'))
Expand All @@ -415,11 +416,11 @@
% Throws: HedFileError if valueColumns and skipColumns
% overlap.
%
amod = py.importlib.import_module('hed');
amod = py.importlib.import_module('hed.tools.analysis');
try
valueList = py.list(valueColumns);
skipList = py.list(skipColumns);
tabularSum = amod.tools.analysis.tabular_summary.TabularSummary(valueList, skipList);
tabularSum = amod.tabular_summary.TabularSummary(valueList, skipList);
catch ME
throw(MException('HedToolsPythonGetTabularSummary:ColumnNameOverlap', ...
'valueColumns and skipColumns can not overlap'))
Expand Down
35 changes: 18 additions & 17 deletions tests/test_hed_tools/TestHedToolsPython.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,28 @@ function setUp(testCase)

methods (Test)

function testGenerateSidecar(testCase)
% Valid char events should not have errors or warnings
eventsChar = fileread(testCase.goodEventsPath);
testCase.verifyTrue(ischar(eventsChar))

% no types, no context, no replace
sidecar = testCase.hed.generateSidecar(eventsChar, ...
{'trial', 'rep_lag', 'stim_file'}, ...
{'onset', 'duration', 'sample'});
testCase.verifyTrue(ischar(sidecar));
sideStruct = jsondecode(sidecar);
testCase.verifyFalse(isfield(sideStruct, 'onset'));
testCase.verifyTrue(isstruct(sideStruct.event_type.HED));
testCase.verifyTrue(ischar(sideStruct.trial.HED));
end
% function testGenerateSidecar(testCase)
% % Valid char events should not have errors or warnings
% eventsChar = fileread(testCase.goodEventsPath);
% testCase.verifyTrue(ischar(eventsChar))
%
% % no types, no context, no replace
% sidecar = testCase.hed.generateSidecar(eventsChar, ...
% {'trial', 'rep_lag', 'stim_file'}, ...
% {'onset', 'duration', 'sample'});
% testCase.verifyTrue(ischar(sidecar));
% sideStruct = jsondecode(sidecar);
% testCase.verifyFalse(isfield(sideStruct, 'onset'));
% testCase.verifyTrue(isstruct(sideStruct.event_type.HED));
% testCase.verifyTrue(ischar(sideStruct.trial.HED));
% end

function testGetHedAnnotations(testCase)
% Valid char events should not have errors or warnings
sidecarChar = fileread(testCase.goodSidecarPath);
eventsChar = fileread(testCase.goodEventsPath);
testCase.verifyTrue(ischar(eventsChar))
testCase.verifyTrue(ischar(eventsChar));
testCase.verifyTrue(ischar(sidecarChar));

% no types, no context, no replace
annotations = testCase.hed.getHedAnnotations(eventsChar, ...
Expand Down Expand Up @@ -315,7 +316,7 @@ function testSidecarValid(testCase)
issueString = testCase.hed.validateSidecar(...
sidecarObj, 'checkWarnings', true);
testCase.verifyEqual(strlength(issueString), 0);
end
end

function testSidecarInvalid(testCase)
% Invalid char sidecar should have errors
Expand Down

0 comments on commit 3cd1db4

Please sign in to comment.