Example #1
0
def test_select_shared_vertices(src, fwd):
    # SourceSpaces
    src2 = src.copy()
    src1_r = restrict_src_to_vertices(
        src, ([14, 54, 59, 108, 228], [30, 98, 180, 187, 230]))
    vert_inds = select_shared_vertices([src1_r, src2])
    assert_array_equal(vert_inds[0], src1_r[0]['vertno'])
    assert_array_equal(vert_inds[1], src1_r[1]['vertno'])

    subjects_dir = op.join(mne.datasets.sample.data_path(), 'subjects')
    vert_inds = select_shared_vertices([src1_r, src2],
                                       ref_src=src2,
                                       subjects_dir=subjects_dir)
    assert_array_equal(vert_inds[0][0], src1_r[0]['vertno'])
    assert_array_equal(vert_inds[0][1], src1_r[1]['vertno'])

    src2_r = restrict_src_to_vertices(
        src2, [[108, 228, 364, 582, 627], [187, 230, 263, 271, 313]])
    vert_inds = select_shared_vertices([src1_r, src2_r, src, src2])
    assert_array_equal(vert_inds[1], np.array([187, 230]))
    assert_array_equal(vert_inds[0], np.array([108, 228]))

    # Forward
    fwd2 = fwd.copy()
    fwd1_r = restrict_forward_to_vertices(fwd, ([1170, 1609], [2159]))
    vert_inds = select_shared_vertices([fwd1_r, fwd2])
    assert_array_equal(vert_inds[0], fwd1_r['src'][0]['vertno'])
    assert_array_equal(vert_inds[1], fwd1_r['src'][1]['vertno'])

    # Incorrect input
    with pytest.raises(ValueError):
        select_shared_vertices('wrong', fwd)
Example #2
0
def test_select_vertices_in_sensor_range(fwd, src):
    """Test selecting and restricting vertices in the sensor range"""
    fwd_r = restrict_forward_to_vertices(fwd, ([1170, 1609], [2159]))

    verts = select_vertices_in_sensor_range(fwd_r, 0.05)
    assert_array_equal(verts[0], np.array([1170]))
    assert_array_equal(verts[1], np.array([]))
    # Test indices
    verts = select_vertices_in_sensor_range(fwd_r, 0.07, indices=True)
    assert_array_equal(verts, np.array([0, 1, 2]))

    # Test restricting
    fwd_rs = restrict_forward_to_sensor_range(fwd_r, 0.05)
    assert_array_equal(fwd_rs['src'][0]['vertno'], np.array([1170]))
    assert_array_equal(fwd_rs['src'][1]['vertno'], np.array([]))

    verts = select_vertices_in_sensor_range(fwd_r, 0.07)
    assert_array_equal(verts[0], np.array([1170, 1609]))
    assert_array_equal(verts[1], np.array([2159]))

    src_r = restrict_src_to_vertices(src, ([1170, 1609], [2159]))

    with pytest.raises(ValueError):  # info missing
        select_vertices_in_sensor_range(src_r, 0.07)
    info = _info()
    with pytest.raises(ValueError):  # trans missing
        select_vertices_in_sensor_range(src_r, 0.07, info=info)

    # Correct input
    trans = _trans()
    verts2 = select_vertices_in_sensor_range(src_r,
                                             0.05,
                                             info=info,
                                             trans=trans)
    assert_array_equal(verts2[0], np.array([1170]))
    verts2 = select_vertices_in_sensor_range(src_r,
                                             0.07,
                                             info=info,
                                             trans=trans)
    assert_array_equal(verts[0], verts2[0])
    assert_array_equal(verts[1], verts2[1])
    # Indices
    verts2 = select_vertices_in_sensor_range(src_r,
                                             0.07,
                                             info=info,
                                             trans=trans,
                                             indices=True)
    assert_array_equal(verts2, np.array([0, 1, 2]))
    # Try with only EEG
    info = mne.pick_info(info, sel=mne.pick_types(info, meg=False, eeg=True))
    verts2 = select_vertices_in_sensor_range(src_r,
                                             0.05,
                                             info=info,
                                             trans=trans)
    assert_array_equal(verts2[0], np.array([1170, 1609]))
    assert_array_equal(verts2[1], np.array([2159]))
