[FieldTrip] help

Schoffelen, J.M. (Jan Mathijs) jan.schoffelen at donders.ru.nl
Sat Mar 27 16:31:15 CET 2021


Hi Michelle,

In order to be able to think along, you’d need to pinpoint us to where the problem occurs. The pasted script is simply too long.

E.g: What’s the nature of the data? i.e. how many trials etc. -> if there’s only a single trial, Fieldtrip will probably return a coherence that is 1 (or perhaps NaN). The reason for this is that coherence is to be computed across observations, and FieldTrip uses only the ’trial’ as observation dimension, not time.

Also, as you may have seen in the documentation about time-frequency decomposition: TFRs will contain NaNs for those time-frequency points at which the specified time-window is not completely filled with data. However, I don’t know whether localized NaNs in the data will spread to other time-frequency points as well during the coherence computation.

Good luck,
Jan-Mathijs





On 26 Mar 2021, at 21:01, Yee Wong <ywong252 at uwo.ca<mailto:ywong252 at uwo.ca>> wrote:

Hello,

I am running into an error with the connectivity script, where the output values are NaN. See below for the code section.

%% Compute coherence

if computecoh == 1

    if strcmp(reference,'car')
        load([restdir 'rest_preproc_car_matched.mat'])
    elseif strcmp(reference,'csd')
        load([restdir 'rest_preproc_csd_matched.mat'])
    end

    % 1. Create TFRs by condition - frequency over time
    cfg             = [];
    cfg.channel     = [lfchans rfchans lpchans rpchans];
    cfg.output      = 'fourier';
    cfg.method      = 'mtmconvol';
    cfg.taper       = 'hanning';
    cfg.foi         = 2:1:50;
    cfg.toi         = -1:0.05:3;
    cfg.t_ftimwin   = 3./cfg.foi; % [lf hf].*cfg.foi; % length of time window; number of cycles per time window
    cfg.keeptrials  = 'yes';
    cfg.pad         = 10;
    [tfa_rest]      = ft_freqanalysis(cfg,rest_preproc);

    save([conndir 'tfa_forconn_rest_' reference '.mat'],'tfa_rest');


    % COMPUTE CONNECTIVITY METRICS

    % ~~ select relevant time period
    cfg             = [];
    cfg.latency     = [0.001 2];
    [tfa_rest]      = ft_selectdata(cfg, tfa_rest);

    % Coherence
    cfg             = [];
    cfg.method      = 'coh';
    cfg.complex     = 'imag'; %'abs'
    cfg.channelcmb  = ft_channelcombination('all',tfa_rest.label);
    rest_cohconn    = ft_connectivityanalysis(cfg,tfa_rest);

    save([conndir 'rest_cohconn_',reference,'.mat'],'rest_cohconn');

    clear *_coh tfa* rest_preproc

end

% to visualize each pair's coherence values
% figure;imagesc(squeeze(rest_cohconn.cohspctrm(1,6,:,:))); colorbar

%% Extract coherence values

