def test_plot3d_surf_value_colorbar():

    surf = Surf(surf_path)

    v = Viz3()
    v.add_surf(surf, values=surf.vert[:, 2], limits_c=(0, 50), colorbar=True)

    v.save(VIZ_PATH / 'viz3_04_surf_colorbar.png')
    v.close()
def test_plot3d_surf_color():

    surf = Surf(surf_path)

    v = Viz3()
    v.add_surf(surf, color=(.5, .5, 0))

    v.save(VIZ_PATH / 'viz3_02_surf_color.png')
    v.close()
def test_plot3d_surf_value():

    surf = Surf(surf_path)

    v = Viz3()
    v.add_surf(surf, values=surf.vert[:, 2], limits_c=(0, 50))

    v.save(VIZ_PATH / 'viz3_03_surf_values.png')
    v.close()
def test_plot3d_surf():

    surf = Surf(surf_path)

    v = Viz3()
    v.add_surf(surf)

    v.save(VIZ_PATH / 'viz3_01_surf.png')
    v.close()
def test_plot3d_surf_chan_color_one_alpha():

    surf = Surf(surf_path)
    channels = Channels(chan_path)

    v = Viz3()
    v.add_chan(channels, color=(1, 0, 0), alpha=0.5)
    v.add_surf(surf, alpha=.5)

    v.save(VIZ_PATH / 'viz3_07_surf_chan_color_one_alpha.png')
    v.close()
def test_plot3d_surf_chan():

    surf = Surf(surf_path)
    channels = Channels(chan_path)

    v = Viz3()
    v.add_chan(channels)
    v.add_surf(surf, alpha=.8)

    v.save(VIZ_PATH / 'viz3_05_surf_chan.png')
    v.close()
def test_plot3d_surf_chan_color_everyother():

    from numpy import tile

    surf = Surf(surf_path)
    channels = Channels(chan_path)

    color_everyother = tile([[1, 0, 0], [0, 0, 1]], (14, 1))

    v = Viz3()
    v.add_chan(channels, color=color_everyother)
    v.add_surf(surf, alpha=.5)

    v.save(VIZ_PATH / 'viz3_08_surf_chan_color_everyother.png')
    v.close()
def test_plot3d_surf_chan_values():

    from numpy import arange

    surf = Surf(surf_path)
    channels = Channels(chan_path)

    v = Viz3()
    v.add_chan(channels, values=arange(channels.n_chan), colorbar=True)
    v.add_surf(surf, alpha=.5)

    v.save(VIZ_PATH / 'viz3_09_surf_chan_values.png')
    v.close()

    # reuse the limits_c
    v.add_chan(channels, values=arange(channels.n_chan))
示例#9
0
def test_source_morph():
    surf = Surf(surf_path)
    Morph(from_surf=surf)
示例#10
0
def test_Surf_02():
    Surf(str(surf_path))
示例#11
0
def test_Surf_01():
    Surf(surf_path)
