def get_epochs(filename):
    '''
    Produce epoch object from raw file. E.g. detect eye-tracking artifacts and
    chunk into epochs. This method will cache and save it's results to disk.
    '''
    raw = mne.io.read_raw_ctf(filename)
    # Detect blinks.
    raw.annotations = artifacts.annotate_blinks(
        raw,
        ch_mapping={
            'x':
            'UADC001-3705',  # These are the eye-tracking channels in the MEG data.
            'y': 'UADC003-3705',
            'p': 'UADC004-3705'
        })
    # Finds triggers in default trigger channels
    events = preprocessing.get_events(raw)[0]
    # We are only interested in triggers 11, 31 and 51
    ide = np.in1d(events[:, 2], [11, 31, 51])
    target_events = events[ide, :]
    # Cut out epochs. Reject peak-to-peak distances with 4e-12 threshold
    epochs = mne.Epochs(raw,
                        target_events,
                        tmin=-0.2,
                        tmax=0.9,
                        baseline=(-0.1, 0),
                        reject=dict(mag=4e-12))
    # Actually load the data and resample to 300Hz.
    epochs.load_data()
    epochs.resample(300, n_jobs=4)
    return epochs
def blocks(raw, full_file_cache=False):
    '''
    Return a dictionary that encodes information about trials in raw.
    '''
    if full_file_cache:
        trigs, buts = pymegprepr.get_events_from_file(raw.info['filename'])
    else:
        trigs, buts = pymegprepr.get_events(raw)
    es, ee, trl, bl = metadata.define_blocks(trigs)
    return {'start': es, 'end': ee, 'trial': trl, 'block': bl}
def get_meta(data, raw, snum, block, filename):
    '''
    Return meta and timing data for a raw file and align it with behavioral data.

    Parameters
    ----------
    data : DataFrame
        Contains trial meta data from behavioral responses.
    raw : mne.io.raw objects
        MEG data that needs to be aligned to the behavioral data.
    snum : int
        Subject number that this raw object corresponds to.
    block : int
        Block within recording that this raw object corresponds to.

    Note: Data is matched againts the behavioral data with snum, recording, trial
    number and block number. Since the block number is not encoded in MEG data it
    needs to be passed explicitly. The order of responses is encoded in behavioral
    data and MEG data and is compared to check for alignment.
    '''
    trigs, buts = pymegprepr.get_events(raw)
    es, ee, trl, bl = metadata.define_blocks(trigs)

    megmeta = metadata.get_meta(trigs,
                                es,
                                ee,
                                trl,
                                bl,
                                metadata.fname2session(filename),
                                snum,
                                buttons=buts)
    assert len(np.unique(megmeta.snum) == 1)
    assert len(np.unique(megmeta.day) == 1)
    assert len(np.unique(megmeta.block_num) == 1)

    dq = data.query('snum==%i & day==%i & block_num==%i' %
                    (megmeta.snum.ix[0], megmeta.day.ix[0], block))
    # dq.loc[:, 'trial'] = data.loc[:, 'trial']
    trial_idx = np.in1d(dq.trial, np.unique(megmeta.trial))
    dq = dq.iloc[trial_idx, :]
    dq = dq.set_index(['day', 'block_num', 'trial'])
    megmeta = metadata.correct_recording_errors(megmeta)
    megmeta.loc[:, 'block_num'] = block

    megmeta = megmeta.set_index(['day', 'block_num', 'trial'])
    del megmeta['snum']
    meta = pd.concat([megmeta, dq], axis=1)
    meta = metadata.cleanup(meta)  # Performs some alignment checks
    cols = [x for x in meta.columns if x[-2:] == '_t']
    timing = meta.loc[:, cols]
    return meta.drop(cols, axis=1), timing
def blocks_from_marker(raw, sfreq=1200.0):
    """
    Find breaks to cut data into pieces.

    Returns a dictionary ob blocks with a namedtuple that
    defines start and end of a block. Start and endpoint
    are inclusive (!).    
    """
    events, _ = get_events(raw)
    onsets = events[events[:, 2] == 34, 0] / sfreq / 60
    ends = events[events[:, 2] == 33, 0] / sfreq / 60

    Block = namedtuple("Block", ["start", "end"])
    blocks = {
        block: Block(start, end) for block, (start, end) in enumerate(zip(onsets, ends))
    }
    return blocks
예제 #5
0
def blocks_from_marker(subject, raw, sfreq=1200.):
    '''
    Find breaks to cut data into pieces.

    Returns a dictionary ob blocks with a namedtuple that
    defines start and end of a block. Start and endpoint
    are inclusive (!).    
    '''
    events, _ = get_events(raw)
    if subject <= 2:
        #no triggers for start/endblock, so manually provide onset/end times
        trials = events[events[:,2]==42,0] #find all coh motion onsets
        assert(len(trials)==480)
        onsets = trials[[0, 120, 240, 360]]  / sfreq / 60 #manually select start & endpoint
        ends = trials[[119, 239, 359, 480-1]]  / sfreq / 60
    else:   
        onsets = events[events[:,2]==34, 0] / sfreq / 60
        ends = events[events[:,2]==33, 0] / sfreq / 60    

    Block = namedtuple('Block', ['start', 'end'])
    blocks = {block: Block(start, end) for block, (start, end)
              in enumerate(zip(onsets, ends))}
    return blocks