Skip to content

Commit

Permalink
Merge branch 'develop' into 758-pspm_expand_epochs
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikbach authored Oct 28, 2024
2 parents d4bd1ec + 6aba0b6 commit cd43295
Show file tree
Hide file tree
Showing 6 changed files with 931 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/pspm_eye.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@
Y = settings.lateral.char.r;
end
case 'cell'
Y{Y=='l'} = settings.lateral.char.l;
Y{Y=='r'} = settings.lateral.char.r;
Y{Y=='lr'} = settings.lateral.char.c;
Y{Y=='rl'} = settings.lateral.char.c;
%Y{Y=='l'} = settings.lateral.char.l;
%Y{Y=='r'} = settings.lateral.char.r;
%Y{Y=='lr'} = settings.lateral.char.c;
%Y{Y=='rl'} = settings.lateral.char.c;
Y(strcmp(Y, 'l')) = {settings.lateral.char.l};
Y(strcmp(Y, 'r')) = {settings.lateral.char.r};
Y(strcmp(Y, 'lr')) = {settings.lateral.char.c};
Y(strcmp(Y, 'rl')) = {settings.lateral.char.c};

end
case 'char2cell'
% Examples
Expand Down
211 changes: 211 additions & 0 deletions test/pspm_check_data_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
classdef pspm_check_data_test < matlab.unittest.TestCase
% ● Description
% unittest class for pspm_check_data, PsPM TestEnvironment
% ● History
% Written in 2024 by Bernhard Agoué von Raußendorf
% ● Developer's notes



methods (Test)
function testValidDataTest(testCase)
% Test pspm_check_data with valid data and infos structures.

% Generate valid test data
duration = 10; % seconds
channels{1}.chantype = 'scr';
channels{1}.sr = 100; % sampling rate
channels{1}.units = 'microsiemens';
dataStruct = pspm_testdata_gen(channels, duration);

data = dataStruct.data;
infos = dataStruct.infos;

[sts, dataOut] = pspm_check_data(data, infos);

testCase.verifyEqual(sts, 1);
testCase.verifyNotEmpty(dataOut);
end
function channeltype2chantypeTest(testCase)
% Test pspm_check_data channeltype to chantype

% Generate valid test data
duration = 10;
channels{1}.chantype = 'scr';
channels{1}.sr = 100; % sampling rate
channels{1}.units = 'microsiemens';
dataStruct = pspm_testdata_gen(channels, duration);

% change chantype to channeltype
dataStruct.data{1, 1}.header.channeltype = dataStruct.data{1, 1}.header.chantype;
dataStruct.data{1, 1}.header = rmfield(dataStruct.data{1, 1}.header, 'chantype');

data = dataStruct.data;
infos = dataStruct.infos;

[~ , dataOut] = pspm_check_data(data, infos);

%dataOut{1, 1}.header
testCase.verifyTrue(isfield(dataOut{1, 1}.header,"chantype"))
end
function MissingHeaderFieldsTest(testCase)
% Test pspm_check_data with missing required fields in the header.

% Generate valid test data
duration = 10;
channels{1}.chantype = 'scr';
channels{1}.sr = 100;
channels{1}.units = 'microsiemens';
dataStruct = pspm_testdata_gen(channels, duration);

data = dataStruct.data;
infos = dataStruct.infos;

% Remove the 'chantype' field from the header
data{1}.header = rmfield(data{1}.header, 'chantype');

[sts, dataOut] = pspm_check_data(data, infos);

% Verify that the status indicates an error (-1)
testCase.verifyEqual(sts, -1);
% Optionally, verify that an appropriate warning was issued
% (requires capturing warnings)
end
function UnknownChannelTypeTest(testCase)
% Test pspm_check_data with an unknown channel type.

duration = 10;
channels{1}.chantype = 'scr';
channels{1}.sr = 100;
channels{1}.units = 'microsiemens';
dataStruct = pspm_testdata_gen(channels, duration);

dataStruct.data{1, 1}.header.chantype = 'unknow channeltype';

data = dataStruct.data;
infos = dataStruct.infos;

[sts, ~ ] = pspm_check_data(data, infos);


testCase.verifyEqual(sts, -1); % maybe check over warrning ID?
end
function EmptyDataTest(testCase)
% Test pspm_check_data with empty data field.

