Skip to content

Commit

Permalink
Merge pull request #17 from VisLab/main
Browse files Browse the repository at this point in the history
Updated the tests for the hed_wrappers
  • Loading branch information
VisLab authored May 14, 2024
2 parents 0766227 + cf93d34 commit ce6c13e
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 27 deletions.
15 changes: 7 additions & 8 deletions hedmat/hed_wrappers/get_schema_obj.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
% Returns:
% schema_obj - A hedSchema object
%
hedModule = py.importlib.import_module('hed');

if py.isinstance(schema, hedModule.HedSchema) || ...
py.isinstance(schema, hedModule.HedSchemaGroup)
schema_obj = schema;
elseif ischar(schema)
schema_obj = hedModule.schema.load_schema_version(schema);
hmod = py.importlib.import_module('hed');
if ischar(schema)
schema_obj = hmod.load_schema_version(schema);
elseif iscell(schema)
schema_obj = hedModule.schema.load_schema_version(py.list(schema));
schema_obj = hmod.load_schema_version(py.list(schema));
elseif py.isinstance(schema, hmod.HedSchema) || ...
py.isinstance(schema, hmod.HedSchemaGroup)
schema_obj = schema;
else
schema_obj = py.None;
end
6 changes: 3 additions & 3 deletions hedmat/hed_wrappers/get_string_objs.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
% To manipulate directly in MATLAB -- convert to a cell array of char
% using string(cell(hedObjs))

