Esempio n. 1
0
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()
Esempio n. 2
0
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()
Esempio n. 3
0
def random_timecourses(hdf, model, N, nsim, name=None):
    """ Plot <N> randomly selected BOLD and design matrix timecourses 
    from <nsim> options for <model> from <hdf>.  

    If <name> is not None, the results are saved as a pdf. """

    # Open a handle the hdf file
    f = h5py.File(hdf, 'r')

    # Create (or use) a handle to a multi-page pdf
    if name != None:
        if isinstance(name, PdfPages):
            pdf = name  ## Use
        else:
            pdf = PdfPages('{0}.pdf'.format(name))  ## Create

    # Randomly select sims between 0-nsim
    # And mae seperate plots for each
    # adding them to the same pdf.
    selected = np.random.randint(0, nsim, N)
    for sel in selected:

        # Get metadata
        meta = get_model_meta(hdf, model)

        # Create a figure window
        fig = plt.figure()
        ax = fig.add_subplot(111)

        # Get the data
        dm = f[os.path.join("/", str(sel), model, "dm")].value
        bold = f[os.path.join("/", str(sel), model, "bold")].value

        # PLot it
        ax.plot(dm)
        ax.plot(bold, color="grey")

        # Pretty up the plot
        boldleg = [
            "".join([
                "BOLD: ",
            ] + meta["bold"].tolist()),
        ]
        plt.legend(meta["dm"] + boldleg)
        plt.title('Timecourse {0} from {1}.'.format(sel, model))
        ## TODO add labels?

        if name != None:
            # Add to pdf...
            plt.savefig(pdf, format="pdf")

    if name != None:
        pdf.close()
Esempio n. 4
0
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()
Esempio n. 5
0
def hist_t(hdf, model, name=None):
    """ 
    Plot histograms of the t values in <hdf> for each condition in 
    <model>.
    
    If <name> is not None the plot is saved as <name>.pdf.
    """

    meta = get_model_meta(hdf, model)
    hist_list = []
    for dm_col in meta['dm']:
        # Make an instance RHist for the list.
        hist = RHist(name=dm_col, decimals=1)
        hist_list.append(hist)

    # read_hdf_inc returns a generator so....
    tdata = read_hdf_inc(hdf, '/' + model + '/t')
    for ts in tdata:
        # get the tvals for each instance of model
        # and add them to the hist_list,
        [hist_list[ii].add(ts[ii]) for ii in range(len(ts) - 1)]
        ## The last t in ts is the constant, which we
        ## do not want to plot.

    # Create a fig, loop over the hist_list
    # plotting each on fig.axes = 0.
    fig = plt.figure()
    fig.add_subplot(111)
    colors = itertools.cycle(
        ['DarkGray', 'DarkBlue', 'DarkGreen', 'MediumSeaGreen'])
    ## Using html colors...

    [h.plot(fig=fig, color=colors.next(), norm=True) for h in hist_list]

    # Prettify the plot
    ax = fig.axes[0]
    ax.set_xlabel('t-values')
    ax.set_ylabel('P(t)')

    # Add vetical lines representing significance tresholds
    ax.axvline(x=1.7822, label='p < 0.05', color='red', linewidth=4)
    ax.axvline(x=2.6810, label='p < 0.01', color='red', linewidth=3)
    ax.axvline(x=3.0545, label='p < 0.005', color='red', linewidth=2)
    ax.axvline(x=4.3178, label='p < 0.0005', color='red', linewidth=1)
    ## tval lines assume N=12 subjects

    plt.xlim(-10, 15)
    plt.legend()
    plt.title('{0} -- BOLD: {1}'.format(model, meta['bold']))

    if name != None:
        plt.savefig(name, format="pdf")
Esempio n. 6
0
def hist_t(hdf, model, name=None):
    """ 
    Plot histograms of the t values in <hdf> for each condition in 
    <model>.
    
    If <name> is not None the plot is saved as <name>.pdf.
    """

    meta = get_model_meta(hdf, model)
    hist_list = []
    for dm_col in meta["dm"]:
        # Make an instance RHist for the list.
        hist = RHist(name=dm_col, decimals=1)
        hist_list.append(hist)

    # read_hdf_inc returns a generator so....
    tdata = read_hdf_inc(hdf, "/" + model + "/t")
    for ts in tdata:
        # get the tvals for each instance of model
        # and add them to the hist_list,
        [hist_list[ii].add(ts[ii]) for ii in range(len(ts) - 1)]
        ## The last t in ts is the constant, which we
        ## do not want to plot.

    # Create a fig, loop over the hist_list
    # plotting each on fig.axes = 0.
    fig = plt.figure()
    fig.add_subplot(111)
    colors = itertools.cycle(["DarkGray", "DarkBlue", "DarkGreen", "MediumSeaGreen"])
    ## Using html colors...

    [h.plot(fig=fig, color=colors.next(), norm=True) for h in hist_list]

    # Prettify the plot
    ax = fig.axes[0]
    ax.set_xlabel("t-values")
    ax.set_ylabel("P(t)")

    # Add vetical lines representing significance tresholds
    ax.axvline(x=1.7822, label="p < 0.05", color="red", linewidth=4)
    ax.axvline(x=2.6810, label="p < 0.01", color="red", linewidth=3)
    ax.axvline(x=3.0545, label="p < 0.005", color="red", linewidth=2)
    ax.axvline(x=4.3178, label="p < 0.0005", color="red", linewidth=1)
    ## tval lines assume N=12 subjects

    plt.xlim(-10, 15)
    plt.legend()
    plt.title("{0} -- BOLD: {1}".format(model, meta["bold"]))

    if name != None:
        plt.savefig(name, format="pdf")
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()
Esempio n. 8
0
def random_timecourses(hdf, model, N, nsim, name=None):
    """ Plot <N> randomly selected BOLD and design matrix timecourses 
    from <nsim> options for <model> from <hdf>.  

    If <name> is not None, the results are saved as a pdf. """

    # Open a handle the hdf file
    f = h5py.File(hdf, "r")

    # Create (or use) a handle to a multi-page pdf
    if name != None:
        if isinstance(name, PdfPages):
            pdf = name  ## Use
        else:
            pdf = PdfPages("{0}.pdf".format(name))  ## Create

    # Randomly select sims between 0-nsim
    # And mae seperate plots for each
    # adding them to the same pdf.
    selected = np.random.randint(0, nsim, N)
    for sel in selected:

        # Get metadata
        meta = get_model_meta(hdf, model)

        # Create a figure window
        fig = plt.figure()
        ax = fig.add_subplot(111)

        # Get the data
        dm = f[os.path.join("/", str(sel), model, "dm")].value
        bold = f[os.path.join("/", str(sel), model, "bold")].value

        # PLot it
        ax.plot(dm)
        ax.plot(bold, color="grey")

        # Pretty up the plot
        boldleg = ["".join(["BOLD: "] + meta["bold"].tolist())]
        plt.legend(meta["dm"] + boldleg)
        plt.title("Timecourse {0} from {1}.".format(sel, model))
        ## TODO add labels?

        if name != None:
            # Add to pdf...
            plt.savefig(pdf, format="pdf")

    if name != None:
        pdf.close()
