Skip to content

Commit

Permalink
Merge pull request #25 from sccn/master
Browse files Browse the repository at this point in the history
merge from upstream
  • Loading branch information
nguyen-td authored Feb 28, 2023
2 parents 902b3da + fa9b779 commit 18e826e
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pop_roi_connectplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
plotrow = [1 1];
uigeom = { [1 1] [1 1] [1 0.5 0.2] 1 [1 1] plotrow [1] [3.3 1.5 1.1 1.2] plotrow };
uilist = {{ 'style' 'text' 'string' 'Select a measure to plot' 'fontweight' 'bold'} ...
{ 'style' 'popupmenu' 'string' {splot.label} 'callback' cb_select 'value' 4 'tag' 'selection' } ...
{ 'style' 'popupmenu' 'string' {splot.label} 'callback' cb_select 'value' 1 'tag' 'selection' } ...
{ 'style' 'text' 'string' 'Only from/to region' } ...
{ 'style' 'popupmenu' 'string' areaNames 'tag' 'seed_region'} ...
{ 'style' 'text' 'string' 'Frequency range in Hz [min max]:'} ...
Expand Down
97 changes: 62 additions & 35 deletions roi_connect_compare.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@
[g,addopts] = finputcheck(varargin, { 'measure' 'string' {} 'MIM';
'freqrange' {'real' 'cell' } [] [];
'quantile' 'real' { } [];
'plotsubjmat' 'string' { 'on' 'off' } 'off'; ...
'threshold' 'real' { } 1.1;
'outputdir' 'string' { } '.';
'network' 'string' { 'on' 'off' } 'off'; ...
'addrois' 'string' { 'on' 'off' } 'off'; ...
'pmask' 'string' { 'on' 'off' } 'off' }, 'pop_roi_connectplot', 'ignore');
if ischar(g), error(g); end
if isempty(g.freqrange)
Expand All @@ -76,39 +79,48 @@

% compute stats
% -------------
freqTextSig = cell(1, length(g.freqrange));
freqTextSig(:) = { '' };
if strcmpi(g.pmask, 'on')
pcond = std_stat(dataCell', 'condstats', 'on', 'method', 'permutation', 'naccu', 200, 'mcorrect', 'fdr');
%pcond = std_stat(dataCell', 'condstats', 'on', 'method', 'permutation', 'naccu', 200); %, 'mcorrect', 'fdr'); pcond{1} = pcond{1} < 0.5;
pcond = pcond{1};

for iDiag = 1:size(pcond,1), pcond(iDiag, iDiag) = 1; end
if ~any(pcond(:) < 1)
fprintf(2,'Nothing significant\n');
return;
else
for iCond = 1:length(datCond(:))
dataCell{iCond} = bsxfun(@times,dataCell{iCond}, pcond<1 );
freqTextSig = {};
for iFreq = 1:length(g.freqrange)
pcond = std_stat(dataCell(iFreq,:)', 'condstats', 'on', 'method', 'permutation', 'naccu', 50, 'mcorrect', 'fdr');
%pcond = std_stat(dataCell', 'condstats', 'on', 'method', 'permutation', 'naccu', 200); %, 'mcorrect', 'fdr'); pcond{1} = pcond{1} < 0.5;
pcond = pcond{1};

for iDiag = 1:size(pcond,1), pcond(iDiag, iDiag) = 1; end
if all(pcond(:) == 1)
fprintf('Frequency %1.1f to %1.1f -> Nothing significant\n', g.freqrange{iFreq}(1), g.freqrange{iFreq}(2));
freqTextSig{iFreq} = 'ns';
else
fprintf(2, 'Frequency %1.1f to %1.1f -> Something significant\n', g.freqrange{iFreq}(1), g.freqrange{iFreq}(2));
freqTextSig{iFreq} = 'significant';
for iCond = 1:length(datCond(:))
dataCell{iFreq, iCond} = bsxfun(@times,dataCell{iFreq, iCond}, pcond<1 );
end
end
end
end

% plot matrices
% -------------
for iFreq = 1:length(g.freqrange)
figure('position', [275 1075 1725 262]);
numDat = size(dataCell{1},3);
numRows = length(dataCell(:));
cl = [0.03 0.1];
%cl = [0.0 1];
for iCond = 1:numRows
for iDat = 1:numDat
subplot(numRows, numDat, iDat+numDat*(iCond-1));
imagesc(dataCell{iCond}(:,:,iDat));
caxis(cl);
axis off;
if strcmpi(g.plotsubjmat, 'on')
for iFreq = 1:length(g.freqrange)
figure('position', [275 1075 1725 262]);
numDat = size(dataCell{1},3);
numRows = length(dataCell(:));
cl = [0.03 0.1];
%cl = [0.0 1];
for iCond = 1:numRows
for iDat = 1:numDat
subplot(numRows, numDat, iDat+numDat*(iCond-1));
imagesc(dataCell{iCond}(:,:,iDat));
clim(cl);
axis off;
end
end
textsc('title', sprintf('Connectivity %g-%g Hz %s', g.freqrange{iFreq}(1), g.freqrange{iFreq}(2)), freqTextSig{iFreq});
end
textsc('title', sprintf('Connectivity %g-%g Hz', g.freqrange{iFreq}(1), g.freqrange{iFreq}(2)));
end

% plot surfaces
Expand Down Expand Up @@ -136,21 +148,36 @@
if strcmpi(g.network, 'on')
tab1 = readtable('NGNetworkROIs_v4.txt','delimiter', char(9));
for iCond = 1:length(dataCell(:))
[~,net,dataCell{iCond}] = roi_definenetwork(EEG(1), tab1, 'connectmat', dataCell{iCond}, 'ignoremissing', 'on'); % adding missing ROIs
if strcmpi(g.addrois, 'on')
tab2 = readtable('NGNetworkROIs_area_definition_v2.txt','delimiter', char(9));
[EEG(1),net,dataCell{iCond}] = roi_definenetwork(EEG(1), tab1, 'addrois', tab2, 'connectmat', dataCell{iCond}, 'ignoremissing', 'on'); % adding missing ROIs
else
[EEG(1),net,dataCell{iCond}] = roi_definenetwork(EEG(1), tab1, 'connectmat', dataCell{iCond}, 'ignoremissing', 'on'); % adding missing ROIs
end
end
for iNetwork = 1:length(net)
netTmp = net([iNetwork iNetwork iNetwork]);
netTmp(1).name = [ netTmp(1).name 'Condition 1' ];
netTmp(2).name = [ netTmp(2).name 'Condition 2' ];
netTmp(2).name = [ netTmp(2).name 'Difference' ];
if size(dataCell,2) > 1
for iNetwork = 1:length(net)
netTmp = net([iNetwork iNetwork iNetwork]);
netTmp(1).name = [ netTmp(1).name 'Condition 1' ];
netTmp(2).name = [ netTmp(2).name 'Condition 2' ];
netTmp(2).name = [ netTmp(2).name 'Difference' ];
for iFreq = 1:length(g.freqrange)

tit = sprintf('MIM %g_%g Hz', g.freqrange{iFreq}(1), g.freqrange{iFreq}(2));

medthresh = nanmedian(dataCell{iFreq,1}(:))*g.threshold;
roi_networkplot(EEG(1), netTmp, [ dataCell(iFreq,:) {dataCell{iFreq,2}-dataCell{iFreq,1}}], 'threshold', medthresh, 'subplots', 'on', 'title', tit, 'columns', 3, addopts{:}); %, 'limits'); %, [0.05 0.08]);
tit(tit == ' ') = '_';
print('-djpeg', [g.outputdir filesep 'Connectivity_maps_MIM_' net(iNetwork).name '_' tit '.jpg']);
close;
end
end
else
for iFreq = 1:length(g.freqrange)

tit = sprintf('MIM %g_%g Hz', g.freqrange{iFreq}(1), g.freqrange{iFreq}(2));

medthresh = nanmedian(dataCell{iFreq,1}(:))*g.threshold;
roi_networkplot(EEG(1), netTmp, [ dataCell(iFreq,:) {dataCell{iFreq,2}-dataCell{iFreq,1}}], 'threshold', medthresh, 'subplots', 'off', 'title', tit, 'columns', 3, addopts{:}); %, 'limits'); %, [0.05 0.08]);
roi_networkplot(EEG(1), net, dataCell{iFreq}, 'threshold', 0, 'subplots', 'on', 'title', tit, 'limits', [0.1 0.2]);
tit(tit == ' ') = '_';
print('-djpeg', ['../results/Connectivity_maps_MIM_' net(iNetwork).name '_' tit '.jpg']);
print('-djpeg', [g.outputdir filesep 'Connectivity_maps_' tit '_fixed.jpg']);
close;
end
end
Expand Down
46 changes: 40 additions & 6 deletions roi_definenetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,18 @@
indTmp1 = strmatch(val, allLabels, 'exact');
indTmp2 = strmatch([ 'Brodmann area ' val], allLabels, 'exact');
indTmp = [ indTmp1 indTmp2 ];
if length(indTmp) ~= 1
if length(indTmp) == 0
if strcmpi(g.ignoremissing, 'off')
error('Area %s not found or duplicate', val);
error('Area %s not found ', val);
else
fprintf('Area %s not found or duplicate, ignoring\n', val);
fprintf('Area %s not found, ignoring it\n', val);
indTmp = [];
end
elseif length(indTmp) > 1
if strcmpi(g.ignoremissing, 'off')
error('Area %s duplicate', val);
else
fprintf('Area %s duplicate, ignoring\n', val);
indTmp = [];
end
else
Expand Down Expand Up @@ -185,11 +192,18 @@
indTmp1 = strmatch(val, allLabels, 'exact');
indTmp2 = strmatch([ 'Brodmann area ' val], allLabels, 'exact');
indTmp = [ indTmp1 indTmp2 ];
if length(indTmp) ~= 1
if length(indTmp) == 1
if strcmpi(g.ignoremissing, 'off')
error('Area %s not found', val);
else
fprintf('Area %s not found\n', val);
if length(indTmp) > 1 indTmp = indTmp(1); end
end
else
if strcmpi(g.ignoremissing, 'off')
error('Area %s not found or duplicate', val);
error('Area %s not found', val);
else
fprintf('Area %s not found or duplicate, using the first one\n', val);
fprintf('Area %s duplicate, using the first one\n', val);
if length(indTmp) > 1 indTmp = indTmp(1); end
end
end
Expand All @@ -213,7 +227,26 @@

function newconnectmat = augmentConnectivity(connectmat,ROIinds)

sz = [size(connectmat) 1 1];
permFlag = false;
if sz(2) == sz(3) && sz(1) ~= sz(2)
connectmat = permute(connectmat, [2 3 1]);
permFlag = true;
end

% accross subjects if any
nVals = size(connectmat,1);
if size(connectmat,3) > 1
newconnectmat = zeros(nVals+length(ROIinds), nVals+length(ROIinds), size(connectmat,3));
for iSubject = 1:size(connectmat,3)
newconnectmat(:,:,iSubject) = augmentConnectivity(connectmat(:,:,iSubject),ROIinds);
end
if permFlag
newconnectmat = permute(newconnectmat, [3 1 2]);
end
return;
end

newconnectmat = zeros(nVals+length(ROIinds), nVals+length(ROIinds));
newconnectmat(1:nVals,1:nVals) = connectmat;

Expand Down Expand Up @@ -241,3 +274,4 @@
end
end
end

48 changes: 40 additions & 8 deletions roi_networkplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
% 'freqrange' - [min max] frequency range in Hz. Default is to plot broadband power.
% 'subplots' - ['on'|'off'] create subplots (when more than one plot).
% Default is 'off'.
% 'exporttxt' - ['on'|'off'] export results as text files (see filename)
% Default is 'off'.
% 'exportmean' - ['on'|'off'] export results as text files (when filename defined)
% Default is 'on'.
% 'exportstd' - ['on'|'off'] export results as text files (when filename defined)
% Default is 'on'.
% 'title' - ['string'] figure title. Default none.
% 'plotmode' - ['2D'|'3D'|'both'] figure plotting mode. Default is '2D'
% 'filename' - ['string'] base file name (without extension). This is
Expand Down Expand Up @@ -89,7 +91,8 @@

[g, addopts] = finputcheck(varargin, { ...
'subplots' 'string' {'on' 'off'} 'off';
'exporttxt' 'string' {'on' 'off'} 'on';
'exportmean' 'string' {'on' 'off'} 'on';
'exportstd' 'string' {'on' 'off'} 'off';
'title' 'string' {} '';
'netstat' 'string' {'on' 'off'} 'off';
'addrois' '' {} [];
Expand Down Expand Up @@ -197,8 +200,25 @@
else
networkMatSubj = matrix(networks(iNet).ROI_inds, networks(iNet).ROI_inds, :);
end
sumVals{iNet} = sum(sum(networkMatSubj,1),2);
if 0
% sum
sumVals{iNet} = nansum(nansum(networkMatSubj,1),2);
elseif 0
% max
sumVals{iNet} = nanmax(nanmax(networkMatSubj,[],1),[],2);
else
% median
tmp = networkMatSubj;
for i1 = 1:size(tmp,1)
for i2 = 1:size(tmp,3)
tmp(i1,i1,i2) = NaN;
end
end
tmp = reshape(tmp, size(tmp,1)*size(tmp,2), size(tmp,3));
sumVals{iNet} = nanmedian(tmp, 1);
end
networkMat = mean(networkMatSubj,3);
networkMatStd = std(networkMatSubj,[],3);

if ~strcmpi(g.plotmode, 'none')
if strcmpi(g.subplots, 'on')
Expand Down Expand Up @@ -242,15 +262,23 @@
end

% save text
if strcmpi(g.exporttxt, 'on') && ~isempty(g.filename)
if strcmpi(g.exportmean, 'on') && ~isempty(g.filename)
tmptable = array2table(networkMat(:,:), 'variablenames', labels, 'rownames', labels);
tmpFileName = [ g.filename '_' networks(iNet).name '.txt' ];
tmpFileName = [ g.filename '_' networks(iNet).name '_mean.txt' ];
txtFileName{end+1} = tmpFileName;
writetable(tmptable, tmpFileName,'WriteRowNames', true );
end

if strcmpi(g.exportstd, 'on') && ~isempty(g.filename)
tmptable = array2table(networkMatStd(:,:), 'variablenames', labels, 'rownames', labels);
tmpFileName = [ g.filename '_' networks(iNet).name '_std.txt' ];
txtFileName{end+1} = tmpFileName;
writetable(tmptable, tmpFileName,'WriteRowNames', true );
end

if nargin > 2
tmpTitle(tmpTitle == ' ') = '_';
tmpTitle(tmpTitle == '.') = '_';
tmpTitle(tmpTitle == '-') = '_';
measures.(tmpTitle).mean = networkMat;
measures.(tmpTitle).labels = labels;
end
Expand All @@ -260,7 +288,11 @@
% perform some stats
if length(networks) > 1
[H,P,CI,STATS] = ttest(sumVals{1}, sumVals{2});
fprintf('Network %s effect size %1.2f p-value: %1.3f\n', networks(end).name, STATS.tstat, P);
if P< 0.05
fprintf(2, 'Network %s effect size %1.2f p-value: %1.3f\n', networks(end).name, STATS.tstat, P);
else
fprintf('Network %s effect size %1.2f p-value: %1.3f\n', networks(end).name, STATS.tstat, P);
end
%[ef,~,pvals] = statcond(sumVals(1:2), 'mode', 'perm');
%fprintf('Network %s effect size %1.2f p-value: %1.3f\n', networks(end).name, ef, pvals);
end
Expand Down
3 changes: 2 additions & 1 deletion test_pipes/pipeline_connectivity.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@
tic
EEG = pop_roi_connect(EEG, 'methods', measures(iMeasure));
t(iMeasure) = toc;
end
end
pop_roi_connectplot(EEG, 'measure', 'MIM', 'plot3d', 'on', 'plotcortex', 'off', 'plot3dparams', {'threshold', 0.29, 'brainmovieopt' {'nodeColorDataRange', [], 'nodeSizeLimits', [0 0.2]}});

0 comments on commit 18e826e

Please sign in to comment.