def main(hdf, stat, name): """ Given a <path> and the <hdf> name, plot and save all the models in the <hdf>, prefixing each with <basename>. """ stat = str(stat) # Create a csv file to tabulate into f = open('{0}_hist_{1}.csv'.format(name, stat), 'w') csvw = csv.writer(f) # Make a header for the table header = [stat, "count", "cond", "boldmeta", "model"] csvw.writerow(header) # Make a list of the models # to tabulate and get going... models = get_model_names(hdf) for mod in models: meta = get_model_meta(hdf, mod) hist_list = create_hist(hdf, mod, stat) for hist in hist_list: cond = hist.name boldmeta = "_".join([str(b) for b in meta["bold"]]) [csvw.writerow([k, v, cond, boldmeta, mod]) for k, v in hist.h.items()] f.close()
def main(hdf, name): # Create a csv file to tabulate into f = open('{0}.csv'.format(name), 'w') csvw = csv.writer(f) # Make a header for the table header = ["x", "count", "cond"] csvw.writerow(header) conds = set(["acc", "value", "rpe", "p", "rand", "box"]) # Find a dmcol from models, # pick the first model with # that dm cond. Conds across # models are the same. locations = {} models = get_model_names(hdf) for model in models: # Get model meta data and compare it too conds meta = get_model_meta(hdf, model) for ii, dmname in enumerate(meta["dm"]): if dmname in conds: locations[dmname] = (model, ii) conds.remove(dmname) ## conds loses elements! # If conds is empty, stop if not conds: print("All conds found. {0}".format(model)) break # Get each dm's data and pick a col using pos; # pos matches cond. dmdata = {} for cond, loci in locations.items(): print("Getting {0}".format(cond)) model, pos = loci dm = np.array(read_hdf(hdf, '/' + model + '/dm')) dmdata[cond] = dm[:,:,pos].flatten() # Create a histogram then write it out. for cond, data in dmdata.items(): print("Histogramming {0}".format(cond)) # Instantiate a RHist instance and # use it to make a histogram hist = RHist(name=cond, decimals=2) [hist.add(x) for x in data] # Tell that textfile a tale. [csvw.writerow([k, v, cond]) for k, v in hist.h.items()] f.close()
def random_timecourses_all_models(hdf, N, nsim, basename): """ Plot <N> randomly selected BOLD and design matrix timecourses from <nsim> options for all models in <hdf>. Each model's plots are saved as a pdf, prefixed with tc_<basename>. """ # Make a list of the models # to plot and plot them models = get_model_names(hdf) for mod in models: print("Plotting {0}.".format(mod)) random_timecourses(hdf, mod, N, nsim, "tc_" + basename + "_" + mod)
def main(hdf, name): f = open('{0}.csv'.format(name), 'w') csvw = csv.writer(f) # A header for the table head = ["tau", "p_tau", "r", "p_r", "rho", "p_rho", "cond", "boldmeta", "model"] csvw.writerow(head) models = get_model_names(hdf) for model in models: # Get model meta data meta = get_model_meta(hdf, model) boldmeta = "_".join([str(b) for b in meta["bold"]]) ## meta['bold'] can be a list ## but we need a string.... # Get all the design matrices and # Get all the bold signals as a 1d array dm = np.array(read_hdf(hdf, '/' + model + '/dm')) bold = np.array(read_hdf(hdf, '/' + model + '/bold')).flatten() # Loop over the dm cols in the dm: # axis 1 is a sim index/count, # axis 2 is the dm row # axis 3 is the dm cols for jj in range(dm.shape[2]): # Get all this cols in a 1d array # matching what was done to # bold above x1 = dm[:, :, jj].flatten() x1name = meta["dm"][jj] # And calc the corr between # the two 1d arrays tau, p_tau = kendalltau(bold, x1) r, p_r = pearsonr(bold, x1) rho, p_rho = spearmanr(bold, x1) # then write it all out. csvw.writerow([ tau, p_tau, r, p_r, rho, p_rho, x1name, boldmeta, model]) f.close()
def main(posargs): # Check then handle the positional arguements # brough in from the command line if len(posargs) < 3: raise ValueError("At least three arguments are required.\n") name = posargs.pop() stat = posargs[0] tocompare = posargs[1:] # Create a csv file to tabulate into f = open('{0}_{1}.csv'.format(name, stat), 'w') csvw = csv.writer(f) csvw.writerow(["mean", "sd", "D", "dataset", "model", "boldmeta", "dmmeta", "cond"]) models = get_model_names(tocompare[0]) ## Assume models are identical for ## each hdf to compare for model in models: for ii, comp in enumerate(tocompare): meta = get_model_meta(comp, model) boldmeta = "_".join([str(b) for b in meta["bold"]]) dmmeta = "_".join([str(d) for d in meta["dm"]]) # Generate the data to add to the table. hist_list = create_hist_list(comp, model, stat) means = [hist.mean() for hist in hist_list] stdevs = [hist.stdev() for hist in hist_list] names = [hist.name for hist in hist_list] # Calculate effect sizes ns = [hist.n() for hist in hist_list] k = len(hist_list) + 1 ## Number of predcitors ## +1 for the dummy cohen_ds = [(2.0 * mean) / np.sqrt(n - k - 1.0) for mean, n in zip(means, ns)] ## d = 2*t / sqrt(DF) ## DF = n - k - 1 ## n = sample number ## k = predictor number # And add it. databasename = os.path.splitext(os.path.basename(comp))[0] for mean, sd, d, name in zip(means, stdevs, cohen_ds, names): row = [mean, sd, d, databasename, model, boldmeta, dmmeta, name] csvw.writerow(row) f.close()
def hist_t_all_models(path, hdf, basename): """ Given a <path> and the <hdf> name, plot and save all the models in the <hdf>, prefixing each with <basename>. """ # Create a handle to create a multi-page pdf # for the mod plots pdf = PdfPages(os.path.join(path, "{0}.pdf".format(basename))) # Make a list of the models # to plot and plot them hdfpath = os.path.join(path, hdf) models = get_model_names(hdfpath) for mod in models: print("Plotting {0}.".format(mod)) hist_t(hdfpath, mod, pdf) pdf.close()
def hist_t_all_models(path, hdf, basename): """ Given a <path> and the <hdf> name, plot and save all the models in the <hdf>, prefixing each with <basename>. """ # Create a handle to create a multi-page pdf # for the mod plots pdf = PdfPages(os.path.join(path, '{0}.pdf'.format(basename))) # Make a list of the models # to plot and plot them hdfpath = os.path.join(path, hdf) models = get_model_names(hdfpath) for mod in models: print("Plotting {0}.".format(mod)) hist_t(hdfpath, mod, pdf) pdf.close()
def main(posargs): # Check then handle the positional arguements # brough in from the command line if len(posargs) != 4: raise ValueError("Four arguments are required.\n") hdf = posargs[0] stat = posargs[1] criterion = float(posargs[2]) name = posargs[3] # Create csv writer named name # And give it a header f = open("{0}_{1}{2}.csv".format(name, stat, criterion), "w") csvw = csv.writer(f) csvw.writerow(["area", "model", "boldmeta", "dmmeta", "cond"]) models = get_model_names(hdf) for model in models: hist_list = create_hist_list(hdf, model, stat) # Loop over the hist_list adding # the results from each hist.above(criterion) # to a bar plot. areas = [hist.above(criterion) for hist in hist_list] names = [hist.name for hist in hist_list] # Pretty things up then save # this barplot to the pdf # and move onto the next model meta = get_model_meta(hdf, model) boldmeta = "_".join([str(b) for b in meta["bold"]]) dmmeta = "_".join([str(d) for d in meta["dm"]]) for area, name in zip(areas, names): row = [area, model, boldmeta, dmmeta, name] csvw.writerow(row) f.close()
def main(hdf, name): # Create a csv file to tabulate into f = open('{0}.csv'.format(name), 'w') csvw = csv.writer(f) # A header for the table head = ["tau", "p_tau", "r", "p_r", "rho", "p_rho", "cond0", "cond1"] csvw.writerow(head) conds = set(["acc", "value", "rpe", "p", "rand", "box"]) pairs = permutations(conds, 2) # Find a dmcol from models, # pick the first model with # that dm cond. Conds across # models are the same. locations = {} models = get_model_names(hdf) for model in models: # Get model meta data and compare it too conds meta = get_model_meta(hdf, model) for ii, dmname in enumerate(meta["dm"]): if dmname in conds: locations[dmname] = (model, ii) conds.remove(dmname) ## conds loses elements! # If conds is empty, stop if not conds: print("All conds found by {0}".format(model)) break # Get the data dmdata = {} for cond, loci in locations.items(): print("Getting {0}".format(cond)) model, pos = loci dm = np.array(read_hdf(hdf, '/' + model + '/dm')) ## Get all the dm's data dmdata[cond] = dm[:,:,pos].flatten() ## pick a col using pos, as this is ## the data matching name # Calculate all pair-wise correlations for pair in pairs: print("Correlate: {0}".format(pair)) cond0, cond1 = pair x0 = dmdata[cond0] x1 = dmdata[cond1] tau, p_tau = kendalltau(x0, x1) r, p_r = pearsonr(x0, x1) rho, p_rho = spearmanr(x0, x1) # then write it all out. csvw.writerow([ tau, p_tau, r, p_r, rho, p_rho, cond0, cond1]) f.close()