def test_random_parcellation(): """Test generation of random cortical parcellation.""" hemi = 'both' n_parcel = 50 surface = 'sphere.reg' subject = 'sample_ds' rng = np.random.RandomState(0) # Parcellation labels = random_parcellation(subject, n_parcel, hemi, subjects_dir, surface=surface, random_state=rng) # test number of labels assert_equal(len(labels), n_parcel) if hemi == 'both': hemi = ['lh', 'rh'] hemis = np.atleast_1d(hemi) for hemi in set(hemis): vertices_total = [] for label in labels: if label.hemi == hemi: # test that labels are not empty assert (len(label.vertices) > 0) # vertices of hemi covered by labels vertices_total = np.append(vertices_total, label.vertices) # test that labels don't intersect assert_equal(len(np.unique(vertices_total)), len(vertices_total)) surf_fname = op.join(subjects_dir, subject, 'surf', hemi + '.' + surface) vert, _ = read_surface(surf_fname) # Test that labels cover whole surface assert_array_equal(np.sort(vertices_total), np.arange(len(vert)))
def make_random_parcellation(path_annot, n, hemi, subjects_dir, random_state, subject, remove_corpus_callosum=False): parcels = random_parcellation(subject, n, hemi, subjects_dir=subjects_dir, surface='white', random_state=random_state) if remove_corpus_callosum: corpus_callosum = find_corpus_callosum(subject, subjects_dir, hemi=hemi) # instead of removing all the overlapping parcels we will remove only # the vertices which belong to corpus callosum to_remove = [] for idx, parcel in enumerate(parcels): cc_free = set(parcel.vertices) - set(corpus_callosum.vertices) parcel.vertices = np.array(list(cc_free)) if len(parcel.vertices) == 0: to_remove.append(idx) # remove all the parcels which after removing corpus callosum now # have 0 indices [parcels.pop(idc) for idc in to_remove[::-1]] write_labels_to_annot(parcels, subjects_dir=subjects_dir, subject=subject, annot_fname=path_annot, overwrite=True)