mne-python icon indicating copy to clipboard operation
mne-python copied to clipboard

Python based `make_forward_solution()` fails with KIT files

Open christianbrodbeck opened this issue 13 years ago • 55 comments

@Eric89GXL @t3on When trying to run make_forward_solution() with a raw file converted from KIT data I get the error below.

KIT files contain channels for measuring environment noise which we use during pre-processing, and those channels are selected by mne.fiff.pick_types(..., ref_meg=True). However it seems those channels then don't satisfy some criterion downstream. I don't assume they should be used by the forward solution.

>>> info = '/Volumes/MyBook/araref/meg/AD001/raw/AD001_araref_0-40-raw.fif'
>>> mri = '/Volumes/MyBook/araref/meg/AD001/raw/AD001-trans.fif'
>>> src = '/Volumes/MyBook/araref/mri/AD001/bem/AD001-ico-4-src.fif'
>>> bem = '/Volumes/MyBook/araref/mri/AD001/bem/AD001-inner_skull-bem-sol.fif'
>>> fwd = mne.make_forward_solution(info, mri, src, bem)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/christian/Dropbox/scripts/test mne/make_forward_solution.py", line 6, in <module>
    fwd = mne.make_forward_solution(info, mri, src, bem)
  File "/Users/christian/Documents/Eclipse/projects/mne-python/mne/utils.py", line 384, in dec
    return function(*args, **kwargs)
  File "/Users/christian/Documents/Eclipse/projects/mne-python/mne/forward/_make_forward.py", line 369, in make_forward_solution
    meg_xform, coil_type='meg')
  File "/Users/christian/Documents/Eclipse/projects/mne-python/mne/forward/_make_forward.py", line 149, in _create_coils
    coils.append(_create_meg_coil(coilset, ch, acc, t))
  File "/Users/christian/Documents/Eclipse/projects/mne-python/mne/forward/_make_forward.py", line 88, in _create_meg_coil
    '(type = %d acc = %d)' % (ch['coil_type'], acc))
RuntimeError: Desired coil definition not found (type = 0 acc = 1)

christianbrodbeck avatar Nov 09 '13 19:11 christianbrodbeck

If there is a KIT file in the test suite, I can take a look on Monday, fix, and add a test.

larsoner avatar Nov 09 '13 21:11 larsoner

Great, thanks.

larsoner avatar Nov 10 '13 03:11 larsoner

@christianmbrodbeck can you tell me how to use mne_kit2fiff with the test files in the repo? I can't get it to work, and I'd like to test end-to-end using C code (mne_kit2fiff followed by do_forward_solution) versus Python (read_raw_kit followed by make_forward_solution). This is what I've tried, but I get a segfault in C:

mne_kit2fiff
--out test_kit_raw.fif
--elp /home/larsoner/custombuilds/mne-python/mne/fiff/kit/tests/data/test_elp.txt
--hsp /home/larsoner/custombuilds/mne-python/mne/fiff/kit/tests/data/test_hsp.txt
--sns /home/larsoner/custombuilds/mne-python/mne/fiff/kit/tests/data/test.sqd
--hpi /home/larsoner/custombuilds/mne-python/mne/fiff/kit/tests/data/test_mrk.sqd

How should I fix it?

By the way, @t3on we should probably have an equivalency test in test_kit.py that compares the output of the C code conversion tool with doing read_raw_kit followed by save(). I can add this functionality while fixing this bug, because it will make clear whether the bug has to do with reading KIT (unlikely) or with using KIT information appropriately in forward solution creation (more likely).

larsoner avatar Nov 12 '13 18:11 larsoner

@Eric89GXL Some of the input files are different (and not all are included in the repo because they are not needed any longer for the python kit2fiff conversion) but test_bin.fif in the test data folder was produced with C mne_kit2fiff. If I remember correctly though the python version includes more channels than the C version did (@t3on?)

christianbrodbeck avatar Nov 12 '13 22:11 christianbrodbeck

