
%% Initialization
close all;
clearvars;

%% load data
fname		= fullfile('..','..','data', 'soundripple-subject-001-date-18-12-25-time-09-55-12.csv');
T1			= readtable(fname);
% fname		= fullfile('..','data', 'visualsearch_sub-001_2025-12-17_15-28-05_seed-270243087.csv');

fname		= fullfile('..','..','data', 'soundripple-subject-001-date-18-12-25-time-14-30-02.csv');
T			= readtable(fname);

% T = [T1;T2]
%% Exercise 1
% Read all data from a single directory

RT			= T.reactiontime;
v			= T.velocity;
d			= T.density;
durstat		= T.durstat;
RT			= RT*1000-durstat;


% Not predictive
sel		= RT>100;
RT = RT(sel);
v = v(sel);
d = d(sel);

% Late responses
sel		= RT>2000;
RT(sel) = 3000;

% First let's do a standard analysis, plotting median reaction times
% only
sel = d==0;
x = v(sel);
y = RT(sel);

[mu,ux] = assemble(1000./y,x,'fun',@median);

figure(1);
clf
subplot(131)
plot(1:3,mu(2:4),'o-','MarkerFaceColor','w','LineWidth',2,'MarkerSize',12);
hold on
set(gca,'XTick',1:3,'XTickLabel',ux(2:4));
xlabel('velocity (Hz)');
ylabel('promptness (s^{-1})');
nicegraph;
xlim([0 4]);
% ylim([0 1])
% First let's do a standard analysis, plotting median reaction times
% only
sel = v==0;
x = d(sel);
y = RT(sel);

[mu,ux] = assemble(1000./y,x,'fun',@median);


subplot(132)
plot(1:3,mu(2:4),'o-','MarkerFaceColor','w','LineWidth',2,'MarkerSize',12);
hold on
set(gca,'XTick',1:3,'XTickLabel',ux(2:4));
xlabel('density (cyc/oct)');
ylabel('promptness (s^{-1})');

nicegraph;
xlim([0 4]);


% First let's do a standard analysis, plotting median reaction times
% only
x =[v d];
y = RT;

[mu,ux] = assemble(1000./y,x,'fun',@median);

mu = reshape(mu,4,4);
subplot(133)
imagesc(mu);
nicegraph;
xlabel('velocity');
ylabel('density');
set(gca,'XTick',1:4,'XTickLabel',unique(v),'YTick',1:4,'YTickLabel',unique(d))


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





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

RT			= T.reactiontime;
v			= T.velocity;
d			= T.density;
durstat		= T.durstat;
RT			= RT*1000-durstat;

% Not predictive
sel		= RT>100;
RT = RT(sel);
v = v(sel);
d = d(sel);

% Late responses
sel		= RT>2000;
RT(sel) = 3000;

uv = unique(v);
nv = numel(uv);

ud = unique(d);
nd = numel(ud);

col = lines(3);


figure(3);
clf
subplot(121)
hold on;
xl = sort(-1000./[200 1000]);
for ii = 2:nv
	% subplot(2,2,ii)
sel = v==uv(ii) & d==0;

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

subplot(122)
hold on;
xl = sort(-1000./[200 1000]);
for ii = 2:nd
	% subplot(2,2,ii)
sel = d==ud(ii) & v==0;

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


%%
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');
