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_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_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 pspec_with_hist(f_spec, f_dpl, f_spk, dfig, f_param, key_types, xlim=None, ylim=None): # Generate file prefix # print('f_spec:',f_spec) fprefix = f_spec.split('/')[-1].split('.')[0] # Create the fig name fig_name = os.path.join(dfig, fprefix + '.png') # print('fig_name:',fig_name) # load param dict _, p_dict = paramrw.read(f_param) f = ac.FigSpecWithHist() # load spec data spec = specfn.Spec(f_spec) # Plot TFR data and add colorbar pc = spec.plot_TFR(f.ax['spec'], 'agg', xlim, ylim) f.f.colorbar(pc, ax=f.ax['spec']) # set xlim based on TFR plot xlim_new = f.ax['spec'].get_xlim() # grab the dipole data dpl = dipolefn.Dipole(f_dpl) dpl.baseline_renormalize(f_param) dpl.convert_fAm_to_nAm() # plot routine dpl.plot(f.ax['dipole'], xlim_new, 'agg') # data_dipole = np.loadtxt(open(f_dpl, 'r')) # t_dpl = data_dipole[xmin_ind:xmax_ind+1, 0] # dp_total = data_dipole[xmin_ind:xmax_ind+1, 1] # f.ax['dipole'].plot(t_dpl, dp_total) # x = (xmin, xmax) # # grab alpha feed data. spikes_from_file() from spikefn.py # s_dict = spikefn.spikes_from_file(f_param, f_spk) # # check for existance of alpha feed keys in s_dict. # s_dict = spikefn.alpha_feed_verify(s_dict, p_dict) # # Account for possible delays # s_dict = spikefn.add_delay_times(s_dict, p_dict) # Get extinput data and account for delays extinputs = spikefn.ExtInputs(f_spk, f_param) extinputs.add_delay_times() extinputs.get_envelope(dpl.t, feed='dist') # set number of bins (150 bins per 1000ms) bins = ceil(150. * (xlim_new[1] - xlim_new[0]) / 1000.) # bins should be int # plot histograms hist = {} hist['feed_prox'] = extinputs.plot_hist(f.ax['feed_prox'], 'prox', dpl.t, bins=bins, xlim=xlim_new, color='red') hist['feed_dist'] = extinputs.plot_hist(f.ax['feed_dist'], 'dist', dpl.t, bins=bins, xlim=xlim_new, color='green') f.ax['feed_dist'].invert_yaxis() # for now, set the xlim for the other one, force it! f.ax['dipole'].set_xlim(xlim_new) f.ax['spec'].set_xlim(xlim_new) f.ax['feed_prox'].set_xlim(xlim_new) f.ax['feed_dist'].set_xlim(xlim_new) # set hist axis props f.set_hist_props(hist) # axis labels f.ax['spec'].set_xlabel('Time (ms)') f.ax['spec'].set_ylabel('Frequency (Hz)') # Add legend to histogram for key in f.ax.keys(): if 'feed' in key: f.ax[key].legend() # create title title_str = ac.create_title(p_dict, key_types) f.f.suptitle(title_str) f.savepng(fig_name) f.close()
def pspec_dpl(f_spec, f_dpl, dfig, p_dict, key_types, xlim=None, ylim=None, f_param=None): # Generate file prefix fprefix = f_spec.split('/')[-1].split('.')[0] # using png for now # fig_name = os.path.join(dfig, fprefix+'.eps') fig_name = os.path.join(dfig, fprefix + '.png') # f.f is the figure handle! f = ac.FigSpec() # load spec data spec = specfn.Spec(f_spec) # Plot TFR data and add colorbar pc = spec.plot_TFR(f.ax['spec'], 'agg', xlim, ylim) f.f.colorbar(pc, ax=f.ax['spec']) # grab the dipole data # data_dipole = np.loadtxt(open(f_dpl, 'r')) dpl = dipolefn.Dipole(f_dpl) # If f_param supplied, renormalize dipole data if f_param: dpl.baseline_renormalize(f_param) dpl.convert_fAm_to_nAm() # plot routine dpl.plot(f.ax['dipole'], xlim, 'agg') # Plot Welch data # Use try/except for backwards compatibility try: spec.plot_pgram(f.ax['pgram']) except KeyError: pgram = specfn.Welch(dpl.t, dpl.dpl['agg'], p_dict['dt']) pgram.plot_to_ax(f.ax['pgram'], spec.spec['agg']['f'][-1]) # plot and create an xlim xlim_new = f.ax['spec'].get_xlim() xticks = f.ax['spec'].get_xticks() xticks[0] = xlim_new[0] # for now, set the xlim for the other one, force it! f.ax['dipole'].set_xlim(xlim_new) f.ax['dipole'].set_xticks(xticks) f.ax['spec'].set_xticks(xticks) # axis labels f.ax['spec'].set_xlabel('Time (ms)') f.ax['spec'].set_ylabel('Frequency (Hz)') # create title title_str = ac.create_title(p_dict, key_types) f.f.suptitle(title_str) # use our fig classes to save and close f.savepng(fig_name) # f.saveeps(dfig, fprefix) f.close()
def aggregate_with_hist(f, ax, f_spec, f_dpl, f_spk, f_param): # load param dict _, p_dict = paramrw.read(f_param) # load spec data from file spec = specfn.Spec(f_spec) # data_spec = np.load(f_spec) # timevec = data_spec['time'] # freqvec = data_spec['freq'] # TFR = data_spec['TFR'] xmin = timevec[0] xmax = p_dict['tstop'] x = (xmin, xmax) pc = spec.plot_TFR(ax['spec'], layer='agg', xlim=x) # pc = ax['spec'].imshow(TFR, extent=[timevec[0], timevec[-1], freqvec[-1], freqvec[0]], aspect='auto', origin='upper') f.f.colorbar(pc, ax=ax['spec'], norm=plt.colors.Normalize(vmin=0, vmax=90000), cmap=plt.get_cmap('jet')) # grab the dipole data dpl = dipolefn.Dipole(f_dpl) dpl.plot(ax['dipole'], x, layer='agg') # data_dipole = np.loadtxt(open(f_dpl, 'r')) # t_dpl = data_dipole[xmin/p_dict['dt']:, 0] # dp_total = data_dipole[xmin/p_dict['dt']:, 1] # ax['dipole'].plot(t_dpl, dp_total) # grab alpha feed data. spikes_from_file() from spikefn.py s_dict = spikefn.spikes_from_file(f_param, f_spk) # check for existance of alpha feed keys in s_dict. s_dict = spikefn.alpha_feed_verify(s_dict, p_dict) # Account for possible delays s_dict = spikefn.add_delay_times(s_dict, p_dict) # set number of bins (150 bins/1000ms) bins = 150. * (xmax - xmin) / 1000. hist = {} # Proximal feed hist['feed_prox'] = ax['feed_prox'].hist( s_dict['alpha_feed_prox'].spike_list, bins, range=[xmin, xmax], color='red', label='Proximal feed') # Distal feed hist['feed_dist'] = ax['feed_dist'].hist( s_dict['alpha_feed_dist'].spike_list, bins, range=[xmin, xmax], color='green', label='Distal feed') # for now, set the xlim for the other one, force it! ax['dipole'].set_xlim(x) ax['spec'].set_xlim(x) ax['feed_prox'].set_xlim(x) ax['feed_dist'].set_xlim(x) # set hist axis props f.set_hist_props(ax, hist) # axis labels ax['spec'].set_xlabel('Time (ms)') ax['spec'].set_ylabel('Frequency (Hz)') # Add legend to histogram for key in ax.keys(): if 'feed' in key: ax[key].legend()
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_phaselock(ddata, opts): p = { 't_interval': [50, 1000], 'f_max': 60., } args_check(p, opts) # Do this per expmt group for expmt_group in ddata.expmt_groups: # Get paths to relevant files list_dpl = ddata.file_match(expmt_group, 'rawdpl') list_spk = ddata.file_match(expmt_group, 'rawspk') list_param = ddata.file_match(expmt_group, 'param') avg_spec = ddata.file_match(expmt_group, 'avgspec')[0] tmp_array_dpl = [] tmp_array_spk = [] for f_dpl, f_spk, f_param in zip(list_dpl, list_spk, list_param): # load Dpl data, do stuff, and store it print f_dpl dpl = dipolefn.Dipole(f_dpl) dpl.baseline_renormalize(f_param) dpl.convert_fAm_to_nAm() t, dp = dpl.truncate_ext(p['t_interval'][0], p['t_interval'][1]) dp = dp['agg'] tmp_array_dpl.append(dp) # Load extinput data, do stuff, and store it try: extinput = spikefn.ExtInputs(f_spk, f_param) except ValueError: print("Error: could not load spike timings from %s" % f_spk) return extinput.add_delay_times() extinput.get_envelope(dpl.t, feed='dist', bins=150) inputs, t = extinput.truncate_ext('env', p['t_interval']) tmp_array_spk.append(inputs) # Convert tmp arrays (actually lists) to numpy nd arrays array_dpl = np.array(tmp_array_dpl) array_spk = np.array(tmp_array_spk) # Phase-locking analysis phase = specfn.PhaseLock(array_dpl, array_spk, list_param[0], p['f_max']) fname_d = os.path.join(ddata.dsim, expmt_group, 'phaselock-%iHz.npz' %p['f_max']) np.savez_compressed(fname_d, t=phase.data['t'], f=phase.data['f'], B=phase.data['B']) # Plotting # Should be moved elsewhere avg_dpl = np.mean(array_dpl, axis=0) avg_spk = np.mean(array_spk, axis=0) f = ac.FigPhase() extent_xy = [t[0], t[-1], phase.data['f'][-1], 0] pc1 = f.ax['phase'].imshow(phase.data['B'], extent=extent_xy, aspect='auto', origin='upper',cmap=plt.get_cmap('jet')) pc1.set_clim([0, 1]) cb1 = f.f.colorbar(pc1, ax=f.ax['phase']) # cb1.set_clim([0, 1]) spec = specfn.Spec(avg_spec) pc2 = spec.plot_TFR(f.ax['spec'], xlim=[t[0], t[-1]]) pc2.set_clim([0, 3.8e-7]) cb2 = f.f.colorbar(pc2, ax=f.ax['spec']) # cb2.set_clim([0, 3.6e-7]) f.ax['dipole'].plot(t, avg_dpl) f.ax['dipole'].set_xlim([t[0], t[-1]]) f.ax['dipole'].set_ylim([-0.0015, 0.0015]) f.ax['input'].plot(t, avg_spk) f.ax['input'].set_xlim([t[0], t[-1]]) f.ax['input'].set_ylim([-1, 5]) f.ax['input'].invert_yaxis() f.ax['phase'].set_xlabel('Time (ms)') f.ax['phase'].set_ylabel('Frequency (Hz)') fname = os.path.join(ddata.dsim, expmt_group, 'phaselock-%iHz.png' %p['f_max']) print fname f.savepng(fname)