def exec_specmax(ddata, opts): p = { 'expmt_group': '', 'n_sim': 0, 'n_trial': 0, 't_interval': None, 'f_interval': None, 'f_sort': None, # 't_interval': [0., -1], # 'f_interval': [0., -1], } args_check(p, opts) p_exp = paramrw.ExpParams(ddata.fparam) # trial_prefix = p_exp.trial_prefix_str % (p['n_sim'], p['n_trial']) if not p['expmt_group']: p['expmt_group'] = ddata.expmt_groups[0] # Get the associated dipole and spec file fspec = ddata.return_specific_filename(p['expmt_group'], 'rawspec', p['n_sim'], p['n_trial']) # Load the spec data spec = specfn.Spec(fspec) # get max data data_max = spec.max('agg', p['t_interval'], p['f_interval'], p['f_sort']) if data_max: print "Max power of %4.2e at f of %4.2f Hz at %4.3f ms" % (data_max['pwr'], data_max['f_at_max'], data_max['t_at_max'])
def exec_throwaway(ddata, opts): p = { 'n_sim': 0, 'n_trial': 0, } args_check(p, opts) p_exp = paramrw.ExpParams(ddata.fparam) N_trials = p_exp.N_trials print opts, p if p['n_sim'] == -1: for i in range(p_exp.N_sims): if p['n_trial'] == -1: for j in range(N_trials): dipolefn.dpl_convert_and_save(ddata, i, j) else: j = p['n_trial'] dipolefn.dpl_convert_and_save(ddata, i, j) else: i = p['n_sim'] if p['n_trial'] == -1: for j in range(N_trials): dipolefn.dpl_convert_and_save(ddata, i, j) else: j = p['n_trial'] dipolefn.dpl_convert_and_save(ddata, i, j)
def dpl_convert_and_save(ddata, i=0, j=0): """ trial is currently undefined function is broken for N_trials > 1 """ # take the ith sim, jth trial, do some stuff to it, resave it # only uses first expmt_group expmt_group = ddata.expmt_groups[0] # need n_trials p_exp = paramrw.ExpParams(ddata.fparam) if not p_exp.N_trials: N_trials = 1 else: N_trials = p_exp.N_trials # absolute number n = i*N_trials + j # grab the correct files f_dpl = ddata.file_match(expmt_group, 'rawdpl')[n] f_param = ddata.file_match(expmt_group, 'param')[n] # print ddata.sim_prefix, ddata.dsim f_name_short = '%s-%03d-T%02d-dpltest.txt' % (ddata.sim_prefix, i, j) f_name = os.path.join(ddata.dsim, expmt_group, f_name_short) print(f_name) dpl = Dipole(f_dpl) dpl.baseline_renormalize(f_param) print("baseline renormalized") dpl.convert_fAm_to_nAm() print("converted to nAm") dpl.write(f_name)
def exec_replot(ddata, opts): # def regenerate_plots(ddata, xlim=[0, 'tstop']): p = { 'xlim': None, 'ylim': None, } args_check(p, opts) # recreate p_exp ... don't like this # ** should be guaranteed to be identical ** p_exp = paramrw.ExpParams(ddata.fparam) # grab the list of spec results that exists # there is a method in SimulationPaths/ddata for this specifically, this should be deprecated # fspec_list = fio.file_match(ddata.dsim, '-spec.npz') # generate data if no spec exists here if not fio.file_match(ddata.dsim, '-spec.npz'): # if not fspec_list: print "No saved spec data found. Performing spec anaylsis ... " exec_spec_regenerate(ddata) # spec_results = exec_spec_regenerate(ddata) # run our core pall plot plotfn.pall(ddata, p_exp, p['xlim'], p['ylim'])
def freqpwr_with_hist(ddata, dsim): fspec_list = fio.file_match(ddata.dsim, '-spec.npz') spk_list = fio.file_match(ddata.dsim, '-spk.txt') fparam_list = fio.file_match(ddata.dsim, '-param.txt') p_exp = paramrw.ExpParams(ddata.fparam) key_types = p_exp.get_key_types() # If no save spec reslts exist, redo spec analysis if not fspec_list: print "No saved spec data found. Performing spec analysis...", exec_spec_regenerate(ddata) fspec_list = fio.file_match(ddata.dsim, '-spec.npz') # spec_results = exec_spec_regenerate(ddata) print "now doing spec freq-pwr analysis" # perform freqpwr analysis freqpwr_results_list = [specfn.freqpwr_analysis(fspec) for fspec in fspec_list] # Plot for freqpwr_result, f_spk, fparam in zip(freqpwr_results_list, spk_list, fparam_list): gid_dict, p_dict = paramrw.read(fparam) file_name = 'freqpwr.png' specfn.pfreqpwr_with_hist(file_name, freqpwr_result, f_spk, gid_dict, p_dict, key_types)
def __init__(self, paramf, index, parent=None, width=12, height=10, dpi=120, title='Spectrogram Viewer'): FigureCanvas.__init__(self, Figure(figsize=(width, height), dpi=dpi)) self.title = title self.setParent(parent) self.gui = parent self.index = index FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) self.paramf = paramf self.invertedhistax = False self.G = gridspec.GridSpec(10, 1) self.dat = [] self.lextspec = [] self.lax = [] self.avgdipole = [] self.avgspec = [] # get spec_cmap p_exp = paramrw.ExpParams(self.paramf, 0) if len(p_exp.expmt_groups) > 0: expmt_group = p_exp.expmt_groups[0] else: expmt_group = None p = p_exp.return_pdict(expmt_group, 0) self.spec_cmap = p['spec_cmap'] self.plot()
def __init__(self, paramf, index, parent=None, width=12, height=10, dpi=120, title='LFP Viewer'): FigureCanvas.__init__(self, Figure(figsize=(width, height), dpi=dpi)) self.title = title self.setParent(parent) self.index = index FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) self.paramf = paramf self.drawwavelet = True # get spec_cmap p_exp = paramrw.ExpParams(self.paramf, 0) if len(p_exp.expmt_groups) > 0: expmt_group = p_exp.expmt_groups[0] else: expmt_group = None p = p_exp.return_pdict(expmt_group, 0) self.spec_cmap = p['spec_cmap'] self.plot()
def exec_spec_stationary_avg(ddata, dsim, maxpwr): # Prompt user for type of analysis (per expmt or whole sim) analysis_type = raw_input('Would you like analysis per expmt or for whole sim? (expmt or sim): ') fspec_list = fio.file_match(ddata.dsim, '-spec.npz') fparam_list = fio.file_match(ddata.dsim, '-param.txt') # fspec_list = fio.file_match(ddata.dsim, '-spec.npz') # fparam_list = fio.file_match(ddata.dsim, '-param.txt') p_exp = paramrw.ExpParams(ddata.fparam) key_types = p_exp.get_key_types() # If no saved spec results exist, redo spec analysis if not fspec_list: print "No saved spec data found. Performing spec analysis...", exec_spec_regenerate(ddata) fspec_list = fio.file_match(ddata.dsim, '-spec.npz') # spec_results = exec_spec_regenerate(ddata) print "now doing spec freq-pwr analysis" # perform time-averaged stationary analysis # specpwr_results = [specfn.specpwr_stationary_avg(fspec) for fspec in fspec_list] specpwr_results = [] for fspec in fspec_list: spec = specfn.Spec(fspec) specpwr_results.append(spec.stationary_avg()) # plot for whole simulation if analysis_type == 'sim': file_name = os.path.join(dsim, 'specpwr.eps') pspec.pspecpwr(file_name, specpwr_results, fparam_list, key_types) # if maxpwr plot indicated if maxpwr: f_name = os.path.join(dsim, 'maxpwr.png') specfn.pmaxpwr(f_name, specpwr_results, fparam_list) # plot per expmt if analysis_type == 'expmt': for expmt_group in ddata.expmt_groups: # create name for figure. Figure saved to expmt directory file_name = os.path.join(dsim, expmt_group, 'specpwr.png') # compile list of freqpwr results and param pathways for expmt partial_results_list = [result for result in specpwr_results if result['expmt']==expmt_group] partial_fparam_list = [fparam for fparam in fparam_list if expmt_group in fparam] # plot results pspec.pspecpwr(file_name, partial_results_list, partial_fparam_list, key_types) # if maxpwr plot indicated if maxpwr: f_name = os.path.join(dsim, expmt_group, 'maxpwr.png') specfn.pmaxpwr(f_name, partial_results_list, partial_fparam_list)
def exec_specmax_dpl_tmpl(ddata, opts): p = { 'expmt_group': '', 'n_sim': 0, 'trials': [0, -1], 't_interval': None, 'f_interval': None, 'f_sort': None, } args_check(p, opts) # set expmt group if not p['expmt_group']: p['expmt_group'] = ddata.expmt_groups[0] # set directory to save template in and check that it exists dir_out = os.path.join(ddata.dsim, p['expmt_group'], 'tmpldpl') fio.dir_create(dir_out) # if p['trials'][1] is -1, assume all trials are wanted # 1 is subtracted from N_trials to be consistent with manual entry of trial range if p['trials'][1] == -1: p_exp = paramrw.ExpParams(ddata.fparam) p['trials'][1] = p_exp.N_trials - 1 # Get spec, dpl, and param files # Sorry for lack of readability spec_list = [ddata.return_specific_filename(p['expmt_group'], 'rawspec', p['n_sim'], i) for i in range(p['trials'][0], p['trials'][1]+1)] dpl_list = [ddata.return_specific_filename(p['expmt_group'], 'rawdpl', p['n_sim'], i) for i in range(p['trials'][0], p['trials'][1]+1)] param_list = [ddata.return_specific_filename(p['expmt_group'], 'param', p['n_sim'], i) for i in range(p['trials'][0], p['trials'][1]+1)] # Get max spectral data data_max_list = [] for fspec in spec_list: spec = specfn.Spec(fspec) data_max_list.append(spec.max('agg', p['t_interval'], p['f_interval'], p['f_sort'])) # Get time intervals of max spectral pwr t_interval_list = [dmax['t_int'] for dmax in data_max_list if dmax is not None] # truncate dpl_list to include only sorted trials # kind of crazy that this works. Just sayin'... dpl_list = [fdpl for fdpl, dmax in zip(dpl_list, data_max_list) if dmax is not None] # create file name if p['f_sort']: fname_short = "sim-%03i-T%03i-T%03d-sort-%i-%i-tmpldpl.txt" %(p['n_sim'], p['trials'][0], p['trials'][1], p['f_sort'][0], p['f_sort'][1]) else: fname_short = "sim-%03i-T%03i-T%03i-tmpldpl.txt" %(p['n_sim'], p['trials'][0], p['trials'][1]) fname = os.path.join(dir_out, fname_short) # Create dpl template dipolefn.create_template(fname, dpl_list, param_list, t_interval_list)
def exec_aggregatespec(ddata, labels): p_exp = paramrw.ExpParams(ddata.fparam) fspec_list = fio.file_match(ddata.dsim, '-spec.npz') # generate data if no spec exists here if not fspec_list: print "No saved spec data found. Performing spec anaylsis ... " exec_spec_regenerate(ddata) plotfn.aggregate_spec_with_hist(ddata, p_exp, labels)
def exec_specmax_dpl_match(ddata, opts): p = { 'expmt_group': '', 'n_sim': 0, 'trials': [0, -1], 't_interval': None, 'f_interval': None, 'f_sort': None, } args_check(p, opts) # set expmt group if not p['expmt_group']: p['expmt_group'] = ddata.expmt_groups[0] # set directory to save fig in and check that it exists dir_fig = os.path.join(ddata.dsim, p['expmt_group'], 'figint') fio.dir_create(dir_fig) # if p['trials'][1] is -1, assume all trials are wanted # 1 is subtracted from N_trials to be consistent with manual entry of trial range if p['trials'][1] == -1: p_exp = paramrw.ExpParams(ddata.fparam) p['trials'][1] = p_exp.N_trials - 1 # Get spec, dpl, and param files # Sorry for lack of readability spec_list = [ddata.return_specific_filename(p['expmt_group'], 'rawspec', p['n_sim'], i) for i in range(p['trials'][0], p['trials'][1]+1)] dpl_list = [ddata.return_specific_filename(p['expmt_group'], 'rawdpl', p['n_sim'], i) for i in range(p['trials'][0], p['trials'][1]+1)] param_list = [ddata.return_specific_filename(p['expmt_group'], 'param', p['n_sim'], i) for i in range(p['trials'][0], p['trials'][1]+1)] # Get max spectral data data_max_list = [] for fspec in spec_list: spec = specfn.Spec(fspec) data_max_list.append(spec.max('agg', p['t_interval'], p['f_interval'], p['f_sort'])) # create fig name if p['f_sort']: fname_short = "sim-%03i-T%03i-T%03d-sort-%i-%i" %(p['n_sim'], p['trials'][0], p['trials'][1], p['f_sort'][0], p['f_sort'][1]) else: fname_short = "sim-%03i-T%03i-T%03i" %(p['n_sim'], p['trials'][0], p['trials'][1]) fname = os.path.join(dir_fig, fname_short) # plot time-series over proper intervals dipolefn.plot_specmax_interval(fname, dpl_list, param_list, data_max_list)
def do_load(self, args): """Load parameter file and regens all vars Date needs to be set correctly for this to work. See 'help setdate' Usage example: [s1sh] setdate 2013-01-01 [s1sh] load mucomplex-000 Running without arguments will load the last modified directory found in the date dir: [s1] load """ if not args: # attempt to load the most recent in the dproj/ddate # find the most recent directory in this folder list_d = [] for dsim_short in os.listdir(self.ddate): # check to see if dsim_tmp is actually a dir dsim_tmp = os.path.join(self.ddate, dsim_short) # append to list along with its modified time (mtime) if os.path.isdir(dsim_tmp): list_d.append( (dsim_tmp, time.ctime(os.path.getmtime(dsim_tmp)))) # sort by mtime list_d.sort(key=lambda x: x[1]) # grab the directory name of the most recent dir dcheck = list_d[-1][0] else: # dir_check is the attempt at creating this directory dcheck = os.path.join(self.dproj, self.ddate, args) # check existence of the path if os.path.exists(dcheck): # create blank ddata structure from SimPaths self.ddata = fio.SimulationPaths() # set dsim after using ddata's readsim method self.dsim = self.ddata.read_sim(self.dproj, dcheck) self.p_exp = paramrw.ExpParams(self.ddata.fparam) print self.ddata.fparam self.var_list = paramrw.changed_vars(self.ddata.fparam) else: print dcheck print "Could not find that dir, maybe check your date?"
def exec_addalphahist(ddata, opts): # def exec_addalphahist(ddata, xlim=[0, 'tstop']): p = { 'xlim': None, 'ylim': None, } args_check(p, opts) p_exp = paramrw.ExpParams(ddata.fparam) # generate data if no spec exists here if not fio.file_match(ddata.dsim, '-spec.npz'): print "No saved spec data found. Performing spec anaylsis ... " exec_spec_regenerate(ddata) plotfn.pdpl_pspec_with_hist(ddata, p_exp, p['xlim'], p['ylim'])
def exec_dipolemin(ddata, expmt_group, n_sim, n_trial, t_interval): p_exp = paramrw.ExpParams(ddata.fparam) trial_prefix = p_exp.trial_prefix_str % (n_sim, n_trial) # list of all the dipoles dpl_list = ddata.file_match(expmt_group, 'rawdpl') # load the associated dipole file # find the specific file # assume just the first file fdpl = [file for file in dpl_list if trial_prefix in file][0] data = np.loadtxt(open(fdpl, 'r')) t_vec = data[:, 0] data_dpl = data[:, 1] data_dpl_range = data_dpl[(t_vec >= t_interval[0]) & (t_vec <= t_interval[1])] dpl_min_range = data_dpl_range.min() t_min_range = t_vec[data_dpl == dpl_min_range] print "Minimum value over t range %s was %4.4f at %4.4f." % (str(t_interval), dpl_min_range, t_min_range)
def generate_missing_spec(ddata, f_max=40): # just check first expmt_group expmt_group = ddata.expmt_groups[0] # list of spec data l_spec = ddata.file_match(expmt_group, 'rawspec') # if this list is empty, assume it is everywhere and run the analysis function if not l_spec: opts = { 'type': 'dpl_laminar', 'f_max': f_max, 'save_data': 1, 'runtype': 'parallel', } analysis_typespecific(ddata, opts) else: # this is currently incorrect, it should actually return the data that has been referred to # as spec_results. such a function to properly get this without analysis (eg. reader to this data) # should exist spec = [] # do the one for current, too. Might as well at this point l_speccurrent = ddata.file_match(expmt_group, 'rawspeccurrent') if not l_speccurrent: p_exp = paramrw.ExpParams(ddata.fparam) opts = { 'type': 'current', 'f_max': 90., 'save_data': 1, 'runtype': 'parallel', } analysis_typespecific(ddata, opts) else: spec_current = []
# if pcID==0: print('setoutfiles:',trial,ntrial) doutf = {} doutf['file_dpl'] = getfname(ddir, 'rawdpl', trial, ntrial) doutf['file_current'] = getfname(ddir, 'rawcurrent', trial, ntrial) doutf['file_param'] = getfname(ddir, 'param', trial, ntrial) doutf['file_spikes'] = getfname(ddir, 'rawspk', trial, ntrial) doutf['file_spec'] = getfname(ddir, 'rawspec', trial, ntrial) doutf['filename_debug'] = 'debug.dat' doutf['file_dpl_norm'] = getfname(ddir, 'normdpl', trial, ntrial) doutf['file_vsoma'] = getfname(ddir, 'vsoma', trial, ntrial) doutf['file_lfp'] = getfname(ddir, 'lfp', trial, ntrial) #if pcID==0: print('doutf:',doutf) return doutf p_exp = paramrw.ExpParams( f_psim, debug=debug) # creates p_exp.sim_prefix and other param structures ddir = setupsimdir(f_psim, p_exp, pcID) # one directory for all experiments # create rotating data files doutf = setoutfiles(ddir) # core iterator through experimental groups expmt_group = p_exp.expmt_groups[0] simparams = p = p_exp.return_pdict( expmt_group, 0) # return the param dict for this simulation pc.barrier() # get all nodes to this place before continuing pc.gid_clear() # global variables, should be node-independent h("dp_total_L2 = 0.") h("dp_total_L5 = 0.")
def ppsth_grid(simpaths): # get filename lists in dictionaries of experiments dict_exp_param = simpaths.exp_files_of_type('param') dict_exp_spk = simpaths.exp_files_of_type('rawspk') # recreate the ExpParams object used in the simulation p_exp = paramrw.ExpParams(simpaths.fparam[0]) # need number of lambda vals (cols) and number of sigma vals (rows) try: N_rows = len(p_exp.p_all['L2Pyr_Gauss_A_weight']) except TypeError: N_rows = 1 try: N_cols = len(p_exp.p_all['L2Basket_Pois_lamtha']) except TypeError: N_cols = 1 tstop = p_exp.p_all['tstop'] print N_rows, N_cols, tstop # ugly but slightly less ugly than the index arithmetic i had planned. muahaha f = ac.FigGrid(N_rows, N_cols, tstop) # create coordinates for axes # this is backward-looking for a reason! axes_coords = [ (j, i) for i, j in it.product(np.arange(N_cols), np.arange(N_rows)) ] if len(simpaths.expnames) != len(axes_coords): print "um ... see ppsth.py" # assumes a match between expnames and the keys of the previous dicts for expname, axis_coord in zip(simpaths.expnames, axes_coords): # get the tstop exp_param_list = dict_exp_param[expname] exp_spk_list = dict_exp_spk[expname] gid_dict, p = paramrw.read(exp_param_list[0]) tstop = p['tstop'] lamtha = p['L2Basket_Pois_lamtha'] sigma = p['L2Pyr_Gauss_A_weight'] # these are total spike dicts for the experiments s_L2Pyr_list = [] # s_L5Pyr_list = [] # iterate through params and spikes for a given experiment for fparam, fspk in zip(dict_exp_param[expname], dict_exp_spk[expname]): # get gid dict gid_dict, p = paramrw.read(fparam) # get spike dict s_dict = spikefn.spikes_from_file(gid_dict, fspk) # add a new entry to list for each different file assoc with an experiment s_L2Pyr_list.append( np.array( list( it.chain.from_iterable( s_dict['L2_pyramidal'].spike_list)))) # s_L5Pyr_list.append(np.array(list(it.chain.from_iterable(s_dict['L5_pyramidal'].spike_list)))) # now aggregate over all spikes s_L2Pyr = np.array(list(it.chain.from_iterable(s_L2Pyr_list))) # s_L5Pyr = np.array(list(it.chain.from_iterable(s_L5Pyr_list))) # optimize bins, currently unused for comparison reasons! N_trials = len(fparam) bin_L2 = 250 # bin_L5 = 120 # bin_L2 = spikefn.hist_bin_opt(s_L2Pyr, N_trials) # bin_L5 = spikefn.hist_bin_opt(s_L5Pyr, N_trials) r = axis_coord[0] c = axis_coord[1] # create standard fig and axes f.ax[r][c].hist(s_L2Pyr, bin_L2, facecolor='g', alpha=0.75) if r == 0: f.ax[r][c].set_title(r'$\lambda_i$ = %d' % lamtha) if c == 0: f.ax[r][c].set_ylabel(r'$A_{gauss}$ = %.3e' % sigma) # f.ax[r][c].set_ylabel(r'$\sigma_{gauss}$ = %d' % sigma) # normalize these axes y_L2 = f.ax[r][c].get_ylim() # y_L2 = f.ax['L2_psth'].get_ylim() print expname, lamtha, sigma, r, c, y_L2[1] f.ax[r][c].set_ylim((0, 250.)) # f.ax['L2_psth'].set_ylim((0, 450.)) # f.ax['L5_psth'].set_ylim((0, 450.)) # spikefn.spike_png(f.ax['L2'], s_dict_L2) # spikefn.spike_png(f.ax['L5'], s_dict_L5) # spikefn.spike_png(f.ax['L2_extpois'], s_dict_L2_extpois) # spikefn.spike_png(f.ax['L2_extgauss'], s_dict_L2_extgauss) # spikefn.spike_png(f.ax['L5_extpois'], s_dict_L5_extpois) # spikefn.spike_png(f.ax['L5_extgauss'], s_dict_L5_extgauss) # testfig.ax0.plot(t_vec, dp_total) fig_name = os.path.join(simpaths.dsim, 'aggregate.eps') plt.savefig(fig_name) f.close() # run the compression fio.epscompress(simpaths.dsim, '.eps', 1)
def exec_spec_avg_stationary_avg(ddata, dsim, opts): # Prompt user for type of analysis (per expmt or whole sim) analysis_type = raw_input('Would you like analysis per expmt or for whole sim? (expmt or sim): ') spec_results_avged = fio.file_match(ddata.dsim, '-specavg.npz') fparam_list = fio.file_match(ddata.dsim, '-param.txt') p_exp = paramrw.ExpParams(ddata.fparam) key_types = p_exp.get_key_types() # If no avg spec data found, generate it. if not spec_results_avged: exec_avgtrials(ddata, 'spec') spec_results_avged = fio.file_match(ddata.dsim, '-specavg.npz') # perform time-averaged stationarity analysis # specpwr_results = [specfn.specpwr_stationary_avg(dspec) for dspec in spec_results_avged] specpwr_results = [] for fspec in spec_results_avged: spec = specfn.Spec(fspec) specpwr_results.append(spec.stationary_avg()) # create fparam list to match avg'ed data N_trials = p_exp.N_trials nums = np.arange(0, len(fparam_list), N_trials) fparam_list = [fparam_list[num] for num in nums] # plot for whole simulation if analysis_type == 'sim': # if error bars indicated if opts['errorbars']: # get raw (non avg'ed) spec data raw_spec_data = fio.file_match(ddata.dsim, '-spec.npz') # perform freqpwr analysis on raw data # raw_specpwr = [specfn.specpwr_stationary_avg(dspec)['p_avg'] for dspec in raw_spec_data] raw_specpwr = [] for fspec in raw_spec_data: spec = specfn.Spec(fspec) raw_specpwr.append(spec.stationary_avg()['p_avg']) # calculate standard error error_vec = specfn.calc_stderror(raw_specpwr) else: error_vec = [] file_name = os.path.join(dsim, 'specpwr-avg.eps') pspec.pspecpwr(file_name, specpwr_results, fparam_list, key_types, error_vec) # # if maxpwr plot indicated # if maxpwr: # f_name = os.path.join(dsim, 'maxpwr-avg.png') # specfn.pmaxpwr(f_name, freqpwr_results_list, fparam_list) # plot per expmt if analysis_type == 'expmt': for expmt_group in ddata.expmt_groups: # if error bars indicated if opts['errorbars']: # get exmpt group raw spec data raw_spec_data = ddata.file_match(expmt_group, 'rawspec') # perform stationary analysis on raw data raw_specpwr = [specfn.specpwr_stationary_avg(dspec)['p_avg'] for dspec in raw_spec_data] # calculate standard error error_vec = specfn.calc_stderror(raw_specpwr) else: error_vec = [] # create name for figure. Figure saved to expmt directory file_name = os.path.join(dsim, expmt_group, 'specpwr-avg.png') # compile list of specpwr results and param pathways for expmt partial_results_list = [result for result in specpwr_results if result['expmt']==expmt_group] partial_fparam_list = [fparam for fparam in fparam_list if expmt_group in fparam] # plot results pspec.pspecpwr(file_name, partial_results_list, partial_fparam_list, key_types, error_vec)
def exec_plotaverages(ddata, ylim=[]): # runtype = 'parallel' runtype = 'debug' # this is a qnd check to create the fig dir if it doesn't already exist # backward compatibility check for sims that didn't auto-create these dirs for expmt_group in ddata.expmt_groups: dfig_avgdpl = ddata.dfig[expmt_group]['figavgdpl'] dfig_avgspec = ddata.dfig[expmt_group]['figavgspec'] # create them if they did not previously exist fio.dir_create(dfig_avgdpl) fio.dir_create(dfig_avgspec) # presumably globally true information p_exp = paramrw.ExpParams(ddata.fparam) key_types = p_exp.get_key_types() # empty lists to be used/appended dpl_list = [] spec_list = [] dfig_list = [] dfig_dpl_list = [] dfig_spec_list = [] pdict_list = [] # by doing all file operations sequentially by expmt_group in this iteration # trying to guarantee order better than before for expmt_group in ddata.expmt_groups: # print expmt_group, ddata.dfig[expmt_group] # avgdpl and avgspec data paths # fio.file_match() returns lists sorted # dpl_list_expmt is so i can iterate through them in a sec dpl_list_expmt = fio.file_match(ddata.dfig[expmt_group]['avgdpl'], '-dplavg.txt') dpl_list += dpl_list_expmt spec_list += fio.file_match(ddata.dfig[expmt_group]['avgspec'], '-specavg.npz') # create redundant list of avg dipole dirs and avg spec dirs # unique parts are expmt group names # create one entry for each in dpl_list dfig_list_expmt = [ddata.dfig[expmt_group] for path in dpl_list_expmt] dfig_list += dfig_list_expmt dfig_dpl_list += [dfig['figavgdpl'] for dfig in dfig_list_expmt] dfig_spec_list += [dfig['figavgspec'] for dfig in dfig_list_expmt] # param list to match avg data lists fparam_list = fio.fparam_match_minimal(ddata.dfig[expmt_group]['param'], p_exp) pdict_list += [paramrw.read(f_param)[1] for f_param in fparam_list] if dpl_list: # new input to dipolefn pdipole_dict = { 'xlim': None, 'ylim': None, # 'xmin': 0., # 'xmax': None, # 'ymin': None, # 'ymax': None, } # if there is a length, assume it's 2 (it should be!) if len(ylim): pdipole_dict['ymin'] = ylim[0] pdipole_dict['ymax'] = ylim[1] if runtype == 'debug': for f_dpl, f_param, dfig_dpl in zip(dpl_list, fparam_list, dfig_dpl_list): dipolefn.pdipole(f_dpl, dfig_dpl, pdipole_dict, f_param, key_types) elif runtype == 'parallel': pl = Pool() for f_dpl, f_param, dfig_dpl in zip(dpl_list, fparam_list, dfig_dpl_list): pl.apply_async(dipolefn.pdipole, (f_dpl, f_param, dfig_dpl, key_types, pdipole_dict)) pl.close() pl.join() else: print "No avg dipole data found." return 0 # if avg spec data exists if spec_list: if runtype == 'debug': for f_spec, f_dpl, f_param, dfig_spec, pdict in zip(spec_list, dpl_list, fparam_list, dfig_spec_list, pdict_list): pspec.pspec_dpl(f_spec, f_dpl, dfig_spec, pdict, key_types, f_param=f_param) elif runtype == 'parallel': pl = Pool() for f_spec, f_dpl, dfig_spec, pdict in zip(spec_list, dpl_list, dfig_spec_list, pdict_list): pl.apply_async(pspec.pspec_dpl, (f_spec, f_dpl, dfig_spec, pdict, key_types)) pl.close() pl.join() else: print "No averaged spec data found. Run avgtrials()." return 0
def exec_avgtrials(ddata, datatype): # create the relevant key for the data datakey = 'raw' + datatype datakey_avg = 'avg' + datatype # assumes N_Trials are the same in both p_exp = paramrw.ExpParams(ddata.fparam) sim_prefix = p_exp.sim_prefix N_trials = p_exp.N_trials # fix for N_trials=0 if not N_trials: N_trials = 1 # prefix strings exp_prefix_str = p_exp.exp_prefix_str trial_prefix_str = p_exp.trial_prefix_str # Averaging must be done per expmt for expmt_group in ddata.expmt_groups: ddatatype = ddata.dfig[expmt_group][datakey] dparam = ddata.dfig[expmt_group]['param'] param_list = ddata.file_match(expmt_group, 'param') rawdata_list = ddata.file_match(expmt_group, datakey) # if nothing in the raw data list, then generate it for spec if datakey == 'rawspec': if not len(rawdata_list): # generate the data! exec_spec_regenerate(ddata) rawdata_list = ddata.file_match(expmt_group, datakey) # simple length check, but will proceed bluntly anyway. # this will result in truncated lists, per zip function if len(param_list) != len(rawdata_list): print "warning, some weirdness detected in list length in exec_avgtrials. Check yo' lengths!" # number of unique simulations, per trial # this had better be equivalent as an integer or a float! N_unique = len(param_list) / N_trials # go through the unique simulations for i in range(N_unique): # fills in the correct int for the experimental prefix string formatter 'exp_prefix_str' prefix_unique = exp_prefix_str % i fprefix_long = os.path.join(ddatatype, prefix_unique) fprefix_long_param = os.path.join(dparam, prefix_unique) # create the sublist of just these trials unique_list = [rawdatafile for rawdatafile in rawdata_list if rawdatafile.startswith(fprefix_long)] unique_param_list = [pfile for pfile in param_list if pfile.startswith(fprefix_long_param)] # one filename per unique # length of the unique list is the number of trials for this sim, should match N_trials fname_unique = ddata.create_filename(expmt_group, datakey_avg, prefix_unique) # Average data for each trial # average dipole data if datakey == 'rawdpl': for f_dpl, f_param in zip(unique_list, unique_param_list): dpl = dipolefn.Dipole(f_dpl) # dpl = dipolefn.Dipole(f_dpl, f_param) # ah, this is required becaused the dpl *file* still contains the raw, un-normalized data dpl.baseline_renormalize(f_param) # initialize and use x_dpl if f_dpl is unique_list[0]: # assume time vec stays the same throughout t_vec = dpl.t x_dpl_agg = dpl.dpl['agg'] x_dpl_L2 = dpl.dpl['L2'] x_dpl_L5 = dpl.dpl['L5'] else: x_dpl_agg += dpl.dpl['agg'] x_dpl_L2 += dpl.dpl['L2'] x_dpl_L5 += dpl.dpl['L5'] # poor man's mean x_dpl_agg /= len(unique_list) x_dpl_L2 /= len(unique_list) x_dpl_L5 /= len(unique_list) # write this data to the file # np.savetxt(fname_unique, avg_data, '%5.4f') with open(fname_unique, 'w') as f: for t, x_agg, x_L2, x_L5 in zip(t_vec, x_dpl_agg, x_dpl_L2, x_dpl_L5): f.write("%03.3f\t%5.4f\t%5.4f\t%5.4f\n" % (t, x_agg, x_L2, x_L5)) # average spec data elif datakey == 'rawspec': specfn.average(fname_unique, unique_list)
def exec_show(ddata, dict_opts): dict_opts_default = { 'run': 0, 'trial': 0, 'expmt_group': '', 'key': 'changed', 'var_list': [], } # hack for now to get backward compatibility with this original function var_list = dict_opts_default['var_list'] exclude_list = [ 'sim_prefix', 'N_trials', 'Run_Date', ] args_check(dict_opts_default, dict_opts) if dict_opts_default['expmt_group'] not in ddata.expmt_groups: # print "Warning: expmt_group %s not found" % dict_opts_default['expmt_group'] dict_opts_default['expmt_group'] = ddata.expmt_groups[0] # output the expmt group used print "expmt_group: %s" % dict_opts_default['expmt_group'] # find the params p_exp = paramrw.ExpParams(ddata.fparam) if dict_opts_default['key'] == 'changed': print "Showing changed ... \n" # create a list var_list = [val[0] for val in paramrw.changed_vars(ddata.fparam)] elif dict_opts_default['key'] in p_exp.keys(): # create a list with just this element var_list = [dict_opts_default['key']] else: key_part = dict_opts_default['key'] var_list = [key for key in p_exp.keys() if key_part in key] if not var_list: print "Keys were not found by exec_show()" return 0 # files fprefix = ddata.trial_prefix_str % (dict_opts_default['run'], dict_opts_default['trial']) fparam = ddata.create_filename(dict_opts_default['expmt_group'], 'param', fprefix) list_param = ddata.file_match(dict_opts_default['expmt_group'], 'param') if fparam in list_param: # this version of read returns the gid dict as well ... _, p = paramrw.read(fparam) # use var_list to print values for key in var_list: if key not in exclude_list: try: print '%s: %s' % (key, p[key]) except KeyError: print "Value %s not found in file %s!" % (key, fparam)