mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.7k stars 1.31k forks source link

ENH: Make the extract_label_time_course work with stcs that does not contain a full source space #1146

Closed MadsJensen closed 7 years ago

MadsJensen commented 10 years ago

Make extract_label_time_course compatible with stcs without a full source space.

E.g.parsing label to apply_inverse_raw will result in stc with a not full source space making it hard to extract source time courses as extract_label_time_course will fail.

Goal. redo extract_label_time_course to accept stc without a full source space.

@agramfort

dengemann commented 10 years ago

ping @agramfort I do need this right now. What's the cleanest way to bypass the source space lookup?

dengemann commented 10 years ago

@agramfort can't we simply create a fake label_veridx list inside _gen_extract_label_time_course or simply move the extraction to another private function we dispatch without the source space lookups in case the source_estimate instance method is called from a sub-stc?

agramfort commented 10 years ago

we should just check that the vertices required are in the source space. And raise an exception otherwise.

dengemann commented 10 years ago

@agramfort it's more complicated. The sign flipping functions require full source spaces and there are a couple of additional checks for source spaces ...

agramfort commented 10 years ago

the sign flip should only require the normals of the label of interest. But I agree it might be a bit tricky...

dengemann commented 10 years ago

@agramfort I see. Will look into it somewhen else ...

ImmanuelSamuel commented 8 years ago

Any plans on getting this running? Any alternate route to get the label time series?

agramfort commented 8 years ago

please share a code snippet using sample data that creates the issue

ImmanuelSamuel commented 8 years ago

Do a recon-all on colin27 t1 image - http://www.bic.mni.mcgill.ca/ServicesAtlases/Colin27 then do

src = mne.setup_source_space('colin27', spacing='oct6', subjects_dir=<subject-dir>)

The following will not work if you run mne_watrershed_bem to get the surf files.

model = mne.make_bem_model('colin27',subjects_dir=<subject-dir>)
RuntimeError: Surface inner skull is not completely inside surface outer skull.

So just use the surf files from spm/canonical folder which is based on colin27. This is where the problem occurs.

Then do

bem_sol = mne.make_bem_solution(model) 
fwd = mne.make_forward_solution(info, trans, src, bem_sol)

What happens is that when you are making the forward model some sources are removed because they are outside the inner skull surface. This makes the stc estimates incompatible with the source space. I am thinking that this problem may be resolved if you can get the mne_wartershed_bem to work on the colin27 T1 image such that mne.make_bem_model() doesn't return the error.

dgwakeman commented 8 years ago

@ImmanuelSamuel removing the points is the correct thing. What is the problem? Can you show the error?

ImmanuelSamuel commented 8 years ago

The error occurs when extracting labels time course. The function checks if the number of source estimates are the same as the number of sources in the source space. Since they are different because of the dropped sources while calculating the fwd solution there is an error that says "stc and source space are incompatible"

ImmanuelSamuel commented 8 years ago

I thought that the make source space function used all the surfaces generated by recon-all and then generated the sources which would mean that most sources should be within the inner skull surface. I am having about 750 sources being removed of the 4096. That is a lot.

dgwakeman commented 8 years ago

Two things:

  1. You can improve this ratio by not using such a radically downsampled surface
  2. You still haven't provided an error (is there one?). You must remove points outside the inner-skull: it doesn't make sense with a BEM to calculate a source from a location outside the innermost layer. 2a. Furthermore, even if you could the results you would get there would be innacurate. 2b. ideally you should from just inside of it as well using the mindist and a value around ~4-5mm assuming a BEM calculated with ico 4ish.
ImmanuelSamuel commented 8 years ago

Ah I see. I initially assumed that the algo only creates sources inside the brain in the first place and then removes the points outside that might occur due to some error. From what you are saying I am thinking the sources are first generated across the entire head mask and then points outside the brain mask is then removed. I mentioned the error in my previous comment. ValueError: stc not compatible with source space in _gen_extract_label_time_course(stcs, labels, src, mode, allow_empty, verbose)

There is another error that occurs when I run make _bem_model on the colin27 recon-all folder RuntimeError: Surface inner skull is not completely inside surface outer skull

ImmanuelSamuel commented 8 years ago

Also if removing the sources is done typically as you mentioned. Why is there a check in _gen_extract_label_time_course.

image The two numbers must be the same for the check to pass

dgwakeman commented 8 years ago

Yeah, first you define your source space, which is based off the lh.white/rh.white. These are then adjusted when you calculate the forward model.

hmmm. yeah, it sounds like that function might be better off asking for the fwd model in that script, and then pull src from withing fwd solution file, so people don't run into that error.

ImmanuelSamuel commented 8 years ago

I have only briefly looked into the code. Need this to work soon. Will open a PR if I can somehow get this to work. Just getting better at python (I am from the matlab world). Any ideas on the other error?

There is another error that occurs when I run make _bem_model on the colin27 recon-all folder RuntimeError: Surface inner skull is not completely inside surface outer skull

dgwakeman commented 8 years ago

Use the BEM surfaces from the spm cannonical directory, don't generate your own. I did a lot of work to get it to work on that. Use them, it will save you time.

ImmanuelSamuel commented 8 years ago

Yeah I already got SPM surfaces working. I was just thinking that if I have a group of subjects I can then make a surface of the average. If you have time to spare please give me a high-level overview of the things you did to get it working. I might try it later. Thanks.

dgwakeman commented 8 years ago

huh? I don't understand?

ImmanuelSamuel commented 8 years ago

You mentioned that you did a lot of work to get the colin27 surfaces. Can you briefly mention the steps you did to get it?

dgwakeman commented 8 years ago

It was a lot of tweaking and using lots of different freesurfer tools. What are you trying to do?

ImmanuelSamuel commented 8 years ago

Say I have a group of subjects' T1. If I can make a surface for the non-linear averaged T1 for the group wouldn't that make the source estimates better?

dgwakeman commented 8 years ago

If you have the individual's MRIs you should use them.

If you mean you have a group of other people's MRIs and want to use them: No, it won't be better, there are costs to averaging and you get strange shapes, which cause their own problems. If you want proof, load Colin's pial surface in Freeview and compare it to the pial surface of fsaverage. You don't want those effects. hth d

ImmanuelSamuel commented 8 years ago

Ah I see thanks for the explanation. Very insightful.