Commit a0d94aab authored by Julius Welzel's avatar Julius Welzel
Browse files

Upload New File

parent b997ce7d
Loading
Loading
Loading
Loading
+239 −0
Original line number Diff line number Diff line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   TopoSurfArea differences for juw_el_table // All participants
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Topography analysis
% Author: Julius Welzel // University of Oldenburg, 2019
% Version: 1.0 // 28.02.2019 // Initial setup

% define paths
PATHIN_ERD =  [MAIN '04_Data\ana04_erd\'];
PATHIN_RT = [MAIN '04_Data\'];
PATHIN_EMG = [MAIN '04_Data\ana03_emg\'];

PATHOUT_TOPODIFF = [MAIN '04_Data\ana07_TOPOdiff\'];
setGlobal(PATHOUT_TOPODIFF);

if ~exist(PATHOUT_TOPODIFF)
  mkdir(PATHOUT_TOPODIFF);
end 

list = dir(fullfile([PATHIN_RT 'ana02_clean\*clean*.set']));
SUBJ = extractBefore({list.name},'_');

load([PATHIN_ERD 'ERD_mue.mat']);
load([PATHIN_ERD 'cfg_mue.mat']);
load([PATHIN_EMG 'emg_all.mat']);

% define trails of interest
ME_trials = [cfg.blck_idx{[1 4],1}];
MI_trials = [cfg.blck_idx{[2 3],1}];

SUBJ = str2double(SUBJ);
old = (SUBJ>=80);
young = (SUBJ<80);

%% Zscore data over all participants only for MI trials
z_top = zscore(nanmean(m_rERD_trial,3),[],1);
top_old = mean(z_top(old,:));
top_young = mean(z_top(young,:));

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%                   Extract surface area
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
p_interp = 100; % points to interpolate

% get cartesian coordinates by MIKE X COHEN :D and grid data
[elocsX,elocsY] = pol2cart(pi/180*[cfg.chanlocs.theta],[cfg.chanlocs.radius]);
elocsX = elocsX*-1; %swap left/ right
xlin = linspace(min(elocsX),max(elocsX),p_interp);
ylin = linspace(min(elocsY),max(elocsY),p_interp);
[Xgrid, Ygrid] = meshgrid(xlin,ylin);

%% calculate surface area  of topoplot for each trial
%{
surf_old = {};
surf_young = {};

for s = find(young)
    disp(['SUBJ: ' num2str(SUBJ(s))])
   for t = MI_trials
       
       topo = zscore((m_rERD_trial(s,:,t)));
       surf_st = griddata(elocsX,elocsY,topo,Xgrid,Ygrid,'cubic');
       top_thresh = min(min(topo))*0.5;
       surf_st(surf_st>top_thresh) = NaN;
       surf_young{end+1} = SurfArea(surf_st,Xgrid,Ygrid);
       
   end
end

for s = find(old)
    disp(['SUBJ: ' num2str(SUBJ(s))])
   for t = MI_trials
       
       topo = zscore((m_rERD_trial(s,:,t)));
       surf_st = griddata(elocsX,elocsY,topo,Xgrid,Ygrid,'cubic');
       top_thresh = min(min(topo))*0.5;
       surf_st(surf_st>top_thresh) = NaN;
       surf_old{end+1} = SurfArea(surf_st,Xgrid,Ygrid);
       
   end
end
%}


%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%                   3D topographies of ERD maps
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot topographie interpolated with surface area scatter
figure
ax1 = subplot(2,4,[1 2]);
Z_young = griddata(elocsX,elocsY,top_young,Xgrid,Ygrid,'cubic');
mesh(Xgrid,Ygrid,Z_young) % interpolate
colormap(ax1,c_young_map)
axis tight;hold on;
plot3(elocsX,elocsY,top_young,'.k','MarkerSize',15)
title 'Interpolated Topoplot Young'
hold on
surf(Xgrid,Ygrid,min(min(Z_young))*0.5*ones(size(Z_young)),'FaceAlpha',0.5,'EdgeColor','none')
stlwrite('mod_young3D.stl',Xgrid,Ygrid,Z_young*0.2,'FaceColor',c_young) 

ax2 = subplot(2,4,[5 6]);
Z_old = griddata(elocsX,elocsY,top_old,Xgrid,Ygrid,'cubic');
mesh(Xgrid,Ygrid,Z_old) % interpolate
colormap(ax2,c_old_map)
axis tight;hold on;
plot3(elocsX,elocsY,top_old,'.k','MarkerSize',15)
title 'Interpolated Topoplot Old'
hold on
surf(Xgrid,Ygrid,min(min(Z_old))*0.5*ones(size(Z_old)),'FaceAlpha',0.5,'EdgeColor','none')
stlwrite('mod_old3D.stl',Xgrid,Ygrid,Z_old*0.2,'FaceColor',c_old*255) 

x_values = linspace(0,3,length(surf_old));
subplot(2,4,[3 4 7 8])
pd = fitdist([surf_old{:}]','normal');
y = pdf(pd,x_values);
p1 = plot(x_values,y,'Color',c_old,'LineWidth',2);
hold on
scatter([surf_old{:}],x_values,2,c_old)
hold on
pd = fitdist([surf_young{:}]','normal');
y = pdf(pd,x_values);
p2 = plot(x_values,y,'Color',c_young,'LineWidth',2);
hold on
scatter([surf_young{:}],x_values,2,c_young)
legend([p1 p2],{'OLD','YOUNG'})
title ({'Surface Area of highest 50% ERD','All trials each group'})

% save_fig(gcf,PATHOUT_TOPODIFF,'surf_area_ERD_st','fontsize',15,'figsize',[0 0 35 25]);


%% only 3D topoplots over both groups with topoplot
c_lines = 10;

figure;
ax1 = subplot(1,2,1);
Z_young = griddata(elocsX,elocsY,mean(z_top(young,:)),Xgrid,Ygrid,'cubic');
mesh(Xgrid,Ygrid,Z_young) % interpolate
colormap(ax1,c_map)
axis tight;hold on;    
plot3(elocsX,elocsY,mean(z_top(young,:)),'.k','MarkerSize',15) %plot electrodes
title '3D Topoplot Young'
zlabel 'z-Score'
set(gca,'YTickLabel',[]);
set(gca,'XTickLabel',[]);
hold on
%intersect lines
surf(Xgrid,Ygrid,0*ones(size(Z_young)),'FaceColor','k','FaceAlpha',0.08,'EdgeColor','none')

%topoplot
[~,h] = contourf(Xgrid,Ygrid,Z_young,c_lines);       %# get handle to contourgroup object
colormap (ax1,c_map)
%# change the ZData property of the inner patches
h.ContourZLevel = 1.1*min(min(Z_young));

% OLD 3D PLOT
ax2 = subplot(1,2,2);
Z_old = griddata(elocsX,elocsY,mean(z_top(old,:)),Xgrid,Ygrid,'cubic');
mesh(Xgrid,Ygrid,Z_old) % interpolate
colormap(ax2,c_map)
axis tight;hold on;
plot3(elocsX,elocsY,mean(z_top(old,:)),'.k','MarkerSize',15)
title '3D Topolot Old'
zlabel 'z-Score'
set(gca,'YTickLabel',[]);
set(gca,'XTickLabel',[]);
hold on
%intetsect planes
surf(Xgrid,Ygrid,0*ones(size(Z_old)),'FaceColor','k','FaceAlpha',0.08,'EdgeColor','none')
hold on

%topoplot
[~,h] = contourf(Xgrid,Ygrid,Z_old,c_lines);       %# get handle to contourgroup object
colormap (ax1,c_map)
%# change the ZData property of the inner patches
h.ContourZLevel = 1.1*min(min(Z_old));

% save_fig(gcf,PATHOUT_TOPODIFF,'3D_tops','figtype','.fig')

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%                  Calculate area of intersection
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%{
gd(1).Z= griddata(elocsX,elocsY,mean(z_top(young,:)),Xgrid,Ygrid,'cubic'); %young
gd(2).Z = griddata(elocsX,elocsY,mean(z_top(old,:)),Xgrid,Ygrid,'cubic'); %old
group = {'Young','Old'};

plane_sect = linspace(0,-1.2,6);

for g = 1:length(gd)
    
    figure
    for i = 1:length(plane_sect)
        subplot(2,3,i)
        mesh(Xgrid,Ygrid,gd(g).Z) % interpolate
        hold on
        surf(Xgrid,Ygrid,plane_sect(i)*ones(size(gd(g).Z)),'FaceColor','k','FaceAlpha',0.08,'EdgeColor','none')
        hold on

        % Take the difference between the two surface heights and find the contour
        % where that surface is zero.
        zdiff = gd(g).Z - (plane_sect(i)*ones(size(gd(g).Z)));
        C = contour(Xgrid, Ygrid, zdiff,[0,0]);

        [a, ca] = cont_area(C,Xgrid,Ygrid,gd(g).Z);
        % Visualize the line.
        cl = line(ca.xL, ca.yL, ca.zL, 'Color', 'k', 'LineWidth', 3);
        title(['Area : ' num2str(a) ' at z = ' num2str(plane_sect(i))])
    end
    sgtitle ([group{g} ' participants'])
    save_fig(gcf,PATHOUT_TOPODIFF,['SurfArea_' group{g}],'figtype','.fig')

end
%}
maps_st = zscore(nanmean(m_rERD_trial,3),[],1);

for s = 1:length(SUBJ)
    if SUBJ(s)<50;c_map = c_y_map;else;c_map = c_o_map;end;
    area_z_t_avg(s,:) = inter_area(cfg.chanlocs,maps_st(s,:),24,SUBJ(s),PATHOUT_TOPODIFF,c_map);
end

[m_a_old se_a_old] = mean_SEM(area_z_t_avg(old,:)');
[m_a_young se_a_young] = mean_SEM(area_z_t_avg(young,:)');

AO = shadedErrorBar(1:24,m_a_old,se_a_old,'lineprops',{'-','Color',c_old,'LineWidth',2})
hold on
AY = shadedErrorBar(1:24,m_a_young,se_a_young,'lineprops',{'-','Color',c_young,'LineWidth',2})