示例#12
0
def load(what, parameters, ieeg_file, opts=None):
    """
    WHAT:
      - 'continuous' returns: ChanTime, event_names, events_onsets
      - 'data' returns: ChanTime, event_names
      - 'events' returns: ndarray
      - 'dataglove' returns: ndarray
      - 'electrodes'
      - 'freesurfer'
      - 'pial' returns original freesurfer pial mesh
      - 'surface' returns full mesh which should better reflect brain size
      - 'aparc'
      - 'aparc.a2009s'
      - 'aparc.DKTatlas'
      - 'BA_exvivo'

    EVENT_TYPE:
      - cues : all cues (to open and close)
      - open : cues to open fingers
      - close : cues to close fingers
      - movements : all actual movements (from dataglove)
      - extension : actual extension of all fingers
      - flexion : actual flexion of all fingers
      - realigned : realigned movement
    """
    if opts in ('data', 'continuous'):
        if opts is None:
            raise ValueError('You need to specify event_type')
        if opts not in ['cues', 'open', 'close', 'movements', 'extension', 'flexion', 'realigned']:
            raise ValueError(f'"{opts}" is not one of the possible event types')

    if what in ('data', 'continuous'):
        if opts is None:
            raise ValueError('You need to specify opts which is a dict with event_type, pre, post')

    ieeg = Task(ieeg_file)

    if what in ('continuous', 'data'):
        events_tsv = load('events', parameters, ieeg_file, opts['event_type'])
        events = events_tsv['trial_type']
        onsets = events_tsv['onset']

        if what == 'continuous':
            data = read_data(parameters, ieeg_file, event_onsets=onsets, opts=opts, continuous=True)
            return data, events, onsets
        elif what == 'data':
            data = read_data(parameters, ieeg_file, event_onsets=onsets, opts=opts, continuous=False)
            return data, events

    if what == 'electrodes':
        pattern = f'sub-{ieeg.subject}_*_acq-{ieeg.acquisition}_electrodes.tsv'
        folder = parameters['paths']['input']

    elif what == 'events':
        if opts in ('cues', 'open', 'close'):
            pattern = f'sub-{ieeg.subject}_*_run-{ieeg.run}_events.tsv'
            folder = parameters['paths']['input']
        elif opts in ('movements', 'extension', 'flexion'):
            pattern = f'sub-{ieeg.subject}_*_run-{ieeg.run}_dataglove.tsv'
            folder = parameters['paths']['movements']
        elif opts in ('realigned', ):
            event_path = name(parameters, 'realign_tsv', ieeg_file)
            pattern = event_path.name
            folder = event_path.parent

    elif what == 'dataglove':
        pattern = f'sub-{ieeg.subject}_*_run-{ieeg.run}_recording-dataglove_physio.tsv.gz'
        folder = parameters['paths']['input']

    elif what in ['pial', 'freesurfer', ] + FS_LABELS:
        pattern = 'sub-' + ieeg.subject
        folder = parameters['paths']['freesurfer_subjects_dir']

    elif what == 'surface':

        elec = load('electrodes', parameters, ieeg_file)
        right_or_left = (elec['x'] > 0).sum() / elec.shape[0]
        if right_or_left > 0.5:
            pattern = 'rh.pial'
        else:
            pattern = 'lh.pial'

        folder = name(parameters, 'surface_dir', ieeg_file)

    else:
        raise ValueError(f'Unrecognize "{what}" selection')

    found = list(folder.rglob(pattern))
    if len(found) == 0:
        raise FileNotFoundError(f'Could not find any file matching {pattern} in {folder}')
    elif len(found) > 1:
        raise ValueError('You need to specify more parameters')
    filename = found[0]

    if what == 'electrodes':
        elec = Electrodes(filename)
        return elec.electrodes.tsv[['name', 'x', 'y', 'z']]

    elif what == 'events':
        with filename.open() as f:
            x = f.readline()
        n_columns = x.count('\t') + 1
        dtypes = [
            ('onset', 'float'),
            ('duration', 'float'),
            ('trial_type', 'U4096'),
            ]
        if n_columns >= 4:
            dtypes.insert(3, ('value', 'int'))
        if n_columns == 5:
            dtypes.insert(3, ('response_time', 'float'))  # if -1, it means that we can reject trial

        events = genfromtxt(filename, delimiter='\t', skip_header=1, dtype=dtypes)

        if n_columns == 4:
            x = empty(len(events), dtype='float')
            x.fill(NaN)
            events = append_fields(events, 'response_time', x, usemask=False)

        return select_events(events, opts)

    elif what == 'dataglove':
        return read_physio(filename)

    elif what == 'pial':
        elec = load('electrodes', parameters, ieeg_file)
        right_or_left = (elec['x'] > 0).sum() / elec.shape[0]
        return read_surf(filename, right_or_left)

    elif what == 'surface':
        return Surf(filename)

    elif what in FS_LABELS:
        fs = load('freesurfer', parameters, ieeg_file)
        pial = load('pial', parameters, ieeg_file)
        hemi = pial.surf_file.stem

        aparc_file = fs.dir / 'label' / f'{hemi}.{what}.annot'
        region_values, region_ctab, region_names = read_annot(aparc_file)

        out = {
            'aparc': what,
            'ras_shift': fs.surface_ras_shift,
            'vert': pial.vert,
            'regions': {
                'values': region_values,
                'names': [x.decode() for x in region_names],
                'colors': read_brainregion_colors(region_names, region_ctab),
                'colorscale': read_brainregion_colorscale(region_ctab),
                }
            }

        return out

    elif what == 'freesurfer':
        return Freesurfer(filename)
示例#13
0
from wonambi import Dataset
from wonambi.attr.chan import create_sphere_around_elec
from wonambi.attr import Freesurfer, Surf
from wonambi.source import Morph
from wonambi.utils import create_data

from .paths import (
    fs_path,
    surf_path,
    eeglab_hdf5_1_file,
    hdf5_file,
)

fs = Freesurfer(fs_path)
data = create_data()
surf = Surf(surf_path)


def test_import_chan():
    with raises(ImportError):
        create_sphere_around_elec(None, '')


def test_import_anat():
    with raises(ImportError):
        fs.surface_ras_shift

    with raises(ImportError):
        fs.read_label('')

    with raises(ImportError):
def test_source_linear():
    surf = Surf(surf_path)
    channels = Channels(chan_path)

    Linear(surf, channels)