예제 #1
0
def test_compute_ica():
    """Test compute ICA on raw data."""

    ecg_ch_name = 'ECG'
    eog_ch_name = 'HEOG, VEOG'
    variance = 0.5
    reject = dict(mag=5e-12, grad=5000e-13)

    # read raw data
    raw = mne.io.read_raw_fif(raw_fname)

    # save short segment of raw data
    segment_raw_fname = raw_fname.replace('raw.fif', '0_60s_raw.fif')
    raw.save(segment_raw_fname, tmin=0, tmax=60, overwrite=True)

    # create CompIca node to compute ica on raw data
    ica_node = pe.Node(interface=CompIca(), name='ica')
    ica_node.inputs.fif_file = segment_raw_fname
    ica_node.inputs.raw_fif_file = segment_raw_fname
    ica_node.inputs.ecg_ch_name = ecg_ch_name
    ica_node.inputs.eog_ch_name = eog_ch_name
    ica_node.inputs.n_components = variance
    ica_node.inputs.reject = reject

    ica_node.run()

    assert ica_node.result.outputs.ica_file
    assert ica_node.result.outputs.ica_sol_file
    assert ica_node.result.outputs.ica_ts_file
    assert ica_node.result.outputs.report_file

    raw_cleaned = mne.io.read_raw_fif(ica_node.result.outputs.ica_file)

    assert raw.get_data().shape == raw_cleaned.get_data().shape
예제 #2
0
def ica(n_components, ecg_ch_name, eog_ch_name):
    """Compute ica solution for raw fif file"""
    from ephypype.interfaces.mne.preproc import CompIca
    ica_node = pe.Node(interface=CompIca(), name='ica')
    ica_node.inputs.n_components = n_components
    ica_node.inputs.ecg_ch_name = ecg_ch_name
    ica_node.inputs.eog_ch_name = eog_ch_name
    return ica_node
예제 #3
0
def test_set_IC_components():
    """Test compute ICA on raw data."""

    ecg_ch_name = 'ECG'
    eog_ch_name = 'HEOG, VEOG'
    variance = 0.5
    reject = dict(mag=5e-12, grad=5000e-13)

    # read raw data
    raw = mne.io.read_raw_fif(raw_fname)

    # save short segment of raw data
    segment_raw_fname = raw_fname.replace('raw.fif', '0_60s_raw.fif')
    raw.save(segment_raw_fname, tmin=0, tmax=60, overwrite=True)

    # create CompIca node to compute ica on raw data
    ica_node = pe.Node(interface=CompIca(), name='ica')
    ica_node.inputs.fif_file = segment_raw_fname
    ica_node.inputs.raw_fif_file = segment_raw_fname
    ica_node.inputs.ecg_ch_name = ecg_ch_name
    ica_node.inputs.eog_ch_name = eog_ch_name
    ica_node.inputs.n_components = variance
    ica_node.inputs.reject = reject

    ica_node.run()

    n_comp_exclude = {'sample': {'run': [0, 1, 2]}}
    ts_file, chs_file, chs_n_file, sf = _preprocess_set_ica_comp_fif_to_ts(
        ica_node.result.outputs.ica_file, 'sample', n_comp_exclude, True)

    assert ts_file
    assert chs_file
    assert chs_n_file
    assert sf

    data = np.load(ts_file)

    idx = mne.pick_types(raw.info, meg=True)
    assert raw.get_data()[idx, :].shape == data.shape
