def volstats (invol, groupname, groupsize, outdir=''): slicesdir = os.path.dirname(invol) if not outdir: outdir = slicesdir base = os.path.basename(au.remove_ext(invol)) outmeanf = outdir + os.path.sep + base + '_' + au.mean_str() outvarf = outdir + os.path.sep + base + '_' + au.var_str() outstdf = outdir + os.path.sep + base + '_' + au.std_str() outsumsf = outdir + os.path.sep + base + '_' + au.sums_str() vol = nib.load(invol).get_data() aff = nib.load(invol).get_affine() if not os.path.exists(outmeanf): mean = np.mean(vol, axis=3) au.save_nibabel(outmeanf, mean, aff) if not os.path.exists(outstdf): std = np.std(vol, axis=3) au.save_nibabel(outstdf, std, aff) if not os.path.exists(outvarf): var = np.var(vol, axis=3) au.save_nibabel(outvarf, var, aff) if not os.path.exists(outsumsf): sums = np.sum(vol, axis=3) au.save_nibabel(outsumsf, sums, aff) return [outsumsf,outmeanf,outvarf,outstdf]
def volstats(invol, groupname, groupsize, outdir=''): slicesdir = os.path.dirname(invol) if not outdir: outdir = slicesdir base = os.path.basename(au.remove_ext(invol)) outmeanf = outdir + os.path.sep + base + '_' + au.mean_str() outvarf = outdir + os.path.sep + base + '_' + au.var_str() outstdf = outdir + os.path.sep + base + '_' + au.std_str() outsumsf = outdir + os.path.sep + base + '_' + au.sums_str() vol = nib.load(invol).get_data() aff = nib.load(invol).get_affine() if not os.path.exists(outmeanf): mean = np.mean(vol, axis=3) au.save_nibabel(outmeanf, mean, aff) if not os.path.exists(outstdf): std = np.std(vol, axis=3) au.save_nibabel(outstdf, std, aff) if not os.path.exists(outvarf): var = np.var(vol, axis=3) au.save_nibabel(outvarf, var, aff) if not os.path.exists(outsumsf): sums = np.sum(vol, axis=3) au.save_nibabel(outsumsf, sums, aff) return [outsumsf, outmeanf, outvarf, outstdf]
def get_stats_fnames (groupnames, outdir=''): if np.ndim(groupnames) == 0: groupnames = [groupnames] if outdir: outdir += outdir + os.path.sep mnames = [au.sums_str(), au.mean_str(), au.var_str(), au.std_str()] ngroups = len(groupnames) statfnames = np.zeros ([ngroups, len(mnames)], dtype=np.dtype('a2000')) for g in np.arange(ngroups): group = groupnames[g] for m in np.arange(len(mnames)): measure = mnames[m] statfnames[g,m] = outdir + group + '_' + measure + au.ext_str() return [statfnames, mnames]
def get_stats_fnames(groupnames, outdir=''): if np.ndim(groupnames) == 0: groupnames = [groupnames] if outdir: outdir += outdir + os.path.sep mnames = [au.sums_str(), au.mean_str(), au.var_str(), au.std_str()] ngroups = len(groupnames) statfnames = np.zeros([ngroups, len(mnames)], dtype=np.dtype('a2000')) for g in np.arange(ngroups): group = groupnames[g] for m in np.arange(len(mnames)): measure = mnames[m] statfnames[g, m] = outdir + group + '_' + measure + au.ext_str() return [statfnames, mnames]
def group_distance (measure_function, datadir, groups, groupsizes, chkf, absolute=False, outdir='', foldno='', expname='', exclude_idx=-1, exclude_subj='', exclude_subjclass=''): olddir = os.getcwd() if not outdir: outdir = datadir ngroups = len(groups) #matrix of strings of 2000 characters maximum, to save filepaths gfnames = np.zeros ([ngroups,3], dtype=np.dtype('a2000')) subject_excluded = False for g1 in range(ngroups): g1name = groups[g1] #mean1fname gfnames[g1,0] = datadir + os.path.sep + g1name + '_' + au.mean_str() #var1fname gfnames[g1,1] = datadir + os.path.sep + g1name + '_' + au.var_str() #std1fname gfnames[g1,2] = datadir + os.path.sep + g1name + '_' + au.std_str() for g2 in range(g1+1, ngroups): g2name = groups[g2] gfnames[g2,0] = datadir + os.path.sep + g2name + '_' + au.mean_str() gfnames[g2,1] = datadir + os.path.sep + g2name + '_' + au.var_str() gfnames[g2,2] = datadir + os.path.sep + g2name + '_' + au.std_str() experiment = g1name + '_vs_' + g2name #check if exclude_subjclass is any of both current groups eg = -1 if exclude_idx > -1: if exclude_subjclass == g1name: eg = g2 elif exclude_subjclass == g2name: eg = g1 step = au.measure_str() + ' ' + measure_function.func_name + ' ' + experiment + ' ' + datadir #remove subject from stats if eg > -1: exclude_str = '_' + au.excluded_str() + str(exclude_idx) step += exclude_str experiment += exclude_str if not au.is_done(chkf, step): if not subject_excluded: newmeanfname = gfnames[eg,0] + exclude_str newvarfname = gfnames[eg,1] + exclude_str newstdfname = gfnames[eg,2] + exclude_str rstep = au.remove_str() + ' ' + au.subject_str() + ' ' + str(exclude_subj) + ' ' + au.fromstats_str() + ' ' + datadir if not au.is_done(chkf, rstep): #(meanfname, varfname, samplesize, subjvolfname, newmeanfname, newvarfname, newstdfname='') remove_subject_from_stats (gfnames[eg,0], gfnames[eg,1], groupsizes[eg][1], exclude_subj, newmeanfname, newvarfname, newstdfname) au.checklist_add (chkf, rstep) gfnames[eg,0] += exclude_str gfnames[eg,1] += exclude_str gfnames[eg,2] += exclude_str groupsizes[eg][1] -= 1 subject_excluded = True #calculating distance if not au.is_done(chkf, step): mean1fname = au.add_extension_if_needed (gfnames[g1,0], au.ext_str()) mean2fname = au.add_extension_if_needed (gfnames[g2,0], au.ext_str()) var1fname = au.add_extension_if_needed (gfnames[g1,1], au.ext_str()) var2fname = au.add_extension_if_needed (gfnames[g2,1], au.ext_str()) std1fname = au.add_extension_if_needed (gfnames[g1,2], au.ext_str()) std2fname = au.add_extension_if_needed (gfnames[g2,2], au.ext_str()) outfname = measure_function (mean1fname, mean2fname, var1fname, var2fname, std1fname, std2fname, groupsizes[g1][1], groupsizes[g2][1], experiment, outdir, exclude_idx) if absolute: change_to_absolute_values (outfname) au.checklist_add (chkf, step) return outfname
def group_distance(measure_function, datadir, groups, groupsizes, chkf, absolute=False, outdir='', foldno='', expname='', exclude_idx=-1, exclude_subj='', exclude_subjclass=''): olddir = os.getcwd() if not outdir: outdir = datadir ngroups = len(groups) #matrix of strings of 2000 characters maximum, to save filepaths gfnames = np.zeros([ngroups, 3], dtype=np.dtype('a2000')) subject_excluded = False for g1 in range(ngroups): g1name = groups[g1] #mean1fname gfnames[g1, 0] = datadir + os.path.sep + g1name + '_' + au.mean_str() #var1fname gfnames[g1, 1] = datadir + os.path.sep + g1name + '_' + au.var_str() #std1fname gfnames[g1, 2] = datadir + os.path.sep + g1name + '_' + au.std_str() for g2 in range(g1 + 1, ngroups): g2name = groups[g2] gfnames[g2, 0] = datadir + os.path.sep + g2name + '_' + au.mean_str() gfnames[g2, 1] = datadir + os.path.sep + g2name + '_' + au.var_str() gfnames[g2, 2] = datadir + os.path.sep + g2name + '_' + au.std_str() experiment = g1name + '_vs_' + g2name #check if exclude_subjclass is any of both current groups eg = -1 if exclude_idx > -1: if exclude_subjclass == g1name: eg = g2 elif exclude_subjclass == g2name: eg = g1 step = au.measure_str( ) + ' ' + measure_function.func_name + ' ' + experiment + ' ' + datadir #remove subject from stats if eg > -1: exclude_str = '_' + au.excluded_str() + str(exclude_idx) step += exclude_str experiment += exclude_str if not au.is_done(chkf, step): if not subject_excluded: newmeanfname = gfnames[eg, 0] + exclude_str newvarfname = gfnames[eg, 1] + exclude_str newstdfname = gfnames[eg, 2] + exclude_str rstep = au.remove_str() + ' ' + au.subject_str( ) + ' ' + str(exclude_subj) + ' ' + au.fromstats_str( ) + ' ' + datadir if not au.is_done(chkf, rstep): #(meanfname, varfname, samplesize, subjvolfname, newmeanfname, newvarfname, newstdfname='') remove_subject_from_stats(gfnames[eg, 0], gfnames[eg, 1], groupsizes[eg][1], exclude_subj, newmeanfname, newvarfname, newstdfname) au.checklist_add(chkf, rstep) gfnames[eg, 0] += exclude_str gfnames[eg, 1] += exclude_str gfnames[eg, 2] += exclude_str groupsizes[eg][1] -= 1 subject_excluded = True #calculating distance if not au.is_done(chkf, step): mean1fname = au.add_extension_if_needed( gfnames[g1, 0], au.ext_str()) mean2fname = au.add_extension_if_needed( gfnames[g2, 0], au.ext_str()) var1fname = au.add_extension_if_needed(gfnames[g1, 1], au.ext_str()) var2fname = au.add_extension_if_needed(gfnames[g2, 1], au.ext_str()) std1fname = au.add_extension_if_needed(gfnames[g1, 2], au.ext_str()) std2fname = au.add_extension_if_needed(gfnames[g2, 2], au.ext_str()) outfname = measure_function(mean1fname, mean2fname, var1fname, var2fname, std1fname, std2fname, groupsizes[g1][1], groupsizes[g2][1], experiment, outdir, exclude_idx) if absolute: change_to_absolute_values(outfname) au.checklist_add(chkf, step) return outfname