def test_cifti2_labeltable(): lt = ci.Cifti2LabelTable() assert_equal(len(lt), 0) assert_raises(ci.Cifti2HeaderError, lt.to_xml) assert_raises(ci.Cifti2HeaderError, lt._to_xml_element) label = ci.Cifti2Label(label='Test', key=0) lt[0] = label assert_equal(len(lt), 1) assert_equal(dict(lt), {label.key: label}) lt.clear() lt.append(label) assert_equal(len(lt), 1) assert_equal(dict(lt), {label.key: label}) lt.clear() test_tuple = (label.label, label.red, label.green, label.blue, label.alpha) lt[label.key] = test_tuple assert_equal(len(lt), 1) v = lt[label.key] assert_equal((v.label, v.red, v.green, v.blue, v.alpha), test_tuple) assert_raises(ValueError, lt.__setitem__, 1, label) assert_raises(ValueError, lt.__setitem__, 0, test_tuple[:-1]) assert_raises(ValueError, lt.__setitem__, 0, ('foo', 1.1, 0, 0, 1)) assert_raises(ValueError, lt.__setitem__, 0, ('foo', 1.0, -1, 0, 1)) assert_raises(ValueError, lt.__setitem__, 0, ('foo', 1.0, 0, -0.1, 1))
def test_cifti2_label(): lb = ci.Cifti2Label() lb.label = 'Test' lb.key = 0 assert_equal(lb.rgba, (0, 0, 0, 0)) assert_true( compare_xml_leaf( lb.to_xml().decode('utf-8'), "<Label Key='0' Red='0' Green='0' Blue='0' Alpha='0'>Test</Label>") ) lb.red = 0 lb.green = 0.1 lb.blue = 0.2 lb.alpha = 0.3 assert_equal(lb.rgba, (0, 0.1, 0.2, 0.3)) assert_true( compare_xml_leaf( lb.to_xml().decode('utf-8'), "<Label Key='0' Red='0' Green='0.1' Blue='0.2' Alpha='0.3'>Test</Label>" )) lb.red = 10 assert_raises(ci.Cifti2HeaderError, lb.to_xml) lb.red = 0 lb.key = 'a' assert_raises(ci.Cifti2HeaderError, lb.to_xml) lb.key = 0
def test_cifti2_label(): lb = ci.Cifti2Label() lb.label = 'Test' lb.key = 0 assert lb.rgba == (0, 0, 0, 0) assert compare_xml_leaf( lb.to_xml().decode('utf-8'), "<Label Key='0' Red='0' Green='0' Blue='0' Alpha='0'>Test</Label>") lb.red = 0 lb.green = 0.1 lb.blue = 0.2 lb.alpha = 0.3 assert lb.rgba == (0, 0.1, 0.2, 0.3) assert compare_xml_leaf( lb.to_xml().decode('utf-8'), "<Label Key='0' Red='0' Green='0.1' Blue='0.2' Alpha='0.3'>Test</Label>" ) lb.red = 10 with pytest.raises(ci.Cifti2HeaderError): lb.to_xml() lb.red = 0 lb.key = 'a' with pytest.raises(ci.Cifti2HeaderError): lb.to_xml() lb.key = 0
def yeo_to_91k(dlabel, medial_wall, reference, out): """Convert Yeo-style dlabels (Yeo and Schaefer parcellations) to 91k grayordinate space The Yeo lab generates dlabel's inclusive of medial wall vertices and only for the cortical surfaces. This is different from how typical dlabels are formatted, which exclude medial wall vertices and include voxels from all subcortical and cerebellar structures (i.e. the full 91k grayordinate space). This function corrects Yeo dlabels to proper 91k grayordinates. Parameters ---------- dlabel : str A Yeo-style .dlabel.nii atlas medial_wall : str HCP medial wall mask (.dlabel.nii) reference : str A reference .dlabel.nii file with 91k grayordinates and all brain models included out : str Output 91k grayordinate .dlabel.nii file """ dlabel = nib.load(dlabel) medial_wall = nib.load(medial_wall) ref = nib.load(reference) # remove medial wall vertices array = dlabel.get_fdata() corrected_array = array[np.logical_not(medial_wall.get_fdata())] # expand to 91k grayordinates = np.zeros(ref.shape) grayordinates[0, :corrected_array.shape[0]] = corrected_array # make header labels = dlabel.header.get_axis(index=0).label[0] label_table = ci.Cifti2LabelTable() for key, (tag, rgba) in labels.items(): label_table[key] = ci.Cifti2Label(key, tag, *rgba) maps = [ci.Cifti2NamedMap('labels', ci.Cifti2MetaData({}), label_table)] label_map = ci.Cifti2MatrixIndicesMap( applies_to_matrix_dimension=(0, ), indices_map_to_data_type='CIFTI_INDEX_TYPE_LABELS', maps=maps) model_map = ci.Cifti2MatrixIndicesMap( applies_to_matrix_dimension=(1, ), indices_map_to_data_type='CIFTI_INDEX_TYPE_BRAIN_MODELS', maps=list(ref.header.get_index_map(1).brain_models)) model_map.volume = ref.header.get_index_map(1).volume matrix = ci.Cifti2Matrix() matrix.append(label_map) matrix.append(model_map) hdr = ci.Cifti2Header(matrix) out_dtseries = ci.Cifti2Image(grayordinates, hdr) out_dtseries.to_filename(out) return out
def create_label_map(applies_to_matrix_dimension): maps = [] for name, meta, label in labels: label_table = ci.Cifti2LabelTable() for key, (tag, rgba) in label.items(): label_table[key] = ci.Cifti2Label(key, tag, *rgba) maps.append( ci.Cifti2NamedMap(name, ci.Cifti2MetaData(meta), label_table)) return ci.Cifti2MatrixIndicesMap(applies_to_matrix_dimension, 'CIFTI_INDEX_TYPE_LABELS', maps=maps)
def test_cifti2_labeltable(): lt = ci.Cifti2LabelTable() assert len(lt) == 0 with pytest.raises(ci.Cifti2HeaderError): lt.to_xml() with pytest.raises(ci.Cifti2HeaderError): lt._to_xml_element() label = ci.Cifti2Label(label='Test', key=0) lt[0] = label assert len(lt) == 1 assert dict(lt) == {label.key: label} lt.clear() lt.append(label) assert len(lt) == 1 assert dict(lt) == {label.key: label} lt.clear() test_tuple = (label.label, label.red, label.green, label.blue, label.alpha) lt[label.key] = test_tuple assert len(lt) == 1 v = lt[label.key] assert (v.label, v.red, v.green, v.blue, v.alpha) == test_tuple with pytest.raises(ValueError): lt[1] = label with pytest.raises(ValueError): lt[0] = test_tuple[:-1] with pytest.raises(ValueError): lt[0] = ('foo', 1.1, 0, 0, 1) with pytest.raises(ValueError): lt[0] = ('foo', 1.0, -1, 0, 1) with pytest.raises(ValueError): lt[0] = ('foo', 1.0, 0, -0.1, 1)