Back to the main page.

Bug 1808 - wrong counter variable in elseif branch in loop defining the grad-struct, n instead of k

Reported 2012-11-02 14:04:00 +0100
Modified 2012-12-31 11:46:22 +0100
Product: FieldTrip
Component: fileio
Version: unspecified
Hardware: PC
Operating System: Windows
Importance: P3 normal
Assigned to: Robert Oostenveld
Depends on:
See also:

Tim - 2012-11-02 14:04:33 +0100

dear developers, I found a little mistake in the elseif-branch of the loop defining the grad structure in the file mne2grad.m which is invoked by ft_read_header.m in case 'neuromag_fif'. file: mne2grad line:100 problem: counter variable in elseif-branch counts not the channels (n) but k. This is especially a problem if there are EEG-channels at the beginning of the channel list, since n is increasing but because of the EEG-channel the else-branch is invoked and n and k and not further "compatible" Cheers, Tim

Robert Oostenveld - 2012-11-02 16:27:07 +0100

Hi Tim, Just from reading the code I don't yet understand how the presence of electrodes could cause the line 114 elseif section to be executed and not the line 134 else section. do you have a (small) data file that we could use to pinpoint the problem?

Robert Oostenveld - 2012-11-03 10:43:27 +0100

--- Tim wrote at the beginning of the channels there were 25 EEG channels followed by 306 MEG channels. if the grad structure is built, the loop is 25 times (EEG) in the else-section, i.e. n increases to 26 but k is still 1. If now the MEG channels begin, the if-section looks for magnetometer, which is found at n=27 and k becomes 2. that is good. the next channel MUST be a gradiometer though. that means that at n=28 the loop should end in the elseif-section, but because orig.chs(k) does not look for the 28th channel but for k instead, the loop actually looks at channel 2 again. I thought, that the k in the elseif-statement must be changed to an n. --- where is orig.chs(k) being used? Might it be that you are looking at an older version of the file? See for the latest.

Robert Oostenveld - 2012-11-06 08:59:04 +0100

Created attachment 360 screenshot

Robert Oostenveld - 2012-11-06 08:59:47 +0100

On 6 Nov 2012, at 0:22, Tim Kunze wrote: I think I forgot to provide you with some additional information of how I encountered the problem: I am using SPM8 with MEG and got an error which said: Error using ==> mne2grad at 121 Number of MEG channels identified does not match number of channels in grad structure!!!!! I opended the file as shown in the little screenshot, which you find attached. Also, please find attached a small sample set ( cropped it myself and hope everything is included). When you open it with SPM EEG, as I did, you will encounter the error message BEFORE you answer the GUI-question whether to "save the channel selection or not". If you put a breakpoint at the beginning of the loop in mne2grad.m at line ~91, open the data set till the breakpoint does it work and look at orig.chs(:). You will see, that the list begins with 25 EEG channels. As you go step by step through the loop the first 25 iterations end in the end-section, n is increasing but k remains 1 what leads to problems during the treatment of the MEG-channels. I hope the data file supports my report. I also identified another problem with the newest version of neuromag systems (triux) at another part of SPM which is also based on fieldtrip. But lets solve this problem first:)

Robert Oostenveld - 2012-11-06 09:00:13 +0100

