<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div class="">Dear Jed,</div>
<div class=""><br class="">
</div>
<div class="">Sorry about the confusion, and well-spotted. After thinking about it for a while, I wasn’t sure myself anymore (although I’d have assumed that the documentation in the function is incorrect). Therefore, I decided to simulated some data (with a
 known delay), in order to evaluate the results from the connectivity computation.</div>
<div class="">The code is pasted below, but I would appreciate it if this were made into a piece of documentation on the website (hint). Also, based on the outcome, we should update the help documentation in the relevant function (in this case: ft_connectivity_psi).</div>
<div class=""><br class="">
</div>
<div class="">If you run this piece of code, you’ll see that the (2,1) combination (both when represented as a ‘full’ 2x2xnfreq, and as a ‘sparse’ 1xnfreq matrix, as indexed with {‘chan02’ ‘chan01’}) will have a positive psi-peak. Since by construction the
 second channel is leading in time, I’d say that a positive psi (as outputted by fieldtrip) means ‘leading’ in time. This is confirmed by the granger causality analysis.</div>
<div class=""><br class="">
</div>
<div class="">Could I ask you (and anyone else interested) to verify this, and if you agree, to make a suggestion for an update of the help documentation in ft_connectivity_psi, as well as a draft for a FAQ on the website? Both should be submitted as pull requests
 on <a href="https://github.com/fieldtrip/fieldtrip" class="">https://github.com/fieldtrip/fieldtrip</a> (for code changes) and
<a href="https://github.com/fieldtrip/website" class="">https://github.com/fieldtrip/website</a> (for website changes), respectively.</div>
<div class=""><br class="">
</div>
<div class="">Thanks and with best wishes,</div>
<div class=""><br class="">
</div>
<div class="">Jan-Mathijs</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</div>
<div class="">% THE BELOW CODE SHOULD IDEALLY GO INTO A FAQ OR EXAMPLE SCRIPT ON THE WEBSITE</div>
<div class=""><br class="">
</div>
<div class="">% simulate some data</div>
<div class="">dat = ft_preproc_bandpassfilter(randn(1,100050),1000,[15 25],[],'firws');</div>
<div class=""><br class="">
</div>
<div class="">dat1 = reshape(dat(1:100000),[],100)';</div>
<div class="">dat2 = reshape(dat(51:end),[],100)'; % 2 is leading 1</div>
<div class=""><br class="">
</div>
<div class="">tlck.trial = zeros(100,2,1000);</div>
<div class="">tlck.trial(:,1,:) = dat1+randn(100,1000)./4;</div>
<div class="">tlck.trial(:,2,:) = dat2+randn(100,1000)./4;</div>
<div class="">tlck.label  = {'chan01';'chan02'};</div>
<div class="">tlck.dimord = 'rpt_chan_time';</div>
<div class="">tlck.time   = (0:999)./1000;</div>
<div class=""><br class="">
</div>
<div class="">data = ft_checkdata(tlck,'datatype', 'raw');</div>
<div class=""><br class="">
</div>
<div class="">% spectral decomposition</div>
<div class="">cfg        = [];</div>
<div class="">cfg.method = 'mtmfft';</div>
<div class="">%cfg.foilim = [0 100];</div>
<div class="">cfg.tapsmofrq = 2;</div>
<div class="">cfg.output = 'fourier';</div>
<div class="">cfg.pad    = 4;</div>
<div class="">freq       = ft_freqanalysis(cfg, data);</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">cfg.output = 'powandcsd';</div>
<div class="">cfg.channelcmb = {'all' 'all'};</div>
<div class="">freq_csd   = ft_freqanalysis(cfg, data);</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">% connectivity estimation</div>
<div class="">cfg           = [];</div>
<div class="">cfg.method    = 'psi';</div>
<div class="">cfg.bandwidth = 5;</div>
<div class="">psi           = ft_connectivityanalysis(cfg, freq);</div>
<div class="">psi2          = ft_connectivityanalysis(cfg, freq_csd);</div>
<div class=""><br class="">
</div>
<div class="">% bonus: estimate delay from the phase difference spectrum</div>
<div class="">cfg           = [];</div>
<div class="">cfg.method    = 'coh';</div>
<div class="">cfg.complex   = 'complex';</div>
<div class="">coh           = ft_connectivityanalysis(cfg, freq);</div>
<div class=""><br class="">
</div>
<div class="">findx = nearest(freq.freq,18):nearest(freq.freq,22);</div>
<div class="">X     = [ones(1,numel(findx));freq.freq(findx)-mean(freq.freq(findx))];</div>
<div class="">b     = unwrap(squeeze(angle(coh.cohspctrm(2,1,findx))))'/X;</div>
<div class="">delay = 1000.*b(2)./(2.*pi);</div>
<div class=""><br class="">
</div>
<div class="">% another bonus: estimate granger causality</div>
<div class="">cfg        = [];</div>
<div class="">cfg.method = 'granger';</div>
<div class="">granger    = ft_connectivityanalysis(cfg, freq);</div>
<div class="">granger2   = ft_connectivityanalysis(cfg, freq_csd);</div>
<div class=""><br class="">
</div>
<div class="">cfg = [];</div>
<div class="">cfg.xlim = [0 50];</div>
<div class="">cfg.parameter = 'psispctrm';</div>
<div class="">ft_connectivityplot(cfg, psi);</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div>
<blockquote type="cite" class="">
<div class="">On 29 Apr 2019, at 23:27, Jed Meltzer <<a href="mailto:jedmeltzer@yahoo.com" class="">jedmeltzer@yahoo.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="">
<div class="yahoo-style-wrap" style="font-family:times new roman, new york, times, serif;font-size:13px;">
<div class="">Hello, </div>
<div class="">I'm Jed Meltzer, a researcher at the Rotman Research Institute of Baycrest Hospital in Toronto. A student in my lab is working on an MEG project involving connectivity between the left and right motor cortex during hand movement, and we are interested
 in quantifying causal influences between the two regions using methods such as Granger Causality and phase-slope index (PSI). When calculating PSI in field-trip, we are a bit confused about which direction is which, and are seeking clarification. Here is our
 cfg setup: </div>
