Back to the main page.

Bug 3362 - ft_math with biosemi64.lay in data changes channel order

Reported 2017-10-11 10:58:00 +0200
Modified 2019-08-10 12:41:42 +0200
Product: FieldTrip
Component: core
Version: unspecified
Hardware: PC
Operating System: Windows
Importance: P5 major
Assigned to: Ingmar de Vries
Depends on:
See also:

Ingmar de Vries - 2017-10-11 10:58:27 +0200

I'm using fieldtrip version: 20171001 I'm using ft_math with biosemi64.lay in the data structure and the following configuration: cfg = []; cfg.operation = 'subtract'; cfg.parameter = 'avg'; GA_cond1vscond2 = ft_math(cfg,GAcondition1,GAcondition2); This changes the channel label ordering! Specifically channel 37 is in the single condition structures (i.e. GAcondition1 and GAcondition2) channel 'AFz', but after subtracting the conditions using ft_math it becomes 'Fp1'. I'm sure there'll be more label swaps. I'm using this to plot the result of a cluster-based permutation test (condition contrast, time-by-channel data), and the plot is highlighting the wrong channels as significant because of this label swapping bug!

Jan-Mathijs Schoffelen - 2017-11-09 17:21:19 +0100

It's not clear what is happening here. Is there only a swap of labels, or is the data also re-ordered as well? I don't understand why this would lead to 'wrong' plotting if the data is kept internally consistent. In other words, are you doing anything to your data outside FieldTrip which assumes a fixed channel order?

Ingmar de Vries - 2017-11-10 10:10:58 +0100

(In reply to Jan-Mathijs Schoffelen from comment #1) Hi, The function ft_math swaps the labels and the data so it stays internally consistent indeed. When only plotting the data there is nothing wrong. The problem is that if you use the ft_timelockstatistics function to run a cluster-based permutation test, then the labels and data are NOT swapped. Specifically, if you follow the tutorial on the Fieldtrip website: You run into a problem when you plot the topoplots. The data for the topoplots is based on ft_math and is internally consistent, while the highlighted channels in the signficant clusters are channel-by-time indices extracted from the result of ft_timelockstatistics. The order of the channel indices is thus not consistent anymore with the result of ft_math. So the problem is that if you follow this exact tutorial, it will in some cases highlight the wrong channels in the topoplots. Now I'm not sure if it is because of the way I import my data. I have a different data structure and transform the structure manually so it looks exactly like the example data ERF_orig.mat that is being using in the tutorial. But is does not have the same history and thus not the same cfg history. I don't know if that could be a problem. Kind Regards,

Jan-Mathijs Schoffelen - 2017-11-10 10:28:37 +0100

OK, thanks. So this is indeed a 'user-error', which the writer of the tutorial did not take into account, either. With lists of labels that are alphabetically ordered from the beginning (as is the case with the CTF MEG data) this is not an issue, but once the channels are not alphabetically ordered (as with a lot of other data, in particular EEG data with labels according to the 10/20 system), this might pose a problem. So, I guess what needs to be done, is to add a few lines of code in the tutorial, to make it robust against this. This can be inserted just before the for-loop that does the actual plotting. It could be something like this: % ensure the channels to have the same order in the average, and the statistical output % this might not be the case, because ft_math might shuffle the order [i1,i2] = match_str(raweffectFICvsFC.label, stat.label); and then, within the for-loop: % Next, check which channels are significant over the % entire time interval of interest. pos_int = zeros(numel(raweffectFICvsFC.label),1); neg_int = zeros(numel(raweffectFICvsFC.label),1); pos_int(i1) = all(pos(i2, m(k):m(k+1)), 2); neg_int(i1) = all(neg(i2, m(k):m(k+1)), 2); Zou ik je mogen vragen, om dit op de wiki te veranderen? Bij voorbaat dank.

Ingmar de Vries - 2017-11-10 14:00:30 +0100

(In reply to Jan-Mathijs Schoffelen from comment #3) Ik ga het extra stukje code checken / aanpassen a.h.v. EEG data en zal het daarna toevoegen aan de tutorial. Groet, Ingmar

Ingmar de Vries - 2017-11-10 14:31:24 +0100

(In reply to Jan-Mathijs Schoffelen from comment #3) It works like a charm. I added it on the three places in the tutorial where it's needed (three spots where there's something being plotted). cheers, Ingmar

Jan-Mathijs Schoffelen - 2017-11-12 13:10:56 +0100

Thanks for fixing!

Robert Oostenveld - 2019-08-10 12:35:29 +0200

This closes a whole series of bugs that have been resolved (either FIXED/WONTFIX/INVALID) for quite some time. If you disagree, please file a new issue on

Robert Oostenveld - 2019-08-10 12:41:42 +0200

This closes a whole series of bugs that have been resolved (either FIXED/WONTFIX/INVALID) for quite some time. If you disagree, please file a new issue on