def tractography_from_trackvis_file(filename): tracts_and_data, header = trackvis.read(filename, points_space='rasmm') tracts, scalars, properties = izip(*tracts_and_data) scalar_names = [n for n in header['scalar_name'] if len(n) > 0] #scalar_names_unique = [] #scalar_names_subcomp = {} #for sn in scalar_names: # if re.match('.*_[0-9]{2}', sn): # prefix = sn[:sn.rfind('_')] # if prefix not in scalar_names_unique: # scalar_names_unique.append(prefix) # scalar_names_subcomp[prefix] = int(sn[-2:]) # scalar_names_subcomp[prefix] = max(sn[-2:], scalar_names_subcomp[prefix]) # else: # scalar_names_unique.append(sn) tracts_data = {} for i, sn in enumerate(scalar_names): tracts_data[sn] = [scalar[:, i][:, None] for scalar in scalars] affine = header['vox_to_ras'] image_dims = header['dim'] tr = Tractography(tracts, tracts_data) tr.affine = affine tr.image_dims = image_dims return tr
def tractography_from_vtk_files(vtk_file_names): tr = Tractography() if isinstance(vtk_file_names, str): vtk_file_names = [vtk_file_names] for file_name in vtk_file_names: tracts = read_vtkPolyData(file_name) tr.append(tracts.tracts(), tracts.tracts_data()) return tr
def vtkPolyData_to_tracts(polydata, return_tractography_object=True): r''' Reads a VTKPolyData object and outputs a tracts/tracts_data pair Parameters ---------- polydata : vtkPolyData VTKPolyData Object Returns ------- tracts : list of float array N_ix3 Each element of the list is a tract represented as point array, the length of the i-th tract is N_i tract_data : dict of <data name>= list of float array of N_ixM Each element in the list corresponds to a tract, N_i is the length of the i-th tract and M is the number of components of that data type. ''' result = {} result['lines'] = ns.vtk_to_numpy(polydata.GetLines().GetData()) result['points'] = ns.vtk_to_numpy(polydata.GetPoints().GetData()) result['numberOfLines'] = polydata.GetNumberOfLines() data = {} if polydata.GetPointData().GetScalars(): data['ActiveScalars'] = polydata.GetPointData().GetScalars().GetName() if polydata.GetPointData().GetVectors(): data['ActiveVectors'] = polydata.GetPointData().GetVectors().GetName() if polydata.GetPointData().GetTensors(): data['ActiveTensors'] = polydata.GetPointData().GetTensors().GetName() for i in xrange(polydata.GetPointData().GetNumberOfArrays()): array = polydata.GetPointData().GetArray(i) np_array = ns.vtk_to_numpy(array) if np_array.ndim == 1: np_array = np_array.reshape(len(np_array), 1) data[polydata.GetPointData().GetArrayName(i)] = np_array result['pointData'] = data tracts, data = vtkPolyData_dictionary_to_tracts_and_data(result) if return_tractography_object: tr = Tractography() tr.append(tracts, data) return tr else: return tracts, data
def tractography_from_vtkPolyData(polydata): tractography = Tractography() tractography._originalFibers = [] tractography._tractData = {} tractography._originalLines = [] tractography._originalData = {} tractography._tracts = [] lines, lines_ids, point_data = vtkPolyData_to_lines(polydata) tractography._tracts = lines tractography._tractData = point_data tractography._originalFibers = np.vstack(lines) tractography._originalLines = lines_ids tractography._originalData = dict( ((key, np.vstack(value)) for key, value in tractography._tractData))
def tractography_from_vtkPolyData(polydata): tractography = Tractography() tractography._originalFibers = [] tractography._tractData = {} tractography._originalLines = [] tractography._originalData = {} tractography._tracts = [] lines, lines_ids, point_data = vtkPolyData_to_lines(polydata) tractography._tracts = lines tractography._tractData = point_data tractography._originalFibers = np.vstack(lines) tractography._originalLines = lines_ids tractography._originalData = dict(( (key, np.vstack(value)) for key, value in tractography._tractData ))