def bss_write_pvalue_as_log_color(nimg_datain, nimg_dataout): nimg_data_type = NimgDataio.validatetype(nimg_datain) if nimg_data_type == 'surface': s1 = dfsio.readdfs(nimg_datain) if not hasattr(s1, 'attributes'): sys.stdout.write('Error: The surface ' + nimg_datain + ' is missing p-value attributes.\n') return if s1.attributes is None: sys.stdout.write('Error: The surface ' + nimg_datain + ' is missing p-value attributes.\n') return log_pvalues = s1.attributes s1.vColor, pex, cmap = cm.Colormap.log_pvalues_to_rgb(log_pvalues) dfsio.writedfs(nimg_dataout, s1) elif nimg_data_type == 'nifti_image': sys.stdout.write('Not implemented.\n') log_pvalues = NimgDataio.read(nimg_datain, attributes_only=True) cdict_pvalues, pex, cmap = cm.Colormap.log_pvalues_to_rgb(log_pvalues) LUT = cmap._lut[0:256, 0:3] fid = open(nimg_dataout, 'wt') for i in range(0, len(LUT)): fid.write("{0:f} {1:f} {2:f}\n".format(float(LUT[i, 0]), float(LUT[i, 1]), float(LUT[i, 2]))) fid.close() else: raise TypeError( 'Error: Unsupported data type. Supported data types are: ' + ', '.join(NimgDataio.datatype.keys()) + '.\n') sys.stdout.write('Done.\n')
def bss_write_corr_as_color(surfin, surfout): s1 = dfsio.readdfs(surfin) if not hasattr(s1, 'attributes'): sys.stdout.write('Error: The surface ' + surfin + ' is missing p-value attributes.\n') sys.exit(0) s1.vColor, cex, cmap = cm.Colormap.correlation_to_rgb(s1.attributes) # cm.Colormap.exportParaviewCmap(cdict_orig, surfout + '.xml') dfsio.writedfs(surfout, s1) return
def bss_mean_surface(args): try: # Parse the arguments if args.txt: # If txt file present fid = open(args.txt, 'rt') filelist = [filename.rstrip('\n') for filename in fid.readlines()] elif args.csv: # If csv file present demographic_data = pandas.read_csv(args.csv[0]) # Iterate over all columns, and check if any of the values # are NaN for missing data # TODO: This code is repeated. for columns in demographic_data.columns.values: if len(np.where(demographic_data[columns].isnull())[0]) > 0: sys.stdout.write( 'Error: Some data may be missing from the demographics file: ' + args.csv[0] + '. ' + '\nIf nothing seems wrong at the first glance and if its a csv file, please open it in a ' '\nplain text editor and check if there are missing values, rows, or columns.' + '. Will quit now.\n') return filelist = demographic_data[args.csv[1]] # Read the file list and compute the average of the coordinates and attributes surf_array = [] for fname in filelist: surf_array.append(dfsio.readdfs(fname)) # Check if all surfaces have same number of vertices nVertices = len(surf_array[0].vertices) s1_average = surf_array[0] for i in range(1, len(surf_array)): if len(surf_array[i].vertices) == nVertices: s1_average.vertices += surf_array[i].vertices if surf_array[i].attributes.any(): s1_average.attributes += surf_array[i].attributes else: sys.stdout.write( 'Not all surfaces have the same number of vertices. Exiting...\n' ) return s1_average.vertices /= len(surf_array) s1_average.attributes /= len(surf_array) dfsio.writedfs(args.dfsfilename, s1_average) except: print "Something went wrong. Please send this error message to the developers." \ "\nUnexpected error:", sys.exc_info()[0] print traceback.print_exc(file=sys.stdout)
def bss_resample_surface_to_target(srcfile, src_regfile, resampfile): surf_src = dfsio.readdfs(srcfile) surf_src_reg = dfsio.readdfs(src_regfile) surf_src.uv = np.c_[surf_src.u, surf_src.v] surf_src_reg.uv = np.c_[surf_src_reg.u, surf_src_reg.v] sys.stdout.write("Resampling source to target...") sys.stdout.write("x ") res_x = griddata(surf_src.uv, surf_src.vertices[:, 0], surf_src_reg.uv, method='nearest') sys.stdout.write("y ") res_y = griddata(surf_src.uv, surf_src.vertices[:, 1], surf_src_reg.uv, method='nearest') sys.stdout.write("z ") res_z = griddata(surf_src.uv, surf_src.vertices[:, 2], surf_src_reg.uv, method='nearest') sys.stdout.write("Done.\n") res_vertices = np.zeros((surf_src_reg.vertices.shape[0], 3)) res_vertices[:, 0] = res_x res_vertices[:, 1] = res_y res_vertices[:, 2] = res_z if len(surf_src.attributes) > 0: sys.stdout.write("Resampling attributes to target ...") res_attributes = griddata(surf_src.vertices, surf_src.attributes, res_vertices, method='nearest') sys.stdout.write("Done.\n") else: res_attributes = [] if len(surf_src.labels) > 0: sys.stdout.write("Resampling labels to target ...") res_labels = griddata(surf_src.vertices, surf_src.labels, res_vertices, method='nearest') sys.stdout.write("Done.\n") else: res_labels = [] class surf_resamp: pass surf_resamp.vertices = res_vertices surf_resamp.faces = surf_src_reg.faces surf_resamp.attributes = res_attributes surf_resamp.labels = res_labels dfsio.writedfs(resampfile, surf_resamp) return None
def shape_fdr(shapein, shapeout, method): rootnamein, extin = os.path.splitext(shapein) rootnameout, extout = os.path.splitext(shapein) if extin != extout: sys.stdout.write('Error: Input and output extensions should be same.\n') return if extin == '.txt': pvalues = np.loadtxt(shapein) fdr_adjusted_pvalues = Stats_Multi_Comparisons.adjust(pvalues, method=method) np.savetxt(shapeout, fdr_adjusted_pvalues) elif extin == '.dfs': s1 = dfsio.readdfs(shapein) s1.attributes = Stats_Multi_Comparisons.adjust(s1.attributes, method=method) dfsio.writedfs(shapeout, s1)
def bss_write_pvalue_as_log_color(surfin, surfout): s1 = dfsio.readdfs(surfin) if not hasattr(s1, 'attributes'): sys.stdout.write('Error: The surface ' + surfin + ' is missing p-value attributes.\n') sys.exit(0) cmap = colormaps.Colormap('pvalue', s1.attributes) rgb_list = cmap.get_rgb_list_from_attribute_list(s1.attributes) s1.vColor = np.empty((3, len(s1.attributes))) for idx, val in enumerate(rgb_list): s1.vColor[:, idx] = [val[0], val[1], val[2]] # s1.vColor[:, idx] = [abs(np.random.random()), abs(np.random.random()), abs(np.random.random())] s1.vColor = np.ndarray.transpose(s1.vColor) exportParaviewCmap(cmap.color_dict, surfout + '.xml') dfsio.writedfs(surfout, s1)
def shape_fdr(shapein, shapeout, method): rootnamein, extin = os.path.splitext(shapein) rootnameout, extout = os.path.splitext(shapein) if extin != extout: sys.stdout.write( 'Error: Input and output extensions should be same.\n') return if extin == '.txt': pvalues = np.loadtxt(shapein) fdr_adjusted_pvalues = Stats_Multi_Comparisons.adjust(pvalues, method=method) np.savetxt(shapeout, fdr_adjusted_pvalues) elif extin == '.dfs': s1 = dfsio.readdfs(shapein) s1.attributes = Stats_Multi_Comparisons.adjust(s1.attributes, method=method) dfsio.writedfs(shapeout, s1)