clear
clc
global subjname

subject_options;
%% remove subject 14 and subject 24
for sb=1:26
     if strcmp(subjname{sb},'sub14')||strcmp(subjname{sb},'sub24')
                id(sb)=0;
            else
                id(sb)=1;
     end
end
id=logical(id);

%% load the decoding results
for sb = 1:26
    clear Xhat X
    load([matpath,subjname{sb},'_decode_exp_20ms_lp.mat'])
    m0_exp(:,:,sb) = mean((Xhat(:,:,X==0)),3);
    m1_exp(:,:,sb) = mean((Xhat(:,:,X==1)),3);
    
    [h,p,c,t_exp]=ttest2(Xhat(:,:,X==0),Xhat(:,:,X==1),'dim',3);
    diff_exp(:,:,sb) = squeeze(t_exp.tstat);
    
    clear Xhat X
    load([matpath,subjname{sb},'_decode_unexp_20ms_lp.mat'])
    m0_unexp(:,:,sb) = mean((Xhat(:,:,X==0)),3);
    m1_unexp(:,:,sb) = mean((Xhat(:,:,X==1)),3);
    [h,p,c,t_unexp]=ttest2(Xhat(:,:,X==0),Xhat(:,:,X==1),'dim',3);
    diff_unexp(:,:,sb) = squeeze(t_unexp.tstat);
end


%% plot the decoding results
figure
load('cmap.mat')

goodplot(-(m0_unexp(:,:,id)-m1_unexp(:,:,id)),time(T),'Unexpected')
colormap(cmap)
largest_cluster(m0_unexp(:,:,id),m1_unexp(:,:,id),time(T))

figure
goodplot(-(m0_exp(:,:,id)-m1_exp(:,:,id)),time(T),'Expected')
colormap(cmap)

%%
[h,p,c,t]  = ttest(diff_exp(:,:,id),diff_unexp(:,:,id),'dim',3);
fig = figure;
goodplot(t.tstat,time(T),'Unexpected > Expected')
caxis([-400 400])
%%
figure
show_dig(m0_unexp(:,:,id)*100,time(T),0.2,'-r')
hold on
show_dig(m1_unexp(:,:,id)*100,time(T),0.2,'-b')
ylim([-10 10])
title('Unexpected')

%%
figure
show_dig(m0_exp(:,:,id)*100,time(T),0.2,'-r')
hold on
show_dig(m1_exp(:,:,id)*100,time(T),0.2,'-b')
ylim([-10 10])
title('Expected')


