def save(self, output_dir): """ Save paradigm to output_dir/paradigm.csv, BOLD to output_dir/bold.nii, mask to output_dir/mask.nii #TODO: handle multi-session Return: tuple of file names in this order: (paradigm, bold, mask) """ paradigm_file = op.join(output_dir,'paradigm.csv') self.paradigm.save_csv(paradigm_file) if self.data_type == 'volume': # unflatten bold bold_vol = expand_array_in_mask(self.bold, self.roiMask, 1) bold_vol = np.rollaxis(bold_vol, 0, 4) bold_file = op.join(output_dir, 'bold.nii') write_volume(bold_vol, bold_file, self.meta_obj) mask_file = op.join(output_dir, 'mask.nii') write_volume(self.roiMask, mask_file, self.meta_obj) elif self.data_type == 'surface': #TODO surface bold_file = op.join(output_dir, 'bold.gii') write_texture(self.bold_vol, bold_file, self.meta_obj) pass return paradigm_file, bold_file, mask_file
def project_fmri_from_kernels(input_mesh, kernels_file, fmri_data_file, output_tex, bin_threshold=None, ): pyhrf.verbose(2,'Project data onto mesh using kernels ...') if 0: print 'Projecting ...' print 'func data:', fmri_data_file print 'Mesh file:', input_mesh print 'Save as:', output_tex pyhrf.verbose(2,'Call AimsFunctionProjection -op 1 ...') data_files = [] output_texs = [] p_ids = None if bin_threshold is not None: d,h = read_volume(fmri_data_file) if np.allclose(d.astype(int), d): tmp_dir = pyhrf.get_tmp_path() p_ids = np.unique(d) pyhrf.verbose(2, 'bin threshold: %f' %bin_threshold) pyhrf.verbose(2, 'pids(n=%d): %d...%d' \ %(len(p_ids),min(p_ids),max(p_ids))) for i,p_id in enumerate(p_ids): if p_id != 0: new_p = np.zeros_like(d) new_p[np.where(d==p_id)] = i + 1 #0 is background ifn = op.join(tmp_dir,'pmask_%d.nii'%p_id) write_volume(new_p, ifn, h) data_files.append(ifn) ofn = op.join(tmp_dir,'ptex_%d.gii'%p_id) output_texs.append(ofn) else: data_files.append(fmri_data_file) output_texs.append(output_tex) else: data_files.append(fmri_data_file) output_texs.append(output_tex) pyhrf.verbose(3, 'input data files: %s' %str(data_files)) pyhrf.verbose(3, 'output data files: %s' %str(output_texs)) for data_file, o_tex in zip(data_files, output_texs): projection = [ 'AimsFunctionProjection', '-op', '1', '-d', kernels_file, '-d1', data_file, '-m', input_mesh, '-o', o_tex ] cmd = ' '.join(map(str,projection)) pyhrf.verbose(3, 'cmd: %s' %cmd) os.system(cmd) if bin_threshold is not None: pyhrf.verbose(2, 'Binary threshold of texture at %f' %bin_threshold) o_tex = output_texs[0] data,data_gii = read_texture(o_tex) data = (data>bin_threshold).astype(np.int32) print 'data:', data.dtype if p_ids is not None: for pid, o_tex in zip(p_ids[1:], output_texs[1:]): pdata,pdata_gii = read_texture(o_tex) data += (pdata>bin_threshold).astype(np.int32) * pid #assert (np.unique(data) == p_ids).all() write_texture(data, output_tex, intent='NIFTI_INTENT_LABEL')
def make_parcellation_surf_from_files(beta_files, mesh_file, parcellation_file, nbparcel, method, mu=10., verbose=0): if method not in ['ward', 'gkm', 'ward_and_gkm', 'kmeans']: raise ValueError('unknown method') # step 1: load the data ---------------------------- # 1.1 the domain pyhrf.verbose(3, 'domain from mesh: %s' %mesh_file) domain = domain_from_mesh(mesh_file) coord = domain.coord # 1.3 read the functional data beta = np.array([read_texture(b)[0] for b in beta_files]).T pyhrf.verbose(3, 'beta: %s' %str(beta.shape)) pyhrf.verbose(3, 'mu * coord / np.std(coord): %s' \ %(mu * coord / np.std(coord)).shape) feature = np.hstack((beta, mu * coord / np.std(coord))) if method is not 'kmeans': # print 'domain.topology:', domain.topology.__class__ # print domain.topology #print dir(domain.topology) # print 'feature:', feature.shape # print feature g = field_from_coo_matrix_and_data(domain.topology, feature) # print 'g:', g.__class__ # print g if method == 'kmeans': _, u, _ = kmeans(feature, nbparcel) if method == 'ward': u, _ = g.ward(nbparcel) if method == 'gkm': seeds = np.argsort(np.random.rand(g.V))[:nbparcel] _, u, _ = g.geodesic_kmeans(seeds) if method == 'ward_and_gkm': w, _ = g.ward(nbparcel) _, u, _ = g.geodesic_kmeans(label=w) # print 'u:' # print u lpa = SubDomains(domain, u, 'parcellation') if verbose: var_beta = np.array( [np.var(beta[lpa.label == k], 0).sum() for k in range(lpa.k)]) var_coord = np.array( [np.var(coord[lpa.label == k], 0).sum() for k in range(lpa.k)]) size = lpa.get_size() vf = np.dot(var_beta, size) / size.sum() va = np.dot(var_coord, size) / size.sum() print nbparcel, "functional variance", vf, "anatomical variance", va # step3: write the resulting label image if parcellation_file is not None: label_image = parcellation_file # elif write_dir is not None: # label_image = os.path.join(write_dir, "parcel_%s.nii" % method) else: label_image = None if label_image is not None: #lpa.to_image(label_image, descrip='Intra-subject parcellation image') write_texture(u.astype(np.int32), label_image) if verbose: print "Wrote the parcellation images as %s" % label_image return u, label_image