Skip to content

Commit

Permalink
Merge pull request #87 from Wirkungstreffer/BS_dev
Browse files Browse the repository at this point in the history
Cancel shuffle over normalized data & adjust abs mean sequence
  • Loading branch information
nguyen-td authored Mar 21, 2024
2 parents d15ff51 + 26f9c18 commit 138f2b0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion test_pipes/test_pac.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

EEG1 = pop_roi_connect(EEG, 'methods', {'PAC', 'MIM', 'COH'}, 'fcomb', fcomb); % test all 3 connectivity functions (data2spwctrgc, data2strgcmim, roi_pac)
tic
EEG2 = pop_roi_connect(EEG, 'methods', {'PAC'}, 'fcomb', fcomb, 'bs_outopts', 1, 'conn_stats', 'on', 'nshuf', 3, 'poolsize', 12); % compute only b_anti, b_anti_norm
EEG2 = pop_roi_connect(EEG, 'methods', {'PAC'}, 'fcomb', fcomb, 'conn_stats', 'on', 'nshuf', 3, 'poolsize', 12); % compute only b_anti, b_anti_norm
toc
EEG3 = pop_roi_connect(EEG, 'methods', {'PAC'}, 'fcomb', fcomb, 'bs_outopts', 1); % compute only b_anti, b_anti_norm

Expand Down
12 changes: 6 additions & 6 deletions utils/calc_pac.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
function [biv_orig, biv_anti, biv_orig_norm, biv_anti_norm] = calc_pac(BS, RTP)

% Calculate absolute values of the cross-bispectrum
biv_orig = squeeze(([abs(mean(BS(1, 2, 2, :), 4)) abs(mean(BS(2, 1, 1, :), 4))])); % [Bkmm, Bmkk], average over frequency bands (4th dimension)
xx = BS - permute(BS, [2 1 3 4 5]);
biv_anti = squeeze(([abs(mean(xx(1, 2, 2, :), 4)) abs(mean(xx(2, 1, 1, :), 4))]));
biv_orig = squeeze(([mean(abs(BS(1, 2, 2, :)), 4) mean(abs(BS(2, 1, 1, :)), 4)])); % [Bkmm, Bmkk], average over frequency bands (4th dimension)
xx = BS - permute(BS, [2 1 3 4]);
biv_anti = squeeze(([mean(abs(xx(1, 2, 2, :)), 4) mean(abs(xx(2, 1, 1, :)), 4)]));

% Calculate absolute values of the cross-bicoherence
bicoh = BS ./ RTP;
biv_orig_norm = squeeze(([abs(mean(bicoh(1, 2, 2, :), 4)) abs(mean(bicoh(2, 1, 1, :), 4))])); % [Bkmm, Bmkk], average over frequency bands
xx = bicoh - permute(bicoh, [2 1 3 4 5]);
biv_anti_norm = squeeze(([abs(mean(xx(1, 2, 2, :), 4)) abs(mean(xx(2, 1, 1,:), 4))]));
biv_orig_norm = squeeze(([mean(abs(bicoh(1, 2, 2, :)), 4) mean(abs(bicoh(2, 1, 1, :)), 4)])); % [Bkmm, Bmkk], average over frequency bands
xx = bicoh - permute(bicoh, [2 1 3 4]);
biv_anti_norm = squeeze(([mean(abs(xx(1, 2, 2, :)), 4) mean(abs(xx(2, 1, 1,:)), 4)]));

end
34 changes: 17 additions & 17 deletions utils/shuffle_BS.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
error('fcomb.high must be smaller than fcomb.low - check the documentation for the "fcomb" input parameter in "shuffle_BS".')
end

[nchan, ndat, nepo] = size(data);
[nchan, ndat, ~] = size(data);

% [inds, PCA_inds] = fp_npcs2inds(npcs);
if isempty(g.roi_selection)
Expand Down Expand Up @@ -133,14 +133,14 @@
% Pre-allocate variables outside the parfor loop
b_orig = zeros(nROI, nROI, nshuf);
b_anti = zeros(nROI, nROI, nshuf);
b_orig_norm = zeros(nROI, nROI, nshuf);
b_anti_norm = zeros(nROI, nROI, nshuf);
% b_orig_norm = zeros(nROI, nROI, nshuf);
% b_anti_norm = zeros(nROI, nROI, nshuf);

