Skip to content

Commit 8da5c22

Browse files
author
mdhumphries
committed
updated unit tests
fixed first trial handling in sticky and alternate too
1 parent 4153b61 commit 8da5c22

4 files changed

Lines changed: 46 additions & 6 deletions

File tree

Development/Test_interpolation_of_null_trials.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
clearvars; close all;
55

6-
clearvars; close all;
7-
86
addpath ../Strategy_models/ % must add this path to access strategy models
97
addpath ../Functions/
108

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
% script to unit test strategy models
2+
% latest version: 26/8/2022
3+
4+
clearvars; close all;
5+
6+
addpath ../Strategy_models/ % must add this path to access strategy models
7+
addpath ../Functions/
8+
9+
% import test data CSV as Table
10+
TestData = readtable('UnitTestData.csv','TextType','string');
11+
12+
% strategies to test
13+
strategies = ["go_left", "go_right", "go_cued","go_uncued",...
14+
"win_stay_spatial","win_stay_cued","lose_shift_cued","lose_shift_spatial",...
15+
"alternate","sticky"];
16+
17+
% storage
18+
TestResults = array2table(zeros(height(TestData),numel(strategies)));
19+
TestResults.Properties.VariableNames = cellstr(strategies);
20+
21+
% run all strategy models on Table data
22+
for iTrials = 1:height(TestData)
23+
row_data = TestData(1:iTrials,:);
24+
for index_strategy = 1:numel(strategies)
25+
charStrategy = char(strategies(index_strategy)); % cast as Char for old MATLAB < 2018
26+
27+
% test current strategy model
28+
trial_type = evaluate_strategy(strategies(index_strategy),row_data);
29+
30+
% compare to target result
31+
TestResults.(char(strategies(index_strategy)))(iTrials) = trial_type == TestData.(char(strategies(index_strategy))){iTrials};
32+
end
33+
end
34+
35+
% report any errors
36+
for index_strategy = 1:numel(strategies)
37+
disp([string(TestResults.Properties.VariableNames{index_strategy}) ' passed? ' string(all(TestResults.(char(strategies(index_strategy)))))])
38+
end

Strategy_models/alternate.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
% ALTERNATE checks if subject chose different spatial options on consecutive trials
44
% TYPE = ALTERNATE(TRIAL_DATA) takes the Table of data TRIAL_DATA up to the current trial, and
5-
% returns the TYPE ('success','failure')
5+
% returns the TYPE ('success','failure'); returns 'null' for the first trial
66
%
77
% Mark Humphries 31/3/2022
88

99
number_trials = size(trial_data,1);
1010

11-
if number_trials > 1 && trial_data.Choice(end) ~= trial_data.Choice(end-1)
11+
if number_trials == 1
12+
trial_type = "null";
13+
elseif trial_data.Choice(end) ~= trial_data.Choice(end-1)
1214
% chose different spatial option on consecutive trials, so is a success
1315
trial_type = "success";
1416
else

Strategy_models/sticky.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
% STICKY checks if subject chose same spatial option on consecutive trials
44
% TYPE = STICKY(TRIAL_DATA) takes the Table of data TRIAL_DATA up to the current trial, and
5-
% returns the TYPE ('success','failure')
5+
% returns the TYPE ('success','failure'); returns "null" on the first trial
66
%
77
% Mark Humphries 31/3/2022
88

99
number_trials = size(trial_data,1);
1010

11-
if number_trials > 1 && trial_data.Choice(end) == trial_data.Choice(end-1)
11+
if number_trials == 1
12+
trial_type = "null";
13+
elseif trial_data.Choice(end) == trial_data.Choice(end-1)
1214
% chose same spatial option on consecutive trials, so is a success
1315
trial_type = "success";
1416
else

0 commit comments

Comments
 (0)