<div class=""><br class="">
</div>
<div class=""><span class="">
<div class=""><span style="white-space: pre-wrap;" class=""></span>data.label   = {'LM1' 'RM1' 'LV1' 'RV1'};</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>data.trial   = tdata;<br class="">
</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>data.time    = tdata_time;</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>data.cfg     = cfg;</div>
<div class=""><br class="">
</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>%% time-frequency analysis</div>
<div class=""><br class="">
</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>% tf</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg = [];</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.output = 'fourier';</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.method = 'mtmconvol';</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.keeptrials = 'yes';</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.taper = 'hanning'; % </div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.toi = timesec;<br class="">
</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.channel = 'all'</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span><span style="white-space: pre-wrap;" class=""></span>cfg.foi = 1:0.5:80; </div>
<div class=""><span style="white-space: pre-wrap;" class=""></span><span style="white-space: pre-wrap;" class=""></span>cfg.t_ftimwin = ones(length(cfg.foi),1).*0.5;  </div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.pad = 10 % this is the parameter to output evenly spaced freq bins</div>
<div class=""><br class="">
</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>freq = ft_freqanalysis(cfg, data)</div>
<div class=""><br class="">
</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>freqdim = freq.freq;</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>timedim = freq.time;</div>
<div class=""><span class="">
<div class=""><span style="white-space: pre-wrap;" class=""></span></div>
<div class="">                                        % psi</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg = [];</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.method = 'psi'; % </div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>cfg.bandwidth = 5; % </div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>stat = ft_connectivityanalysis(cfg, freq);</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>RM1toLM1_psi = squeeze(stat.psispctrm(1,2,:,:));</div>
<div class=""><span style="white-space: pre-wrap;" class=""></span>LM1toRM1_psi = squeeze(stat.psispctrm(2,1,:,:));</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">We are not sure about those last two lines of the code. We would like to have the causal influence of channel 1 to channel 2 be reflected as a positive number. We have read through the documentation and we find two parts of it to seem contradictory.
 First, there is this page: </div>
<div class=""><br class="">
</div>
<div class=""><span class=""><a href="http://www.fieldtriptoolbox.org/faq/in_what_way_can_frequency_domain_data_be_represented_in_fieldtrip/" rel="nofollow" target="_blank" class="">http://www.fieldtriptoolbox.org/faq/in_what_way_can_frequency_domain_data_be_represented_in_fieldtrip/</a></span><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">It states: </div>
<div class="">------</div>
<div class=""><span class="">
<p style="margin-top: 0px; text-align: justify; color: rgb(33, 37, 41); font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px;" class="">
Note that this representation lacks a ‘labelcmb’ field, and that the ‘dimord’ is ‘chan_chan_freq’. This means that the numeric data now implicitly contains both the combinations {‘a’ ‘b’} (in coh.cohspctrm(1,2,:) ) , and the combination {‘b’ ‘a’} (in coh.cohspctrm(2,1,:)
 ). For a quantity like the coherence spectrum the values across the diagonal are symmetric, but for complex-valued quantities, as well as for directional measures of interaction, the values at the entries across the diagonal are typically different. The convention
 used by FieldTrip is that the row-channel ‘causes’ the column-channel.</p>