% Generate valid test data
duration = 10;
channels{1}.chantype = 'scr';
channels{1}.sr = 100;
channels{1}.units = 'microsiemens';
dataStruct = pspm_testdata_gen(channels, duration);

data = dataStruct.data;
infos = dataStruct.infos;

data{1,1}.data = [];

[sts, dataOut] = pspm_check_data(data, infos);

testCase.verifyEqual(sts, -1); % maybe check over warrning ID?
end
function IncorrectDataOrientationTest(testCase)
% Test pspm_check_data with data in incorrect orientation.

% Generate valid test data
duration = 10;
channels{1}.chantype = 'scr';
channels{1}.sr = 100;
channels{1}.units = 'microsiemens';
dataStruct = pspm_testdata_gen(channels, duration);

data = dataStruct.data;
infos = dataStruct.infos;

% Data converted into a row vector
data{1}.data = data{1}.data';

[sts, dataOut] = pspm_check_data(data, infos);

% Verify pspm_check_data transposed the data back and sts remains 1
testCase.verifyEqual(size(dataOut{1}.data, 1), length(data{1}.data));
testCase.verifyEqual(size(dataOut{1}.data, 2), 1);
testCase.verifyEqual(sts, 1);
end
function DataOutOfRangeTest(testCase)
% Test pspm_check_data with data exceeding infos.duration.


% Generate valid test data
duration = 10;
channels{1}.chantype = 'scr';
channels{1}.sr = 100;
channels{1}.units = 'microsiemens';
dataStruct = pspm_testdata_gen(channels, duration);

data = dataStruct.data;
infos = dataStruct.infos;

% Extend the data beyond the expected duration
extraSamples = 50; % number of extra samples
data{1}.data = [data{1}.data; zeros(extraSamples, 1)];

[sts, DataOut ] = pspm_check_data(data, infos);


% Data was not changed
testCase.verifyEqual(size(data{1}.data,1), size(DataOut{1}.data,1))
testCase.verifyEqual(sts, -1);
end
function InvalidDataTypesTest(testCase)
% Test pspm_check_data with invalid data types in the data field.

duration = 10;
channels{1}.chantype = 'scr';
channels{1}.sr = 100;
channels{1}.units = 'microsiemens';
dataStruct = pspm_testdata_gen(channels, duration);

data = dataStruct.data;
infos = dataStruct.infos;

% Set data field to a non-numeric type string
data{1}.data = 'invalid data';

[sts, ~] = pspm_check_data(data, infos);
testCase.verifyEqual(sts, -1);

end

function MultipleChannelsTest(testCase)
% Test pspm_check_data with multiple channels.
% This checks if the function can handle and process multiple channels correctly.

duration = 10;
channels{1}.chantype = 'scr';
channels{1}.sr = 100;
channels{1}.units = 'microsiemens';
channels{2}.chantype = 'hr';
channels{2}.sr = 200;
channels{2}.units = 'mV';

dataStruct = pspm_testdata_gen(channels, duration);

data = dataStruct.data;
infos = dataStruct.infos;

[sts, dataOut] = pspm_check_data(data, infos);

% Verify that sts is 1, indicating successful validation of multiple channels
testCase.verifyEqual(sts, 1);
% Verify that both channels are present in the output
testCase.verifyEqual(numel(dataOut), 2);
% Verify that each channel has the correct sampling rate and units
testCase.verifyEqual(dataOut{1}.header.sr, 100);
testCase.verifyEqual(dataOut{1}.header.units, 'microsiemens');
testCase.verifyEqual(dataOut{2}.header.sr, 200);
testCase.verifyEqual(dataOut{2}.header.units, 'mV');
end

end
end
163 changes: 163 additions & 0 deletions test/pspm_eye_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
classdef pspm_eye_test < matlab.unittest.TestCase
% Unit tests for the pspm_eye function

methods (Test)

function test_lr2c_char(testCase)
% Test 'lr2c' feature with character inputs

% Test inputs
input1 = 'l';
input2 = 'R';
input3 = 'lr';
input4 = 'rL';

% Expected outputs
expected1 = 'l';
expected2 = 'r';
expected3 = 'c';
expected4 = 'c';

% Verify conversions
testCase.verifyEqual(pspm_eye(input1, 'lr2c'), expected1);
testCase.verifyEqual(pspm_eye(input2, 'lr2c'), expected2);
testCase.verifyEqual(pspm_eye(input3, 'lr2c'), expected3);
testCase.verifyEqual(pspm_eye(input4, 'lr2c'), expected4);
end