Esempio n. 9
0
def create_hist_list(hdf, model, stat):
    """ Create a list of Rhist (histogram) objects for <model> and 
    <stat> in the given <hdf>. 
    
    If <stat> has only one entry (as is the case for 'aic') the list will have 
    only one entry.  If however <stat> has n entries per model (like't') 
    the list will have n-1 entries. As n matches the number of columns in
    the design matrix, the rightmost will always correspond to the dummy 
    predictor and is therefore discarded. """

    hist_list = []  ## A list of RHist objects.
    meta = get_model_meta(hdf, model)  ## metadata for naming

    # A handle on the hdf data
    hdfdata = read_hdf(hdf, '/' + model + '/' + stat)

    # Loop over the nodes, adding the data
    # for each to a RHist.
    for node in hdfdata:
        # Some data will be list-like
        # so try to iterate, if that fails
        # assume the data is a single number
        try:
            for ii in range(len(node) - 1):
                # Init entries in hist_list as needed
                try:
                    hist_list[ii].add(node[ii])
                except IndexError:
                    hist_list.append(RHist(name=meta['dm'][ii], decimals=2))
                    hist_list[ii].add(node[ii])
        except TypeError:
            # Assume a number so hist_list has only one
            # entry (i.e. 0).
            #
            # Init entries in hist_list as needed
            try:
                hist_list[0].add(node)
            except IndexError:
                hist_list.append(RHist(name=stat, decimals=2))
                hist_list[0].add(node)

    return hist_list
Esempio n. 10
0
def create_hist_list(hdf, model, stat):
    """ Create a list of Rhist (histogram) objects for <model> and 
    <stat> in the given <hdf>. 
    
    If <stat> has only one entry (as is the case for 'aic') the list will have 
    only one entry.  If however <stat> has n entries per model (like't') 
    the list will have n-1 entries. As n matches the number of columns in
    the design matrix, the rightmost will always correspond to the dummy 
    predictor and is therefore discarded. """
    
    hist_list = [] ## A list of RHist objects.
    meta = get_model_meta(hdf, model) ## metadata for naming
    
    # A handle on the hdf data
    hdfdata = read_hdf(hdf, '/' + model + '/' + stat)   
    
    # Loop over the nodes, adding the data
    # for each to a RHist. 
    for node in hdfdata:
        # Some data will be list-like
        # so try to iterate, if that fails
        # assume the data is a single number
        try:
            for ii in range(len(node)-1):
                # Init entries in hist_list as needed
                try:
                    hist_list[ii].add(node[ii])
                except IndexError:
                    hist_list.append(RHist(name=meta['dm'][ii], decimals=2))
                    hist_list[ii].add(node[ii])
        except TypeError:
            # Assume a number so hist_list has only one
            # entry (i.e. 0).
            #
            # Init entries in hist_list as needed
            try:
                hist_list[0].add(node)
            except IndexError:
                hist_list.append(RHist(name=stat, decimals=2))
                hist_list[0].add(node)

    return hist_list
Esempio n. 11
0
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()
Esempio n. 12
0
def create_hist(hdf, model, stat):
    """ 
    Create histograms of stat values in <hdf> for each condition in 
    <model>.
    """

    meta = get_model_meta(hdf, model)
    hist_list = []
    for dm_col in meta['dm']:
        # Make an instance RHist for the list.
        hist = RHist(name=dm_col, decimals=1)
        hist_list.append(hist)
    
    # read_hdf_inc returns a generator so....
    data = read_hdf_inc(hdf,'/'+ model + '/' + stat)
    for row in data:
        # get the row values for each instance of model
        # and add them to the hist_list,
        [hist_list[ii].add(row[ii]) for ii in range(len(row)-1)]
            ## The last t in ts is the constant, which we 
            ## do not want to plot.

    return hist_list
Esempio n. 13
0
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()