(In reply to comment #4) I copied the datafile to /home/common/matlab/fieldtrip/data/test/bug1808/reduced.fif

Robert Oostenveld - 2012-11-06 09:07:34 +0100

(In reply to comment #4) > Error using ==> mne2grad at 121 > Number of MEG channels identified does not match number of channels in > grad structure!!!!! line 121 reads grad.coilori(kCoil,:) = t(1:3,3); whish means that the bug has been detected in an older version of the code. It might already have been fixed, it might also still persist.

Robert Oostenveld - 2012-11-06 09:10:40 +0100

(In reply to comment #6) I made the following test script dataset = '/home/common/matlab/fieldtrip/data/test/bug1808/reduced.fif'; hdr = ft_read_header(dataset); grad = ft_read_sens(dataset); and after mac001> svn update At revision 6873. I get Error using cell/unique (line 45) Input must be a cell array of strings. Error in match_str (line 63) [dum1, dum2, c] = unique([a; b]); Error in channelposition (line 296) [sel1, sel2] = match_str(sens.label, lab); Error in ft_datatype_sens (line 123) [chanpos, chanori, lab] = channelposition(sens, 'channel', 'all'); Error in ft_read_header (line 1720) hdr.grad = ft_datatype_sens(hdr.grad); which means that the latest version of the code still has problems with this specific file.

Robert Oostenveld - 2012-11-06 09:15:46 +0100

(In reply to comment #7) sens = coilpos: [509x3 double] coilori: [509x3 double] tra: [305x509 double] unit: 'cm' label: {305x1 cell} type: 'neuromag306' There seems to be one channel missing. in > In fileio/private/channelposition at 288 In fileio/private/ft_datatype_sens at 123 In ft_read_header at 1720 this results into ... 'MEG2131' 'MEG2132' 'MEG2133' 'MEG2141' 'MEG2142' 'MEG2143' 'MEG2211' 'MEG2212' 'MEG2213' 'MEG2221' 'MEG2222' 'MEG2223' 'MEG2231' 'MEG2232' 'MEG2233' [] 'MEG2242' 'MEG2243' 'MEG2311' 'MEG2312' 'MEG2313' 'MEG2321' 'MEG2322' 'MEG2323' 'MEG2331' 'MEG2332' 'MEG2333' 'MEG2341' 'MEG2342' 'MEG2343' 'MEG2411' 'MEG2412' 'MEG2413' ... which causes match_str to fail. The question is: why is this one channel missing?

Robert Oostenveld - 2012-11-06 09:19:04 +0100

(In reply to comment #8) > The question is: why is this one channel missing? K>> dbstack > In fileio/private/mne2grad at 101 In ft_read_header at 1260 K>> length(strmatch('MEG', {orig.chs.ch_name})) ans = 305 So the channel is actually missing from the structure returned by the fif reading functions. Hence I would conclude that mne2grad is performing correctly in returning 305 channels. However, there is still an issue with the channelposition function.

Robert Oostenveld - 2012-11-06 09:47:01 +0100

I checked with an other neuromag fif file that does have 306 channels, and it works without a flaw. The problem is in the missing channel, which channelposition does not expect. The neuromag306 section in that function makes one position per triplet of channels. Then later around line 296 it copies the one position to each of the three channels (2*plan+1*mag) at each sensor location. That is where it fails. The relevant code failing can be simulated with >> match_str({'a', 'b', 'c'}, {'b', []}) Error using cell/unique (line 45) Input must be a cell array of strings. Error in match_str (line 63) [dum1, dum2, c] = unique([a; b]); which is because match_str does not expect the empty [] element in the cell array. Had it been an empty string '', then it would not have been a problem. I have enhanced the match_str function to deal with this mac001> svn commit utilities/match_str.m Sending utilities/match_str.m Transmitting file data . Committed revision 6874. mac001> svn update U plotting/private/match_str.m U fileio/private/match_str.m U forward/private/match_str.m Updated to revision 6875. following this update, the test script does not crash any more and returns >> hdr hdr = label: {330x1 cell} nChans: 330 Fs: 1000 grad: [1x1 struct] elec: [1x1 struct] chanunit: {1x330 cell} nSamples: 1001 nSamplesPre: 0 nTrials: 1 orig: [1x1 struct] chantype: {330x1 cell} >> grad grad = chanori: [305x3 double] chanpos: [305x3 double] chantype: {305x1 cell} chanunit: {305x1 cell} coilori: [509x3 double] coilpos: [509x3 double] label: {305x1 cell} tra: [305x509 double] type: 'neuromag306' unit: 'cm' which seems correct to me. @Tim, please update to a FieldTrip version equal to or later than SVN revision 6875. This evening it will be on the server.

Robert Oostenveld - 2012-12-31 11:46:22 +0100

closed several bugs that have been resolved for some time. Feel free to reopen the bug if you disagree.