
%% Initialization
close all;
clearvars;

%% load data
fname		= fullfile('..','..','data', 'visualsearch_sub-001_2025-12-17_15-28-05_seed-270243087.csv');
% fname		= fullfile('..','data', 'visualsearch_sub-001_2025-12-17_15-28-05_seed-270243087.csv');
T			= readtable(fname);


%% Exercise 1
% Read all data from a single directory

RT			= T.rt;
KP			= T.respKey;
Tpresent	= T.targetPresent;
N			= T.setSize;
C			= T.searchType;

% Only correct
sel		= KP==Tpresent ;
[sum(sel) sum(~sel) numel(sel) 100*sum(~sel)/numel(sel)] % some details on accuracy
RT		= RT(sel);
N		= N(sel);
C		= C(sel);

% First let's do a standard analysis, plotting median reaction times
X	= [C N];
Y	= RT;
[mu,ux] = assemble(Y,X,'fun',@median);

figure(1);
clf

sel = ux(:,1)==0;
x = ux(sel,2);
y = mu(sel);
h(1) = plot(x,y,'o-','MarkerFaceColor','w','LineWidth',2,'MarkerSize',12);
hold on
sel = ux(:,1)==1;
x = ux(sel,2);
y = mu(sel);
h(2) = plot(x,y,'s-','MarkerFaceColor','w','LineWidth',2,'MarkerSize',12);
nicegraph;

set(gca,'XTick',unique(X));
xlabel('set size');
ylabel('reaction time (ms)');

xlim([3 17]);

legend(h,{'pop-out','conjunctive'},'Location','NW');


% optional, save figure
fname = fullfile('..','..','images',[mfilename '_standard_ori.svg']);
savegraph(fname,'svg');


%% Exercise 2
% Read all data from a single directory

RT			= T.rt;
KP			= T.respKey;
Tpresent	= T.targetPresent;
N			= T.setSize;
C			= T.searchType;

% Only correct, only when target is present, and only for pop-up
sel		= KP==Tpresent & Tpresent==1 & C==0;
[sum(sel) sum(~sel) numel(sel) 100*sum(~sel)/numel(sel)] % some details on accuracy
RT		= RT(sel);
N		= N(sel);
C		= C(sel);

figure(2);
clf
subplot(1,2,1); % make two plots in this figure, the first one for reaction times (should be a skewed distribution)
plotpost(RT,'xlim',[0 1.5],'showCurve',false); % histogram from custom function, set xlim to meaningful values
xlabel('reaction time (s)'); % always label your axes!
ylabel('proportion of responses'); % always use units if you are able (proportion does not have a unit, and also the quantities are meaningless, as they should sum to 1)
nicegraph; % custom function to improve the layout of figures by default (e.g. large fonts, ticks outside of graph)

% statistics - null-hypothesis testing for normality
h			= lillietest(RT);
normalstr	= {'data is normally distributed','data is not normally distributed'}; % let's put in words whether the data is normally distributed or not
title(normalstr(double(h)+1)); % and put this text as a title of the graph
% Note that you have more statistics available from plotpost: the mean and
% the 95% Highest Density Interval (comparable to the confidence interval,
% but more easily interpretable). The last two measures are point-estimates
% describing the full distribution of the data.

subplot(1,2,2); % second subplot to plot inverse of reaction time = promptness. Check whether this looks like a normal distribution. 
Promptness = 1./RT; % promptness (in s^{-1}, Hz)
plotpost(Promptness,'xlim',[0.5 3],'showCurve',false);
xlabel('promptness (s^{-1})');
ylabel('proportion');
nicegraph;

h = lillietest(Promptness);
title(normalstr(double(h)+1));


% optional, save figure
fname = fullfile('..','..','images',[mfilename '_promptness_ori.svg']);
savegraph(fname,'svg');



%% Exercise 3
% Read all data from a single directory

RT			= T.rt;
KP			= T.respKey;
Tpresent	= T.targetPresent;
N			= T.setSize;
C			= T.searchType;

% Only correct, only when target is present, and only for pop-up
sel		= KP==Tpresent & C==1;
RT		= RT(sel);
N		= N(sel);

uN = unique(N);
n = numel(uN);

col = lines(4);
figure(3);
clf
hold on;
xl = sort(-1000./[500 1000]);
for ii = 1:n
	% subplot(2,2,ii)
sel = N==uN(ii);

[Y, XRECIP, STATS,h(ii)] = reciprobitplot(RT(sel),'regress',true,'Color',col(ii,:),'xlim',xl);
hold on;
end

str = num2str(uN);
legend(h,str,'Location','NW');

%%
return
%% Correct
% We need to determine whether the responses were accurate. Later we will
% check for the correct responses what the reaction times are. Let's do
% this in a similar way as in visualsearchexp.m
correct		= KP==Tpresent; % PEOPLE SHOULD PRESS ONLY 0 OR 1
% How many are correct?
ncorrect	= sum(correct); % Why does this work? Because correct is a selection/logical vector containing only 0s and 1s. 
ntotal		= numel(RT); % total number of trials
nincorrect	= ntotal-ncorrect;
disp([ncorrect ntotal ncorrect]);
perc		= ncorrect/ntotal*100;
str			= ['The participant was correct in ' num2str(perc,3) '% of the trials.'];
disp(str);

%% Select
RT			= RT(correct);
N			= N(correct);
C		= C(correct);
Tpresent	= Tpresent(correct);


