LABSN / mnefun

Facilitating lab integration with mne-python
BSD 3-Clause "New" or "Revised" License
6 stars 15 forks source link

ICA artifact rejection #183

Closed ktavabi closed 7 years ago

ktavabi commented 7 years ago

@larsoner Does this look like anything you are doing for implementing ICA in MNEFUN?

    raw = io.read_raw_fif(op.join(workdir, 'skubi_%s' % subject, 'sss_fif',
                                  'skubi_%s_raw_sss.fif' % subject),
                          preload=True)
    raw.filter(1, 45, n_jobs='cuda', l_trans_bandwidth=0.5,
               h_trans_bandwidth=0.5, filter_length='10s',
               phase='zero-double', fir_design='firwin2')
    picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False,
                           stim=False, exclude='bads')
    raw.annotations = mne.Annotations([1], [10], 'BAD')
    ica = ICA(n_components=.95, method='infomax')
    ica.fit(raw, picks=picks, decim=3, reject=dict(mag=4e-12, grad=4000e-13))
    # maximum number of components to reject
    n_max_ecg = 3
    # generate ECG epochs use detection via phase statistics
    ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5, picks=picks,
                                   ch_name=None)
    ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps',
                                         ch_name=None)
    if scores.max() < .95 and len(ecg_inds) == 0:
        print ('\n      No significant IC found for %s' % subject)
        continue
    ecg_inds = ecg_inds[:n_max_ecg]
    ica.exclude += ecg_inds
    # estimate average artifact
    ecg_evoked = ecg_epochs.average()
    ica.save(op.join(workdir, 'skubi_%s' % subject, 'sss_???_fif',
                     'preproc_ecg-ica.fif'))

I am also plotting and saving ICA properties, scores, components, latent source erfs, erfs before/after projection but left out the statements in gist. WDYT about sticking into mnefun?

larsoner commented 7 years ago

I have shamefully little experience with ICA. Do you want to try opening a PR to add it? Perhaps we could have a second example directory (pulling and processing the same funloc dataset) that uses ICA. That way I could test it.

@drammock you used ICA a bit for EEG data, would you be interested in this?

drammock commented 7 years ago

I basically never use mnefun (not really sure why I'm an admin, to be honest). I played a bit with using ICA for blink processing on EEG data, but ended up doing SSP instead.

That said, my limited understanding of mnefun is that it's for ILABS-specific aspects of the M/EEG analysis pipeline. This seems potentially fairly user-specific, and doesn't appear to have any ILABS-specific bits like copying to/from the various acquisition or computation servers. so, -$0.02

larsoner commented 7 years ago

mnefun does have some ILABS-specific stuff (e.g., data fetchers and MaxFilter runners) but it's really more general. In theory I/we should be using it to pipeline our EEG analyses, too (basically your YAML is the equivalent to the analysis script in mnefun.), but it didn't work all that well when I tried it. It's missing some steps for e.g. manual annotation, variable numbers of projectors per subject, etc. that I found easier to code separately. But maybe someday we can make it easily usable for it.

ktavabi commented 7 years ago

So No and I owe you some change?! 😏 BTW how come you ended opting for SSP over ICA in your EEG data?

drammock commented 7 years ago

how come you ended opting for SSP over ICA in your EEG data?

because it worked better, or at least it was easier to get it working reliably with my data.

The other bit was an obtuse joke, combining "-1" with "my 2 cents", hopefully also implying that a -1 from me isn't worth much on this repo.

drammock commented 7 years ago

@larsoner If I ever get to the point where I do two experiments in a row that have basically the same pipeline, maybe I'll think about adding relevant bits to mnefun. So far each new experiment is a snowflake.

larsoner commented 7 years ago

So far each new experiment is a snowflake.

The idea with expyfun is that it takes you from raw data to Epochs (and Evoked if you want it), taking care of / unifying all preprocessing steps. It doesn't try to do any analyses after that (stats, ML, etc.). So in that sense I imagine all of your experiments probably already fit into mnefun's designed use case, no?

ktavabi commented 7 years ago

@larsoner I'll keep chipping away at this for myself, if and when it gets to a point where I can apply to...

a second example directory (pulling and processing the same funloc dataset) that uses ICA.

I'll open a PR. Feel free to close the issue.

drammock commented 7 years ago

I imagine all of your experiments probably already fit into mnefun's designed use case, no?

I'm guessing not. This README describes my current pipeline, in which preprocessing involves (among other things)

larsoner commented 7 years ago

Points 1 and 3 can be done in a scoring function, so no problem there.

Point 2 is harder. It could be iterations of running a script and adjusting the scoring function, but integrating a new annotation functionality would be useful. It's probably something we need to add at some point anyway.

Is DSS done on raw or epochs?

In any case, it sounds to me like integration would be mutually beneficial in the long run. mnefun would get more functionality, and you would benefit from the sanity checks and other improvements already in the mnefun pipeline.

drammock commented 7 years ago

Is DSS done on raw or epochs?

operator is computed from epochs, can be applied to either Raw or Epochs.

integration would be mutually beneficial in the long run

I'll think about it. I try to only introduce 1 new technology per project; when I start the next one I'll take a look.