Cool, so long as test_bin.fif was produced with mne_kit2fiff, then that's what I need. I should be able to work on fixing this now.

larsoner avatar Nov 12 '13 22:11 larsoner

@christianmbrodbeck @t3on Python and C give equivalent results when I operate on the mne_kit2fiff'ed version of the file, suggesting that the RawKIT class reading function is not converting all the necessary fields of raw.info correctly. I'll see if I can find a fix relatively quickly.

larsoner avatar Nov 12 '13 22:11 larsoner

If I remember correctly though the python version includes more channels than the C version did

yeah, it does. this is because I included the miscellaneous channels in the fif.

teonbrooks avatar Nov 12 '13 22:11 teonbrooks

I think the C version of mne_kit2fiff did not include the reference channels from the KIT file in the fif. These seem to be the channels that confuse the Python forward solution generation. Would reference channels be used at all for the forward solution? Are they somehow mistagged by RawKIT, or just not properly excluded by make_forward?

christianbrodbeck avatar Nov 12 '13 22:11 christianbrodbeck

Good question. I'm going to use viz.compare_fiff() to see which fields are different.

larsoner avatar Nov 12 '13 22:11 larsoner

Guys, I'm not sure fwd is defined in that case. For 4D / CTF ref channels are included via comp. Is there a compensation handling available for Kit in MNE-C?

dengemann avatar Nov 12 '13 22:11 dengemann

@dengemann the C code allows running forward generation on mne_fiff2kit files, so I'm assuming it has been handled appropriately...

larsoner avatar Nov 12 '13 22:11 larsoner

Well the C code handled it by ignoring our ref channels

christianbrodbeck avatar Nov 12 '13 22:11 christianbrodbeck

Well the C code handled it by ignoring our ref channels

@Eric89GXL this is what I had in mind from studying the manual, no 'natural' ref channel support for KIT.

dengemann avatar Nov 12 '13 22:11 dengemann

Interesting... I don't know enough about KIT to know how this should be handled. I can at least make it work the same way as the C code. And I can tell you that the C and Python code can do compensation for CTF files, if that's anything like what needs to happen for KIT.

larsoner avatar Nov 12 '13 22:11 larsoner

And I can tell you that the C and Python code can do compensation for CTF files, if that's anything like what needs to happen for KIT.

the problem will be with the constants. CTF and 4D compensation use the same structure in MNE-C, the only difference is that in the constants 4D compensation has another state flag, 101 IIRC. The problem is we need to know what kind of compensation is applied to the data. If it's the same type as in 4D one could use that constant (I'd shoot for 4D since KIT is also a pure magnetometer system IIRC)>

dengemann avatar Nov 12 '13 22:11 dengemann

@christianmbrodbeck this would imply that you need to have some sort of compensation weight table to populate the named matrix struct that is used by MNE to handle 4D / CTF compensation. But I would carefully inquire and test this.

dengemann avatar Nov 12 '13 22:11 dengemann

I would not worry about this too much: the correction is small due to the relative large distance of the compensation channels from the brain, anyway. - Matti

Sent from my iPad

On Nov 12, 2013, at 2:47 PM, "Denis A. Engemann" [email protected] wrote:

@christianmbrodbeck this would imply that you need to have some sort of compensation weight table to populate the named matrix struct that is used by MNE to handle 4D / CTF compensation. But I would carefully inquire and test this.

— Reply to this email directly or view it on GitHub.

The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.

mshamalainen avatar Nov 12 '13 22:11 mshamalainen

Before converting our files to fif we perform CALM which if I understand it correctly works based on the same principle (subtracting a weighted sum of the reference channels form each sensor data channel) so I think at the point of creating the forward solution we should just ignore the reference channels?

christianbrodbeck avatar Nov 12 '13 23:11 christianbrodbeck

We use a similar procedure here and never included the ref channels in any forward model. I agree one should not worry too much. I guess it's not worth the effort unless you have too much spare time ...

dengemann avatar Nov 12 '13 23:11 dengemann