예제 #4
0
def create_pipeline_preproc_meeg(
        main_path,
        pipeline_name='preproc_meeg',  # noqa
        data_type='fif',
        l_freq=1,
        h_freq=150,
        down_sfreq=300,
        is_ICA=True,
        variance=0.95,
        ECG_ch_name='',
        EoG_ch_name='',
        reject=None,
        is_set_ICA_components=False,
        n_comp_exclude=[],
        is_sensor_space=True):
    """Preprocessing pipeline.

    Parameters
    ----------
    main_path : str
        main path to of the pipeline
    pipeline_name: str (default 'preproc_meeg')
        name of the pipeline
    data_type: str (default 'fif')
        data type: 'fif' or 'ds'
    l_freq: float (default 1)
        low cut-off frequency in Hz
    h_freq: float (default 150)
        high cut-off frequency in Hz
    down_sfreq: float (default 300)
        sampling frequency at which the data are downsampled
    is_ICA : boolean (default True)
        if True apply ICA to automatically remove ECG and EoG artifacts
    variance: float (default 0.95)
        float between 0 and 1: the ICA components will be selected by the
        cumulative percentage of explained variance
    ECG_ch_name: str (default '')
        the name of ECG channels
    EoG_ch_name: str (default '')
        the name of EoG channels
    reject: dict | None
        rejection parameters based on peak-to-peak amplitude. Valid keys
        are 'grad' | 'mag' | 'eeg' | 'eog' | 'ecg'. If reject is None then
        no rejection is done
    is_set_ICA_components: boolean (default False)
        set to True if we had the ICA of the raw data, checked the Report
        and want to exclude some ICA components based on their topographies
        and time series
        if True, we have to fill the dictionary variable n_comp_exclude
    n_comp_exclude: dict
        if is_set_ICA_components=True, it has to be a dict containing for
        each subject and for each session the components to be excluded
    is_sensor_space: boolean (default True)
        True if we perform the analysis in sensor space and we use the
        pipeline as lego with the connectivity or inverse pipeline

    Inputs (inputnode)
    ------------------
    raw_file : str
        path to raw meg data in fif format
    subject_id : str
        subject id

    Outputs
    -------
    pipeline : instance of Workflow
    """
    from ephypype.interfaces.mne.preproc import PreprocFif
    from ephypype.interfaces.mne.preproc import CompIca
    from ephypype.nodes.import_data import ConvertDs2Fif
    from ephypype.preproc import preprocess_set_ica_comp_fif_to_ts
    from nipype.interfaces.utility import IdentityInterface, Function

    import nipype
    print((nipype.__version__))

    import nipype.pipeline.engine as pe

    pipeline = pe.Workflow(name=pipeline_name)
    pipeline.base_dir = main_path

    print(('*** main_path -> %s' % main_path + ' ***'))

    # define the inputs of the pipeline
    inputnode = pe.Node(IdentityInterface(fields=['raw_file', 'subject_id']),
                        name='inputnode')

    if data_type is 'ds':
        ds2fif_node = pe.Node(interface=ConvertDs2Fif(), name='ds2fif')
        pipeline.connect(inputnode, 'raw_file', ds2fif_node, 'ds_file')

    # preprocess
    if not is_set_ICA_components:
        preproc_node = pe.Node(interface=PreprocFif(), name='preproc')

        preproc_node.inputs.l_freq = l_freq
        preproc_node.inputs.h_freq = h_freq
        preproc_node.inputs.down_sfreq = down_sfreq

        if data_type is 'ds':
            pipeline.connect(ds2fif_node, 'fif_file', preproc_node, 'fif_file')
        elif data_type is 'fif':
            pipeline.connect(inputnode, 'raw_file', preproc_node, 'fif_file')

    if is_ICA:
        if is_set_ICA_components:
            inp = [
                'fif_file', 'subject_id', 'n_comp_exclude', 'is_sensor_space'
            ]
            out = [
                'out_file', 'channel_coords_file', 'channel_names_file',
                'sfreq'
            ]
            fcn = preprocess_set_ica_comp_fif_to_ts
            ica_node = pe.Node(interface=Function(input_names=inp,
                                                  output_names=out,
                                                  function=fcn),
                               name='ica_set_comp')

            ica_node.inputs.n_comp_exclude = n_comp_exclude
            ica_node.inputs.is_sensor_space = is_sensor_space

            pipeline.connect(inputnode, 'raw_file', ica_node, 'fif_file')
            pipeline.connect(inputnode, 'subject_id', ica_node, 'subject_id')

        else:

            ica_node = pe.Node(interface=CompIca(), name='ica')
            ica_node.inputs.n_components = variance
            ica_node.inputs.ecg_ch_name = ECG_ch_name
            ica_node.inputs.eog_ch_name = EoG_ch_name

            if reject:
                ica_node.inputs.reject = reject

            pipeline.connect(preproc_node, 'fif_file', ica_node, 'fif_file')

    return pipeline