function test_lr2c_cell(testCase)
% Test 'lr2c' feature with cell array inputs

% Test input
input = {'L', 'r', 'Lr', 'rl'};
% Expected output
expected = {'l', 'r', 'c', 'c'};

% Verify conversion
result = pspm_eye(input, 'lr2c');
testCase.verifyEqual(result, expected);
end

function test_char2cell_single(testCase)
% Test 'char2cell' feature with single character inputs

% Test inputs
input1 = 'l';
input2 = 'R';
input3 = 'C';
input4 = 'lR';

% Expected outputs
expected1 = {'l'};
expected2 = {'r'};
expected3 = {'l', 'r'};
expected4 = {'l', 'r'};


% Verify conversions
testCase.verifyEqual(pspm_eye(input1, 'char2cell'), expected1);
testCase.verifyEqual(pspm_eye(input2, 'char2cell'), expected2);
testCase.verifyEqual(pspm_eye(input3, 'char2cell'), expected3);
testCase.verifyEqual(pspm_eye(input4, 'char2cell'), expected4);
end

function test_char2cell_combined(testCase)
% Test 'char2cell' feature with combined characters

% Test inputs
input1 = 'lr';
input2 = 'RL';

% Expected output
expected = {'l', 'r'};

% Verify conversions
testCase.verifyEqual(pspm_eye(input1, 'char2cell'), expected);
testCase.verifyEqual(pspm_eye(input2, 'char2cell'), expected);
end

function test_channel2lateral_char(testCase)
% Test 'channel2lateral' feature with single channel name inputs

% Test inputs
input1 = 'pupil_l';
input2 = 'gaze_x_r';
input3 = 'gaze_y_lr';
input4 = 'something_y_rl';
input5 = 'pupil_c';

% Expected outputs
expected1 = 'l';
expected2 = 'r';
expected3 = 'c';
expected4 = 'c';
expected5 = 'c';

% Verify conversions
testCase.verifyEqual(pspm_eye(input1, 'channel2lateral'), expected1);
testCase.verifyEqual(pspm_eye(input2, 'channel2lateral'), expected2);
testCase.verifyEqual(pspm_eye(input3, 'channel2lateral'), expected3);
testCase.verifyEqual(pspm_eye(input4, 'channel2lateral'), expected4);
testCase.verifyEqual(pspm_eye(input5, 'channel2lateral'), expected5);
end

function test_channel2lateral_cell(testCase)
% Test 'channel2lateral' feature with cell array of channel names

% Test input
input = {'pupil_r', 'gaze_y_l', 'gaze_x_rl', 'pupil_c', 'gaze_x_lr'};

% Expected output
expected = {'r', 'l', 'c', 'c', 'c'};

% Verify conversion
result = pspm_eye(input, 'channel2lateral');
testCase.verifyEqual(result, expected);
end

function test_channel2lateral_char_wrong(testCase)
% Test 'channel2lateral' feature with single wrong channel name

input1 = 'pupil_';
expected1 = {};
% Verify conversions
testCase.verifyEqual(pspm_eye(input1, 'channel2lateral'), expected1);
end

function test_channel2lateral_cell_wrong(testCase)
% Test 'channel2lateral' feature with cell array of wrong channel names

% Test input
input = {'pupilr', 'gaze_yl', 'gaze_x_r_l', 'pupil_C', 'gaze_x_lR'};

% Expected output
expected = {{}, {}, 'l', 'c', 'c'};

% Verify conversion
result = pspm_eye(input, 'channel2lateral');
testCase.verifyEqual(result, expected);
end

function test_channel2lateral_cell_char_UPPERCASE(testCase)
% Test 'channel2lateral' feature with cell array of channel names

% Test input
input1 = {'pupil_x_R', 'gaze_y_LR', 'gaze_x_RL', 'pupil_C', 'gaze_x_L'};
input2 = 'pupil_x_RL';

% Expected output
expected1 = {'r', 'c', 'c', 'c', 'l'};
expected2 = 'c';

% Verify conversion

testCase.verifyEqual(pspm_eye(input1, 'channel2lateral'), expected1);

result = pspm_eye(input2, 'channel2lateral');
testCase.verifyEqual(result, expected2);

end


end
end
Loading

0 comments on commit cd43295

Please sign in to comment.