def filename2vertices_faces_metadata(fn): '''Attempts to get meta data based on the filename Parameters ---------- fn: str Filename Returns ------- meta: tuple Tuple with two gifti.GiftiMetaData objects for vertices and faces. If the filename contains exactly one of 'lh', 'rh', or 'mh' then it is assumed to be of left, right or merged hemispheres. If the filename contains exactly one of 'pial,'smoothwm', 'intermediate',''inflated','sphere','flat', then the geometric type is set ''' _, fn = os.path.split(fn) vertex_map = dict(AnatomicalStructurePrimary=dict( lh='CortexLeft', rh='CortexRight', mh='CortexRightLeft'), AnatomicalStructureSecondary=dict( pial='Pial', smoothwm='GrayWhite', intermediate='MidThickness'), GeometricType=dict( pial='Anatomical', smoothwm='Anatomical', intermediate='Anatomical', inflated='Inflated', sphere='Spherical', flat='Flat')) def just_one(dict_, fn=fn): vs = [v for k, v in dict_.iteritems() if k in fn] return vs[0] if len(vs) == 1 else None v_meta = [gifti.GiftiNVPairs('Name', fn)] for key, dict_ in vertex_map.iteritems(): v = just_one(dict_) if not v is None: v_meta.append(gifti.GiftiNVPairs(key, v)) f_meta = [gifti.GiftiNVPairs('Name', fn)] # XXX maybe also closed or open topology? that's a bit tricky though v = gifti.GiftiMetaData() v.data.extend(v_meta) f = gifti.GiftiMetaData() f.data.extend(f_meta) return v, f
def to_gifti_image(s, add_indices=False, swap_LPI_RAI=False): ''' Converts a surface to nibabel's gifti format. Parameters ---------- s: surf Input surface add_indices: True or False (default: False) if True then indices of the nodes are added. Note: caret may not be able to read these swap_LPI_RAI: True or False (default: False) If True then the diagonal elements of the xform matrix are set to [-1,-1,1,1], otherwise to [1,1,1,1]. Returns ------- img: gifti.GiftiImage Surface representated as GiftiImage ''' vertices = gifti.GiftiDataArray(np.asarray(s.vertices, np.float32)) vertices.intent = gifti.intent_codes.field1['pointset'] vertices.datatype = 16 # this is what gifti likes if add_indices: nvertices = s.nvertices indices = gifti.GiftiDataArray( np.asarray(np.arange(nvertices), np.int32)) indices.datatype = 8 # this is what gifti likes indices.coordsys = None # otherwise SUMA might complain indices.intent = gifti.intent_codes.field1['node index'] faces = gifti.GiftiDataArray(np.asarray(s.faces, np.int32)) faces.intent = gifti.intent_codes.field1['triangle'] faces.datatype = 8 # this is what gifti likes faces.coordsys = None # otherwise SUMA might complain # set some fields common to faces and vertices for arr in (vertices, faces) + ((indices, ) if add_indices else ()): arr.ind_ord = 1 arr.encoding = 3 arr.endian = 'LittleEndian' # XXX this does not work (see below) arr.dims = list(arr.data.shape) if externals.versions['nibabel'] < '2.1': # in later versions it is a computed property arr.num_dim = len(arr.dims) # make the image meta = gifti.GiftiMetaData() labeltable = gifti.GiftiLabelTable() img = gifti.GiftiImage(meta=meta, labeltable=labeltable) if swap_LPI_RAI: xform = np.asarray(vertices.coordsys.xform) xform[0, 0] = -1 xform[1, 1] = -1 vertices.coordsys.xform = xform if add_indices: img.add_gifti_data_array(indices) img.add_gifti_data_array(vertices) img.add_gifti_data_array(faces) return img