Esempio n. 1
0
def create_lut_file_for_atlas(subject, atlas):
    # Read the subcortical segmentation from the freesurfer lut
    lut = utils.read_freesurfer_lookup_table(FREE_SURFER_HOME, get_colors=True)
    lut_new = [list(l) for l in lut if l[0] < 1000]
    for hemi, offset in zip(['lh', 'rh'], [1000, 2000]):
        if hemi == 'lh':
            lut_new.append([1000, 'ctx-lh-unknown', 25, 5,  25, 0])
        else:
            lut_new.append([2000, 'ctx-rh-unknown', 25,  5, 25,  0])
        _, ctab, names = _read_annot(op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, atlas)))
        names = [name.astype(str) for name in names]
        for index, (label, cval) in enumerate(zip(names, ctab)):
            r,g,b,a, _ = cval
            lut_new.append([index + offset + 1, label, r, g, b, a])
    lut_new.sort(key=lambda x:x[0])
    # Add the values above 3000
    for l in [l for l in lut if l[0] >= 3000]:
        lut_new.append(l)
    new_lut_fname = op.join(SUBJECTS_DIR, subject, 'label', '{}ColorLUT.txt'.format(atlas))
    with open(new_lut_fname, 'w') as fp:
        csv_writer = csv.writer(fp, delimiter='\t')
        csv_writer.writerows(lut_new)
    # np.savetxt(new_lut_fname, lut_new, delimiter='\t', fmt="%s")
    utils.make_dir(op.join(MMVT_DIR, subject, 'freeview'))
    shutil.copyfile(new_lut_fname, op.join(MMVT_DIR, subject, 'freeview', '{}ColorLUT.txt'.format(atlas)))
Esempio n. 2
0
def test_read_labels_from_annot(tmp_path):
    """Test reading labels from FreeSurfer parcellation."""
    # test some invalid inputs
    pytest.raises(ValueError,
                  read_labels_from_annot,
                  'sample',
                  hemi='bla',
                  subjects_dir=subjects_dir)
    pytest.raises(ValueError,
                  read_labels_from_annot,
                  'sample',
                  annot_fname='bla.annot',
                  subjects_dir=subjects_dir)
    with pytest.raises(IOError, match='does not exist'):
        _read_annot_cands('foo')
    with pytest.raises(IOError, match='no candidate'):
        _read_annot(str(tmp_path))

    # read labels using hemi specification
    labels_lh = read_labels_from_annot('sample',
                                       hemi='lh',
                                       subjects_dir=subjects_dir)
    for label in labels_lh:
        assert label.name.endswith('-lh')
        assert label.hemi == 'lh'
        assert label.color is not None

    # read labels using annot_fname
    annot_fname = op.join(subjects_dir, 'sample', 'label', 'rh.aparc.annot')
    labels_rh = read_labels_from_annot('sample',
                                       annot_fname=annot_fname,
                                       subjects_dir=subjects_dir)
    for label in labels_rh:
        assert label.name.endswith('-rh')
        assert label.hemi == 'rh'
        assert label.color is not None

    # combine the lh, rh, labels and sort them
    labels_lhrh = list()
    labels_lhrh.extend(labels_lh)
    labels_lhrh.extend(labels_rh)

    names = [label.name for label in labels_lhrh]
    labels_lhrh = [label for (name, label) in sorted(zip(names, labels_lhrh))]

    # read all labels at once
    labels_both = read_labels_from_annot('sample', subjects_dir=subjects_dir)

    # we have the same result
    _assert_labels_equal(labels_lhrh, labels_both)

    # aparc has 68 cortical labels
    assert (len(labels_both) == 68)

    # test regexp
    label = read_labels_from_annot('sample',
                                   parc='aparc.a2009s',
                                   regexp='Angu',
                                   subjects_dir=subjects_dir)[0]
    assert (label.name == 'G_pariet_inf-Angular-lh')
    # silly, but real regexp:
    label = read_labels_from_annot('sample',
                                   'aparc.a2009s',
                                   regexp='.*-.{4,}_.{3,3}-L',
                                   subjects_dir=subjects_dir)[0]
    assert (label.name == 'G_oc-temp_med-Lingual-lh')
    with pytest.raises(RuntimeError, match='did not match any of'):
        read_labels_from_annot('sample',
                               parc='aparc',
                               annot_fname=annot_fname,
                               regexp='foo',
                               subjects_dir=subjects_dir)
Esempio n. 3
0
def get_labels_num(subject, subjects_dir, atlas, hemi='both'):
    from mne.label import _read_annot
    annot_fnames = get_annot_fnames(subject, subjects_dir, atlas, hemi)
    return np.concatenate(
        [_read_annot(annot_fname)[2] for annot_fname in annot_fnames]).shape[0]