Example #3
0
def test_restrict_forward_to_vertices(fwd):
    fwd_r = restrict_forward_to_vertices(fwd, ([1170, 1609], [2159]))
    assert_array_equal(fwd_r['src'][0]['vertno'], [1170, 1609])
    assert_array_equal(fwd_r['src'][1]['vertno'], [2159])
    assert fwd_r['sol']['ncol'] == 3 * 3  # Free orientation
    assert fwd_r['sol']['nrow'] == fwd['sol']['nrow']
    assert fwd_r['sol']['data'].shape == (fwd['sol']['nrow'], 3 * 3)

    fwd_r = restrict_forward_to_vertices(fwd, ([], []))
    assert_array_equal(fwd_r['src'][0]['vertno'], [])
    assert_array_equal(fwd_r['src'][1]['vertno'], [])

    # Test fixed orientation forward solution
    fwd_fixed = mne.forward.convert_forward_solution(fwd,
                                                     force_fixed=True,
                                                     use_cps=True)
    fwd_r = restrict_forward_to_vertices(fwd_fixed, ([1170, 1609], [2159]))
    assert fwd_r['sol']['ncol'] == 3
    assert fwd_r['sol']['data'].shape == (fwd['sol']['nrow'], 3)

    # Test tangential forward solution
    fwd_tan = forward_to_tangential(fwd)
    fwd_r = restrict_forward_to_vertices(fwd_tan, ([1170, 1609], [2159]))
    assert fwd_r['sol']['ncol'] == 3 * 2
    assert fwd_r['sol']['data'].shape == (fwd['sol']['nrow'], 3 * 2)

    # Vertices not present in src
    with pytest.raises(IndexError):
        restrict_forward_to_vertices(fwd, ([30, 1609], [2159]))

    # Use indices
    fwd_r = restrict_forward_to_vertices(fwd, [0, 1, 3732])
    assert fwd_r['sol']['ncol'] == 3 * 3
    assert_array_equal(fwd_r['src'][0]['vertno'], fwd['src'][0]['vertno'][:2])
    assert_array_equal(fwd_r['src'][1]['vertno'], fwd['src'][1]['vertno'][:1])

    # Test in place operation
    restrict_forward_to_vertices(fwd, ([1170, 1609], [2159]), copy=False)
    assert_array_equal(fwd['src'][0]['vertno'], [1170, 1609])
    assert_array_equal(fwd['src'][1]['vertno'], [2159])
Example #4
0
def _load_restricted_forward(source_vertno1, source_vertno2):
    """Load forward models and restrict them so that they include source
    vertices"""
    fwd_free = mne.read_forward_solution(fname_fwd)
    fwd_free = mne.pick_types_forward(fwd_free, meg='grad', eeg=False)

    # Restrict forward
    vertno_lh = np.random.choice(fwd_free['src'][0]['vertno'], 40,
                                 replace=False)
    if source_vertno1 not in vertno_lh:
        vertno_lh = np.append(vertno_lh, source_vertno1)
    vertno_rh = np.random.choice(fwd_free['src'][1]['vertno'], 40,
                                 replace=False)
    if source_vertno2 not in vertno_rh:
        vertno_rh = np.append(vertno_rh, source_vertno2)

    fwd_free = restrict_forward_to_vertices(fwd_free, [vertno_lh, vertno_rh])
    fwd_fixed = mne.convert_forward_solution(fwd_free, force_fixed=True,
                                             use_cps=False)

    return fwd_free, fwd_fixed
Example #5
0
for subject in subjects[1:]:
    fwds.append(mne.read_forward_solution(fname.fwd(subject=subject)))

# Compute the vertices that are shared across the forward operators for all
# subjects. The first one we restricted ourselves, the other ones may have
# dropped vertices which were too close to the inner skull surface. We use the
# fsaverage brain as a reference to determine corresponding vertices across
# subjects.
fsaverage = mne.read_source_spaces(fname.fsaverage_src)
vert_inds = conpy.select_shared_vertices(fwds,
                                         ref_src=fsaverage,
                                         subjects_dir=fname.subjects_dir)

# Restrict all forward operators to the same vertices and save them.
for fwd, vert_ind, subject in zip(fwds, vert_inds, subjects):
    fwd_r = conpy.restrict_forward_to_vertices(fwd, vert_ind)
    mne.write_forward_solution(fname.fwd_r(subject=subject),
                               fwd_r,
                               overwrite=True)

    # Update the forward operator of the first subject
    if subject == subjects[0]:
        fwd1 = fwd_r

    # Save a visualization of the restricted forward operator to the subject's
    # HTML report
    with mne.open_report(fname.report(subject=subject)) as report:
        fig = mne.viz.plot_alignment(fwd['info'],
                                     trans=fname.trans(subject=subject),
                                     src=fwd_r['src'],
                                     meg='sensors',
Example #6
0
            "{dir}nc_{meg}_from-fs_ico4_base-fwd.fif".format(dir=meg_dir,
                                                             meg=meg)))
    fwds_exp.append(
        mne.read_forward_solution(
            "{dir}nc_{meg}_from-fs_ico4_exp-fwd.fif".format(dir=meg_dir,
                                                            meg=meg)))
vert_inds_bas = conpy.select_shared_vertices(fwds_bas,
                                             ref_src=fs_src,
                                             subjects_dir=mri_dir)
vert_inds_exp = conpy.select_shared_vertices(fwds_exp,
                                             ref_src=fs_src,
                                             subjects_dir=mri_dir)
del fwds_bas[0], fwds_exp[0]
del vert_inds_bas[0], vert_inds_exp[0]
for i, subj in enumerate(list(sub_dict.keys())):
    fwd_bas_r = conpy.restrict_forward_to_vertices(fwds_bas[i],
                                                   vert_inds_bas[i])
    mne.write_forward_solution(
        "{dir}nc_{meg}_from-fs_ico4_bas-r-fwd.fif".format(dir=meg_dir,
                                                          meg=subj),
        fwd_bas_r,
        overwrite=True)
    fwd_exp_r = conpy.restrict_forward_to_vertices(fwds_exp[i],
                                                   vert_inds_exp[i])
    mne.write_forward_solution(
        "{dir}nc_{meg}_from-fs_ico4_exp-r-fwd.fif".format(dir=meg_dir,
                                                          meg=subj),
        fwd_exp_r,
        overwrite=True)

# plot it - based on subject NEM_36 (last)
fig = mne.viz.plot_alignment(fwd_exp_r['info'],