for ishuf = 1:nshuf
parfor ishuf = 1:nshuf
b_orig_ishuf = zeros(nROI, nROI);
b_anti_ishuf = zeros(nROI, nROI);
b_orig_norm_ishuf = zeros(nROI, nROI);
b_anti_norm_ishuf = zeros(nROI, nROI);
% b_orig_norm_ishuf = zeros(nROI, nROI);
% b_anti_norm_ishuf = zeros(nROI, nROI);

% Iterate over ROI pairs
for proi = 1:nROI
Expand All @@ -154,29 +154,29 @@
[RTP_low,~] = data2bs_threenorm(X(:, :)', ndat, floor(ndat/2), ndat, freqinds_low);
[RTP_up,~] = data2bs_threenorm(X(:, :)', ndat, floor(ndat/2), ndat, freqinds_up);

[biv_orig_low, biv_anti_low, biv_orig_low_norm, biv_anti_low_norm] = calc_pac(BS_low, RTP_low);
[biv_orig_up, biv_anti_up, biv_orig_up_norm, biv_anti_up_norm] = calc_pac(BS_up, RTP_up);
[biv_orig_low, biv_anti_low] = calc_pac(BS_low, RTP_low);
[biv_orig_up, biv_anti_up] = calc_pac(BS_up, RTP_up);

% PAC_km(f1, f2) = 0.5 * |Bkmm(f1, f2-f1)| + 0.5 * |Bkmm(f1, f2)|
b_orig_ishuf(aroi,proi) = mean([biv_orig_up(1) biv_orig_low(1)]);
b_orig_ishuf(proi,aroi) = mean([biv_orig_up(2) biv_orig_low(2)]);
b_anti_ishuf(aroi,proi) = mean([biv_anti_up(1) biv_anti_low(1)]);
b_anti_ishuf(proi,aroi) = mean([biv_anti_up(2) biv_anti_low(2)]);

% normalized versions (for bicoherence)
b_orig_norm(aroi,proi) = mean([biv_orig_up_norm(1) biv_orig_low_norm(1)]);
b_orig_norm(proi,aroi) = mean([biv_orig_up_norm(2) biv_orig_low_norm(2)]);
b_anti_norm(aroi,proi) = mean([biv_anti_up_norm(1) biv_anti_low_norm(1)]);
b_anti_norm(proi,aroi) = mean([biv_anti_up_norm(2) biv_anti_low_norm(2)]);
% % normalized versions (for bicoherence)
% b_orig_norm(aroi,proi) = mean([biv_orig_up_norm(1) biv_orig_low_norm(1)]);
% b_orig_norm(proi,aroi) = mean([biv_orig_up_norm(2) biv_orig_low_norm(2)]);
% b_anti_norm(aroi,proi) = mean([biv_anti_up_norm(1) biv_anti_low_norm(1)]);
% b_anti_norm(proi,aroi) = mean([biv_anti_up_norm(2) biv_anti_low_norm(2)]);
end

end

% Store the shuffle information
b_orig(:,:, ishuf) = b_orig_ishuf;
b_anti(:,:, ishuf) = b_anti_ishuf;
b_orig_norm(:,:, ishuf) = b_orig_norm_ishuf;
b_anti_norm(:,:, ishuf) = b_anti_norm_ishuf;
% b_orig_norm(:,:, ishuf) = b_orig_norm_ishuf;
% b_anti_norm(:,:, ishuf) = b_anti_norm_ishuf;

end

Expand All @@ -186,8 +186,8 @@
% Save PAC results in the output structure
conn.PAC.b_orig = b_orig;
conn.PAC.b_anti = b_anti;
conn.PAC.b_orig_norm = b_orig_norm;
conn.PAC.b_anti_norm = b_anti_norm;
% conn.PAC.b_orig_norm = b_orig_norm;
% conn.PAC.b_anti_norm = b_anti_norm;

% shut down current parallel pool only if the toolbox is available
if license('test', 'Distrib_Computing_Toolbox') && ~isempty(ver('parallel'))
Expand Down

0 comments on commit 138f2b0

Please sign in to comment.