if extract == 1

    load([conndir 'rest_cohconn_',reference,'.mat']);

    % Extract channel index
    lfchanidx=[]; rfchanidx=[]; lpchanidx=[]; rpchanidx=[];
    for c=1:numel(lfchans)
        lfchanidx(c) = find(strcmp(lfchans{c},rest_cohconn.label));
    end
    for c=1:numel(rfchans)
        rfchanidx(c) = find(strcmp(rfchans{c},rest_cohconn.label));
    end
    for c=1:numel(lpchans)
        lpchanidx(c) = find(strcmp(lpchans{c},rest_cohconn.label));
    end
    for c=1:numel(rpchans)
        rpchanidx(c) = find(strcmp(rpchans{c},rest_cohconn.label));
    end

    % identify frequency range of interest
    lowf = find(lf == rest_cohconn.freq);
    highf = find(hf == rest_cohconn.freq);
    freq = [lowf:highf];
    clear lowf highf

    % average over frequency range of interest, outputs chan x chan matrix
    cohdata = abs(mean(mean(rest_cohconn.cohspctrm(:,:,freq,:),3),4));

    % left - frontal vs parietal
    cnt = 1;
    for c = 1:numel(lfchanidx)
        for d = 1:numel(lpchanidx)
            cohtmp(cnt,1) = cohdata(lfchanidx(c),lpchanidx(d));
            cnt = cnt + 1;
        end
    end
    % ~ average across electrode pairs
    left_fvsp_coh = mean(cohtmp); clear cohtmp

    % right - frontal vs parietal
    cnt = 1;
    for c = 1:numel(rfchanidx)
        for d = 1:numel(rpchanidx)
            cohtmp(cnt,1) = cohdata(rfchanidx(c),rpchanidx(d));
            cnt = cnt + 1;
        end
    end
    % ~ average across electrode pairs
    right_fvsp_coh = mean(cohtmp); clear cohtmp

    % frontal - right vs left
    cnt = 1;
    for c = 1:numel(lfchanidx)
        for d = 1:numel(rfchanidx)
            cohtmp(cnt,1) = cohdata(lfchanidx(c),rfchanidx(d));
            cnt = cnt + 1;
        end
    end
    % ~ average across electrode pairs
    frontal_lvsr_coh = mean(cohtmp); clear cohtmp

    % parietal - right vs left
    cnt = 1;
    for c = 1:numel(lpchanidx)
        for d = 1:numel(rpchanidx)
            cohtmp(cnt,1) = cohdata(lpchanidx(c),rpchanidx(d),:);
            cnt = cnt + 1;
        end
    end
    % ~ average across electrode pairs
    parietal_lvsr_coh = mean(cohtmp); clear cohtmp

    % left frontal vs right parietal
    cnt = 1;
    for c = 1:numel(lfchanidx)
        for d = 1:numel(rpchanidx)
            cohtmp(cnt,1) = cohdata(lfchanidx(c),rpchanidx(d),:);
            cnt = cnt + 1;
        end
    end
    % ~ average across electrode pairs
    lfrpcoh = mean(cohtmp); clear cohtmp

    % right frontal vs left parietal
    cnt = 1;
    for c = 1:numel(rfchanidx)
        for d = 1:numel(lpchanidx)
            cohtmp(cnt,1) = cohdata(rfchanidx(c),lpchanidx(d),:);
            cnt = cnt + 1;
        end
    end
    % ~ average across electrode pairs
    rflpcoh = mean(cohtmp); clear cohtmp

    % right frontal and parietal vs left frontal and parietal
    cnt = 1;
    for c = 1:numel(lfchanidx)+numel(lpchanidx)
        for d = 1:numel(rfchanidx)+numel(rpchanidx)
            if c <= numel(lfchanidx) % left frontal
                if d <= numel(rfchanidx)
                    cohtmp(cnt,1) = cohdata(lfchanidx(c),rfchanidx(d),:);
                else
                    cohtmp(cnt,1) = cohdata(lfchanidx(c),rpchanidx(d-numel(rfchanidx)),:);
                end
            else % left parietal
                if d <= numel(rfchanidx)
                    cohtmp(cnt,1) = cohdata(lpchanidx(c-numel(lfchanidx)),rfchanidx(d),:);
                else
                    cohtmp(cnt,1) = cohdata(lpchanidx(c-numel(lfchanidx)),rpchanidx(d-numel(rfchanidx)),:);
                end
            end

            cnt = cnt + 1;
        end
    end
    % ~ average across electrode pairs
    leftvsright_coh = mean(cohtmp); clear cohtmp


    % frontal vs parietal (left and right)
    cnt = 1;
    for c = 1:numel(lfchanidx)+numel(rfchanidx)
        for d = 1:numel(lpchanidx)+numel(rpchanidx)
            if c <= numel(lfchanidx) %  left
                if d <= numel(lpchanidx)
                    cohtmp(cnt,1) = cohdata(lfchanidx(c),lpchanidx(d),:);
                else
                    cohtmp(cnt,1) = cohdata(lfchanidx(c),rpchanidx(d-numel(lpchanidx)),:);
                end
            else %  right
                if d <= numel(rfchanidx)
                    cohtmp(cnt,1) = cohdata(rfchanidx(c-numel(lfchanidx)),lpchanidx(d),:);
                else
                    cohtmp(cnt,1) = cohdata(rfchanidx(c-numel(lfchanidx)),rpchanidx(d-numel(lpchanidx)),:);
                end
            end

            cnt = cnt + 1;
        end
    end
    % ~ average across electrode pairs
    frontalvsparietal_coh = mean(cohtmp); clear cohtmp

end

% Save File
filename = strcat('rest_cohconn_roi_',num2str(lf),'_',num2str(hf),'_',reference,'.mat');
save([conndir filename], '*_coh','*chans');

Thank you for your time and help.

Sincerely,


Michelle (Yee Suet) Wong
BHK, MSc, PhD Candidate
Western University
School of Kinesiology
Faculty of Health Sciences
Exercise, Mobility, and Brain Health Lab
London, Ontario, Canada



_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://doi.org/10.1371/journal.pcbi.1002202

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20210327/b36a69a1/attachment.htm>


More information about the fieldtrip mailing list