amod = py.importlib.import_module('hed.tools.analysis');
event_manager = amod.event_manager.EventManager(events, schema);
tag_manager = amod.hed_tag_manager.HedTagManager(event_manager, ...
hmod = py.importlib.import_module('hed');
event_manager = hmod.EventManager(events, schema);
tag_manager = hmod.HedTagManager(event_manager, ...
py.list(remove_types));
hed_string_objs = ...
tag_manager.get_hed_objs(include_context, replace_defs);
Expand Down
11 changes: 9 additions & 2 deletions hedmat/hed_wrappers/get_tabular_obj.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
function tabular_obj = get_tabular_obj(events, sidecar)
function tabular_obj = get_tabular_obj(events, sidecar, sampling_rate)
% Returns a HED TabularInput object representing events or other columnar item.
%
% Parameters:
% events - string, struct, or TabularInput for tabular data.
% sidecar - Sidecar object, string, or struct or py.None
% sampling_rate - optional sampling rate if struct doesn't have onset
%
% Returns:
% tabular_obj - HEDTools TabularInput object representing tabular data.
Expand All @@ -19,7 +20,13 @@
else
sidecar_obj = get_sidecar_obj(sidecar);
end
if ischar(events) || isstring(events)
if nargin == 1
sampling_rate = NaN;
end
if isstruct(events)
events = struct2string(rectify_events(events, sampling_rate));
tabular_obj = umod.str_to_tabular(events, sidecar_obj);
elseif ischar(events) || isstring(events)
tabular_obj = umod.str_to_tabular(events, sidecar_obj);
else
throw(MException('getTabularInput:Invalid input'))
Expand Down
5 changes: 3 additions & 2 deletions hedmat/hed_wrappers/validate_bids.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
% issue_string - A string with the validation issues suitable for
% printing (has newlines).
%
py.importlib.import_module('hed');
bids = py.hed.tools.BidsDataset(data_root);
hed = py.importlib.import_module('hed')
% amod = py.importlib.import_module('hed.tools.analysis.annotation_util');
bids = hed.get_bids_dataset(data_root);
issues = bids.validate();
issue_string = string(py.hed.get_printable_issue_string(issues));
5 changes: 2 additions & 3 deletions tests/test_hed_wrappers/TestGetHedFactor.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ function importPythonModules(testCase)
json_path = fullfile(data_path, filesep, ...
'task-FacePerception_events.json');
events_path = fullfile(data_path, filesep, 'sub-002', ...
filesep, 'ses-1', filesep, 'EEG', filesep,...
filesep, 'ses-1', filesep, 'eeg', filesep,...
'sub-002_ses-1_task-FacePerception_run-1_events.tsv');
events = fileread(events_path);
sidecar = fileread(json_path);
testCase.tabular_obj = get_tabular_obj(events, sidecar);
testCase.schema = ...
testCase.hmod.schema.load_schema_version('8.2.0');
testCase.schema = testCase.hmod.load_schema_version('8.2.0');
end
end

Expand Down
12 changes: 10 additions & 2 deletions tests/test_hed_wrappers/TestGetSchemaObj.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ function importPythonModules(testCase)
end

methods (Test)
function testAlreadySchema(testCase)
schema = testCase.hmod.load_schema_version('8.2.0');
schema1 = get_schema_obj(schema);
testCase.assertTrue(py.isinstance(schema, ...
testCase.hmod.HedSchema))
testCase.assertTrue(py.isinstance(schema1, ...
testCase.hmod.HedSchema))
end

function testSimpleVersion(testCase)
% Test single version
version = '8.2.0';
schema = get_schema_obj(version);
assertTrue(testCase, py.isinstance(schema, ...
testCase.hmod.schema.HedSchema), ...
testCase.hmod.HedSchema), ...
'The object is not an instance of HedSchema.');
version = char(schema.version);
testCase.verifyEqual(version, '8.2.0', ...
Expand All @@ -29,7 +37,7 @@ function testMultipleVersions(testCase)
version = {'ts:8.2.0', 'score_1.1.0'};
schema = get_schema_obj(version);
assertTrue(testCase, py.isinstance(schema, ...
testCase.hmod.schema.HedSchemaGroup), ...
testCase.hmod.HedSchemaGroup), ...
'The object is not an instance of HedSchemaGroup.');
versions = cell(schema.get_schema_versions());
testCase.verifyEqual(char(versions{1}), 'ts:8.2.0', ...
Expand Down
5 changes: 2 additions & 3 deletions tests/test_hed_wrappers/TestGetStringObjs.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ function importPythonModules(testCase)
json_path = fullfile(data_path, filesep, ...
'task-FacePerception_events.json');
events_path = fullfile(data_path, filesep, 'sub-002', ...
filesep, 'ses-1', filesep, 'EEG', filesep,...
filesep, 'ses-1', filesep, 'eeg', filesep,...
'sub-002_ses-1_task-FacePerception_run-1_events.tsv');
events = fileread(events_path);
sidecar = fileread(json_path);
testCase.tabular_obj = get_tabular_obj(events, sidecar);
testCase.schema = ...
testCase.hmod.schema.load_schema_version('8.2.0');
testCase.schema = testCase.hmod.load_schema_version('8.2.0');
end
end

Expand Down
2 changes: 1 addition & 1 deletion tests/test_hed_wrappers/TestGetTabularObj.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function importPythonModules(testCase)
testCase.json_path = fullfile(data_path, filesep, ...
'task-FacePerception_events.json');
testCase.events_path = fullfile(data_path, filesep, 'sub-002', ...
filesep, 'ses-1', filesep, 'EEG', filesep,...
filesep, 'ses-1', filesep, 'eeg', filesep,...
'sub-002_ses-1_task-FacePerception_run-1_events.tsv');
end
end
Expand Down
2 changes: 0 additions & 2 deletions tests/test_hed_wrappers/TestValidateBids.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
classdef TestValidateBids < matlab.unittest.TestCase

properties
hmod
data_root
end

methods (TestClassSetup)
function importPythonModules(testCase)
testCase.hmod = py.importlib.import_module('hed');
[cur_dir, ~, ~] = fileparts(mfilename("fullpath"));
testCase.data_root = fullfile(cur_dir, filesep, '..', ...
filesep, '..', filesep, 'data', filesep, ...
Expand Down
2 changes: 1 addition & 1 deletion tests/test_hed_wrappers/TestValidateEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function importPythonModules(testCase)
testCase.bad_sidecar = testCase.hmod.Sidecar(bpath);
epath = fullfile(data_path, ...
filesep, 'eeg_ds003645s_hed_demo', filesep, 'sub-002', ...
filesep, 'ses-1', filesep, 'EEG', filesep,...
filesep, 'ses-1', filesep, 'eeg', filesep,...
'sub-002_ses-1_task-FacePerception_run-1_events.tsv');
testCase.event_string = fileread(epath);

Expand Down

0 comments on commit ce6c13e

Please sign in to comment.