
load('Fig3DoubleDissociationEffect')

for ntp = 1:49
    [h,p(ntp),dc,stats] = ttest2(-AMY_structure.real_t_val_aro(ntp,:) , -AMY_structure.real_t_val_discrimination(ntp,:)  ,'tail', 'right') ; %  
    t_val(ntp) = stats.tstat ;
end
h = p<=0.05;
[labeledA, numRegions] = bwlabel(h , 8);
for n_cluster = 1:numRegions , t_val(labeledA==n_cluster)=sum(t_val(labeledA==n_cluster));end
 
for nperm = 1:1000
    for ntp = 1:49
        [h,p(ntp),dc,stats] = ttest2(-AMY_structure.t_val_aro_permutation(ntp,:,nperm) , -AMY_structure.t_val_dis_permutation(ntp,:,nperm) ,'tail', 'right') ; %   
        t_val_shuffle(ntp) = stats.tstat ;
    end
    h = p<=0.05;
    [labeledA, numRegions] = bwlabel(h , 8);
    for n_cluster = 1:numRegions , t_val_shuffle(labeledA==n_cluster)=sum(t_val_shuffle(labeledA==n_cluster));end
    
    
    pool_shuffle_tval_max(nperm) = max(t_val_shuffle,[],'omitnan');
    pool_shuffle_tval_min(nperm) = min(t_val_shuffle,[],'omitnan');
    
end
figure, plot(linspace(-0.25,0.25,49) , mean( t_val<=pool_shuffle_tval_max' ))
 
  figure, shadedErrorBar(linspace(-0.25,0.25,49) ,  mean( -AMY_structure.real_t_val_aro , 2 ) , std( -AMY_structure.real_t_val_aro ,[], 2 )/sqrt(6) , {'-b','LineWidth',2,'MarkerFaceColor','w'},0.01 )
hold on, shadedErrorBar(linspace(-0.25,0.25,49) ,  mean( -AMY_structure.real_t_val_discrimination, 2 ) , std( -AMY_structure.real_t_val_discrimination ,[], 2 )/sqrt(6) ,  {'-r','LineWidth',2,'MarkerFaceColor','w'},0.01 )
set(gca,'LineWidth',2), xlim([-0.25 0.25]), ylim([-1.5 1.5])
ylabel('t-value'), xlabel('Time from aSWR( s)')

% for the discrimination effect 

t_val = []; 
for ntp = 1:49 
     [h,p(ntp),dc,stats] = ttest(-HPC_structure.real_t_val_aro(ntp,:) , -HPC_structure.real_t_val_discrimination(ntp,:)  ,'tail', 'left') ; %
         t_val(ntp) = stats.tstat ;
end
h = p<0.05;
[labeledA, numRegions] = bwlabel(h , 8);
for n_cluster = 1:numRegions , t_val(labeledA==n_cluster)=sum(t_val(labeledA==n_cluster));end

for nperm = 1:1000
    t_val_shuffle=[];
    for ntp = 1:49
        [h,p(ntp),dc,stats] = ttest(-HPC_structure.t_val_aro_permutation(ntp,:,nperm) , -HPC_structure.t_val_dis_permutation(ntp,:,nperm) ,'tail', 'left') ; % 
        t_val_shuffle(ntp) = stats.tstat ;
    end
    h = p<=0.05;
    [labeledA, numRegions] = bwlabel(h , 8);
    for n_cluster = 1:numRegions , t_val_shuffle(labeledA==n_cluster)=sum(t_val_shuffle(labeledA==n_cluster));end
    
    
    pool_shuffle_tval_max(nperm) = max(t_val_shuffle,[],'omitnan');
    pool_shuffle_tval_min(nperm) = min(t_val_shuffle,[],'omitnan');
    
end
 figure, plot(linspace(-0.25,0.25,49) , mean( t_val>=pool_shuffle_tval_min' ))



 figure, plot(mean( -HPC_structure.real_t_val_aro, 2 , 'omitnan'))
ylim([-1.5 1.5])
 hold on, plot(-mean( HPC_structure.real_t_val_discrimination, 2 , 'omitnan'))
 
 figure, shadedErrorBar(linspace(-0.25,0.25,49) ,  mean( -HPC_structure.real_t_val_aro , 2 ) , std( -HPC_structure.real_t_val_aro ,[], 2 )/sqrt(6) , {'-b','LineWidth',2,'MarkerFaceColor','w'},0.01 )
hold on, shadedErrorBar(linspace(-0.25,0.25,49) ,  mean( -HPC_structure.real_t_val_discrimination, 2 ) , std( -HPC_structure.real_t_val_discrimination ,[], 2 )/sqrt(6) ,  {'-r','LineWidth',2,'MarkerFaceColor','w'},0.01 )
set(gca,'LineWidth',2), xlim([-0.25 0.25]), ylim([-1.5 1.5])
ylabel('t-value'), xlabel('Time from aSWR( s)')
 