%% Plot distributions for every condition
uN = unique(N);
nN = numel(uN);
uT = unique(Tpresent);
nT = numel(uT);

figure(2);
clf
hold on
cnt = 0;
for tt = 1:nT
	for nn = 1:nN
		cnt = cnt+1;
		sel = Tpresent==uT(tt) & N==uN(nn);
		y	= RT(sel);
		x	= linspace(0.3,1.5,100);
		f	= ksdensity(y,x,'Bandwidth',0.03);
		f	= f./max(f);
		subplot(1,2,tt)
		plot(x,f+nn/2);
		hold on
		
		text(1.6,nn/2,num2str(uN(nn)))
		
	end
end
for ii = 1:2
	subplot(1,2,ii)
	nicegraph;
	xlim([0.3 2]);
% text(1.6,nn/2+0.5,'set size')
	
end

% % %% Select only for IN 
% sel = Tpresent==0;
% RT			= RT(sel);
% N			= N(sel);
% Csearch		= Csearch(sel);
% sum(sel)

figure

return
%% Select only for fast responses
sel			= RT<1.5;
RT			= RT(sel);
N			= N(sel);
C		= C(sel);

%% Determine the mean for every condition
X			= [N C];
Y			= RT;
[A,U]		= assemble(Y,X,'fun',@mean); % custom function to apply function/mean to data Y from all subsets/conditions X
l			= length(A); % longest size of vector

% reshape the matrix so that we can easily plot the data
U2			= reshape(U(:,2),2,l/2); % in U2, the top row will be 0, the bottom row will be 1
U1			= reshape(U(:,1),2,l/2); % in U1, the colums will indicate set size: first column - 4, last column - 16
A			= reshape(A,2,l/2);

[SD,U]		= assemble(Y,X,'fun',@std); % custom function to apply function/mean to data Y from all subsets/conditions X
[Nsd,U]		= assemble(ones(size(Y)),X,'fun',@sum); % custom function to apply function/mean to data Y from all subsets/conditions X
SD = 1.96*SD./sqrt(Nsd)/2;
SD			= reshape(SD,2,l/2);

figure(3);
clf;
hold on

col = lines(2);
errorpatch(U1(1,:)',A(1,:)',SD(1,:)',col(1,:));
errorpatch(U1(2,:)',A(2,:)',SD(2,:)',col(2,:));
h(1) = plot(U1(1,:)',A(1,:)','o','MarkerFaceColor','w','LineWidth',1.5,'MarkerSize',15,'Color',col(1,:));
h(2) = plot(U1(2,:)',A(2,:)','o','MarkerFaceColor','w','LineWidth',1.5,'MarkerSize',15,'Color',col(2,:));

sel = C==0;
plot(N(sel)+0.2*randn(size(N(sel))),RT(sel),'*','Color',col(1,:));
hold on
sel = C==1;
plot(N(sel)+0.2*randn(size(N(sel))),RT(sel),'*','Color',col(2,:));
xlim([min(N)-4 max(N)+4]);
% ylim([0.5 1.2]);
xlabel('set size');
ylabel('average reaction time (s)');
set(gca,'XTick',unique(N)); % set some properties of the current axis, here where the ticks are located
nicegraph;

legend(h,{'pop-up','conjunctive search'}); % 
savegraph('visualsearch','png');




figure(4);
clf;
hold on

col = lines(2);
errorpatch(U1(1,:)',A(1,:)',SD(1,:)',col(1,:));
errorpatch(U1(2,:)',A(2,:)',SD(2,:)',col(2,:));
h(1) = plot(U1(1,:)',A(1,:)','o','MarkerFaceColor','w','LineWidth',1.5,'MarkerSize',15,'Color',col(1,:));
h(2) = plot(U1(2,:)',A(2,:)','o','MarkerFaceColor','w','LineWidth',1.5,'MarkerSize',15,'Color',col(2,:));

xlim([min(N)-4 max(N)+4]);
% ylim([0.5 1.2]);
xlabel('set size');
ylabel('average reaction time (s)');
set(gca,'XTick',unique(N)); % set some properties of the current axis, here where the ticks are located
nicegraph;

legend(h,{'pop-up','conjunctive search'}); % 
savegraph('visualsearch2','png');

%%
figure(5)
clf
hold on

sel = C==0;
x = N(sel);
y = RT(sel);
b = regstats(y,x,'linear');
plot([min(N)-2 max(N)+2],b.beta(2)*[min(N)-2 max(N)+2]+b.beta(1),'-','Color',col(1,:),'LineWidth',1.5);
errorbar(U1(1,:)',A(1,:)',SD(1,:)','o','MarkerFaceColor','w','LineWidth',1.5,'MarkerSize',15,'Color',col(1,:));

sel = C==1;
x = N(sel);
y = RT(sel);
b = regstats(y,x,'linear');
plot([min(N)-2 max(N)+2],b.beta(2)*[min(N)-2 max(N)+2]+b.beta(1),'-','Color',col(2,:),'LineWidth',1.5);
errorbar(U1(2,:)',A(2,:)',SD(2,:)','o','MarkerFaceColor','w','LineWidth',1.5,'MarkerSize',15,'Color',col(2,:));


xlim([min(N)-4 max(N)+4]);
% ylim([0.5 1.2]);
xlabel('set size');
ylabel('average reaction time (s)');
set(gca,'XTick',unique(N)); % set some properties of the current axis, here where the ticks are located
nicegraph;


savegraph('visualsearch3','png');
