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
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")