def complete(self, add_centroids=True, graph_attrs={}, atlas={}): super(FiberGraph, self).complete() assert atlas, "One Atlas must exist for any small graph!" # since only 1 atlas atlas_name = atlas.keys()[0] atlas_regions = atlas[atlas.keys()[0]] print "Attempting to add atlas labels ..." if atlas_regions is not None: f_regions = open(atlas_regions, "rb") self.graph.vs["region_name"] = f_regions.read().splitlines() if add_centroids: print "Adding centroids ..." cent_loc = os.path.join(os.environ['M2G_HOME'], "data", "Centroids", "centroids.mat") if not os.path.exists(cent_loc): get_files() cent_mat = sio.loadmat(cent_loc)["centroids"] centroids = [] for row in cent_mat: centroids.append(str(list(row))) self.graph.vs["centroid"] = centroids for key in graph_attrs.keys(): self.graph[key] = graph_attrs[key]
def create(roifn=os.path.join(os.environ["M2G_HOME"], "data", "Atlas", "MNI152_T1_1mm_brain.nii"), start=2): """ Downsamples an atlas from a template brain. Create a new atlas given some scaling factor determined by the start index. Can be useful if looking for parcellation of certain scale for graph generation. **Positional Arguments** roifn: [.nii; nifti image] (default = MNI152) - Nifti roi mask file name start: [int] (default = 2) - The x,y,z start position which determines the scaling. **Returns** atlas: [.nii; nifti image] - Atlas labels in MNI space. """ start_time = time() atlmap = None print "Loading rois as base ..." if not os.path.exists(roifn): get_files() img = nib.load(roifn) base = img.get_data() aff = img.get_affine() fm = img.file_map true_dim = base.shape # Labelling new label_used = False print "Labeling new ..." region_num = 1 step = 1 + (start * 2) mstart = -start mend = start + 1 # Align new to scale factor xdim, ydim, zdim = map(ceil, np.array(base.shape) / float(step)) if step == 1: assert xdim == base.shape[0] and ydim == base.shape[ 1] and zdim == base.shape[2] resized_base = np.zeros((xdim * step, ydim * step, zdim * step), dtype=int) resized_base[:base.shape[0], :base.shape[1], :base.shape[2]] = base base = resized_base del resized_base # Create new matrix new = np.zeros_like(base, dtype=np.int) # poke my finger in the eye of bjarne # TODO: Cythonize for z in xrange(start, base.shape[2] - start, step): for y in xrange(start, base.shape[1] - start, step): for x in xrange(start, base.shape[0] - start, step): if label_used: region_num += 1 # only increase counter when a label was used label_used = False # set other (step*step)-1 around me to same region for zz in xrange(mstart, mend): for yy in xrange(mstart, mend): for xx in xrange(mstart, mend): if (base[x + xx, y + yy, z + zz]): # Masking label_used = True new[x + xx, y + yy, z + zz] = region_num new = new[:true_dim[0], :true_dim[1], : true_dim[2]] # shrink new to correct size print "Your atlas has %d regions ..." % len(np.unique(new)) img = nib.Nifti1Image(new, affine=img.get_affine(), header=img.get_header(), file_map=img.file_map) del new print "Building atlas took %.3f sec ..." % (time() - start_time) return img
def create(roifn=os.path.join(os.environ["M2G_HOME"],"data","Atlas", "MNI152_T1_1mm_brain.nii"), start=2): """ Downsamples an atlas from a template brain. Create a new atlas given some scaling factor determined by the start index. Can be useful if looking for parcellation of certain scale for graph generation. **Positional Arguments** roifn: [.nii; nifti image] (default = MNI152) - Nifti roi mask file name start: [int] (default = 2) - The x,y,z start position which determines the scaling. **Returns** atlas: [.nii; nifti image] - Atlas labels in MNI space. """ start_time = time() atlmap = None print "Loading rois as base ..." if not os.path.exists(roifn): get_files() img = nib.load(roifn) base = img.get_data() aff = img.get_affine() fm = img.file_map true_dim = base.shape # Labelling new label_used = False print "Labeling new ..." region_num = 1 step = 1+(start*2) mstart = -start mend = start+1 # Align new to scale factor xdim, ydim, zdim = map(ceil, np.array(base.shape)/float(step)) if step == 1: assert xdim == base.shape[0] and ydim == base.shape[1] and zdim == base.shape[2] resized_base = np.zeros((xdim*step, ydim*step, zdim*step), dtype=int) resized_base[:base.shape[0], :base.shape[1], :base.shape[2]] = base base = resized_base del resized_base # Create new matrix new = np.zeros_like(base, dtype=np.int) # poke my finger in the eye of bjarne # TODO: Cythonize for z in xrange(start, base.shape[2]-start, step): for y in xrange(start, base.shape[1]-start, step): for x in xrange(start, base.shape[0]-start, step): if label_used: region_num += 1 # only increase counter when a label was used label_used = False # set other (step*step)-1 around me to same region for zz in xrange(mstart,mend): for yy in xrange(mstart,mend): for xx in xrange(mstart,mend): if (base[x+xx,y+yy,z+zz]): # Masking label_used = True new[x+xx,y+yy,z+zz] = region_num new = new[:true_dim[0], :true_dim[1], :true_dim[2]] # shrink new to correct size print "Your atlas has %d regions ..." % len(np.unique(new)) img = nib.Nifti1Image(new, affine=img.get_affine(), header=img.get_header(), file_map=img.file_map) del new print "Building atlas took %.3f sec ..." % (time()-start_time) return img