<div class="">--------</div>
<div class="">So, we interpret this to mean that if channel 1 is LM1, and channel 2 is RM1, then we would probably want: </div>
<div class=""><span class=""><span style="font-family: 'times new roman', 'new york', times, serif; white-space: pre-wrap;" class=""></span><span style="font-family: 'times new roman', 'new york', times, serif;" class="">LM1toRM1_psi = squeeze(stat.psispctrm(1,2,:,:));</span></span><br class="">
</div>
</span>because the first dimension is the "row" channel and the second dimension is the "column" channel. </div>
<div class=""><br class="">
</div>
<div class="">However, in the help for the psi function: </div>
<div class=""><span class=""><a href="http://www.fieldtriptoolbox.org/reference/ft_connectivity_psi/" rel="nofollow" target="_blank" class="">http://www.fieldtriptoolbox.org/reference/ft_connectivity_psi/</a></span><br class="">
</div>
<div class="">it says:</div>
<div class="">-----</div>
<div class=""><span class="">
<pre class="ydp44283aechighlight" style="font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 13.2px; margin-top: 0px; margin-bottom: 0px; color: rgb(33, 37, 41); background: rgb(240, 240, 240);"><code style="font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: inherit; color: inherit;" class=""><span class="ydp44283aecn">If</span> <span class="ydp44283aecn">the</span> <span class="ydp44283aecn">phase</span> <span class="ydp44283aecn">slope</span>
 <span class="ydp44283aecn">index</span> <span class="ydp44283aecnb" style="color: rgb(0, 112, 32);">is</span> <span class="ydp44283aecn">positive</span><span class="ydp44283aecp">,</span> <span class="ydp44283aecn">then</span> <span class="ydp44283aecn">the</span> <span class="ydp44283aecn">first</span> <span class="ydp44283aecn">chan</span> <span class="ydp44283aecp">(</span><span class="ydp44283aecmi" style="color: rgb(64, 160, 112);">1</span><span class="ydp44283aecn">st</span> <span class="ydp44283aecn">dim</span><span class="ydp44283aecp">)</span> <span class="ydp44283aecn">becomes</span> <span class="ydp44283aecnb" style="color: rgb(0, 112, 32);">more</span> <span class="ydp44283aecn">lagged</span> <span class="ydp44283aecp">(</span><span class="ydp44283aecnb" style="color: rgb(0, 112, 32);">or</span>
 <span class="ydp44283aecn">less</span> <span class="ydp44283aecn">leading</span><span class="ydp44283aecp">)</span> <span class="ydp44283aecn">with</span> <span class="ydp44283aecn">higher</span> <span class="ydp44283aecn">frequency</span><span class="ydp44283aecp">,</span> <span class="ydp44283aecn">indicating</span> <span class="ydp44283aecn">that</span> <span class="ydp44283aecn">it</span> <span class="ydp44283aecnb" style="color: rgb(0, 112, 32);">is</span> <span class="ydp44283aecn">causally</span>
 <span class="ydp44283aecn">driven</span> <span class="ydp44283aecn">by</span> <span class="ydp44283aecn">the</span> <span class="ydp44283aecnb" style="color: rgb(0, 112, 32);">second</span> <span class="ydp44283aecn">channel</span> <span class="ydp44283aecp">(</span><span class="ydp44283aecmi" style="color: rgb(64, 160, 112);">2</span><span class="ydp44283aecn">nd</span> <span class="ydp44283aecn">dim</span><span class="ydp44283aecp">)</span></code></pre>
</span>------</div>
</span>So following this logic, if we want a positive number to reflect a causal influence from LM1 (channel 1) to RM1 (channel 2), then we would instead want to write: </div>
<div class=""><span class=""><span style="font-family: 'times new roman', 'new york', times, serif; white-space: pre-wrap;" class=""></span><span style="font-family: 'times new roman', 'new york', times, serif;" class="">LM1toRM1_psi = squeeze(stat.psispctrm(2,1,:,:));</span></span><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Can you help me figure out which one is correct? Our data have periods of both positive and negative PSI (quite interestingly) so it's impossible for us to "guess" which one is correct just from the pattern. </div>
<div class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class="">Jed</div>
</span></div>
</div>
</div>
_______________________________________________<br class="">
fieldtrip mailing list<br class="">
<a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" class="">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br class="">
https://doi.org/10.1371/journal.pcbi.1002202<br class="">
</div>
</blockquote>
</div>
<br class="">
</body>
</html>