def load_and_get_fdata_params(self): pyhrf.verbose(1,'Load mask from: %s' %self.mask_file) if self.data_type == 'surface': pyhrf.verbose(2,'Read mesh from: %s' %self.mesh_file) p = {'mask' : xndarray.load(self.mask_file).data} pyhrf.verbose(1, 'Mask shape %s' %str(p['mask'].shape)) if self.data_type == 'surface': p['graph'] = graph_from_mesh(read_mesh(self.mesh_file)) return p
def extract_sub_mesh_with_files(input_mesh, center_node, radius, output_mesh=None): from nibabel import gifti from nibabel.gifti import GiftiImage, GiftiDataArray from pyhrf.tools.io import read_mesh cor, tri, coord_sys = read_mesh(input_mesh) sub_cor, sub_tri = extract_sub_mesh(cor, tri, center_node, radius) #nimg = GiftiImage_fromTriangles(sub_cor, sub_tri) nimg = GiftiImage() intent = 'NIFTI_INTENT_POINTSET' nimg.add_gifti_data_array(GiftiDataArray.from_array(sub_cor,intent)) intent = 'NIFTI_INTENT_TRIANGLE' nimg.add_gifti_data_array(GiftiDataArray.from_array(sub_tri,intent)) if output_mesh is None: output_mesh = non_existent_file(add_suffix(input_mesh, '_sub')) pyhrf.verbose(1, 'Saving extracted mesh to %s' %output_mesh) gifti.write(nimg, output_mesh) return sub_cor, sub_tri, coord_sys
def load_surf_bold_mask(bold_files, mesh_file, mask_file=None): pyhrf.verbose(1, 'Load mesh: ' + mesh_file) coords,triangles,coord_sys = read_mesh(mesh_file) pyhrf.verbose(2, 'Build graph ... ') fgraph = graph_from_mesh(triangles) assert graph_is_sane(fgraph) pyhrf.verbose(1, 'Mesh has %d nodes' %len(fgraph)) pyhrf.verbose(2, 'Compute length of edges ... ') edges_l = np.array([np.array([distance(coords[i], coords[n], coord_sys.xform) \ for n in nl]) \ for i,nl in enumerate(fgraph)],dtype=object) if mask_file is None or not op.exists(mask_file): pyhrf.verbose(1,'Mask file %s does not exist. Taking '\ ' all nodes ...' %mask_file) mask = np.ones(len(fgraph)) mask_meta_obj = None mask_loaded_from_file = False else: mask, mask_meta_obj = read_texture(mask_file) mask_loaded_from_file = True if not (np.round(mask) == mask).all(): raise Exception("Mask is not n-ary") if len(mask) != len(fgraph): raise Exception('Size of mask (%d) is different from size '\ 'of graph (%d)' %(len(mask),len(fgraph))) mask = mask.astype(np.int32) if mask.min() == -1: mask += 1 #Split graph into rois: graphs = {} edge_lengths = {} for roiId in np.unique(mask): mroi = np.where(mask==roiId) g, nm = sub_graph(fgraph, mroi[0]) edge_lengths[roiId] = edges_l[mroi] graphs[roiId] = g #Load BOLD: lastScan = 0 sessionScans = [] bolds = [] for boldFile in bold_files: pyhrf.verbose(1, 'load bold: ' + boldFile) b,_ = read_texture(boldFile) pyhrf.verbose(1, 'bold shape: ' + str(b.shape)) bolds.append(b) sessionScans.append(np.arange(lastScan, lastScan+b.shape[0], dtype=int)) lastScan += b.shape[0] bold = np.concatenate(tuple(bolds)) if len(fgraph) != bold.shape[1]: raise Exception('Nb positions not consistent between BOLD (%d) '\ 'and mesh (%d)' %(bold.shape[1],len(fgraph))) # discard bad data (bold with var=0 and nan values): discard_bad_data(bold, mask, time_axis=0) return mask, mask_meta_obj, mask_loaded_from_file, bold, sessionScans, \ graphs, edge_lengths