def plinear_regression(ffig_dpl, fdpl): dpl = Dipole(fdpl) layer = 'L5' t0 = 750. # dipole for the given layer, truncated # order matters here x_dpl = dpl.dpl[layer][(dpl.t > t0)] t = dpl.t[dpl.t > t0] # take the transpose (T) of a vector of the times and ones for each element A = np.vstack([t, np.ones(len(t))]).T # find the slope and the y-int of the line fit with least squares method (min. of Euclidean 2-norm) m, c = np.linalg.lstsq(A, x_dpl)[0] print(m, c) # plot me f = ac.FigStd() f.ax0.plot(t, x_dpl) f.ax0.hold(True) f.ax0.plot(t, m*t + c, 'r') # save over the original f.savepng(ffig_dpl) f.close()
def pmaxpwr(file_name, results_list, fparam_list): f = ac.FigStd() f.ax0.hold(True) # instantiate lists for storing x and y data x_data = [] y_data = [] # plot points for result, fparam in zip(results_list, fparam_list): p = paramrw.read(fparam)[1] x_data.append(p['f_input_prox']) y_data.extend(result['freq_at_max']) f.ax0.plot(x_data[-1], y_data[-1], 'kx') # add trendline fit = np.polyfit(x_data, y_data, 1) fit_fn = np.poly1d(fit) f.ax0.plot(x_data, fit_fn(x_data), 'k-') # Axis stuff f.ax0.set_xlabel('Proximal/Distal Input Freq (Hz)') f.ax0.set_ylabel('Freq at which max avg power occurs (Hz)') f.save(file_name)
def pspecpwr(file_name, results_list, fparam_list, key_types, error_vec=[]): # instantiate fig f = ac.FigStd() f.set_fontsize(18) # pspecpwr_ax is a plot kernel for specpwr plotting legend_list = pspecpwr_ax(f.ax0, results_list, fparam_list, key_types) # Add error bars if necessary if len(error_vec): # errors are only used with avg'ed data. There will be only one entry in results_list pyerrorbars_ax(f.ax0, results_list[0]['freq'], results_list[0]['p_avg'], error_vec) # insert legend f.ax0.legend(legend_list, loc='upper right', prop={'size': 8}) # axes labels f.ax0.set_xlabel('Freq (Hz)') f.ax0.set_ylabel('Avgerage Power (nAm^2)') # add title # f.set_title(fparam_list[0], key_types) f.savepng(file_name) # f.save(file_name) # f.saveeps(file_name) f.close()
def pdipole_exp(ddata, ylim=[]): """ over ALL trials in ALL conditions in EACH experiment """ # sim_prefix fprefix = ddata.sim_prefix # create the figure name fname_exp = '%s_dpl' % (fprefix) fname_exp_fig = os.path.join(ddata.dsim, fname_exp + '.png') # create one figure comparing across all N_expmt_groups = len(ddata.expmt_groups) f_exp = ac.FigDipoleExp(ddata.expmt_groups) # empty list for the aggregate dipole data dpl_exp = [] # go through each expmt for expmt_group in ddata.expmt_groups: # create the filename dexp = ddata.dexpmt_dict[expmt_group] fname_short = '%s-%s-dpl' % (fprefix, expmt_group) fname_data = os.path.join(dexp, fname_short + '.txt') fname_fig = os.path.join(ddata.dfig[expmt_group]['figdpl'], fname_short + '.png') # grab the list of raw data dipoles and assoc params in this expmt dpl_list = ddata.file_match(expmt_group, 'rawdpl') param_list = ddata.file_match(expmt_group, 'param') for f_dpl, f_param in zip(dpl_list, param_list): dpl = Dipole(f_dpl) dpl.baseline_renormalize(f_param) # x_tmp = np.loadtxt(open(file, 'r')) # initialize and use x_dpl if f_dpl is dpl_list[0]: # assume time vec stays the same throughout t_vec = dpl.t x_dpl = dpl.dpl['agg'] else: # guaranteed to exist after dpl_list[0] x_dpl += dpl.dpl['agg'] # poor man's mean x_dpl /= len(dpl_list) # save this in a list to do comparison figure # order is same as ddata.expmt_groups dpl_exp.append(x_dpl) # write this data to the file with open(fname_data, 'w') as f: for t, x in zip(t_vec, x_dpl): f.write("%03.3f\t%5.4f\n" % (t, x)) # create the plot I guess? f = ac.FigStd() f.ax0.plot(t_vec, x_dpl) if len(ylim): f.ax0.set_ylim(ylim) f.savepng(fname_fig) f.close() # plot the aggregate data using methods defined in FigDipoleExp() f_exp.plot(t_vec, dpl_exp) # attempt at setting titles for ax, expmt_group in zip(f_exp.ax, ddata.expmt_groups): ax.set_title(expmt_group) f_exp.savepng(fname_exp_fig) f_exp.close()
def pdipole_evoked(dfig, f_dpl, f_spk, f_param, ylim=[]): """ for each individual simulation/trial """ gid_dict, p_dict = paramrw.read(f_param) # get the spike dict from the files s_dict = spikefn.spikes_from_file(f_param, f_spk) s = s_dict.keys() s.sort() # create an empty dict 'spk_unique' spk_unique = dict.fromkeys([key for key in s_dict.keys() if key.startswith(('evprox', 'evdist'))]) for key in spk_unique: spk_unique[key] = s_dict[key].unique_all(0) # draw vertical lines for each item in this # x_dipole is dipole data # x_dipole = np.loadtxt(open(f_dpl, 'r')) dpl = Dipole(f_dpl) # split to find file prefix file_prefix = f_dpl.split('/')[-1].split('.')[0] # # set xmin value # xmin = xlim[0] / p_dict['dt'] # # set xmax value # if xlim[1] == 'tstop': # xmax = p_dict['tstop'] / p_dict['dt'] # else: # xmax = xlim[1] / p_dict['dt'] # these are the vectors for now, but this is going to change t_vec = dpl.t dp_total = dpl.dpl['agg'] f = ac.FigStd() # hold on f.ax0.hold(True) f.ax0.plot(t_vec, dp_total) lines_spk = dict.fromkeys(spk_unique) print(spk_unique) # plot the lines for key in spk_unique: print(key, spk_unique[key]) x_val = spk_unique[key][0] lines_spk[key] = plt.axvline(x=x_val, linewidth=0.5, color='r') # title_txt = [key + ': {:.2e}' % p_dict[key] for key in key_types['dynamic_keys']] title_txt = 'test' f.ax0.set_title(title_txt) if ylim: f.ax0.set_ylim(ylim) fig_name = os.path.join(dfig, file_prefix+'.png') plt.savefig(fig_name, dpi=300) f.close()
def pdipole(f_dpl, dfig, plot_dict, f_param=None, key_types={}): """ single dipole file combination (incl. param file) this should be done with an axis input too two separate functions, a pdipole kernel function and a specific function for this simple plot """ # dpl is an obj of Dipole() class dpl = Dipole(f_dpl) if f_param: dpl.baseline_renormalize(f_param) dpl.convert_fAm_to_nAm() # split to find file prefix file_prefix = f_dpl.split('/')[-1].split('.')[0] # parse xlim from plot_dict if plot_dict['xlim'] is None: xmin = dpl.t[0] xmax = dpl.t[-1] else: xmin, xmax = plot_dict['xlim'] if xmin < 0.: xmin = 0. if xmax < 0.: xmax = self.f[-1] # # get xmin and xmax from the plot_dict # if plot_dict['xmin'] is None: # xmin = 0. # else: # xmin = plot_dict['xmin'] # if plot_dict['xmax'] is None: # xmax = p_dict['tstop'] # else: # xmax = plot_dict['xmax'] # truncate them using logical indexing t_range = dpl.t[(dpl.t >= xmin) & (dpl.t <= xmax)] dpl_range = dpl.dpl['agg'][(dpl.t >= xmin) & (dpl.t <= xmax)] f = ac.FigStd() f.ax0.plot(t_range, dpl_range) # sorry about the parity between vars here and above with xmin/xmax if plot_dict['ylim'] is None: # if plot_dict['ymin'] is None or plot_dict['ymax'] is None: pass else: f.ax0.set_ylim(plot_dict['ylim'][0], plot_dict['ylim'][1]) # f.ax0.set_ylim(plot_dict['ymin'], plot_dict['ymax']) # Title creation if f_param and key_types: # grabbing the p_dict from the f_param _, p_dict = paramrw.read(f_param) # useful for title strings title_str = ac.create_title(p_dict, key_types) f.f.suptitle(title_str) # create new fig name fig_name = os.path.join(dfig, file_prefix+'.png') # savefig plt.savefig(fig_name, dpi=300) f.close()