np.savetxt("tmp_mni152_coords.txt", raw_coords, fmt="%.4f") cmd = "img2imgcoord -src %s/mni152.nii.gz -dest %s/fsaverage.nii.gz -xfm %s/mni152_to_fsaverage.mat -mm %s > %s" cmd = cmd % (whereami_dir, whereami_dir, whereami_dir, "tmp_mni152_coords.txt", "tmp_fs_coords.txt") print(cmd) os.system(cmd) coords = np.loadtxt("tmp_fs_coords.txt", skiprows=1) coords = coords[:-1, :] # program gives last coordinate twice # Now we can take these peak coordinates and get the surface vertex foci_surf = io.Surface("fsaverage_copy", hemi, "white", subjects_dir=subjects_dir) foci_surf.load_geometry() foci_vtxs = utils.find_closest_vertices(foci_surf.coords, coords) # Load the geometry curv_file = op.join(surf_dir, "%s.curv" % hemi) curv = io.read_morph_data(curv_file) # < 0 = gyrus & > 0 = sulcus # Load the parcellations aparc_file = op.join(label_dir, "%s.aparcDKT40JT.annot" % hemi) aparc9_file = op.join(label_dir, "%s.aparc.a2009s.annot" % hemi) ba_file = op.join(label_dir, "%s.PALS_B12_Brodmann.annot" % hemi) yeo_file = op.join(label_dir, "%s.Yeo2011_7Networks_N1000.annot" % hemi) aparc = io.read_annot(aparc_file) ba = io.read_annot(ba_file) yeo = io.read_annot(yeo_file) aparc9 = io.read_annot(aparc9_file)
def make_annot_from_csv(subject, subjects_dir, csv_fname, lab_size=10, parc_fname='standard_garces_2016', n_jobs=4, make_annot=False, return_label_coords=False): """Make annotations from given csv file. For a given subject, given a csv file with set of MNI coordinates, make an annotation of labels grown from these ROIs. Mainly used to generate standard resting state network annotation from MNI coordinates provided in literature. Parameters: ----------- subject: str The name of the subject. subjects_dir: str The SUBJECTS_DIR where the surfaces are available. csv_fname: str Comma separated file with seed MNI coordinates. # example Network,Node,x,y,z,BA,hemi Visual,Left visual cortex,-41,-77,3,19,lh lab_size: int The size of the label (radius in mm) to be grown around ROI coordinates parc_fname: str Name used to save the parcellation as if make_annot is True. n_jobs: int Number of parallel jobs to run. make_annot: bool If True, an annotation file is created and written to disk. return_label_coords: bool If True, the function returns labels and MNI seed coordinates used. """ from mne import grow_labels import pandas as pd import matplotlib.cm as cmx import matplotlib.colors as colors from surfer import (Surface, utils) surf = 'white' hemi = 'both' rsns = pd.read_csv(csv_fname, comment='#') all_coords, all_labels = [], [] all_foci = [] for netw in rsns.Network.unique(): print(netw, end=' ') nodes = rsns[rsns.Network == netw]['Node'].values for node in nodes: mni_coords = rsns[(rsns.Network == netw) & (rsns.Node == node)].loc[:, ('x', 'y', 'z')].values[0] all_coords.append(mni_coords) hemi = rsns[(rsns.Network == netw) & (rsns.Node == node)].hemi.values[0] print(node, ':', mni_coords, hemi, end=' ') # but we are interested in getting the vertices and # growing our own labels foci_surf = Surface(subject, hemi=hemi, surf=surf, subjects_dir=subjects_dir) foci_surf.load_geometry() foci_vtxs = utils.find_closest_vertices(foci_surf.coords, mni_coords) print('Closest vertex on surface chosen:', foci_vtxs) all_foci.append(foci_vtxs) if hemi == 'lh': hemis = 0 else: hemis = 1 # rh lab_name = netw + '_' + node mylabel = grow_labels(subject, foci_vtxs, extents=lab_size, hemis=hemis, subjects_dir=subjects_dir, n_jobs=n_jobs, overlap=True, names=lab_name, surface=surf)[0] all_labels.append(mylabel) # assign colours to the labels # labels within the same network get the same color n_nodes = len(rsns.Node.unique()) # n_networks = len(rsns.Network.unique()) # total number of networks color_norm = colors.Normalize(vmin=0, vmax=n_nodes - 1) scalar_map = cmx.ScalarMappable(norm=color_norm, cmap='hsv') for n, lab in enumerate(all_labels): lab.color = scalar_map.to_rgba(n) if make_annot: mne.label.write_labels_to_annot(all_labels, subject=subject, parc=parc_fname, subjects_dir=subjects_dir) if return_label_coords: # returns the list of labels grown and MNI coords used as seeds return all_labels, all_coords, all_foci
print "...hemi: %s" % hemi # Get coordinates raw_coords = df_both[hemi].ix[:,3:6].as_matrix() np.savetxt("tmp_mni152_coords.txt", raw_coords, fmt="%.4f") cmd = "img2imgcoord -src %s/mni152.nii.gz -dest %s/fsaverage.nii.gz -xfm %s/mni152_to_fsaverage.mat -mm %s > %s" cmd = cmd % (whereami_dir, whereami_dir, whereami_dir, "tmp_mni152_coords.txt", "tmp_fs_coords.txt") print(cmd) os.system(cmd) coords = np.loadtxt("tmp_fs_coords.txt", skiprows=1) coords = coords[:-1,:] # program gives last coordinate twice # Now we can take these peak coordinates and get the surface vertex foci_surf = io.Surface("fsaverage_copy", hemi, "white", subjects_dir=subjects_dir) foci_surf.load_geometry() foci_vtxs = utils.find_closest_vertices(foci_surf.coords, coords) # Load the geometry curv_file = op.join(surf_dir, "%s.curv" % hemi) curv = io.read_morph_data(curv_file) # < 0 = gyrus & > 0 = sulcus # Load the parcellations aparc_file = op.join(label_dir, "%s.aparcDKT40JT.annot" % hemi) aparc9_file = op.join(label_dir, "%s.aparc.a2009s.annot" % hemi) ba_file = op.join(label_dir, "%s.PALS_B12_Brodmann.annot" % hemi) yeo_file = op.join(label_dir, "%s.Yeo2011_7Networks_N1000.annot" % hemi) aparc = io.read_annot(aparc_file) ba = io.read_annot(ba_file) yeo = io.read_annot(yeo_file) aparc9 = io.read_annot(aparc9_file)
def add_coords(self, coords, map_surface=None, scale_factor=1.5, color="red", alpha=1, name=None, labels=None, hemi=None, text_size=5, txt_pos=[1.4, 1.1, 1.1]): """ Plot locations onto the brain surface as spheres. :param coords: list of co-ordinates or (n, 3) numpy array. Co-ordinate space must match that of the underlying MRI image :param map_surface: Freesurfer surf or None. surface to map coordinates through, or None to use raw coords :param scale_factor: int controls the size of the foci spheres :param color: matplotlib color code HTML name, RGB tuple or hex code :param alpha: float in [0, 1] opacity of coordinate spheres :param name: str internal name to use (_foci and _labels will be appended) :param labels: List of text strings used to label co-ordinates :param hemi: str | None If None, assumed to belong to the hemisphere being shown. If two hemispheresa are being shown, an error will be thrown :param text_size: int Text size of labels """ hemi = self._check_hemi(hemi) if map_surface is None: foci_vtxs = surfer.utils.find_closest_vertices( self.geo[hemi].coords, coords) foci_coords = self.geo[hemi].coords[foci_vtxs] else: foci_surf = utils.Surface(self.subject_id, hemi, map_surface, subjects_dir=self.subjects_dir) foci_surf.load_geometry() foci_vtxs = utils.find_closest_vertices(foci_surf.coords, coords) foci_coords = self.geo[hemi].coords[foci_vtxs] # Convert the color code if not isinstance(color, tuple): color = colorConverter.to_rgb(color) if name is None: name = "coords_%s" % (max( len(self.foci_dict) + 1, len(self.labels_dict) + 1)) views = self._toggle_render(False) # Store the coords in the foci list and the label in the labels list fl = [] # Create the visualization for brain in self._brain_list: if brain['hemi'] == hemi: fl.append( mlab.points3d(foci_coords[:, 0], foci_coords[:, 1], foci_coords[:, 2], np.ones(foci_coords.shape[0]), scale_factor=(10. * scale_factor), color=color, opacity=alpha, name=name + '_foci', figure=brain['brain']._f)) self.foci_dict[name + '_foci'] = fl if labels is not None: tl = [] for i in xrange(coords.shape[0]): tl.append( mlab.text3d(foci_coords[i, 0] * txt_pos[0], foci_coords[i, 1] * txt_pos[1], foci_coords[i, 2] * txt_pos[2], labels[i], color=(1.0, 1.0, 1.0), scale=text_size, name=name + '_label', figure=brain['brain']._f)) self.labels_dict[name + '_label'] = fl self._toggle_render(True, views)
def add_arrow(self, coords, map_surface=None, tube_radius=3.0, color="white", alpha=1, name=None, hemi=None): """ Add an arrow across the brain between two co-ordinates :param coords: list of co-ordinates or (n, 3) numpy array. Co-ordinate space must match that of the underlying MRI image :param tube_radius: float controls the size of the arrow :param color: matplotlib color code HTML name, RGB tuple or hex code :param alpha: float in [0, 1] opacity of coordinate spheres :param name: str internal name to use :param hemi: str | None If None, assumed to belong to the hemisphere being shown. If two hemispheresa are being shown, an error will be thrown """ hemi = self._check_hemi(hemi) if map_surface is None: foci_vtxs = surfer.utils.find_closest_vertices( self.geo[hemi].coords, coords) foci_coords = self.geo[hemi].coords[foci_vtxs] else: foci_surf = utils.Surface(self.subject_id, hemi, map_surface, subjects_dir=self.subjects_dir) foci_surf.load_geometry() foci_vtxs = utils.find_closest_vertices(foci_surf.coords, coords) foci_coords = self.geo[hemi].coords[foci_vtxs] # foci_vtxs = surfer.utils.find_closest_vertices(self.geo[hemi].coords, coords) # foci_coords = self.geo[hemi].coords[foci_vtxs] # Convert the color code if not isinstance(color, tuple): color = colorConverter.to_rgb(color) if name is None: name = "arrow_%s" % (len(self.arrows_dict) + 1) nsegs = 100 x = np.linspace(foci_coords[0, 0], foci_coords[1, 0], nsegs) y = np.linspace(foci_coords[0, 1], foci_coords[1, 1], nsegs) z = np.linspace(foci_coords[0, 2], foci_coords[1, 2], nsegs) line_coords = np.vstack((x, y, z)).transpose() step = 5 idx_a = range(0, nsegs + 1, step) idx_b = range(10, nsegs + 1, step) views = self._toggle_render(False) al = [] for brain in self._brain_list: if brain['hemi'] == hemi: for start, end in zip(idx_a, idx_b): seg_width = tube_radius - (start * (tube_radius - .5) / 100.) al.append( mlab.plot3d(line_coords[start:end, 0], line_coords[start:end, 1], line_coords[start:end, 2], np.ones_like(line_coords[start:end, 0]), color=color, opacity=alpha, tube_radius=seg_width, name=name, figure=brain['brain']._f)) self.arrows_dict[name] = al self._toggle_render(True, views)
def add_coords(self, coords, map_surface=None, scale_factor=1.5, color="red", alpha=1, name=None, labels=None, hemi=None, text_size=5, txt_pos=[1.4, 1.1, 1.1]): """ Plot locations onto the brain surface as spheres. :param coords: list of co-ordinates or (n, 3) numpy array. Co-ordinate space must match that of the underlying MRI image :param map_surface: Freesurfer surf or None. surface to map coordinates through, or None to use raw coords :param scale_factor: int controls the size of the foci spheres :param color: matplotlib color code HTML name, RGB tuple or hex code :param alpha: float in [0, 1] opacity of coordinate spheres :param name: str internal name to use (_foci and _labels will be appended) :param labels: List of text strings used to label co-ordinates :param hemi: str | None If None, assumed to belong to the hemisphere being shown. If two hemispheresa are being shown, an error will be thrown :param text_size: int Text size of labels """ hemi = self._check_hemi(hemi) if map_surface is None: foci_vtxs = surfer.utils.find_closest_vertices(self.geo[hemi].coords, coords) foci_coords = self.geo[hemi].coords[foci_vtxs] else: foci_surf = utils.Surface(self.subject_id, hemi, map_surface, subjects_dir=self.subjects_dir) foci_surf.load_geometry() foci_vtxs = utils.find_closest_vertices(foci_surf.coords, coords) foci_coords = self.geo[hemi].coords[foci_vtxs] # Convert the color code if not isinstance(color, tuple): color = colorConverter.to_rgb(color) if name is None: name = "coords_%s" % (max(len(self.foci_dict) + 1, len(self.labels_dict) + 1)) views = self._toggle_render(False) # Store the coords in the foci list and the label in the labels list fl = [] # Create the visualization for brain in self._brain_list: if brain['hemi'] == hemi: fl.append(mlab.points3d(foci_coords[:, 0], foci_coords[:, 1], foci_coords[:, 2], np.ones(foci_coords.shape[0]), scale_factor=(10. * scale_factor), color=color, opacity=alpha, name=name + '_foci', figure=brain['brain']._f)) self.foci_dict[name + '_foci'] = fl if labels is not None: tl = [] for i in xrange(coords.shape[0]): tl.append(mlab.text3d(foci_coords[i, 0]*txt_pos[0], foci_coords[i, 1]*txt_pos[1], foci_coords[i, 2]*txt_pos[2], labels[i], color=(1.0, 1.0, 1.0), scale=text_size, name=name + '_label', figure=brain['brain']._f)) self.labels_dict[name + '_label'] = fl self._toggle_render(True, views)
def add_arrow(self, coords, map_surface=None, tube_radius=3.0, color="white", alpha=1, name=None, hemi=None): """ Add an arrow across the brain between two co-ordinates :param coords: list of co-ordinates or (n, 3) numpy array. Co-ordinate space must match that of the underlying MRI image :param tube_radius: float controls the size of the arrow :param color: matplotlib color code HTML name, RGB tuple or hex code :param alpha: float in [0, 1] opacity of coordinate spheres :param name: str internal name to use :param hemi: str | None If None, assumed to belong to the hemisphere being shown. If two hemispheresa are being shown, an error will be thrown """ hemi = self._check_hemi(hemi) if map_surface is None: foci_vtxs = surfer.utils.find_closest_vertices(self.geo[hemi].coords, coords) foci_coords = self.geo[hemi].coords[foci_vtxs] else: foci_surf = utils.Surface(self.subject_id, hemi, map_surface, subjects_dir=self.subjects_dir) foci_surf.load_geometry() foci_vtxs = utils.find_closest_vertices(foci_surf.coords, coords) foci_coords = self.geo[hemi].coords[foci_vtxs] # foci_vtxs = surfer.utils.find_closest_vertices(self.geo[hemi].coords, coords) # foci_coords = self.geo[hemi].coords[foci_vtxs] # Convert the color code if not isinstance(color, tuple): color = colorConverter.to_rgb(color) if name is None: name = "arrow_%s" % (len(self.arrows_dict) + 1) nsegs = 100 x = np.linspace(foci_coords[0, 0], foci_coords[1, 0], nsegs) y = np.linspace(foci_coords[0, 1], foci_coords[1, 1], nsegs) z = np.linspace(foci_coords[0, 2], foci_coords[1, 2], nsegs) line_coords = np.vstack((x, y, z)).transpose() step = 5 idx_a = range(0, nsegs+1, step) idx_b = range(10, nsegs+1, step) views = self._toggle_render(False) al = [] for brain in self._brain_list: if brain['hemi'] == hemi: for start,end in zip(idx_a, idx_b): seg_width = tube_radius - (start*(tube_radius-.5)/100.) al.append(mlab.plot3d(line_coords[start:end, 0], line_coords[start:end, 1], line_coords[start:end, 2], np.ones_like(line_coords[start:end, 0]), color=color, opacity=alpha, tube_radius=seg_width, name=name, figure=brain['brain']._f)) self.arrows_dict[name] = al self._toggle_render(True, views)